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.

ReactionButton.tsx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import React, { Component } from 'react';
  2. import Tooltip from '../../../base/tooltip/components/Tooltip';
  3. import { TOOLTIP_POSITION } from '../../../base/ui/constants.any';
  4. /**
  5. * The type of the React {@code Component} props of {@link ReactionButton}.
  6. */
  7. interface IProps {
  8. /**
  9. * A succinct description of what the button does. Used by accessibility
  10. * tools and torture tests.
  11. */
  12. accessibilityLabel: string;
  13. /**
  14. * The Icon of this {@code AbstractToolbarButton}.
  15. */
  16. icon: Object;
  17. /**
  18. * The style of the Icon of this {@code AbstractToolbarButton}.
  19. */
  20. iconStyle?: Object;
  21. /**
  22. * Optional label for the button.
  23. */
  24. label?: string;
  25. /**
  26. * On click handler.
  27. */
  28. onClick: Function;
  29. /**
  30. * {@code AbstractToolbarButton} Styles.
  31. */
  32. style?: Array<string> | Object;
  33. /**
  34. * An optional modifier to render the button toggled.
  35. */
  36. toggled?: boolean;
  37. /**
  38. * Optional text to display in the tooltip.
  39. */
  40. tooltip?: string;
  41. /**
  42. * From which direction the tooltip should appear, relative to the
  43. * button.
  44. */
  45. tooltipPosition: TOOLTIP_POSITION;
  46. /**
  47. * The color underlying the button.
  48. */
  49. underlayColor?: any;
  50. }
  51. /**
  52. * The type of the React {@code Component} state of {@link ReactionButton}.
  53. */
  54. interface IState {
  55. /**
  56. * Used to determine zoom level on reaction burst.
  57. */
  58. increaseLevel: number;
  59. /**
  60. * Timeout ID to reset reaction burst.
  61. */
  62. increaseTimeout: number | null;
  63. }
  64. /**
  65. * Represents a button in the reactions menu.
  66. *
  67. * @augments AbstractToolbarButton
  68. */
  69. class ReactionButton extends Component<IProps, IState> {
  70. /**
  71. * Default values for {@code ReactionButton} component's properties.
  72. *
  73. * @static
  74. */
  75. static defaultProps = {
  76. tooltipPosition: 'top'
  77. };
  78. /**
  79. * Initializes a new {@code ReactionButton} instance.
  80. *
  81. * @inheritdoc
  82. */
  83. constructor(props: IProps) {
  84. super(props);
  85. this._onKeyDown = this._onKeyDown.bind(this);
  86. this._onClickHandler = this._onClickHandler.bind(this);
  87. this._onClick = this._onClick.bind(this);
  88. this.state = {
  89. increaseLevel: 0,
  90. increaseTimeout: null
  91. };
  92. }
  93. /**
  94. * Handles clicking/pressing this {@code AbstractToolbarButton} by
  95. * forwarding the event to the {@code onClick} prop of this instance if any.
  96. *
  97. * @protected
  98. * @returns {*} The result returned by the invocation of the {@code onClick}
  99. * prop of this instance if any.
  100. */
  101. _onClick(...args: any) {
  102. const { onClick } = this.props;
  103. return onClick?.(...args);
  104. }
  105. /**
  106. * Handles 'Enter' key on the button to trigger onClick for accessibility.
  107. * We should be handling Space onKeyUp but it conflicts with PTT.
  108. *
  109. * @param {Object} event - The key event.
  110. * @private
  111. * @returns {void}
  112. */
  113. _onKeyDown(event: React.KeyboardEvent) {
  114. // If the event coming to the dialog has been subject to preventDefault
  115. // we don't handle it here.
  116. if (event.defaultPrevented) {
  117. return;
  118. }
  119. if (event.key === 'Enter') {
  120. event.preventDefault();
  121. event.stopPropagation();
  122. this.props.onClick();
  123. }
  124. }
  125. /**
  126. * Handles reaction button click.
  127. *
  128. * @param {Event} event - The click event.
  129. * @returns {void}
  130. */
  131. _onClickHandler(event: any) {
  132. event.preventDefault();
  133. event.stopPropagation();
  134. this.props.onClick();
  135. clearTimeout(this.state.increaseTimeout ?? 0);
  136. const timeout = window.setTimeout(() => {
  137. this.setState({
  138. increaseLevel: 0
  139. });
  140. }, 500);
  141. this.setState(state => {
  142. return {
  143. increaseLevel: state.increaseLevel + 1,
  144. increaseTimeout: timeout
  145. };
  146. });
  147. }
  148. /**
  149. * Renders the button of this {@code ReactionButton}.
  150. *
  151. * @param {Object} children - The children, if any, to be rendered inside
  152. * the button. Presumably, contains the emoji of this {@code ReactionButton}.
  153. * @protected
  154. * @returns {ReactElement} The button of this {@code ReactionButton}.
  155. */
  156. _renderButton(children: React.ReactElement) {
  157. return (
  158. <div
  159. aria-label = { this.props.accessibilityLabel }
  160. aria-pressed = { this.props.toggled }
  161. className = 'toolbox-button'
  162. onClick = { this._onClickHandler }
  163. onKeyDown = { this._onKeyDown }
  164. role = 'button'
  165. tabIndex = { 0 }>
  166. { this.props.tooltip
  167. ? <Tooltip
  168. content = { this.props.tooltip }
  169. position = { this.props.tooltipPosition }>
  170. { children }
  171. </Tooltip>
  172. : children }
  173. </div>
  174. );
  175. }
  176. /**
  177. * Renders the icon (emoji) of this {@code reactionButton}.
  178. *
  179. * @inheritdoc
  180. */
  181. _renderIcon() {
  182. const { toggled, icon, label } = this.props;
  183. const { increaseLevel } = this.state;
  184. return (
  185. <div className = { `toolbox-icon ${toggled ? 'toggled' : ''}` }>
  186. <span className = { `emoji increase-${increaseLevel > 12 ? 12 : increaseLevel}` }>{icon}</span>
  187. {label && <span className = 'text'>{label}</span>}
  188. </div>
  189. );
  190. }
  191. /**
  192. * Implements React's {@link Component#render()}.
  193. *
  194. * @inheritdoc
  195. * @returns {ReactElement}
  196. */
  197. render() {
  198. return this._renderButton(this._renderIcon());
  199. }
  200. }
  201. export default ReactionButton;