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 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React, { useCallback } from 'react';
  2. import { Icon } from '../base/icons';
  3. type Props = {
  4. /**
  5. * Accessibility label for button.
  6. */
  7. accessibilityLabel: string,
  8. /**
  9. * An extra class name to be added at the end of the element's class name
  10. * in order to enable custom styling.
  11. */
  12. customClass?: string,
  13. /**
  14. * Whether or not the button is disabled.
  15. */
  16. disabled?: boolean,
  17. /**
  18. * Click handler.
  19. */
  20. onClick: Function,
  21. /**
  22. * Button icon.
  23. */
  24. icon: Object,
  25. /**
  26. * Whether or not the button is toggled.
  27. */
  28. toggled?: boolean
  29. }
  30. const ToolbarButton = ({
  31. accessibilityLabel,
  32. customClass,
  33. disabled = false,
  34. onClick,
  35. icon,
  36. toggled = false
  37. }: Props) => {
  38. const onKeyPress = useCallback(event => {
  39. if (event.key === 'Enter' || event.key === ' ') {
  40. event.preventDefault();
  41. onClick();
  42. }
  43. }, [ onClick ]);
  44. return (<div
  45. aria-disabled = { disabled }
  46. aria-label = { accessibilityLabel }
  47. aria-pressed = { toggled }
  48. className = { `toolbox-button ${disabled ? ' disabled' : ''}` }
  49. onClick = { disabled ? undefined : onClick }
  50. onKeyPress = { disabled ? undefined : onKeyPress }
  51. role = 'button'
  52. tabIndex = { 0 }>
  53. <div className = { `toolbox-icon ${disabled ? 'disabled' : ''} ${customClass ?? ''}` }>
  54. <Icon src = { icon } />
  55. </div>
  56. </div>);
  57. };
  58. export default ToolbarButton;