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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. * 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._onEscKey = this._onEscKey.bind(this);
  114. this._onThumbClick = this._onThumbClick.bind(this);
  115. }
  116. /**
  117. * Public method for triggering showing the context menu dialog.
  118. *
  119. * @returns {void}
  120. * @public
  121. */
  122. showDialog() {
  123. this.setState({ showDialog: true });
  124. }
  125. /**
  126. * Implements React's {@link Component#render()}.
  127. *
  128. * @inheritdoc
  129. * @returns {ReactElement}
  130. */
  131. render() {
  132. const { children, className, content, id, overflowDrawer, position } = this.props;
  133. if (overflowDrawer) {
  134. return (
  135. <div
  136. className = { className }
  137. id = { id }
  138. onClick = { this._onShowDialog }>
  139. { children }
  140. <DrawerPortal>
  141. <Drawer
  142. isOpen = { this.state.showDialog }
  143. onClose = { this._onHideDialog }>
  144. { content }
  145. </Drawer>
  146. </DrawerPortal>
  147. </div>
  148. );
  149. }
  150. return (
  151. <div
  152. className = { className }
  153. id = { id }
  154. onClick = { this._onThumbClick }
  155. onKeyPress = { this._onKeyPress }
  156. onMouseEnter = { this._onShowDialog }
  157. onMouseLeave = { this._onHideDialog }>
  158. <InlineDialog
  159. content = { this._renderContent() }
  160. isOpen = { this.state.showDialog }
  161. placement = { position }>
  162. { children }
  163. </InlineDialog>
  164. </div>
  165. );
  166. }
  167. _onHideDialog: () => void;
  168. /**
  169. * Stops displaying the {@code InlineDialog}.
  170. *
  171. * @private
  172. * @returns {void}
  173. */
  174. _onHideDialog() {
  175. this.setState({ showDialog: false });
  176. if (this.props.onPopoverClose) {
  177. this.props.onPopoverClose();
  178. }
  179. }
  180. _onShowDialog: (Object) => void;
  181. /**
  182. * Displays the {@code InlineDialog} and calls any registered onPopoverOpen
  183. * callbacks.
  184. *
  185. * @param {Object} event - The mouse event or the keypress event to intercept.
  186. * @private
  187. * @returns {void}
  188. */
  189. _onShowDialog(event) {
  190. event.stopPropagation();
  191. if (!this.props.disablePopover) {
  192. this.setState({ showDialog: true });
  193. if (this.props.onPopoverOpen) {
  194. this.props.onPopoverOpen();
  195. }
  196. }
  197. }
  198. _onThumbClick: (Object) => void;
  199. /**
  200. * Prevents switching from tile view to stage view on accidentally clicking
  201. * the popover thumbs.
  202. *
  203. * @param {Object} event - The mouse event or the keypress event to intercept.
  204. * @private
  205. * @returns {void}
  206. */
  207. _onThumbClick(event) {
  208. event.stopPropagation();
  209. }
  210. _onKeyPress: (Object) => void;
  211. /**
  212. * KeyPress handler for accessibility.
  213. *
  214. * @param {Object} e - The key event to handle.
  215. *
  216. * @returns {void}
  217. */
  218. _onKeyPress(e) {
  219. if (e.key === ' ' || e.key === 'Enter') {
  220. e.preventDefault();
  221. if (this.state.showDialog) {
  222. this._onHideDialog();
  223. } else {
  224. this._onShowDialog(e);
  225. }
  226. }
  227. }
  228. _onEscKey: (Object) => void;
  229. /**
  230. * KeyPress handler for accessibility.
  231. *
  232. * @param {Object} e - The key event to handle.
  233. *
  234. * @returns {void}
  235. */
  236. _onEscKey(e) {
  237. if (e.key === 'Escape') {
  238. e.preventDefault();
  239. e.stopPropagation();
  240. if (this.state.showDialog) {
  241. this._onHideDialog();
  242. }
  243. }
  244. }
  245. /**
  246. * Renders the React Element to be displayed in the {@code InlineDialog}.
  247. * Also adds padding to support moving the mouse from the trigger to the
  248. * dialog to prevent mouseleave events.
  249. *
  250. * @private
  251. * @returns {ReactElement}
  252. */
  253. _renderContent() {
  254. const { content, position } = this.props;
  255. return (
  256. <div
  257. className = 'popover'
  258. onKeyDown = { this._onEscKey }>
  259. { content }
  260. <div className = 'popover-mouse-padding-top' />
  261. <div className = { _mapPositionToPaddingClass(position) } />
  262. </div>
  263. );
  264. }
  265. }
  266. export default Popover;