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

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