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.

ToolboxButtonWithIcon.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // @flow
  2. import React from 'react';
  3. import { NOTIFY_CLICK_MODE } from '../../../../toolbox/constants';
  4. import { Icon } from '../../../icons';
  5. import Tooltip from '../../../tooltip/components/Tooltip';
  6. type Props = {
  7. /**
  8. * The button's key.
  9. */
  10. buttonKey?: string,
  11. /**
  12. * The decorated component (ToolboxButton).
  13. */
  14. children: React$Node,
  15. /**
  16. * Icon of the button.
  17. */
  18. icon: Function,
  19. /**
  20. * Flag used for disabling the small icon.
  21. */
  22. iconDisabled: boolean,
  23. /**
  24. * Notify mode for `toolbarButtonClicked` event -
  25. * whether to only notify or to also prevent button click routine.
  26. */
  27. notifyMode?: string,
  28. /**
  29. * Click handler for the small icon.
  30. */
  31. onIconClick: Function,
  32. /**
  33. * The tooltip used for the icon.
  34. */
  35. iconTooltip: string,
  36. /**
  37. * Additional styles.
  38. */
  39. styles?: Object,
  40. /**
  41. * Aria label for the Icon.
  42. */
  43. ariaLabel?: string,
  44. /**
  45. * Whether the element has a popup.
  46. */
  47. ariaHasPopup?: boolean,
  48. /**
  49. * Whether the element popup is expanded.
  50. */
  51. ariaExpanded?: boolean,
  52. /**
  53. * The id of the element this button icon controls.
  54. */
  55. ariaControls?: string,
  56. /**
  57. * Keydown handler for icon.
  58. */
  59. onIconKeyDown?: Function,
  60. /**
  61. * The ID of the icon button.
  62. */
  63. iconId: string
  64. };
  65. declare var APP: Object;
  66. /**
  67. * Displays the `ToolboxButtonWithIcon` component.
  68. *
  69. * @param {Object} props - Component's props.
  70. * @returns {ReactElement}
  71. */
  72. export default function ToolboxButtonWithIcon(props: Props) {
  73. const {
  74. children,
  75. icon,
  76. iconDisabled,
  77. iconTooltip,
  78. buttonKey,
  79. notifyMode,
  80. onIconClick,
  81. onIconKeyDown,
  82. styles,
  83. ariaLabel,
  84. ariaHasPopup,
  85. ariaControls,
  86. ariaExpanded,
  87. iconId
  88. } = props;
  89. const iconProps = {};
  90. let className = '';
  91. if (iconDisabled) {
  92. className
  93. = 'settings-button-small-icon settings-button-small-icon--disabled';
  94. } else {
  95. className = 'settings-button-small-icon';
  96. iconProps.onClick = () => {
  97. if (typeof APP !== 'undefined' && notifyMode) {
  98. APP.API.notifyToolbarButtonClicked(
  99. buttonKey, notifyMode === NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY
  100. );
  101. }
  102. if (notifyMode !== NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY) {
  103. onIconClick();
  104. }
  105. };
  106. iconProps.onKeyDown = onIconKeyDown;
  107. iconProps.role = 'button';
  108. iconProps.tabIndex = 0;
  109. iconProps.ariaControls = ariaControls;
  110. iconProps.ariaExpanded = ariaExpanded;
  111. iconProps.containerId = iconId;
  112. }
  113. return (
  114. <div
  115. className = 'settings-button-container'
  116. styles = { styles }>
  117. {children}
  118. <div>
  119. <Tooltip
  120. containerClassName = { className }
  121. content = { iconTooltip }
  122. position = 'top'>
  123. <Icon
  124. { ...iconProps }
  125. ariaHasPopup = { ariaHasPopup }
  126. ariaLabel = { ariaLabel }
  127. size = { 16 }
  128. src = { icon } />
  129. </Tooltip>
  130. </div>
  131. </div>
  132. );
  133. }