2020-04-16 23:19:37 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
2020-08-14 20:25:08 +02:00
|
|
|
import { IconButton, Icon, Tooltip } from '../';
|
2020-04-16 23:19:37 +02:00
|
|
|
|
|
|
|
|
const ToolbarButton = ({
|
2020-04-17 22:25:59 +02:00
|
|
|
type,
|
2020-04-16 23:19:37 +02:00
|
|
|
id,
|
|
|
|
|
icon,
|
|
|
|
|
label,
|
2020-09-15 03:18:46 +02:00
|
|
|
commandName,
|
|
|
|
|
commandOptions,
|
|
|
|
|
onInteraction,
|
2020-04-16 23:19:37 +02:00
|
|
|
dropdownContent,
|
2020-09-15 03:18:46 +02:00
|
|
|
//
|
|
|
|
|
isActive: _isActive,
|
|
|
|
|
bState = {},
|
|
|
|
|
//
|
2020-04-16 23:19:37 +02:00
|
|
|
}) => {
|
2020-09-15 03:18:46 +02:00
|
|
|
const { primaryToolId, toggles, groups } = bState;
|
|
|
|
|
const isActive = _isActive || (type === 'tool' && id === primaryToolId);
|
2020-04-17 22:25:59 +02:00
|
|
|
const classes = {
|
|
|
|
|
type: {
|
|
|
|
|
primary: isActive
|
|
|
|
|
? 'text-black'
|
|
|
|
|
: 'text-common-bright hover:bg-primary-dark hover:text-primary-light',
|
|
|
|
|
secondary: isActive
|
|
|
|
|
? 'text-black'
|
|
|
|
|
: 'text-white hover:bg-secondary-dark hover:text-white focus:bg-secondary-dark focus:text-white',
|
|
|
|
|
},
|
|
|
|
|
};
|
2020-04-16 23:19:37 +02:00
|
|
|
const shouldShowDropdown = !!isActive && !!dropdownContent;
|
2020-06-24 17:11:54 +02:00
|
|
|
|
2020-04-16 23:19:37 +02:00
|
|
|
return (
|
|
|
|
|
<div key={id}>
|
|
|
|
|
<Tooltip
|
2020-06-15 05:42:44 +02:00
|
|
|
isSticky={shouldShowDropdown}
|
2020-04-16 23:19:37 +02:00
|
|
|
content={shouldShowDropdown ? dropdownContent : label}
|
|
|
|
|
tight={shouldShowDropdown}
|
|
|
|
|
>
|
|
|
|
|
<IconButton
|
|
|
|
|
variant={isActive ? 'contained' : 'text'}
|
2020-04-17 22:25:59 +02:00
|
|
|
className={classnames('mx-1', classes.type[type])}
|
2020-09-15 03:18:46 +02:00
|
|
|
onClick={() => {
|
|
|
|
|
onInteraction({
|
|
|
|
|
itemId: id,
|
|
|
|
|
interactionType: type,
|
|
|
|
|
commandName: commandName,
|
|
|
|
|
commandOptions: commandOptions,
|
|
|
|
|
});
|
|
|
|
|
}}
|
2020-04-16 23:19:37 +02:00
|
|
|
key={id}
|
|
|
|
|
>
|
|
|
|
|
<Icon name={icon} />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ToolbarButton.defaultProps = {
|
|
|
|
|
dropdownContent: null,
|
|
|
|
|
isActive: false,
|
2020-09-15 03:18:46 +02:00
|
|
|
type: 'action',
|
2020-04-16 23:19:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ToolbarButton.propTypes = {
|
2020-06-15 03:57:45 +02:00
|
|
|
/* Influences background/hover styling */
|
2020-09-15 03:18:46 +02:00
|
|
|
type: PropTypes.oneOf(['action', 'toggle', 'tool']),
|
2020-04-16 23:19:37 +02:00
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
|
isActive: PropTypes.bool,
|
2020-09-15 03:18:46 +02:00
|
|
|
onInteraction: PropTypes.func.isRequired,
|
2020-04-16 23:19:37 +02:00
|
|
|
icon: PropTypes.string.isRequired,
|
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
|
/** Tooltip content can be replaced for a customized content by passing a node to this value. */
|
2020-06-15 06:51:36 +02:00
|
|
|
dropdownContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
|
2020-04-16 23:19:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ToolbarButton;
|