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

Popover.web.js 10KB

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