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.

OverflowMenuButton.tsx 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import React, { useCallback } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { createToolbarEvent } from '../../../analytics/AnalyticsEvents';
  6. import { sendAnalytics } from '../../../analytics/functions';
  7. import Popover from '../../../base/popover/components/Popover.web';
  8. import ContextMenu from '../../../base/ui/components/web/ContextMenu';
  9. import ContextMenuItemGroup from '../../../base/ui/components/web/ContextMenuItemGroup';
  10. import { setGifMenuVisibility } from '../../../gifs/actions';
  11. import { isGifsMenuOpen } from '../../../gifs/functions.web';
  12. import ReactionEmoji from '../../../reactions/components/web/ReactionEmoji';
  13. import ReactionsMenu from '../../../reactions/components/web/ReactionsMenu';
  14. import {
  15. GIFS_MENU_HEIGHT_IN_OVERFLOW_MENU,
  16. RAISE_HAND_ROW_HEIGHT,
  17. REACTIONS_MENU_HEIGHT_DRAWER,
  18. REACTIONS_MENU_HEIGHT_IN_OVERFLOW_MENU
  19. } from '../../../reactions/constants';
  20. import { getReactionsQueue } from '../../../reactions/functions.any';
  21. import { IReactionsMenuParent } from '../../../reactions/types';
  22. import { DRAWER_MAX_HEIGHT } from '../../constants';
  23. import { showOverflowDrawer } from '../../functions.web';
  24. import Drawer from './Drawer';
  25. import JitsiPortal from './JitsiPortal';
  26. import OverflowToggleButton from './OverflowToggleButton';
  27. /**
  28. * The type of the React {@code Component} props of {@link OverflowMenuButton}.
  29. */
  30. interface IProps {
  31. /**
  32. * ID of the menu that is controlled by this button.
  33. */
  34. ariaControls: string;
  35. /**
  36. * Information about the buttons that need to be rendered in the overflow menu.
  37. */
  38. buttons: Object[];
  39. /**
  40. * Whether or not the OverflowMenu popover should display.
  41. */
  42. isOpen: boolean;
  43. /**
  44. * Esc key handler.
  45. */
  46. onToolboxEscKey: (e?: React.KeyboardEvent) => void;
  47. /**
  48. * Callback to change the visibility of the overflow menu.
  49. */
  50. onVisibilityChange: Function;
  51. /**
  52. * Whether to show the raise hand in the reactions menu or not.
  53. */
  54. showRaiseHandInReactionsMenu: boolean;
  55. /**
  56. * Whether or not to display the reactions menu.
  57. */
  58. showReactionsMenu: boolean;
  59. }
  60. const useStyles = makeStyles<{ overflowDrawer: boolean; reactionsMenuHeight: number; }>()(
  61. (_theme, { reactionsMenuHeight, overflowDrawer }) => {
  62. return {
  63. overflowMenuDrawer: {
  64. overflow: 'hidden',
  65. height: `calc(${DRAWER_MAX_HEIGHT} - ${reactionsMenuHeight}px - 16px)`
  66. },
  67. contextMenu: {
  68. position: 'relative' as const,
  69. right: 'auto',
  70. margin: 0,
  71. marginBottom: '8px',
  72. maxHeight: overflowDrawer ? undefined : 'calc(100vh - 100px)',
  73. paddingBottom: overflowDrawer ? undefined : 0,
  74. minWidth: '240px',
  75. overflow: 'hidden'
  76. },
  77. content: {
  78. position: 'relative',
  79. maxHeight: overflowDrawer
  80. ? `calc(100% - ${reactionsMenuHeight}px - 16px)` : `calc(100vh - 100px - ${reactionsMenuHeight}px)`,
  81. overflowY: 'auto'
  82. },
  83. footer: {
  84. position: 'relative',
  85. bottom: 0
  86. }
  87. };
  88. });
  89. const OverflowMenuButton = ({
  90. buttons,
  91. isOpen,
  92. onToolboxEscKey,
  93. onVisibilityChange,
  94. showRaiseHandInReactionsMenu,
  95. showReactionsMenu
  96. }: IProps) => {
  97. const overflowDrawer = useSelector(showOverflowDrawer);
  98. const reactionsQueue = useSelector(getReactionsQueue);
  99. const isGiphyVisible = useSelector(isGifsMenuOpen);
  100. const dispatch = useDispatch();
  101. const onCloseDialog = useCallback(() => {
  102. onVisibilityChange(false);
  103. if (isGiphyVisible && !overflowDrawer) {
  104. dispatch(setGifMenuVisibility(false));
  105. }
  106. }, [ onVisibilityChange, setGifMenuVisibility, isGiphyVisible, overflowDrawer, dispatch ]);
  107. const onOpenDialog = useCallback(() => {
  108. onVisibilityChange(true);
  109. }, [ onVisibilityChange ]);
  110. const onEscClick = useCallback((event: React.KeyboardEvent) => {
  111. if (event.key === 'Escape' && isOpen) {
  112. event.preventDefault();
  113. event.stopPropagation();
  114. onCloseDialog();
  115. }
  116. }, [ onCloseDialog ]);
  117. const toggleDialogVisibility = useCallback(() => {
  118. sendAnalytics(createToolbarEvent('overflow'));
  119. onVisibilityChange(!isOpen);
  120. }, [ isOpen, onVisibilityChange ]);
  121. const toolbarAccLabel = 'toolbar.accessibilityLabel.moreActionsMenu';
  122. const { t } = useTranslation();
  123. let reactionsMenuHeight = 0;
  124. if (showReactionsMenu) {
  125. reactionsMenuHeight = REACTIONS_MENU_HEIGHT_DRAWER;
  126. if (!overflowDrawer) {
  127. reactionsMenuHeight = REACTIONS_MENU_HEIGHT_IN_OVERFLOW_MENU;
  128. }
  129. if (!showRaiseHandInReactionsMenu) {
  130. reactionsMenuHeight -= RAISE_HAND_ROW_HEIGHT;
  131. }
  132. if (!overflowDrawer && isGiphyVisible) {
  133. reactionsMenuHeight += GIFS_MENU_HEIGHT_IN_OVERFLOW_MENU;
  134. }
  135. }
  136. const { classes } = useStyles({
  137. reactionsMenuHeight,
  138. overflowDrawer
  139. });
  140. const groupsJSX = buttons.map((buttonGroup: any) => (
  141. <ContextMenuItemGroup key = { `group-${buttonGroup[0].group}` }>
  142. {buttonGroup.map(({ key, Content, ...rest }: { Content: React.ElementType; key: string; }) => {
  143. const props: { buttonKey?: string; contextMenu?: boolean; showLabel?: boolean; } = { ...rest };
  144. if (key !== 'reactions') {
  145. props.buttonKey = key;
  146. props.contextMenu = true;
  147. props.showLabel = true;
  148. }
  149. return (
  150. <Content
  151. { ...props }
  152. key = { key } />);
  153. })}
  154. </ContextMenuItemGroup>));
  155. const overflowMenu = groupsJSX && (
  156. <ContextMenu
  157. accessibilityLabel = { t(toolbarAccLabel) }
  158. className = { classes.contextMenu }
  159. hidden = { false }
  160. id = 'overflow-context-menu'
  161. inDrawer = { overflowDrawer }
  162. onKeyDown = { onToolboxEscKey }>
  163. <div className = { classes.content }>
  164. { groupsJSX }
  165. </div>
  166. {
  167. showReactionsMenu && (<div className = { classes.footer }>
  168. <ReactionsMenu
  169. parent = {
  170. overflowDrawer ? IReactionsMenuParent.OverflowDrawer : IReactionsMenuParent.OverflowMenu }
  171. showRaisedHand = { showRaiseHandInReactionsMenu } />
  172. </div>)
  173. }
  174. </ContextMenu>);
  175. if (overflowDrawer) {
  176. return (
  177. <div className = 'toolbox-button-wth-dialog context-menu'>
  178. <>
  179. <OverflowToggleButton
  180. handleClick = { toggleDialogVisibility }
  181. isOpen = { isOpen }
  182. onKeyDown = { onEscClick } />
  183. <JitsiPortal>
  184. <Drawer
  185. isOpen = { isOpen }
  186. onClose = { onCloseDialog }>
  187. <>
  188. <div className = { classes.overflowMenuDrawer }>
  189. { overflowMenu }
  190. </div>
  191. </>
  192. </Drawer>
  193. {showReactionsMenu && <div className = 'reactions-animations-container'>
  194. {reactionsQueue.map(({ reaction, uid }, index) => (<ReactionEmoji
  195. index = { index }
  196. key = { uid }
  197. reaction = { reaction }
  198. uid = { uid } />))}
  199. </div>}
  200. </JitsiPortal>
  201. </>
  202. </div>
  203. );
  204. }
  205. return (
  206. <div className = 'toolbox-button-wth-dialog context-menu'>
  207. <Popover
  208. content = { overflowMenu }
  209. headingId = 'overflow-context-menu'
  210. onPopoverClose = { onCloseDialog }
  211. onPopoverOpen = { onOpenDialog }
  212. position = 'top'
  213. trigger = 'click'
  214. visible = { isOpen }>
  215. <OverflowToggleButton
  216. isOpen = { isOpen }
  217. onKeyDown = { onEscClick } />
  218. </Popover>
  219. {showReactionsMenu && <div className = 'reactions-animations-container'>
  220. {reactionsQueue.map(({ reaction, uid }, index) => (<ReactionEmoji
  221. index = { index }
  222. key = { uid }
  223. reaction = { reaction }
  224. uid = { uid } />))}
  225. </div>}
  226. </div>
  227. );
  228. };
  229. export default OverflowMenuButton;