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.3KB

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