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.

ToolbarButton.js 3.1KB

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