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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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._drawerContainerRef = React.createRef();
  113. }
  114. /**
  115. * Sets up an event listener to open a drawer when clicking, rather than entering the
  116. * overflow area.
  117. *
  118. * TODO: This should be done by setting an {@code onClick} handler on the div, but for some
  119. * reason that doesn't seem to work whatsoever.
  120. *
  121. * @inheritdoc
  122. * @returns {void}
  123. */
  124. componentDidMount() {
  125. if (this._drawerContainerRef && this._drawerContainerRef.current) {
  126. this._drawerContainerRef.current.addEventListener('click', this._onShowDialog);
  127. }
  128. }
  129. /**
  130. * Removes the listener set up in the {@code componentDidMount} method.
  131. *
  132. * @inheritdoc
  133. * @returns {void}
  134. */
  135. componentWillUnmount() {
  136. if (this._drawerContainerRef && this._drawerContainerRef.current) {
  137. this._drawerContainerRef.current.removeEventListener('click', this._onShowDialog);
  138. }
  139. }
  140. /**
  141. * Implements React Component's componentDidUpdate.
  142. *
  143. * @inheritdoc
  144. */
  145. componentDidUpdate(prevProps: Props) {
  146. if (prevProps.overflowDrawer !== this.props.overflowDrawer) {
  147. // Make sure the listeners are set up when resizing the screen past the drawer threshold.
  148. if (this.props.overflowDrawer) {
  149. this.componentDidMount();
  150. } else {
  151. this.componentWillUnmount();
  152. }
  153. }
  154. }
  155. /**
  156. * Implements React's {@link Component#render()}.
  157. *
  158. * @inheritdoc
  159. * @returns {ReactElement}
  160. */
  161. render() {
  162. const { children, className, content, id, overflowDrawer, position } = this.props;
  163. if (overflowDrawer) {
  164. return (
  165. <div
  166. className = { className }
  167. id = { id }
  168. ref = { this._drawerContainerRef }>
  169. { children }
  170. <DrawerPortal>
  171. <Drawer
  172. isOpen = { this.state.showDialog }
  173. onClose = { this._onHideDialog }>
  174. { content }
  175. </Drawer>
  176. </DrawerPortal>
  177. </div>
  178. );
  179. }
  180. return (
  181. <div
  182. className = { className }
  183. id = { id }
  184. onMouseEnter = { this._onShowDialog }
  185. onMouseLeave = { this._onHideDialog }>
  186. <InlineDialog
  187. content = { this._renderContent() }
  188. isOpen = { this.state.showDialog }
  189. placement = { position }>
  190. { children }
  191. </InlineDialog>
  192. </div>
  193. );
  194. }
  195. _onHideDialog: () => void;
  196. /**
  197. * Stops displaying the {@code InlineDialog}.
  198. *
  199. * @private
  200. * @returns {void}
  201. */
  202. _onHideDialog() {
  203. this.setState({ showDialog: false });
  204. }
  205. _onShowDialog: () => void;
  206. /**
  207. * Displays the {@code InlineDialog} and calls any registered onPopoverOpen
  208. * callbacks.
  209. *
  210. * @param {MouseEvent} event - The mouse event to intercept.
  211. * @private
  212. * @returns {void}
  213. */
  214. _onShowDialog(event) {
  215. event.stopPropagation();
  216. if (!this.props.disablePopover) {
  217. this.setState({ showDialog: true });
  218. if (this.props.onPopoverOpen) {
  219. this.props.onPopoverOpen();
  220. }
  221. }
  222. }
  223. /**
  224. * Renders the React Element to be displayed in the {@code InlineDialog}.
  225. * Also adds padding to support moving the mouse from the trigger to the
  226. * dialog to prevent mouseleave events.
  227. *
  228. * @private
  229. * @returns {ReactElement}
  230. */
  231. _renderContent() {
  232. const { content, position } = this.props;
  233. return (
  234. <div className = 'popover'>
  235. { content }
  236. <div className = 'popover-mouse-padding-top' />
  237. <div className = { _mapPositionToPaddingClass(position) } />
  238. </div>
  239. );
  240. }
  241. }
  242. export default Popover;