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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * keyDown handler
  23. */
  24. onKeyDown?: Function
  25. };
  26. /**
  27. * Represents a button in the toolbar.
  28. *
  29. * @extends AbstractToolbarButton
  30. */
  31. class ToolbarButton extends AbstractToolbarButton<Props> {
  32. /**
  33. * Default values for {@code ToolbarButton} component's properties.
  34. *
  35. * @static
  36. */
  37. static defaultProps = {
  38. tooltipPosition: 'top'
  39. };
  40. /**
  41. * Initializes a new {@code ToolbarButton} instance.
  42. *
  43. * @inheritdoc
  44. */
  45. constructor(props: Props) {
  46. super(props);
  47. this._onKeyPress = this._onKeyPress.bind(this);
  48. this._onClick = this._onClick.bind(this);
  49. }
  50. _onKeyPress: (Object) => void;
  51. /**
  52. * Handles 'Enter' and Space key on the button to trigger onClick for accessibility.
  53. *
  54. * @param {Object} event - The key event.
  55. * @private
  56. * @returns {void}
  57. */
  58. _onKeyPress(event) {
  59. if (event.key === 'Enter' || event.key === ' ') {
  60. event.preventDefault();
  61. this.props.onClick();
  62. }
  63. }
  64. _onClick: (Object) => void;
  65. /**
  66. * Handles button click.
  67. *
  68. * @param {Object} e - The key event.
  69. * @private
  70. * @returns {void}
  71. */
  72. _onClick(e) {
  73. this.props.onClick(e);
  74. // blur after click to release focus from button to allow PTT.
  75. e && e.currentTarget && e.currentTarget.blur();
  76. }
  77. /**
  78. * Renders the button of this {@code ToolbarButton}.
  79. *
  80. * @param {Object} children - The children, if any, to be rendered inside
  81. * the button. Presumably, contains the icon of this {@code ToolbarButton}.
  82. * @protected
  83. * @returns {ReactElement} The button of this {@code ToolbarButton}.
  84. */
  85. _renderButton(children) {
  86. return (
  87. <div
  88. aria-label = { this.props.accessibilityLabel }
  89. aria-pressed = { this.props.toggled }
  90. className = 'toolbox-button'
  91. onClick = { this._onClick }
  92. onKeyDown = { this.props.onKeyDown }
  93. onKeyPress = { this._onKeyPress }
  94. role = 'button'
  95. tabIndex = { 0 }>
  96. { this.props.tooltip
  97. ? <Tooltip
  98. content = { this.props.tooltip }
  99. position = { this.props.tooltipPosition }>
  100. { children }
  101. </Tooltip>
  102. : children }
  103. </div>
  104. );
  105. }
  106. /**
  107. * Renders the icon of this {@code ToolbarButton}.
  108. *
  109. * @inheritdoc
  110. */
  111. _renderIcon() {
  112. return (
  113. <div className = { `toolbox-icon ${this.props.toggled ? 'toggled' : ''}` }>
  114. <Icon src = { this.props.icon } />
  115. </div>
  116. );
  117. }
  118. }
  119. export default ToolbarButton;