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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* @flow */
  2. import InlineDialog from '@atlaskit/inline-dialog';
  3. import React, { Component } from 'react';
  4. /**
  5. * A map of dialog positions, relative to trigger, to css classes used to
  6. * manipulate elements for handling mouse events.
  7. *
  8. * @private
  9. * @type {object}
  10. */
  11. const DIALOG_TO_PADDING_POSITION = {
  12. 'left': 'popover-mousemove-padding-right',
  13. 'right': 'popover-mousemove-padding-left',
  14. 'top': 'popover-mousemove-padding-bottom'
  15. };
  16. /**
  17. * Takes the position expected by {@code InlineDialog} and maps it to a CSS
  18. * class that can be used styling the elements used for preventing mouseleave
  19. * events when moving from the trigger to the dialog.
  20. *
  21. * @param {string} position - From which position the dialog will display.
  22. * @private
  23. * @returns {string}
  24. */
  25. function _mapPositionToPaddingClass(position = 'left') {
  26. return DIALOG_TO_PADDING_POSITION[position.split(' ')[0]];
  27. }
  28. /**
  29. * The type of the React {@code Component} props of {@link Popover}.
  30. */
  31. type Props = {
  32. /**
  33. * A child React Element to use as the trigger for showing the dialog.
  34. */
  35. children: React$Node,
  36. /**
  37. * Additional CSS classnames to apply to the root of the {@code Popover}
  38. * component.
  39. */
  40. className: string,
  41. /**
  42. * The ReactElement to display within the dialog.
  43. */
  44. content: Object,
  45. /**
  46. * Whether displaying of the popover should be prevented.
  47. */
  48. disablePopover: boolean,
  49. /**
  50. * An id attribute to apply to the root of the {@code Popover}
  51. * component.
  52. */
  53. id: string,
  54. /**
  55. * Callback to invoke when the popover has opened.
  56. */
  57. onPopoverOpen: Function,
  58. /**
  59. * From which side of the dialog trigger the dialog should display. The
  60. * value will be passed to {@code InlineDialog}.
  61. */
  62. position: string
  63. };
  64. /**
  65. * The type of the React {@code Component} state of {@link Popover}.
  66. */
  67. type State = {
  68. /**
  69. * Whether or not the {@code InlineDialog} should be displayed.
  70. */
  71. showDialog: boolean
  72. };
  73. /**
  74. * Implements a React {@code Component} for showing an {@code InlineDialog} on
  75. * mouseenter of the trigger and contents, and hiding the dialog on mouseleave.
  76. *
  77. * @extends Component
  78. */
  79. class Popover extends Component<Props, State> {
  80. /**
  81. * Default values for {@code Popover} component's properties.
  82. *
  83. * @static
  84. */
  85. static defaultProps = {
  86. className: '',
  87. id: ''
  88. };
  89. /**
  90. * Initializes a new {@code Popover} instance.
  91. *
  92. * @param {Object} props - The read-only properties with which the new
  93. * instance is to be initialized.
  94. */
  95. constructor(props: Props) {
  96. super(props);
  97. this.state = {
  98. showDialog: false
  99. };
  100. // Bind event handlers so they are only bound once for every instance.
  101. this._onHideDialog = this._onHideDialog.bind(this);
  102. this._onShowDialog = this._onShowDialog.bind(this);
  103. }
  104. /**
  105. * Implements React's {@link Component#render()}.
  106. *
  107. * @inheritdoc
  108. * @returns {ReactElement}
  109. */
  110. render() {
  111. return (
  112. <div
  113. className = { this.props.className }
  114. id = { this.props.id }
  115. onMouseEnter = { this._onShowDialog }
  116. onMouseLeave = { this._onHideDialog }>
  117. <InlineDialog
  118. content = { this._renderContent() }
  119. isOpen = { this.state.showDialog }
  120. position = { this.props.position }>
  121. { this.props.children }
  122. </InlineDialog>
  123. </div>
  124. );
  125. }
  126. _onHideDialog: () => void;
  127. /**
  128. * Stops displaying the {@code InlineDialog}.
  129. *
  130. * @private
  131. * @returns {void}
  132. */
  133. _onHideDialog() {
  134. this.setState({ showDialog: false });
  135. }
  136. _onShowDialog: () => void;
  137. /**
  138. * Displays the {@code InlineDialog} and calls any registered onPopoverOpen
  139. * callbacks.
  140. *
  141. * @private
  142. * @returns {void}
  143. */
  144. _onShowDialog() {
  145. if (!this.props.disablePopover) {
  146. this.setState({ showDialog: true });
  147. if (this.props.onPopoverOpen) {
  148. this.props.onPopoverOpen();
  149. }
  150. }
  151. }
  152. /**
  153. * Renders the React Element to be displayed in the {@code InlineDialog}.
  154. * Also adds padding to support moving the mouse from the trigger to the
  155. * dialog to prevent mouseleave events.
  156. *
  157. * @private
  158. * @returns {ReactElement}
  159. */
  160. _renderContent() {
  161. const { content, position } = this.props;
  162. return (
  163. <div className = 'popover'>
  164. { content }
  165. <div className = 'popover-mouse-padding-top' />
  166. <div className = { _mapPositionToPaddingClass(position) } />
  167. </div>
  168. );
  169. }
  170. }
  171. export default Popover;