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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* @flow */
  2. import React from 'react';
  3. import { Tooltip } from '../../../base/tooltip';
  4. import AbstractToolbarButton from '../../../toolbox/components/AbstractToolbarButton';
  5. import type { Props as AbstractToolbarButtonProps } from '../../../toolbox/components/AbstractToolbarButton';
  6. /**
  7. * The type of the React {@code Component} props of {@link ReactionButton}.
  8. */
  9. type Props = AbstractToolbarButtonProps & {
  10. /**
  11. * Optional text to display in the tooltip.
  12. */
  13. tooltip?: string,
  14. /**
  15. * From which direction the tooltip should appear, relative to the
  16. * button.
  17. */
  18. tooltipPosition: string,
  19. /**
  20. * Optional label for the button
  21. */
  22. label?: string
  23. };
  24. /**
  25. * Represents a button in the reactions menu.
  26. *
  27. * @extends AbstractToolbarButton
  28. */
  29. class ReactionButton extends AbstractToolbarButton<Props> {
  30. /**
  31. * Default values for {@code ReactionButton} component's properties.
  32. *
  33. * @static
  34. */
  35. static defaultProps = {
  36. tooltipPosition: 'top'
  37. };
  38. /**
  39. * Initializes a new {@code ReactionButton} instance.
  40. *
  41. * @inheritdoc
  42. */
  43. constructor(props: Props) {
  44. super(props);
  45. this._onKeyDown = this._onKeyDown.bind(this);
  46. }
  47. _onKeyDown: (Object) => void;
  48. /**
  49. * Handles 'Enter' key on the button to trigger onClick for accessibility.
  50. * We should be handling Space onKeyUp but it conflicts with PTT.
  51. *
  52. * @param {Object} event - The key event.
  53. * @private
  54. * @returns {void}
  55. */
  56. _onKeyDown(event) {
  57. // If the event coming to the dialog has been subject to preventDefault
  58. // we don't handle it here.
  59. if (event.defaultPrevented) {
  60. return;
  61. }
  62. if (event.key === 'Enter') {
  63. event.preventDefault();
  64. event.stopPropagation();
  65. this.props.onClick();
  66. }
  67. }
  68. /**
  69. * Renders the button of this {@code ReactionButton}.
  70. *
  71. * @param {Object} children - The children, if any, to be rendered inside
  72. * the button. Presumably, contains the emoji of this {@code ReactionButton}.
  73. * @protected
  74. * @returns {ReactElement} The button of this {@code ReactionButton}.
  75. */
  76. _renderButton(children) {
  77. return (
  78. <div
  79. aria-label = { this.props.accessibilityLabel }
  80. aria-pressed = { this.props.toggled }
  81. className = 'toolbox-button'
  82. onClick = { this.props.onClick }
  83. onKeyDown = { this._onKeyDown }
  84. role = 'button'
  85. tabIndex = { 0 }>
  86. { this.props.tooltip
  87. ? <Tooltip
  88. content = { this.props.tooltip }
  89. position = { this.props.tooltipPosition }>
  90. { children }
  91. </Tooltip>
  92. : children }
  93. </div>
  94. );
  95. }
  96. /**
  97. * Renders the icon (emoji) of this {@code reactionButton}.
  98. *
  99. * @inheritdoc
  100. */
  101. _renderIcon() {
  102. return (
  103. <div className = { `toolbox-icon ${this.props.toggled ? 'toggled' : ''}` }>
  104. <span className = 'emoji'>{this.props.icon}</span>
  105. {this.props.label && <span className = 'text'>{this.props.label}</span>}
  106. </div>
  107. );
  108. }
  109. }
  110. export default ReactionButton;