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.js 10KB

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