2021-07-13 06:50:08 +00:00
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import { Tooltip } from '../../../base/tooltip';
|
|
|
|
import AbstractToolbarButton from '../../../toolbox/components/AbstractToolbarButton';
|
|
|
|
import type { Props as AbstractToolbarButtonProps } from '../../../toolbox/components/AbstractToolbarButton';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} props of {@link ReactionButton}.
|
|
|
|
*/
|
|
|
|
type Props = AbstractToolbarButtonProps & {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Optional text to display in the tooltip.
|
|
|
|
*/
|
|
|
|
tooltip?: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From which direction the tooltip should appear, relative to the
|
|
|
|
* button.
|
|
|
|
*/
|
|
|
|
tooltipPosition: string,
|
|
|
|
|
|
|
|
/**
|
2021-11-04 21:10:43 +00:00
|
|
|
* Optional label for the button.
|
2021-07-13 06:50:08 +00:00
|
|
|
*/
|
|
|
|
label?: string
|
|
|
|
};
|
|
|
|
|
2021-08-23 09:57:56 +00:00
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} state of {@link ReactionButton}.
|
|
|
|
*/
|
|
|
|
type State = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to determine zoom level on reaction burst.
|
|
|
|
*/
|
|
|
|
increaseLevel: number,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Timeout ID to reset reaction burst.
|
|
|
|
*/
|
|
|
|
increaseTimeout: TimeoutID | null
|
|
|
|
}
|
|
|
|
|
2021-07-13 06:50:08 +00:00
|
|
|
/**
|
|
|
|
* Represents a button in the reactions menu.
|
|
|
|
*
|
2021-11-04 21:10:43 +00:00
|
|
|
* @augments AbstractToolbarButton
|
2021-07-13 06:50:08 +00:00
|
|
|
*/
|
2021-08-23 09:57:56 +00:00
|
|
|
class ReactionButton extends AbstractToolbarButton<Props, State> {
|
2021-07-13 06:50:08 +00:00
|
|
|
/**
|
|
|
|
* Default values for {@code ReactionButton} component's properties.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static defaultProps = {
|
|
|
|
tooltipPosition: 'top'
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new {@code ReactionButton} instance.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this._onKeyDown = this._onKeyDown.bind(this);
|
2021-08-23 09:57:56 +00:00
|
|
|
this._onClickHandler = this._onClickHandler.bind(this);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
increaseLevel: 0,
|
|
|
|
increaseTimeout: null
|
|
|
|
};
|
2021-07-13 06:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onKeyDown: (Object) => void;
|
|
|
|
|
2021-08-23 09:57:56 +00:00
|
|
|
_onClickHandler: () => void;
|
|
|
|
|
2021-07-13 06:50:08 +00:00
|
|
|
/**
|
|
|
|
* Handles 'Enter' key on the button to trigger onClick for accessibility.
|
|
|
|
* We should be handling Space onKeyUp but it conflicts with PTT.
|
|
|
|
*
|
|
|
|
* @param {Object} event - The key event.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onKeyDown(event) {
|
|
|
|
// If the event coming to the dialog has been subject to preventDefault
|
|
|
|
// we don't handle it here.
|
|
|
|
if (event.defaultPrevented) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
this.props.onClick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 09:57:56 +00:00
|
|
|
/**
|
|
|
|
* Handles reaction button click.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onClickHandler() {
|
|
|
|
this.props.onClick();
|
|
|
|
clearTimeout(this.state.increaseTimeout);
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
this.setState({
|
|
|
|
increaseLevel: 0
|
|
|
|
});
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
this.setState(state => {
|
|
|
|
return {
|
|
|
|
increaseLevel: state.increaseLevel + 1,
|
|
|
|
increaseTimeout: timeout
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-13 06:50:08 +00:00
|
|
|
/**
|
|
|
|
* Renders the button of this {@code ReactionButton}.
|
|
|
|
*
|
|
|
|
* @param {Object} children - The children, if any, to be rendered inside
|
|
|
|
* the button. Presumably, contains the emoji of this {@code ReactionButton}.
|
|
|
|
* @protected
|
|
|
|
* @returns {ReactElement} The button of this {@code ReactionButton}.
|
|
|
|
*/
|
|
|
|
_renderButton(children) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
aria-label = { this.props.accessibilityLabel }
|
|
|
|
aria-pressed = { this.props.toggled }
|
|
|
|
className = 'toolbox-button'
|
2021-08-23 09:57:56 +00:00
|
|
|
onClick = { this._onClickHandler }
|
2021-07-13 06:50:08 +00:00
|
|
|
onKeyDown = { this._onKeyDown }
|
|
|
|
role = 'button'
|
|
|
|
tabIndex = { 0 }>
|
|
|
|
{ this.props.tooltip
|
|
|
|
? <Tooltip
|
|
|
|
content = { this.props.tooltip }
|
|
|
|
position = { this.props.tooltipPosition }>
|
|
|
|
{ children }
|
|
|
|
</Tooltip>
|
|
|
|
: children }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the icon (emoji) of this {@code reactionButton}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
_renderIcon() {
|
2021-08-23 09:57:56 +00:00
|
|
|
const { toggled, icon, label } = this.props;
|
|
|
|
const { increaseLevel } = this.state;
|
|
|
|
|
2021-07-13 06:50:08 +00:00
|
|
|
return (
|
2021-08-23 09:57:56 +00:00
|
|
|
<div className = { `toolbox-icon ${toggled ? 'toggled' : ''}` }>
|
|
|
|
<span className = { `emoji increase-${increaseLevel > 12 ? 12 : increaseLevel}` }>{icon}</span>
|
|
|
|
{label && <span className = 'text'>{label}</span>}
|
2021-07-13 06:50:08 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ReactionButton;
|