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 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* @flow */
  2. import InlineDialog from '@atlaskit/inline-dialog';
  3. import React, { Component } from 'react';
  4. import { Drawer, DrawerPortal } from '../../../toolbox/components/web';
  5. import { isMobileBrowser } from '../../environment/utils';
  6. /**
  7. * A map of dialog positions, relative to trigger, to css classes used to
  8. * manipulate elements for handling mouse events.
  9. *
  10. * @private
  11. * @type {object}
  12. */
  13. const DIALOG_TO_PADDING_POSITION = {
  14. 'left': 'popover-mousemove-padding-right',
  15. 'right': 'popover-mousemove-padding-left',
  16. 'top': 'popover-mousemove-padding-bottom'
  17. };
  18. /**
  19. * Takes the position expected by {@code InlineDialog} and maps it to a CSS
  20. * class that can be used styling the elements used for preventing mouseleave
  21. * events when moving from the trigger to the dialog.
  22. *
  23. * @param {string} position - From which position the dialog will display.
  24. * @private
  25. * @returns {string}
  26. */
  27. function _mapPositionToPaddingClass(position = 'left') {
  28. return DIALOG_TO_PADDING_POSITION[position.split('-')[0]];
  29. }
  30. /**
  31. * The type of the React {@code Component} props of {@link Popover}.
  32. */
  33. type Props = {
  34. /**
  35. * A child React Element to use as the trigger for showing the dialog.
  36. */
  37. children: React$Node,
  38. /**
  39. * Additional CSS classnames to apply to the root of the {@code Popover}
  40. * component.
  41. */
  42. className: string,
  43. /**
  44. * The ReactElement to display within the dialog.
  45. */
  46. content: Object,
  47. /**
  48. * Whether displaying of the popover should be prevented.
  49. */
  50. disablePopover: boolean,
  51. /**
  52. * An id attribute to apply to the root of the {@code Popover}
  53. * component.
  54. */
  55. id: string,
  56. /**
  57. * Callback to invoke when the popover has closed.
  58. */
  59. onPopoverClose: Function,
  60. /**
  61. * Callback to invoke when the popover has opened.
  62. */
  63. onPopoverOpen: Function,
  64. /**
  65. * Whether to display the Popover as a drawer.
  66. */
  67. overflowDrawer: boolean,
  68. /**
  69. * From which side of the dialog trigger the dialog should display. The
  70. * value will be passed to {@code InlineDialog}.
  71. */
  72. position: string
  73. };
  74. /**
  75. * The type of the React {@code Component} state of {@link Popover}.
  76. */
  77. type State = {
  78. /**
  79. * Whether or not the {@code InlineDialog} should be displayed.
  80. */
  81. showDialog: boolean
  82. };
  83. /**
  84. * Implements a React {@code Component} for showing an {@code InlineDialog} on
  85. * mouseenter of the trigger and contents, and hiding the dialog on mouseleave.
  86. *
  87. * @extends Component
  88. */
  89. class Popover extends Component<Props, State> {
  90. /**
  91. * Default values for {@code Popover} component's properties.
  92. *
  93. * @static
  94. */
  95. static defaultProps = {
  96. className: '',
  97. id: ''
  98. };
  99. /**
  100. * Reference to the Popover that is meant to open as a drawer.
  101. */
  102. _drawerContainerRef: Object;
  103. /**
  104. * Initializes a new {@code Popover} instance.
  105. *
  106. * @param {Object} props - The read-only properties with which the new
  107. * instance is to be initialized.
  108. */
  109. constructor(props: Props) {
  110. super(props);
  111. this.state = {
  112. showDialog: false
  113. };
  114. // Bind event handlers so they are only bound once for every instance.
  115. this._onHideDialog = this._onHideDialog.bind(this);
  116. this._onShowDialog = this._onShowDialog.bind(this);
  117. this._onKeyPress = this._onKeyPress.bind(this);
  118. this._drawerContainerRef = React.createRef();
  119. this._onEscKey = this._onEscKey.bind(this);
  120. }
  121. /**
  122. * Public method for triggering showing the context menu dialog.
  123. *
  124. * @returns {void}
  125. * @public
  126. */
  127. showDialog() {
  128. this.setState({ showDialog: true });
  129. }
  130. /**
  131. * Sets up an event listener to open a drawer when clicking, rather than entering the
  132. * overflow area.
  133. *
  134. * TODO: This should be done by setting an {@code onClick} handler on the div, but for some
  135. * reason that doesn't seem to work whatsoever.
  136. *
  137. * @inheritdoc
  138. * @returns {void}
  139. */
  140. componentDidMount() {
  141. if (this._drawerContainerRef && this._drawerContainerRef.current && !isMobileBrowser()) {
  142. this._drawerContainerRef.current.addEventListener('click', this._onShowDialog);
  143. }
  144. }
  145. /**
  146. * Removes the listener set up in the {@code componentDidMount} method.
  147. *
  148. * @inheritdoc
  149. * @returns {void}
  150. */
  151. componentWillUnmount() {
  152. if (this._drawerContainerRef && this._drawerContainerRef.current) {
  153. this._drawerContainerRef.current.removeEventListener('click', this._onShowDialog);
  154. }
  155. }
  156. /**
  157. * Implements React Component's componentDidUpdate.
  158. *
  159. * @inheritdoc
  160. */
  161. componentDidUpdate(prevProps: Props) {
  162. if (prevProps.overflowDrawer !== this.props.overflowDrawer) {
  163. // Make sure the listeners are set up when resizing the screen past the drawer threshold.
  164. if (this.props.overflowDrawer) {
  165. this.componentDidMount();
  166. } else {
  167. this.componentWillUnmount();
  168. }
  169. }
  170. }
  171. /**
  172. * Implements React's {@link Component#render()}.
  173. *
  174. * @inheritdoc
  175. * @returns {ReactElement}
  176. */
  177. render() {
  178. const { children, className, content, id, overflowDrawer, position } = this.props;
  179. if (overflowDrawer) {
  180. return (
  181. <div
  182. className = { className }
  183. id = { id }
  184. ref = { this._drawerContainerRef }>
  185. { children }
  186. <DrawerPortal>
  187. <Drawer
  188. isOpen = { this.state.showDialog }
  189. onClose = { this._onHideDialog }>
  190. { content }
  191. </Drawer>
  192. </DrawerPortal>
  193. </div>
  194. );
  195. }
  196. return (
  197. <div
  198. className = { className }
  199. id = { id }
  200. onKeyPress = { this._onKeyPress }
  201. onMouseEnter = { this._onShowDialog }
  202. onMouseLeave = { this._onHideDialog }>
  203. <InlineDialog
  204. content = { this._renderContent() }
  205. isOpen = { this.state.showDialog }
  206. placement = { position }>
  207. { children }
  208. </InlineDialog>
  209. </div>
  210. );
  211. }
  212. _onHideDialog: () => void;
  213. /**
  214. * Stops displaying the {@code InlineDialog}.
  215. *
  216. * @private
  217. * @returns {void}
  218. */
  219. _onHideDialog() {
  220. this.setState({ showDialog: false });
  221. if (this.props.onPopoverClose) {
  222. this.props.onPopoverClose();
  223. }
  224. }
  225. _onShowDialog: (Object) => void;
  226. /**
  227. * Displays the {@code InlineDialog} and calls any registered onPopoverOpen
  228. * callbacks.
  229. *
  230. * @param {Object} event - The mouse event or the keypress event to intercept.
  231. * @private
  232. * @returns {void}
  233. */
  234. _onShowDialog(event) {
  235. event.stopPropagation();
  236. if (!this.props.disablePopover) {
  237. this.setState({ showDialog: true });
  238. if (this.props.onPopoverOpen) {
  239. this.props.onPopoverOpen();
  240. }
  241. }
  242. }
  243. _onKeyPress: (Object) => void;
  244. /**
  245. * KeyPress handler for accessibility.
  246. *
  247. * @param {Object} e - The key event to handle.
  248. *
  249. * @returns {void}
  250. */
  251. _onKeyPress(e) {
  252. if (e.key === ' ' || e.key === 'Enter') {
  253. e.preventDefault();
  254. if (this.state.showDialog) {
  255. this._onHideDialog();
  256. } else {
  257. this._onShowDialog(e);
  258. }
  259. }
  260. }
  261. _onEscKey: (Object) => void;
  262. /**
  263. * KeyPress handler for accessibility.
  264. *
  265. * @param {Object} e - The key event to handle.
  266. *
  267. * @returns {void}
  268. */
  269. _onEscKey(e) {
  270. if (e.key === 'Escape') {
  271. e.preventDefault();
  272. e.stopPropagation();
  273. if (this.state.showDialog) {
  274. this._onHideDialog();
  275. }
  276. }
  277. }
  278. /**
  279. * Renders the React Element to be displayed in the {@code InlineDialog}.
  280. * Also adds padding to support moving the mouse from the trigger to the
  281. * dialog to prevent mouseleave events.
  282. *
  283. * @private
  284. * @returns {ReactElement}
  285. */
  286. _renderContent() {
  287. const { content, position } = this.props;
  288. return (
  289. <div
  290. className = 'popover'
  291. onKeyDown = { this._onEscKey }>
  292. { content }
  293. <div className = 'popover-mouse-padding-top' />
  294. <div className = { _mapPositionToPaddingClass(position) } />
  295. </div>
  296. );
  297. }
  298. }
  299. export default Popover;