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

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