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.

OverflowMenuButton.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* @flow */
  2. import InlineDialog from '@atlaskit/inline-dialog';
  3. import React, { Component } from 'react';
  4. import { createToolbarEvent, sendAnalytics } from '../../../analytics';
  5. import { translate } from '../../../base/i18n';
  6. import { IconHorizontalPoints } from '../../../base/icons';
  7. import { connect } from '../../../base/redux';
  8. import { ReactionEmoji, ReactionsMenu } from '../../../reactions/components';
  9. import { type ReactionEmojiProps } from '../../../reactions/constants';
  10. import { getReactionsQueue } from '../../../reactions/functions.any';
  11. import Drawer from './Drawer';
  12. import DrawerPortal from './DrawerPortal';
  13. import ToolbarButton from './ToolbarButton';
  14. /**
  15. * The type of the React {@code Component} props of {@link OverflowMenuButton}.
  16. */
  17. type Props = {
  18. /**
  19. * ID of the menu that is controlled by this button.
  20. */
  21. ariaControls: String,
  22. /**
  23. * A child React Element to display within {@code InlineDialog}.
  24. */
  25. children: React$Node,
  26. /**
  27. * Whether or not the OverflowMenu popover should display.
  28. */
  29. isOpen: boolean,
  30. /**
  31. * Callback to change the visibility of the overflow menu.
  32. */
  33. onVisibilityChange: Function,
  34. /**
  35. * Whether to display the OverflowMenu as a drawer.
  36. */
  37. overflowDrawer: boolean,
  38. /**
  39. * Invoked to obtain translated strings.
  40. */
  41. t: Function,
  42. /**
  43. * The array of reactions to be displayed.
  44. */
  45. reactionsQueue: Array<ReactionEmojiProps>,
  46. /**
  47. * Whether or not to display the reactions in the mobile menu.
  48. */
  49. showMobileReactions: boolean
  50. };
  51. /**
  52. * A React {@code Component} for opening or closing the {@code OverflowMenu}.
  53. *
  54. * @extends Component
  55. */
  56. class OverflowMenuButton extends Component<Props> {
  57. /**
  58. * Initializes a new {@code OverflowMenuButton} instance.
  59. *
  60. * @param {Object} props - The read-only properties with which the new
  61. * instance is to be initialized.
  62. */
  63. constructor(props: Props) {
  64. super(props);
  65. // Bind event handlers so they are only bound once per instance.
  66. this._onCloseDialog = this._onCloseDialog.bind(this);
  67. this._onToggleDialogVisibility
  68. = this._onToggleDialogVisibility.bind(this);
  69. this._onEscClick = this._onEscClick.bind(this);
  70. }
  71. _onEscClick: (KeyboardEvent) => void;
  72. /**
  73. * Click handler for the more actions entries.
  74. *
  75. * @param {KeyboardEvent} event - Esc key click to close the popup.
  76. * @returns {void}
  77. */
  78. _onEscClick(event) {
  79. if (event.key === 'Escape' && this.props.isOpen) {
  80. event.preventDefault();
  81. event.stopPropagation();
  82. this._onCloseDialog();
  83. }
  84. }
  85. /**
  86. * Implements React's {@link Component#render()}.
  87. *
  88. * @inheritdoc
  89. * @returns {ReactElement}
  90. */
  91. render() {
  92. const { children, isOpen, overflowDrawer, reactionsQueue, showMobileReactions } = this.props;
  93. return (
  94. <div className = 'toolbox-button-wth-dialog'>
  95. {
  96. overflowDrawer ? (
  97. <>
  98. {this._renderToolbarButton()}
  99. <DrawerPortal>
  100. <Drawer
  101. isOpen = { isOpen }
  102. onClose = { this._onCloseDialog }>
  103. {children}
  104. {showMobileReactions && <ReactionsMenu overflowMenu = { true } />}
  105. </Drawer>
  106. {showMobileReactions && <div className = 'reactions-animations-container'>
  107. {reactionsQueue.map(({ reaction, uid }, index) => (<ReactionEmoji
  108. index = { index }
  109. key = { uid }
  110. reaction = { reaction }
  111. uid = { uid } />))}
  112. </div>}
  113. </DrawerPortal>
  114. </>
  115. ) : (
  116. <InlineDialog
  117. content = { children }
  118. isOpen = { isOpen }
  119. onClose = { this._onCloseDialog }
  120. placement = 'top-end'>
  121. {this._renderToolbarButton()}
  122. </InlineDialog>
  123. )
  124. }
  125. </div>
  126. );
  127. }
  128. _renderToolbarButton: () => React$Node;
  129. /**
  130. * Renders the actual toolbar overflow menu button.
  131. *
  132. * @returns {ReactElement}
  133. */
  134. _renderToolbarButton() {
  135. const { ariaControls, isOpen, t } = this.props;
  136. return (
  137. <ToolbarButton
  138. accessibilityLabel =
  139. { t('toolbar.accessibilityLabel.moreActions') }
  140. aria-controls = { ariaControls }
  141. aria-haspopup = 'true'
  142. icon = { IconHorizontalPoints }
  143. onClick = { this._onToggleDialogVisibility }
  144. onKeyDown = { this._onEscClick }
  145. toggled = { isOpen }
  146. tooltip = { t('toolbar.moreActions') } />
  147. );
  148. }
  149. _onCloseDialog: () => void;
  150. /**
  151. * Callback invoked when {@code InlineDialog} signals that it should be
  152. * close.
  153. *
  154. * @private
  155. * @returns {void}
  156. */
  157. _onCloseDialog() {
  158. this.props.onVisibilityChange(false);
  159. }
  160. _onToggleDialogVisibility: () => void;
  161. /**
  162. * Callback invoked to signal that an event has occurred that should change
  163. * the visibility of the {@code InlineDialog} component.
  164. *
  165. * @private
  166. * @returns {void}
  167. */
  168. _onToggleDialogVisibility() {
  169. sendAnalytics(createToolbarEvent('overflow'));
  170. this.props.onVisibilityChange(!this.props.isOpen);
  171. }
  172. }
  173. /**
  174. * Maps (parts of) the Redux state to the associated props for the
  175. * {@code OverflowMenuButton} component.
  176. *
  177. * @param {Object} state - The Redux state.
  178. * @returns {Props}
  179. */
  180. function mapStateToProps(state) {
  181. const { overflowDrawer } = state['features/toolbox'];
  182. return {
  183. overflowDrawer,
  184. reactionsQueue: getReactionsQueue(state)
  185. };
  186. }
  187. export default translate(connect(mapStateToProps)(OverflowMenuButton));