2020-04-16 23:19:37 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2024-03-28 21:45:11 +01:00
|
|
|
import classNames from 'classnames';
|
2020-04-16 23:19:37 +02:00
|
|
|
|
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 = ({
|
|
|
|
|
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-01-06 21:00:23 +01:00
|
|
|
className,
|
2024-03-28 21:45:11 +01:00
|
|
|
disabled,
|
2024-04-03 05:33:04 +02:00
|
|
|
disabledText,
|
2024-03-27 21:01:32 +01:00
|
|
|
size,
|
|
|
|
|
toolTipClassName,
|
2024-03-28 15:40:32 +01:00
|
|
|
disableToolTip = false,
|
2023-01-06 21:00:23 +01:00
|
|
|
...rest
|
2020-09-15 03:18:46 +02:00
|
|
|
//
|
2020-04-16 23:19:37 +02:00
|
|
|
}) => {
|
2024-03-27 21:01:32 +01:00
|
|
|
const shouldShowDropdown = !!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
|
|
|
|
2024-03-27 21:01:32 +01:00
|
|
|
const sizeToUse = size ?? 'toolbar';
|
|
|
|
|
const toolTipClassNameToUse =
|
|
|
|
|
toolTipClassName !== undefined
|
|
|
|
|
? toolTipClassName
|
|
|
|
|
: sizeToUse === 'toolbar'
|
|
|
|
|
? 'w-[40px] h-[40px]'
|
|
|
|
|
: 'w-[32px] h-[32px]';
|
|
|
|
|
|
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}
|
2024-04-03 05:33:04 +02:00
|
|
|
secondaryContent={disabledText}
|
2020-04-16 23:19:37 +02:00
|
|
|
tight={shouldShowDropdown}
|
2024-03-27 21:01:32 +01:00
|
|
|
className={toolTipClassNameToUse}
|
2024-03-28 15:40:32 +01:00
|
|
|
isDisabled={disableToolTip}
|
2020-04-16 23:19:37 +02:00
|
|
|
>
|
|
|
|
|
<IconButton
|
2024-03-27 21:01:32 +01:00
|
|
|
size={sizeToUse}
|
2024-03-28 21:45:11 +01:00
|
|
|
className={classNames(className, disabled ? '!cursor-default' : '')}
|
2020-09-15 03:18:46 +02:00
|
|
|
onClick={() => {
|
|
|
|
|
onInteraction({
|
|
|
|
|
itemId: id,
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ToolbarButton.propTypes = {
|
2020-06-15 03:57:45 +02:00
|
|
|
/* Influences background/hover styling */
|
2020-04-16 23:19:37 +02:00
|
|
|
id: PropTypes.string.isRequired,
|
2023-09-22 16:23:44 +02:00
|
|
|
className: PropTypes.string,
|
2024-03-27 21:01:32 +01:00
|
|
|
commands: PropTypes.oneOfType([PropTypes.array, PropTypes.object, PropTypes.string]),
|
|
|
|
|
onInteraction: PropTypes.func,
|
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]),
|
2024-03-27 21:01:32 +01:00
|
|
|
size: PropTypes.string,
|
|
|
|
|
toolTipClassName: PropTypes.string,
|
2024-03-28 15:40:32 +01:00
|
|
|
disableToolTip: PropTypes.bool,
|
2024-03-28 21:45:11 +01:00
|
|
|
disabled: PropTypes.bool,
|
2020-04-16 23:19:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ToolbarButton;
|