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.

ContextMenu.tsx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* eslint-disable lines-around-comment */
  2. import { makeStyles } from '@material-ui/core';
  3. import clsx from 'clsx';
  4. import React, { ReactNode, useEffect, useLayoutEffect, useRef, useState } from 'react';
  5. import { useSelector } from 'react-redux';
  6. // @ts-ignore
  7. import { getComputedOuterHeight } from '../../../participants-pane/functions';
  8. // @ts-ignore
  9. import { Drawer, JitsiPortal } from '../../../toolbox/components/web';
  10. // @ts-ignore
  11. import { showOverflowDrawer } from '../../../toolbox/functions.web';
  12. import participantsPaneTheme from '../themes/participantsPaneTheme.json';
  13. type Props = {
  14. /**
  15. * Accessibility label for menu container.
  16. */
  17. accessibilityLabel?: string,
  18. /**
  19. * Children of the context menu.
  20. */
  21. children: ReactNode,
  22. /**
  23. * Class name for context menu. Used to overwrite default styles.
  24. */
  25. className?: string,
  26. /**
  27. * The entity for which the context menu is displayed.
  28. */
  29. entity?: Object,
  30. /**
  31. * Whether or not the menu is hidden. Used to overwrite the internal isHidden.
  32. */
  33. hidden?: boolean,
  34. /**
  35. * Whether or not the menu is already in a drawer.
  36. */
  37. inDrawer?: boolean,
  38. /**
  39. * Whether or not drawer should be open.
  40. */
  41. isDrawerOpen?: boolean,
  42. /**
  43. * Target elements against which positioning calculations are made.
  44. */
  45. offsetTarget?: HTMLElement,
  46. /**
  47. * Callback for click on an item in the menu.
  48. */
  49. onClick?: (e?: React.MouseEvent) => void,
  50. /**
  51. * Callback for drawer close.
  52. */
  53. onDrawerClose?: (e?: React.MouseEvent) => void,
  54. /**
  55. * Keydown handler.
  56. */
  57. onKeyDown?: (e?: React.KeyboardEvent) => void,
  58. /**
  59. * Callback for the mouse entering the component.
  60. */
  61. onMouseEnter?: (e?: React.MouseEvent) => void,
  62. /**
  63. * Callback for the mouse leaving the component.
  64. */
  65. onMouseLeave?: (e?: React.MouseEvent) => void
  66. };
  67. const MAX_HEIGHT = 400;
  68. const useStyles = makeStyles((theme: any) => {
  69. return {
  70. contextMenu: {
  71. backgroundColor: theme.palette.ui02,
  72. borderRadius: `${theme.shape.borderRadius / 2}px`,
  73. boxShadow: '0px 3px 16px rgba(0, 0, 0, 0.6), 0px 0px 4px 1px rgba(0, 0, 0, 0.25)',
  74. color: theme.palette.text01,
  75. ...theme.typography.bodyShortRegular,
  76. lineHeight: `${theme.typography.bodyShortRegular.lineHeight}px`,
  77. marginTop: `${(participantsPaneTheme.panePadding * 2) + theme.typography.bodyShortRegular.fontSize}px`,
  78. position: 'absolute',
  79. right: `${participantsPaneTheme.panePadding}px`,
  80. top: 0,
  81. zIndex: 2,
  82. maxHeight: `${MAX_HEIGHT}px`,
  83. overflowY: 'auto'
  84. },
  85. contextMenuHidden: {
  86. pointerEvents: 'none',
  87. visibility: 'hidden'
  88. },
  89. drawer: {
  90. '& > div': {
  91. ...theme.typography.bodyShortRegularLarge,
  92. lineHeight: `${theme.typography.bodyShortRegularLarge.lineHeight}px`,
  93. '& svg': {
  94. fill: theme.palette.icon01
  95. }
  96. },
  97. '& > *:first-child': {
  98. paddingTop: '15px!important'
  99. }
  100. }
  101. };
  102. });
  103. const ContextMenu = ({
  104. accessibilityLabel,
  105. children,
  106. className,
  107. entity,
  108. hidden,
  109. inDrawer,
  110. isDrawerOpen,
  111. offsetTarget,
  112. onClick,
  113. onKeyDown,
  114. onDrawerClose,
  115. onMouseEnter,
  116. onMouseLeave
  117. }: Props) => {
  118. const [ isHidden, setIsHidden ] = useState(true);
  119. const containerRef = useRef<HTMLDivElement | null>(null);
  120. const styles = useStyles();
  121. const _overflowDrawer = useSelector(showOverflowDrawer);
  122. useLayoutEffect(() => {
  123. if (_overflowDrawer) {
  124. return;
  125. }
  126. if (entity && offsetTarget
  127. && containerRef.current
  128. && offsetTarget?.offsetParent
  129. && offsetTarget.offsetParent instanceof HTMLElement
  130. ) {
  131. const { current: container } = containerRef;
  132. const { offsetTop, offsetParent: { offsetHeight, scrollTop } } = offsetTarget;
  133. const outerHeight = getComputedOuterHeight(container);
  134. const height = Math.min(MAX_HEIGHT, outerHeight);
  135. container.style.top = offsetTop + height > offsetHeight + scrollTop
  136. ? `${offsetTop - outerHeight}`
  137. : `${offsetTop}`;
  138. setIsHidden(false);
  139. } else {
  140. hidden === undefined && setIsHidden(true);
  141. }
  142. }, [ entity, offsetTarget, _overflowDrawer ]);
  143. useEffect(() => {
  144. if (hidden !== undefined) {
  145. setIsHidden(hidden);
  146. }
  147. }, [ hidden ]);
  148. if (_overflowDrawer && inDrawer) {
  149. return (<div
  150. className = { styles.drawer }
  151. onClick = { onDrawerClose }>
  152. {children}
  153. </div>);
  154. }
  155. return _overflowDrawer
  156. ? <JitsiPortal>
  157. <Drawer
  158. isOpen = { isDrawerOpen && _overflowDrawer }
  159. onClose = { onDrawerClose }>
  160. <div
  161. className = { styles.drawer }
  162. onClick = { onDrawerClose }>
  163. {children}
  164. </div>
  165. </Drawer>
  166. </JitsiPortal>
  167. : <div
  168. aria-label = { accessibilityLabel }
  169. className = { clsx(participantsPaneTheme.ignoredChildClassName,
  170. styles.contextMenu,
  171. isHidden && styles.contextMenuHidden,
  172. className
  173. ) }
  174. onClick = { onClick }
  175. onKeyDown = { onKeyDown }
  176. onMouseEnter = { onMouseEnter }
  177. onMouseLeave = { onMouseLeave }
  178. ref = { containerRef }>
  179. {children}
  180. </div>
  181. ;
  182. };
  183. export default ContextMenu;