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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { makeStyles } from '@material-ui/styles';
  2. import clsx from 'clsx';
  3. import React, { ReactNode } from 'react';
  4. import { useSelector } from 'react-redux';
  5. // @ts-ignore
  6. import { showOverflowDrawer } from '../../../toolbox/functions.web';
  7. import Icon from '../../icons/components/Icon';
  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: any) => {
  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)}px`
  70. },
  71. '&:hover': {
  72. backgroundColor: theme.palette.ui04
  73. }
  74. },
  75. contextMenuItemDisabled: {
  76. pointerEvents: 'none'
  77. },
  78. contextMenuItemDrawer: {
  79. padding: '12px 16px'
  80. },
  81. contextMenuItemIcon: {
  82. '& svg': {
  83. fill: theme.palette.icon01
  84. }
  85. }
  86. };
  87. });
  88. const ContextMenuItem = ({
  89. accessibilityLabel,
  90. className,
  91. customIcon,
  92. disabled,
  93. id,
  94. icon,
  95. onClick,
  96. onKeyDown,
  97. onKeyPress,
  98. testId,
  99. text,
  100. textClassName }: Props) => {
  101. const styles = useStyles();
  102. const _overflowDrawer: boolean = useSelector(showOverflowDrawer);
  103. return (
  104. <div
  105. aria-disabled = { disabled }
  106. aria-label = { accessibilityLabel }
  107. className = { clsx(styles.contextMenuItem,
  108. _overflowDrawer && styles.contextMenuItemDrawer,
  109. disabled && styles.contextMenuItemDisabled,
  110. className
  111. ) }
  112. data-testid = { testId }
  113. id = { id }
  114. key = { text }
  115. onClick = { disabled ? undefined : onClick }
  116. onKeyDown = { disabled ? undefined : onKeyDown }
  117. onKeyPress = { disabled ? undefined : onKeyPress }>
  118. {customIcon ? customIcon
  119. : icon && <Icon
  120. className = { styles.contextMenuItemIcon }
  121. size = { 20 }
  122. src = { icon } />}
  123. <span className = { textClassName ?? '' }>{text}</span>
  124. </div>
  125. );
  126. };
  127. export default ContextMenuItem;