2020-04-16 23:19:37 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
2023-06-07 15:05:35 +02:00
|
|
|
import IconButton from '../IconButton';
|
|
|
|
|
import Icon from '../Icon';
|
|
|
|
|
import Tooltip from '../Tooltip';
|
2020-04-16 23:19:37 +02:00
|
|
|
|
|
|
|
|
const ToolbarButton = ({
|
2022-11-21 16:44:19 +01:00
|
|
|
type = 'tool',
|
2020-04-16 23:19:37 +02:00
|
|
|
id,
|
|
|
|
|
icon,
|
|
|
|
|
label,
|
2022-07-27 18:39:04 +02:00
|
|
|
commands,
|
2020-09-15 03:18:46 +02:00
|
|
|
onInteraction,
|
2020-04-16 23:19:37 +02:00
|
|
|
dropdownContent,
|
2020-09-15 03:18:46 +02:00
|
|
|
//
|
2023-09-22 16:23:44 +02:00
|
|
|
isActive,
|
2023-01-06 21:00:23 +01:00
|
|
|
className,
|
|
|
|
|
...rest
|
2020-09-15 03:18:46 +02:00
|
|
|
//
|
2020-04-16 23:19:37 +02:00
|
|
|
}) => {
|
2020-04-17 22:25:59 +02:00
|
|
|
const classes = {
|
2022-11-21 16:44:19 +01:00
|
|
|
tool: isActive
|
|
|
|
|
? 'text-black'
|
2023-02-28 21:57:19 +01:00
|
|
|
: 'text-common-bright hover:!bg-primary-dark hover:text-primary-light',
|
2022-11-21 16:44:19 +01:00
|
|
|
toggle: isActive
|
2023-02-28 21:57:19 +01:00
|
|
|
? '!text-[#348CFD]'
|
|
|
|
|
: 'text-common-bright hover:!bg-primary-dark hover:text-primary-light',
|
2022-11-21 16:44:19 +01:00
|
|
|
action: isActive
|
|
|
|
|
? 'text-black'
|
2023-02-28 21:57:19 +01:00
|
|
|
: 'text-common-bright hover:!bg-primary-dark hover:text-primary-light',
|
2022-11-21 16:44:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const bgClasses = {
|
|
|
|
|
toggle: isActive && 'bg-transparent',
|
2020-04-17 22:25:59 +02:00
|
|
|
};
|
2021-03-01 16:52:54 +01:00
|
|
|
|
|
|
|
|
const activeClass = isActive ? 'active' : '';
|
2020-04-16 23:19:37 +02:00
|
|
|
const shouldShowDropdown = !!isActive && !!dropdownContent;
|
2023-09-01 22:19:39 +02:00
|
|
|
const iconEl = icon ? <Icon name={icon} /> : <div>{label || 'Missing icon and label'}</div>;
|
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'}
|
2022-11-21 16:44:19 +01:00
|
|
|
bgColor={bgClasses[type]}
|
2020-09-21 10:42:53 +02:00
|
|
|
size="toolbar"
|
2023-01-06 21:00:23 +01:00
|
|
|
className={classnames(activeClass, classes[type], className)}
|
2020-09-15 03:18:46 +02:00
|
|
|
onClick={() => {
|
|
|
|
|
onInteraction({
|
|
|
|
|
itemId: id,
|
|
|
|
|
interactionType: type,
|
2022-07-27 18:39:04 +02:00
|
|
|
commands,
|
2020-09-15 03:18:46 +02:00
|
|
|
});
|
|
|
|
|
}}
|
2021-03-01 16:52:54 +01:00
|
|
|
name={label}
|
2020-04-16 23:19:37 +02:00
|
|
|
key={id}
|
2021-03-01 16:52:54 +01:00
|
|
|
id={id}
|
2023-01-06 21:00:23 +01:00
|
|
|
{...rest}
|
2020-04-16 23:19:37 +02:00
|
|
|
>
|
2023-03-15 17:41:41 +01:00
|
|
|
{iconEl}
|
2020-04-16 23:19:37 +02:00
|
|
|
</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,
|
2023-09-22 16:23:44 +02:00
|
|
|
className: PropTypes.string,
|
|
|
|
|
commands: PropTypes.arrayOf(
|
|
|
|
|
PropTypes.shape({
|
|
|
|
|
commandName: PropTypes.string.isRequired,
|
|
|
|
|
commandOptions: PropTypes.object,
|
|
|
|
|
})
|
|
|
|
|
),
|
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;
|