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.

Popover.web.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import React, { Component, ReactNode } from 'react';
  2. import { IReduxState } from '../../../app/types';
  3. import DialogPortal from '../../../toolbox/components/web/DialogPortal';
  4. import Drawer from '../../../toolbox/components/web/Drawer';
  5. import JitsiPortal from '../../../toolbox/components/web/JitsiPortal';
  6. import { isMobileBrowser } from '../../environment/utils';
  7. import { connect } from '../../redux/functions';
  8. import { getContextMenuStyle } from '../functions.web';
  9. /**
  10. * The type of the React {@code Component} props of {@link Popover}.
  11. */
  12. interface IProps {
  13. /**
  14. * A child React Element to use as the trigger for showing the dialog.
  15. */
  16. children: ReactNode;
  17. /**
  18. * Additional CSS classnames to apply to the root of the {@code Popover}
  19. * component.
  20. */
  21. className?: string;
  22. /**
  23. * The ReactElement to display within the dialog.
  24. */
  25. content: ReactNode;
  26. /**
  27. * Whether displaying of the popover should be prevented.
  28. */
  29. disablePopover?: boolean;
  30. /**
  31. * An id attribute to apply to the root of the {@code Popover}
  32. * component.
  33. */
  34. id?: string;
  35. /**
  36. * Callback to invoke when the popover has closed.
  37. */
  38. onPopoverClose: Function;
  39. /**
  40. * Callback to invoke when the popover has opened.
  41. */
  42. onPopoverOpen?: Function;
  43. /**
  44. * Whether to display the Popover as a drawer.
  45. */
  46. overflowDrawer?: boolean;
  47. /**
  48. * Where should the popover content be placed.
  49. */
  50. position: string;
  51. /**
  52. * Whether the trigger for open/ close should be click or hover.
  53. */
  54. trigger?: 'hover' | 'click';
  55. /**
  56. * Whether the popover is visible or not.
  57. */
  58. visible: boolean;
  59. }
  60. /**
  61. * The type of the React {@code Component} state of {@link Popover}.
  62. */
  63. interface IState {
  64. /**
  65. * The style to apply to the context menu in order to position it correctly.
  66. */
  67. contextMenuStyle?: {
  68. bottom?: string;
  69. left?: string;
  70. position: string;
  71. top?: string;
  72. } | null;
  73. }
  74. /**
  75. * Implements a React {@code Component} for showing an {@code Popover} on
  76. * mouseenter of the trigger and contents, and hiding the dialog on mouseleave.
  77. *
  78. * @augments Component
  79. */
  80. class Popover extends Component<IProps, IState> {
  81. /**
  82. * Default values for {@code Popover} component's properties.
  83. *
  84. * @static
  85. */
  86. static defaultProps = {
  87. className: '',
  88. id: '',
  89. trigger: 'hover'
  90. };
  91. /**
  92. * Reference to the dialog container.
  93. */
  94. _containerRef: React.RefObject<HTMLDivElement>;
  95. _contextMenuRef: HTMLElement;
  96. /**
  97. * Initializes a new {@code Popover} instance.
  98. *
  99. * @param {Object} props - The read-only properties with which the new
  100. * instance is to be initialized.
  101. */
  102. constructor(props: IProps) {
  103. super(props);
  104. this.state = {
  105. contextMenuStyle: null
  106. };
  107. // Bind event handlers so they are only bound once for every instance.
  108. this._onHideDialog = this._onHideDialog.bind(this);
  109. this._onShowDialog = this._onShowDialog.bind(this);
  110. this._onKeyPress = this._onKeyPress.bind(this);
  111. this._containerRef = React.createRef();
  112. this._onEscKey = this._onEscKey.bind(this);
  113. this._onClick = this._onClick.bind(this);
  114. this._onTouchStart = this._onTouchStart.bind(this);
  115. this._setContextMenuRef = this._setContextMenuRef.bind(this);
  116. this._setContextMenuStyle = this._setContextMenuStyle.bind(this);
  117. this._getCustomDialogStyle = this._getCustomDialogStyle.bind(this);
  118. this._onOutsideClick = this._onOutsideClick.bind(this);
  119. }
  120. /**
  121. * Sets up a touch event listener to attach.
  122. *
  123. * @inheritdoc
  124. * @returns {void}
  125. */
  126. componentDidMount() {
  127. window.addEventListener('touchstart', this._onTouchStart);
  128. if (this.props.trigger === 'click') {
  129. // @ts-ignore
  130. window.addEventListener('click', this._onOutsideClick);
  131. }
  132. }
  133. /**
  134. * Removes the listener set up in the {@code componentDidMount} method.
  135. *
  136. * @inheritdoc
  137. * @returns {void}
  138. */
  139. componentWillUnmount() {
  140. window.removeEventListener('touchstart', this._onTouchStart);
  141. if (this.props.trigger === 'click') {
  142. // @ts-ignore
  143. window.removeEventListener('click', this._onOutsideClick);
  144. }
  145. }
  146. /**
  147. * Handles click outside the popover.
  148. *
  149. * @param {MouseEvent} e - The click event.
  150. * @returns {void}
  151. */
  152. _onOutsideClick(e: React.MouseEvent) { // @ts-ignore
  153. if (!this._containerRef?.current?.contains(e.target) && this.props.visible) {
  154. this._onHideDialog();
  155. }
  156. }
  157. /**
  158. * Implements React's {@link Component#render()}.
  159. *
  160. * @inheritdoc
  161. * @returns {ReactElement}
  162. */
  163. render() {
  164. const { children, className, content, id, overflowDrawer, visible, trigger } = this.props;
  165. if (overflowDrawer) {
  166. return (
  167. <div
  168. className = { className }
  169. id = { id }
  170. onClick = { this._onShowDialog }>
  171. { children }
  172. <JitsiPortal>
  173. <Drawer
  174. isOpen = { visible }
  175. onClose = { this._onHideDialog }>
  176. { content }
  177. </Drawer>
  178. </JitsiPortal>
  179. </div>
  180. );
  181. }
  182. return (
  183. <div
  184. className = { className }
  185. id = { id }
  186. onClick = { this._onClick }
  187. onKeyPress = { this._onKeyPress }
  188. { ...(trigger === 'hover' ? {
  189. onMouseEnter: this._onShowDialog,
  190. onMouseLeave: this._onHideDialog
  191. } : {}) }
  192. ref = { this._containerRef }>
  193. { visible && (
  194. <DialogPortal
  195. getRef = { this._setContextMenuRef }
  196. setSize = { this._setContextMenuStyle }
  197. style = { this.state.contextMenuStyle }>
  198. {this._renderContent()}
  199. </DialogPortal>
  200. )}
  201. { children }
  202. </div>
  203. );
  204. }
  205. /**
  206. * Sets the context menu dialog style for positioning it on screen.
  207. *
  208. * @param {DOMRectReadOnly} size -The size info of the current context menu.
  209. *
  210. * @returns {void}
  211. */
  212. _setContextMenuStyle(size: DOMRectReadOnly) {
  213. const style = this._getCustomDialogStyle(size);
  214. this.setState({ contextMenuStyle: style });
  215. }
  216. /**
  217. * Sets the context menu's ref.
  218. *
  219. * @param {HTMLElement} elem -The html element of the context menu.
  220. *
  221. * @returns {void}
  222. */
  223. _setContextMenuRef(elem: HTMLElement) {
  224. this._contextMenuRef = elem;
  225. }
  226. /**
  227. * Hide dialog on touch outside of the context menu.
  228. *
  229. * @param {TouchEvent} event - The touch event.
  230. * @private
  231. * @returns {void}
  232. */
  233. _onTouchStart(event: TouchEvent) {
  234. if (this.props.visible
  235. && !this.props.overflowDrawer
  236. && this._contextMenuRef
  237. && this._contextMenuRef.contains // @ts-ignore
  238. && !this._contextMenuRef.contains(event.target)) {
  239. this._onHideDialog();
  240. }
  241. }
  242. /**
  243. * Stops displaying the {@code Popover}.
  244. *
  245. * @private
  246. * @returns {void}
  247. */
  248. _onHideDialog() {
  249. this.setState({
  250. contextMenuStyle: null
  251. });
  252. if (this.props.onPopoverClose) {
  253. this.props.onPopoverClose();
  254. }
  255. }
  256. /**
  257. * Displays the {@code Popover} and calls any registered onPopoverOpen
  258. * callbacks.
  259. *
  260. * @param {Object} event - The mouse event or the keypress event to intercept.
  261. * @private
  262. * @returns {void}
  263. */
  264. _onShowDialog(event?: React.MouseEvent | React.KeyboardEvent) {
  265. event?.stopPropagation();
  266. if (!this.props.disablePopover) {
  267. this.props.onPopoverOpen?.();
  268. }
  269. }
  270. /**
  271. * Prevents switching from tile view to stage view on accidentally clicking
  272. * the popover thumbs.
  273. *
  274. * @param {Object} event - The mouse event or the keypress event to intercept.
  275. * @private
  276. * @returns {void}
  277. */
  278. _onClick(event: React.MouseEvent) {
  279. const { trigger, visible } = this.props;
  280. event.stopPropagation();
  281. if (trigger === 'click') {
  282. if (visible) {
  283. this._onHideDialog();
  284. } else {
  285. this._onShowDialog();
  286. }
  287. }
  288. }
  289. /**
  290. * KeyPress handler for accessibility.
  291. *
  292. * @param {Object} e - The key event to handle.
  293. *
  294. * @returns {void}
  295. */
  296. _onKeyPress(e: React.KeyboardEvent) {
  297. if (e.key === ' ' || e.key === 'Enter') {
  298. e.preventDefault();
  299. if (this.props.visible) {
  300. this._onHideDialog();
  301. } else {
  302. this._onShowDialog(e);
  303. }
  304. }
  305. }
  306. /**
  307. * KeyPress handler for accessibility.
  308. *
  309. * @param {Object} e - The key event to handle.
  310. *
  311. * @returns {void}
  312. */
  313. _onEscKey(e: React.KeyboardEvent) {
  314. if (e.key === 'Escape') {
  315. e.preventDefault();
  316. e.stopPropagation();
  317. if (this.props.visible) {
  318. this._onHideDialog();
  319. }
  320. }
  321. }
  322. /**
  323. * Gets style for positioning the context menu on screen in regards to the trigger's
  324. * position.
  325. *
  326. * @param {DOMRectReadOnly} size -The current context menu's size info.
  327. *
  328. * @returns {Object} - The new style of the context menu.
  329. */
  330. _getCustomDialogStyle(size: DOMRectReadOnly) {
  331. if (this._containerRef?.current) {
  332. const bounds = this._containerRef.current.getBoundingClientRect();
  333. return getContextMenuStyle(bounds, size, this.props.position);
  334. }
  335. }
  336. /**
  337. * Renders the React Element to be displayed in the {@code Popover}.
  338. * Also adds padding to support moving the mouse from the trigger to the
  339. * dialog to prevent mouseleave events.
  340. *
  341. * @private
  342. * @returns {ReactElement}
  343. */
  344. _renderContent() {
  345. const { content } = this.props;
  346. return (
  347. <div
  348. className = 'popover'
  349. onKeyDown = { this._onEscKey }>
  350. { content }
  351. {!isMobileBrowser() && (
  352. <>
  353. <div className = 'popover-mousemove-padding-top' />
  354. <div className = 'popover-mousemove-padding-right' />
  355. <div className = 'popover-mousemove-padding-left' />
  356. <div className = 'popover-mousemove-padding-bottom' />
  357. </>)}
  358. </div>
  359. );
  360. }
  361. }
  362. /**
  363. * Maps (parts of) the Redux state to the associated {@code Popover}'s props.
  364. *
  365. * @param {Object} state - The Redux state.
  366. * @param {Object} ownProps - The own props of the component.
  367. * @private
  368. * @returns {IProps}
  369. */
  370. function _mapStateToProps(state: IReduxState) {
  371. return {
  372. overflowDrawer: state['features/toolbox'].overflowDrawer
  373. };
  374. }
  375. export default connect(_mapStateToProps)(Popover);