2018-10-30 05:02:23 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2017-10-06 16:14:45 +00:00
|
|
|
import Tooltip from '@atlaskit/tooltip';
|
2018-04-10 03:27:19 +00:00
|
|
|
import React from 'react';
|
2017-02-16 23:02:40 +00:00
|
|
|
|
2019-08-30 16:39:06 +00:00
|
|
|
import { Icon } from '../../../base/icons';
|
|
|
|
|
2018-05-10 23:01:55 +00:00
|
|
|
import AbstractToolbarButton from '../AbstractToolbarButton';
|
2018-10-30 05:02:23 +00:00
|
|
|
import type { Props as AbstractToolbarButtonProps }
|
|
|
|
from '../AbstractToolbarButton';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} props of {@link ToolbarButton}.
|
|
|
|
*/
|
|
|
|
type Props = AbstractToolbarButtonProps & {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The text to display in the tooltip.
|
|
|
|
*/
|
|
|
|
tooltip: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From which direction the tooltip should appear, relative to the
|
|
|
|
* button.
|
|
|
|
*/
|
|
|
|
tooltipPosition: string
|
|
|
|
};
|
2017-02-16 23:02:40 +00:00
|
|
|
|
|
|
|
/**
|
2018-04-10 03:27:19 +00:00
|
|
|
* Represents a button in the toolbar.
|
2017-02-16 23:02:40 +00:00
|
|
|
*
|
|
|
|
* @extends AbstractToolbarButton
|
|
|
|
*/
|
2018-10-30 05:02:23 +00:00
|
|
|
class ToolbarButton extends AbstractToolbarButton<Props> {
|
2018-04-10 03:27:19 +00:00
|
|
|
/**
|
|
|
|
* Default values for {@code ToolbarButton} component's properties.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static defaultProps = {
|
|
|
|
tooltipPosition: 'top'
|
|
|
|
};
|
2017-08-15 22:39:42 +00:00
|
|
|
|
2020-04-03 13:19:32 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new {@code ToolbarButton} instance.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this._onKeyDown = this._onKeyDown.bind(this);
|
|
|
|
this._onKeyUp = this._onKeyUp.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onKeyDown: (Object) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles 'Enter' key on the button to trigger onClick for accessibility.
|
|
|
|
*
|
|
|
|
* @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();
|
|
|
|
} else if (event.key === ' ') {
|
|
|
|
// Space triggers button onKeyUp but we need to prevent default here
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onKeyUp: (Object) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles ' '(Space) key on the button to trigger onClick for
|
|
|
|
* accessibility.
|
|
|
|
*
|
|
|
|
* @param {Object} event - The key event.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onKeyUp(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 === ' ') {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
this.props.onClick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 23:02:40 +00:00
|
|
|
/**
|
2018-04-10 03:27:19 +00:00
|
|
|
* Renders the button of this {@code ToolbarButton}.
|
2017-02-16 23:02:40 +00:00
|
|
|
*
|
2018-04-10 03:27:19 +00:00
|
|
|
* @param {Object} children - The children, if any, to be rendered inside
|
|
|
|
* the button. Presumably, contains the icon of this {@code ToolbarButton}.
|
|
|
|
* @protected
|
|
|
|
* @returns {ReactElement} The button of this {@code ToolbarButton}.
|
2017-02-16 23:02:40 +00:00
|
|
|
*/
|
2018-04-10 03:27:19 +00:00
|
|
|
_renderButton(children) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
aria-label = { this.props.accessibilityLabel }
|
2020-04-03 13:19:32 +00:00
|
|
|
aria-pressed = { this.props.toggled }
|
2018-04-10 03:27:19 +00:00
|
|
|
className = 'toolbox-button'
|
2020-04-03 13:19:32 +00:00
|
|
|
onClick = { this.props.onClick }
|
|
|
|
onKeyDown = { this._onKeyDown }
|
|
|
|
onKeyUp = { this._onKeyUp }
|
|
|
|
role = 'button'
|
|
|
|
tabIndex = { 0 }>
|
2018-04-12 21:14:14 +00:00
|
|
|
{ this.props.tooltip
|
|
|
|
? <Tooltip
|
2018-04-16 17:21:01 +00:00
|
|
|
content = { this.props.tooltip }
|
2018-04-12 21:14:14 +00:00
|
|
|
position = { this.props.tooltipPosition }>
|
|
|
|
{ children }
|
|
|
|
</Tooltip>
|
|
|
|
: children }
|
2018-04-10 03:27:19 +00:00
|
|
|
</div>
|
|
|
|
);
|
2017-02-16 23:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-10 03:27:19 +00:00
|
|
|
* Renders the icon of this {@code ToolbarButton}.
|
2017-02-16 23:02:40 +00:00
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2018-04-10 03:27:19 +00:00
|
|
|
_renderIcon() {
|
2017-12-26 20:28:32 +00:00
|
|
|
return (
|
2019-08-30 16:39:06 +00:00
|
|
|
<div className = { `toolbox-icon ${this.props.toggled ? 'toggled' : ''}` }>
|
|
|
|
<Icon src = { this.props.icon } />
|
2017-12-26 20:28:32 +00:00
|
|
|
</div>
|
|
|
|
);
|
2017-02-16 23:02:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 03:27:19 +00:00
|
|
|
export default ToolbarButton;
|