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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // @flow
  2. import { makeStyles } from '@material-ui/styles';
  3. import clsx from 'clsx';
  4. import React from 'react';
  5. import { useSelector } from 'react-redux';
  6. import { showOverflowDrawer } from '../../../toolbox/functions.web';
  7. import { Icon } from '../../icons';
  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?: React$Node,
  22. /**
  23. * Whether or not the action is disabled.
  24. */
  25. disabled?: boolean,
  26. /**
  27. * Id of the action container.
  28. */
  29. id?: string,
  30. /**
  31. * Default icon for action.
  32. */
  33. icon?: Function,
  34. /**
  35. * Click handler.
  36. */
  37. onClick?: Function,
  38. /**
  39. * Keydown handler.
  40. */
  41. onKeyDown?: Function,
  42. /**
  43. * Keypress handler.
  44. */
  45. onKeyPress?: Function,
  46. /**
  47. * Action text.
  48. */
  49. text: string,
  50. /**
  51. * Class name for the text.
  52. */
  53. textClassName?: string
  54. }
  55. const useStyles = makeStyles(theme => {
  56. return {
  57. contextMenuItem: {
  58. alignItems: 'center',
  59. cursor: 'pointer',
  60. display: 'flex',
  61. minHeight: '40px',
  62. padding: '10px 16px',
  63. boxSizing: 'border-box',
  64. '& > *:not(:last-child)': {
  65. marginRight: `${theme.spacing(3)}px`
  66. },
  67. '&:hover': {
  68. backgroundColor: theme.palette.ui04
  69. }
  70. },
  71. contextMenuItemDisabled: {
  72. pointerEvents: 'none'
  73. },
  74. contextMenuItemDrawer: {
  75. padding: '12px 16px'
  76. },
  77. contextMenuItemIcon: {
  78. '& svg': {
  79. fill: theme.palette.icon01
  80. }
  81. }
  82. };
  83. });
  84. const ContextMenuItem = ({
  85. accessibilityLabel,
  86. className,
  87. customIcon,
  88. disabled,
  89. id,
  90. icon,
  91. onClick,
  92. onKeyDown,
  93. onKeyPress,
  94. text,
  95. textClassName }: Props) => {
  96. const styles = useStyles();
  97. const _overflowDrawer = useSelector(showOverflowDrawer);
  98. return (
  99. <div
  100. aria-disabled = { disabled }
  101. aria-label = { accessibilityLabel }
  102. className = { clsx(styles.contextMenuItem,
  103. _overflowDrawer && styles.contextMenuItemDrawer,
  104. disabled && styles.contextMenuItemDisabled,
  105. className
  106. ) }
  107. id = { id }
  108. key = { text }
  109. onClick = { disabled ? undefined : onClick }
  110. onKeyDown = { disabled ? undefined : onKeyDown }
  111. onKeyPress = { disabled ? undefined : onKeyPress }>
  112. {customIcon ? customIcon
  113. : icon && <Icon
  114. className = { styles.contextMenuItemIcon }
  115. size = { 20 }
  116. src = { icon } />}
  117. <span className = { textClassName ?? '' }>{text}</span>
  118. </div>
  119. );
  120. };
  121. export default ContextMenuItem;