You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContextMenuItem.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { Theme } from '@mui/material';
  2. import React, { ReactNode } from 'react';
  3. import { useSelector } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { showOverflowDrawer } from '../../../../toolbox/functions.web';
  6. import Icon from '../../../icons/components/Icon';
  7. import { withPixelLineHeight } from '../../../styles/functions.web';
  8. export type Props = {
  9. /**
  10. * Label used for accessibility.
  11. */
  12. accessibilityLabel: string;
  13. /**
  14. * CSS class name used for custom styles.
  15. */
  16. className?: string;
  17. /**
  18. * Custom icon. If used, the icon prop is ignored.
  19. * Used to allow custom children instead of just the default icons.
  20. */
  21. customIcon?: ReactNode;
  22. /**
  23. * Whether or not the action is disabled.
  24. */
  25. disabled?: boolean;
  26. /**
  27. * Default icon for action.
  28. */
  29. icon?: Function;
  30. /**
  31. * Id of the action container.
  32. */
  33. id?: string;
  34. /**
  35. * Click handler.
  36. */
  37. onClick?: (e?: React.MouseEvent) => void;
  38. /**
  39. * Keydown handler.
  40. */
  41. onKeyDown?: (e?: React.KeyboardEvent) => void;
  42. /**
  43. * Keypress handler.
  44. */
  45. onKeyPress?: (e?: React.KeyboardEvent) => void;
  46. /**
  47. * TestId of the element, if any.
  48. */
  49. testId?: string;
  50. /**
  51. * Action text.
  52. */
  53. text: string;
  54. /**
  55. * Class name for the text.
  56. */
  57. textClassName?: string;
  58. };
  59. const useStyles = makeStyles()((theme: Theme) => {
  60. return {
  61. contextMenuItem: {
  62. alignItems: 'center',
  63. cursor: 'pointer',
  64. display: 'flex',
  65. minHeight: '40px',
  66. padding: '10px 16px',
  67. boxSizing: 'border-box',
  68. '& > *:not(:last-child)': {
  69. marginRight: theme.spacing(3)
  70. },
  71. '&:hover': {
  72. backgroundColor: theme.palette.ui02
  73. },
  74. '&:active': {
  75. backgroundColor: theme.palette.ui03
  76. }
  77. },
  78. contextMenuItemDisabled: {
  79. pointerEvents: 'none'
  80. },
  81. contextMenuItemDrawer: {
  82. padding: '13px 16px'
  83. },
  84. contextMenuItemIcon: {
  85. '& svg': {
  86. fill: theme.palette.icon01
  87. }
  88. },
  89. text: {
  90. ...withPixelLineHeight(theme.typography.bodyShortRegular),
  91. color: theme.palette.text01
  92. },
  93. drawerText: {
  94. ...withPixelLineHeight(theme.typography.bodyShortRegularLarge)
  95. }
  96. };
  97. });
  98. const ContextMenuItem = ({
  99. accessibilityLabel,
  100. className,
  101. customIcon,
  102. disabled,
  103. id,
  104. icon,
  105. onClick,
  106. onKeyDown,
  107. onKeyPress,
  108. testId,
  109. text,
  110. textClassName }: Props) => {
  111. const { classes: styles, cx } = useStyles();
  112. const _overflowDrawer: boolean = useSelector(showOverflowDrawer);
  113. return (
  114. <div
  115. aria-disabled = { disabled }
  116. aria-label = { accessibilityLabel }
  117. className = { cx(styles.contextMenuItem,
  118. _overflowDrawer && styles.contextMenuItemDrawer,
  119. disabled && styles.contextMenuItemDisabled,
  120. className
  121. ) }
  122. data-testid = { testId }
  123. id = { id }
  124. key = { text }
  125. onClick = { disabled ? undefined : onClick }
  126. onKeyDown = { disabled ? undefined : onKeyDown }
  127. onKeyPress = { disabled ? undefined : onKeyPress }>
  128. {customIcon ? customIcon
  129. : icon && <Icon
  130. className = { styles.contextMenuItemIcon }
  131. size = { 20 }
  132. src = { icon } />}
  133. <span className = { cx(textClassName) }>{text}</span>
  134. </div>
  135. );
  136. };
  137. export default ContextMenuItem;