您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ContextMenu.tsx 5.7KB

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