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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Icon } from '../../icons';
  4. import { Tooltip } from '../../tooltip';
  5. type Props = {
  6. /**
  7. * The decorated component (ToolboxButton).
  8. */
  9. children: React$Node,
  10. /**
  11. * Icon of the button.
  12. */
  13. icon: Function,
  14. /**
  15. * Flag used for disabling the small icon.
  16. */
  17. iconDisabled: boolean,
  18. /**
  19. * Click handler for the small icon.
  20. */
  21. onIconClick: Function,
  22. /**
  23. * The tooltip used for the icon.
  24. */
  25. iconTooltip: string,
  26. /**
  27. * Additional styles.
  28. */
  29. styles?: Object,
  30. };
  31. type State = {
  32. /**
  33. * Whether the button is hovered or not.
  34. */
  35. isHovered: boolean,
  36. };
  37. /**
  38. * Displayes the `ToolboxButtonWithIcon` component.
  39. *
  40. * @returns {ReactElement}
  41. */
  42. export default class ToolboxButtonWithIcon extends Component<Props, State> {
  43. /**
  44. * Initializes a new {@code ToolboxButtonWithIcon} instance.
  45. *
  46. * @param {Props} props - The props of the component.
  47. */
  48. constructor(props: Props) {
  49. super(props);
  50. this.state = {
  51. isHovered: false
  52. };
  53. this._onMouseEnter = this._onMouseEnter.bind(this);
  54. this._onMouseLeave = this._onMouseLeave.bind(this);
  55. }
  56. _onMouseEnter: () => void;
  57. /**
  58. * Handler for when the small button has the mouse over.
  59. *
  60. * @returns {void}.
  61. */
  62. _onMouseEnter() {
  63. this.setState({
  64. isHovered: true
  65. });
  66. }
  67. _onMouseLeave: () => void;
  68. /**
  69. * Handler for when the mouse leaves the small button.
  70. *
  71. * @returns {void}
  72. */
  73. _onMouseLeave() {
  74. this.setState({
  75. isHovered: false
  76. });
  77. }
  78. /**
  79. * Implements React's {@link Component#render()}.
  80. *
  81. * @inheritdoc
  82. * @returns {React$Node}
  83. */
  84. render() {
  85. const {
  86. children,
  87. icon,
  88. iconDisabled,
  89. iconTooltip,
  90. onIconClick,
  91. styles
  92. } = this.props;
  93. const iconProps = {};
  94. let size = 9;
  95. if (iconDisabled) {
  96. iconProps.className
  97. = 'settings-button-small-icon settings-button-small-icon--disabled';
  98. } else {
  99. iconProps.className = 'settings-button-small-icon';
  100. iconProps.onClick = onIconClick;
  101. if (this.state.isHovered) {
  102. iconProps.className = `${iconProps.className} settings-button-small-icon--hovered`;
  103. size = 11;
  104. }
  105. }
  106. return (
  107. <div
  108. className = 'settings-button-container'
  109. styles = { styles }>
  110. {children}
  111. <div
  112. onMouseEnter = { this._onMouseEnter }
  113. onMouseLeave = { this._onMouseLeave }>
  114. <Tooltip
  115. content = { iconTooltip }
  116. position = 'top'>
  117. <Icon
  118. { ...iconProps }
  119. size = { size }
  120. src = { icon } />
  121. </Tooltip>
  122. </div>
  123. </div>
  124. );
  125. }
  126. }