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

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