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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import InlineDialog from '@atlaskit/inline-dialog';
  2. import PropTypes from 'prop-types';
  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. * Implements a React {@code Component} for showing an {@code InlineDialog} on
  30. * mouseenter of the trigger and contents, and hiding the dialog on mouseleave.
  31. *
  32. * @extends Component
  33. */
  34. class Popover extends Component {
  35. /**
  36. * Default values for {@code Popover} component's properties.
  37. *
  38. * @static
  39. */
  40. static defaultProps = {
  41. className: '',
  42. id: ''
  43. };
  44. /**
  45. * {@code Popover} component's property types.
  46. *
  47. * @static
  48. */
  49. static propTypes = {
  50. /**
  51. * A child React Element to use as the trigger for showing the dialog.
  52. */
  53. children: PropTypes.object,
  54. /**
  55. * Additional CSS classnames to apply to the root of the {@code Popover}
  56. * component.
  57. */
  58. className: PropTypes.string,
  59. /**
  60. * The ReactElement to display within the dialog.
  61. */
  62. content: PropTypes.object,
  63. /**
  64. * Whether displaying of the popover should be prevented.
  65. */
  66. disablePopover: PropTypes.bool,
  67. /**
  68. * An id attribute to apply to the root of the {@code Popover}
  69. * component.
  70. */
  71. id: PropTypes.string,
  72. /**
  73. * Callback to invoke when the popover has opened.
  74. */
  75. onPopoverOpen: PropTypes.func,
  76. /**
  77. * From which side of the dialog trigger the dialog should display. The
  78. * value will be passed to {@code InlineDialog}.
  79. */
  80. position: PropTypes.string
  81. };
  82. /**
  83. * Initializes a new {@code Popover} instance.
  84. *
  85. * @param {Object} props - The read-only properties with which the new
  86. * instance is to be initialized.
  87. */
  88. constructor(props) {
  89. super(props);
  90. this.state = {
  91. /**
  92. * Whether or not the {@code InlineDialog} should be displayed.
  93. *
  94. * @type {boolean}
  95. */
  96. showDialog: false
  97. };
  98. // Bind event handlers so they are only bound once for every instance.
  99. this._onHideDialog = this._onHideDialog.bind(this);
  100. this._onShowDialog = this._onShowDialog.bind(this);
  101. }
  102. /**
  103. * Implements React's {@link Component#render()}.
  104. *
  105. * @inheritdoc
  106. * @returns {ReactElement}
  107. */
  108. render() {
  109. return (
  110. <div
  111. className = { this.props.className }
  112. id = { this.props.id }
  113. onMouseEnter = { this._onShowDialog }
  114. onMouseLeave = { this._onHideDialog }>
  115. <InlineDialog
  116. content = { this._renderContent() }
  117. isOpen = { this.state.showDialog }
  118. position = { this.props.position }>
  119. { this.props.children }
  120. </InlineDialog>
  121. </div>
  122. );
  123. }
  124. /**
  125. * Stops displaying the {@code InlineDialog}.
  126. *
  127. * @private
  128. * @returns {void}
  129. */
  130. _onHideDialog() {
  131. this.setState({ showDialog: false });
  132. }
  133. /**
  134. * Displays the {@code InlineDialog} and calls any registered onPopoverOpen
  135. * callbacks.
  136. *
  137. * @private
  138. * @returns {void}
  139. */
  140. _onShowDialog() {
  141. if (!this.props.disablePopover) {
  142. this.setState({ showDialog: true });
  143. if (this.props.onPopoverOpen) {
  144. this.props.onPopoverOpen();
  145. }
  146. }
  147. }
  148. /**
  149. * Renders the React Element to be displayed in the {@code InlineDialog}.
  150. * Also adds padding to support moving the mouse from the trigger to the
  151. * dialog to prevent mouseleave events.
  152. *
  153. * @private
  154. * @returns {ReactElement}
  155. */
  156. _renderContent() {
  157. const { content, position } = this.props;
  158. return (
  159. <div className = 'popover'>
  160. { content }
  161. <div className = 'popover-mouse-padding-top' />
  162. <div className = { _mapPositionToPaddingClass(position) } />
  163. </div>
  164. );
  165. }
  166. }
  167. export default Popover;