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.

ContextMenuItemGroup.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // @flow
  2. import { makeStyles } from '@material-ui/core';
  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 Action = {
  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. * Id of the action container.
  24. */
  25. id?: string,
  26. /**
  27. * Default icon for action.
  28. */
  29. icon?: Function,
  30. /**
  31. * Click handler.
  32. */
  33. onClick?: Function,
  34. /**
  35. * Action text.
  36. */
  37. text: string
  38. }
  39. type Props = {
  40. /**
  41. * List of actions in this group.
  42. */
  43. actions?: Array<Action>,
  44. /**
  45. * The children of the component.
  46. */
  47. children?: React$Node,
  48. };
  49. const useStyles = makeStyles(theme => {
  50. return {
  51. contextMenuItemGroup: {
  52. '&:not(:empty)': {
  53. padding: `${theme.spacing(2)}px 0`
  54. },
  55. '& + &:not(:empty)': {
  56. borderTop: `1px solid ${theme.palette.ui04}`
  57. }
  58. },
  59. contextMenuItem: {
  60. alignItems: 'center',
  61. cursor: 'pointer',
  62. display: 'flex',
  63. minHeight: '40px',
  64. padding: '10px 16px',
  65. boxSizing: 'border-box',
  66. '& > *:not(:last-child)': {
  67. marginRight: `${theme.spacing(3)}px`
  68. },
  69. '&:hover': {
  70. backgroundColor: theme.palette.ui04
  71. }
  72. },
  73. contextMenuItemDrawer: {
  74. padding: '12px 16px'
  75. },
  76. contextMenuItemIcon: {
  77. '& svg': {
  78. fill: theme.palette.icon01
  79. }
  80. }
  81. };
  82. });
  83. const ContextMenuItemGroup = ({
  84. actions,
  85. children
  86. }: Props) => {
  87. const styles = useStyles();
  88. const _overflowDrawer = useSelector(showOverflowDrawer);
  89. return (
  90. <div className = { styles.contextMenuItemGroup }>
  91. {children}
  92. {actions && actions.map(({ accessibilityLabel, className, customIcon, id, icon, onClick, text }) => (
  93. <div
  94. aria-label = { accessibilityLabel }
  95. className = { clsx(styles.contextMenuItem,
  96. _overflowDrawer && styles.contextMenuItemDrawer,
  97. className
  98. ) }
  99. id = { id }
  100. key = { text }
  101. onClick = { onClick }>
  102. {customIcon ? customIcon
  103. : icon && <Icon
  104. className = { styles.contextMenuItemIcon }
  105. size = { 20 }
  106. src = { icon } />}
  107. <span>{text}</span>
  108. </div>
  109. ))}
  110. </div>
  111. );
  112. };
  113. export default ContextMenuItemGroup;