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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 closed.
  57. */
  58. onPopoverClose: Function,
  59. /**
  60. * Callback to invoke when the popover has opened.
  61. */
  62. onPopoverOpen: Function,
  63. /**
  64. * Whether to display the Popover as a drawer.
  65. */
  66. overflowDrawer: boolean,
  67. /**
  68. * From which side of the dialog trigger the dialog should display. The
  69. * value will be passed to {@code InlineDialog}.
  70. */
  71. position: string
  72. };
  73. /**
  74. * The type of the React {@code Component} state of {@link Popover}.
  75. */
  76. type State = {
  77. /**
  78. * Whether or not the {@code InlineDialog} should be displayed.
  79. */
  80. showDialog: boolean
  81. };
  82. /**
  83. * Implements a React {@code Component} for showing an {@code InlineDialog} on
  84. * mouseenter of the trigger and contents, and hiding the dialog on mouseleave.
  85. *
  86. * @extends Component
  87. */
  88. class Popover extends Component<Props, State> {
  89. /**
  90. * Default values for {@code Popover} component's properties.
  91. *
  92. * @static
  93. */
  94. static defaultProps = {
  95. className: '',
  96. id: ''
  97. };
  98. /**
  99. * Reference to the dialog container.
  100. */
  101. _containerRef: Object;
  102. /**
  103. * Initializes a new {@code Popover} instance.
  104. *
  105. * @param {Object} props - The read-only properties with which the new
  106. * instance is to be initialized.
  107. */
  108. constructor(props: Props) {
  109. super(props);
  110. this.state = {
  111. showDialog: false
  112. };
  113. // Bind event handlers so they are only bound once for every instance.
  114. this._onHideDialog = this._onHideDialog.bind(this);
  115. this._onShowDialog = this._onShowDialog.bind(this);
  116. this._onKeyPress = this._onKeyPress.bind(this);
  117. this._containerRef = React.createRef();
  118. this._onEscKey = this._onEscKey.bind(this);
  119. this._onThumbClick = this._onThumbClick.bind(this);
  120. this._onTouchStart = this._onTouchStart.bind(this);
  121. }
  122. /**
  123. * Public method for triggering showing the context menu dialog.
  124. *
  125. * @returns {void}
  126. * @public
  127. */
  128. showDialog() {
  129. this._onShowDialog();
  130. }
  131. /**
  132. * Sets up a touch event listener to attach.
  133. *
  134. * @inheritdoc
  135. * @returns {void}
  136. */
  137. componentDidMount() {
  138. window.addEventListener('touchstart', this._onTouchStart);
  139. }
  140. /**
  141. * Removes the listener set up in the {@code componentDidMount} method.
  142. *
  143. * @inheritdoc
  144. * @returns {void}
  145. */
  146. componentWillUnmount() {
  147. window.removeEventListener('touchstart', this._onTouchStart);
  148. }
  149. /**
  150. * Implements React's {@link Component#render()}.
  151. *
  152. * @inheritdoc
  153. * @returns {ReactElement}
  154. */
  155. render() {
  156. const { children, className, content, id, overflowDrawer, position } = this.props;
  157. if (overflowDrawer) {
  158. return (
  159. <div
  160. className = { className }
  161. id = { id }
  162. onClick = { this._onShowDialog }>
  163. { children }
  164. <DrawerPortal>
  165. <Drawer
  166. isOpen = { this.state.showDialog }
  167. onClose = { this._onHideDialog }>
  168. { content }
  169. </Drawer>
  170. </DrawerPortal>
  171. </div>
  172. );
  173. }
  174. return (
  175. <div
  176. className = { className }
  177. id = { id }
  178. onClick = { this._onThumbClick }
  179. onKeyPress = { this._onKeyPress }
  180. onMouseEnter = { this._onShowDialog }
  181. onMouseLeave = { this._onHideDialog }
  182. ref = { this._containerRef }>
  183. <InlineDialog
  184. content = { this._renderContent() }
  185. isOpen = { this.state.showDialog }
  186. placement = { position }>
  187. { children }
  188. </InlineDialog>
  189. </div>
  190. );
  191. }
  192. _onTouchStart: (event: TouchEvent) => void;
  193. /**
  194. * Hide dialog on touch outside of the context menu.
  195. *
  196. * @param {TouchEvent} event - The touch event.
  197. * @private
  198. * @returns {void}
  199. */
  200. _onTouchStart(event) {
  201. if (this.state.showDialog
  202. && !this.props.overflowDrawer
  203. && this._containerRef
  204. && this._containerRef.current
  205. && !this._containerRef.current.contains(event.target)) {
  206. this._onHideDialog();
  207. }
  208. }
  209. _onHideDialog: () => void;
  210. /**
  211. * Stops displaying the {@code InlineDialog}.
  212. *
  213. * @private
  214. * @returns {void}
  215. */
  216. _onHideDialog() {
  217. this.setState({ showDialog: false });
  218. if (this.props.onPopoverClose) {
  219. this.props.onPopoverClose();
  220. }
  221. }
  222. _onShowDialog: (Object) => void;
  223. /**
  224. * Displays the {@code InlineDialog} and calls any registered onPopoverOpen
  225. * callbacks.
  226. *
  227. * @param {Object} event - The mouse event or the keypress event to intercept.
  228. * @private
  229. * @returns {void}
  230. */
  231. _onShowDialog(event) {
  232. event && event.stopPropagation();
  233. if (!this.props.disablePopover) {
  234. this.setState({ showDialog: true });
  235. if (this.props.onPopoverOpen) {
  236. this.props.onPopoverOpen();
  237. }
  238. }
  239. }
  240. _onThumbClick: (Object) => void;
  241. /**
  242. * Prevents switching from tile view to stage view on accidentally clicking
  243. * the popover thumbs.
  244. *
  245. * @param {Object} event - The mouse event or the keypress event to intercept.
  246. * @private
  247. * @returns {void}
  248. */
  249. _onThumbClick(event) {
  250. event.stopPropagation();
  251. }
  252. _onKeyPress: (Object) => void;
  253. /**
  254. * KeyPress handler for accessibility.
  255. *
  256. * @param {Object} e - The key event to handle.
  257. *
  258. * @returns {void}
  259. */
  260. _onKeyPress(e) {
  261. if (e.key === ' ' || e.key === 'Enter') {
  262. e.preventDefault();
  263. if (this.state.showDialog) {
  264. this._onHideDialog();
  265. } else {
  266. this._onShowDialog(e);
  267. }
  268. }
  269. }
  270. _onEscKey: (Object) => void;
  271. /**
  272. * KeyPress handler for accessibility.
  273. *
  274. * @param {Object} e - The key event to handle.
  275. *
  276. * @returns {void}
  277. */
  278. _onEscKey(e) {
  279. if (e.key === 'Escape') {
  280. e.preventDefault();
  281. e.stopPropagation();
  282. if (this.state.showDialog) {
  283. this._onHideDialog();
  284. }
  285. }
  286. }
  287. /**
  288. * Renders the React Element to be displayed in the {@code InlineDialog}.
  289. * Also adds padding to support moving the mouse from the trigger to the
  290. * dialog to prevent mouseleave events.
  291. *
  292. * @private
  293. * @returns {ReactElement}
  294. */
  295. _renderContent() {
  296. const { content, position } = this.props;
  297. return (
  298. <div
  299. className = 'popover'
  300. onKeyDown = { this._onEscKey }>
  301. { content }
  302. <div className = 'popover-mouse-padding-top' />
  303. <div className = { _mapPositionToPaddingClass(position) } />
  304. </div>
  305. );
  306. }
  307. }
  308. export default Popover;