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.7KB

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