2020-09-15 15:01:07 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
const styled = require('styled-components').default;
|
|
|
|
const { space } = require('styled-system');
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied;
|
|
|
|
type StyleProps = any;
|
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
export enum ButtonLevel {
|
|
|
|
Primary = 'primary',
|
|
|
|
Secondary = 'secondary',
|
|
|
|
Tertiary = 'tertiary',
|
2021-01-12 14:28:55 +02:00
|
|
|
SidebarSecondary = 'sidebarSecondary',
|
2021-01-20 00:58:09 +02:00
|
|
|
Recommended = 'recommended',
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
export enum ButtonSize {
|
|
|
|
Small = 1,
|
|
|
|
Normal = 2,
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
interface Props {
|
2020-11-12 21:29:22 +02:00
|
|
|
title?: string;
|
|
|
|
iconName?: string;
|
|
|
|
level?: ButtonLevel;
|
2024-04-25 16:31:18 +02:00
|
|
|
iconLabel?: string;
|
2020-11-12 21:29:22 +02:00
|
|
|
className?: string;
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-13 19:09:28 +02:00
|
|
|
onClick?: Function;
|
2020-11-12 21:29:22 +02:00
|
|
|
color?: string;
|
|
|
|
iconAnimation?: string;
|
|
|
|
tooltip?: string;
|
|
|
|
disabled?: boolean;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied;
|
2020-11-12 21:29:22 +02:00
|
|
|
style?: any;
|
2021-10-03 17:00:49 +02:00
|
|
|
size?: ButtonSize;
|
2021-11-15 19:19:51 +02:00
|
|
|
isSquare?: boolean;
|
|
|
|
iconOnly?: boolean;
|
|
|
|
fontSize?: number;
|
2024-08-02 15:49:15 +02:00
|
|
|
|
|
|
|
'aria-controls'?: string;
|
|
|
|
'aria-expanded'?: string;
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const StyledTitle = styled.span`
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
2024-01-28 19:57:41 +02:00
|
|
|
export const buttonSizePx = (props: Props | ButtonSize) => {
|
|
|
|
const buttonSize = typeof props === 'number' ? props : props.size;
|
|
|
|
if (!buttonSize || buttonSize === ButtonSize.Normal) return 32;
|
|
|
|
if (buttonSize === ButtonSize.Small) return 26;
|
|
|
|
throw new Error(`Unknown size: ${buttonSize}`);
|
2021-10-03 17:00:49 +02:00
|
|
|
};
|
|
|
|
|
2021-11-15 19:19:51 +02:00
|
|
|
const isSquare = (props: Props) => {
|
|
|
|
return props.iconOnly || props.isSquare;
|
|
|
|
};
|
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
const StyledButtonBase = styled.button`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
flex-direction: row;
|
2021-11-11 17:33:37 +02:00
|
|
|
height: ${(props: Props) => buttonSizePx(props)}px;
|
|
|
|
min-height: ${(props: Props) => buttonSizePx(props)}px;
|
|
|
|
max-height: ${(props: Props) => buttonSizePx(props)}px;
|
2021-11-15 19:19:51 +02:00
|
|
|
width: ${(props: Props) => isSquare(props) ? `${buttonSizePx(props)}px` : 'auto'};
|
|
|
|
${(props: Props) => isSquare(props) ? `min-width: ${buttonSizePx(props)}px;` : ''}
|
|
|
|
${(props: Props) => !isSquare(props) ? 'min-width: 100px;' : ''}
|
|
|
|
${(props: Props) => isSquare(props) ? `max-width: ${buttonSizePx(props)}px;` : ''}
|
2020-09-15 15:01:07 +02:00
|
|
|
box-sizing: border-box;
|
|
|
|
border-radius: 3px;
|
|
|
|
border-style: solid;
|
|
|
|
border-width: 1px;
|
2021-11-15 19:19:51 +02:00
|
|
|
padding: 0 ${(props: Props) => isSquare(props) ? 4 : 14}px;
|
2020-09-15 15:01:07 +02:00
|
|
|
justify-content: center;
|
2021-11-15 19:19:51 +02:00
|
|
|
opacity: ${(props: Props) => props.disabled ? 0.5 : 1};
|
2020-09-15 15:01:07 +02:00
|
|
|
user-select: none;
|
2021-11-15 19:19:51 +02:00
|
|
|
${(props: Props) => props.fontSize ? `font-size: ${props.fontSize}px;` : ''}
|
2020-09-15 15:01:07 +02:00
|
|
|
`;
|
|
|
|
|
|
|
|
const StyledIcon = styled(styled.span(space))`
|
2024-04-05 13:16:49 +02:00
|
|
|
font-size: ${(props: StyleProps) => props.theme.toolbarIconSize}px;
|
|
|
|
${(props: StyleProps) => props.animation ? `animation: ${props.animation}` : ''};
|
2020-09-15 15:01:07 +02:00
|
|
|
`;
|
|
|
|
|
|
|
|
const StyledButtonPrimary = styled(StyledButtonBase)`
|
|
|
|
border: none;
|
2024-04-05 13:16:49 +02:00
|
|
|
background-color: ${(props: StyleProps) => props.theme.backgroundColor5};
|
2020-09-15 15:01:07 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
${(props: StyleProps) => props.disabled} {
|
2020-11-13 19:09:28 +02:00
|
|
|
&:hover {
|
2024-04-05 13:16:49 +02:00
|
|
|
background-color: ${(props: StyleProps) => props.theme.backgroundColorHover5};
|
2020-11-13 19:09:28 +02:00
|
|
|
}
|
2020-09-15 15:01:07 +02:00
|
|
|
|
2020-11-13 19:09:28 +02:00
|
|
|
&:active {
|
2024-04-05 13:16:49 +02:00
|
|
|
background-color: ${(props: StyleProps) => props.theme.backgroundColorActive5};
|
2020-11-13 19:09:28 +02:00
|
|
|
}
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${StyledIcon} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.color5};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${StyledTitle} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.color5};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const StyledButtonSecondary = styled(StyledButtonBase)`
|
2024-04-05 13:16:49 +02:00
|
|
|
border: 1px solid ${(props: StyleProps) => props.theme.borderColor4};
|
|
|
|
background-color: ${(props: StyleProps) => props.theme.backgroundColor4};
|
2020-09-15 15:01:07 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
${(props: StyleProps) => props.disabled} {
|
2020-11-13 19:09:28 +02:00
|
|
|
&:hover {
|
2024-04-05 13:16:49 +02:00
|
|
|
background-color: ${(props: StyleProps) => props.theme.backgroundColorHover4};
|
2020-11-13 19:09:28 +02:00
|
|
|
}
|
2020-09-15 15:01:07 +02:00
|
|
|
|
2020-11-13 19:09:28 +02:00
|
|
|
&:active {
|
2024-04-05 13:16:49 +02:00
|
|
|
background-color: ${(props: StyleProps) => props.theme.backgroundColorActive4};
|
2020-11-13 19:09:28 +02:00
|
|
|
}
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${StyledIcon} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.color4};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${StyledTitle} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.color4};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const StyledButtonTertiary = styled(StyledButtonBase)`
|
2024-04-05 13:16:49 +02:00
|
|
|
border: 1px solid ${(props: StyleProps) => props.theme.color3};
|
|
|
|
background-color: ${(props: StyleProps) => props.theme.backgroundColor3};
|
2020-09-15 15:01:07 +02:00
|
|
|
|
|
|
|
&:hover {
|
2024-04-05 13:16:49 +02:00
|
|
|
background-color: ${(props: StyleProps) => props.theme.backgroundColorHoverDim3};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
&:active {
|
2024-04-05 13:16:49 +02:00
|
|
|
background-color: ${(props: StyleProps) => props.theme.backgroundColorActive3};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${StyledIcon} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.color};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${StyledTitle} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.color};
|
2020-09-15 15:01:07 +02:00
|
|
|
opacity: 0.9;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2021-01-20 00:58:09 +02:00
|
|
|
const StyledButtonRecommended = styled(StyledButtonBase)`
|
2024-04-05 13:16:49 +02:00
|
|
|
border: 1px solid ${(props: StyleProps) => props.theme.borderColor4};
|
|
|
|
background-color: ${(props: StyleProps) => props.theme.warningBackgroundColor};
|
2021-01-20 00:58:09 +02:00
|
|
|
|
|
|
|
${StyledIcon} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.color};
|
2021-01-20 00:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${StyledTitle} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.color};
|
2021-01-20 00:58:09 +02:00
|
|
|
opacity: 0.9;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2021-01-12 14:28:55 +02:00
|
|
|
const StyledButtonSidebarSecondary = styled(StyledButtonBase)`
|
2020-09-15 15:01:07 +02:00
|
|
|
background: none;
|
2024-04-05 13:16:49 +02:00
|
|
|
border-color: ${(props: StyleProps) => props.theme.color2};
|
|
|
|
color: ${(props: StyleProps) => props.theme.color2};
|
2020-09-15 15:01:07 +02:00
|
|
|
|
|
|
|
&:hover {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.colorHover2};
|
|
|
|
border-color: ${(props: StyleProps) => props.theme.colorHover2};
|
2020-09-15 15:01:07 +02:00
|
|
|
background: none;
|
|
|
|
|
|
|
|
${StyledTitle} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.colorHover2};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${StyledIcon} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.colorHover2};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&:active {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.colorActive2};
|
|
|
|
border-color: ${(props: StyleProps) => props.theme.colorActive2};
|
2020-09-15 15:01:07 +02:00
|
|
|
background: none;
|
|
|
|
|
|
|
|
${StyledTitle} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.colorActive2};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${StyledIcon} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.colorActive2};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
${StyledTitle} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.color2};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${StyledIcon} {
|
2024-04-05 13:16:49 +02:00
|
|
|
color: ${(props: StyleProps) => props.theme.color2};
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function buttonClass(level: ButtonLevel) {
|
2020-09-15 15:01:07 +02:00
|
|
|
if (level === ButtonLevel.Primary) return StyledButtonPrimary;
|
|
|
|
if (level === ButtonLevel.Tertiary) return StyledButtonTertiary;
|
2021-01-12 14:28:55 +02:00
|
|
|
if (level === ButtonLevel.SidebarSecondary) return StyledButtonSidebarSecondary;
|
2021-01-20 00:58:09 +02:00
|
|
|
if (level === ButtonLevel.Recommended) return StyledButtonRecommended;
|
2020-09-15 15:01:07 +02:00
|
|
|
return StyledButtonSecondary;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied;
|
2021-11-15 19:19:51 +02:00
|
|
|
const Button = React.forwardRef((props: Props, ref: any) => {
|
2020-09-15 15:01:07 +02:00
|
|
|
const iconOnly = props.iconName && !props.title;
|
|
|
|
|
|
|
|
const StyledButton = buttonClass(props.level);
|
|
|
|
|
|
|
|
function renderIcon() {
|
|
|
|
if (!props.iconName) return null;
|
2024-08-02 15:49:15 +02:00
|
|
|
return <StyledIcon
|
|
|
|
aria-label={props.iconLabel ?? ''}
|
|
|
|
animation={props.iconAnimation}
|
|
|
|
mr={iconOnly ? '0' : '6px'}
|
|
|
|
color={props.color}
|
|
|
|
className={props.iconName}
|
|
|
|
role='img'
|
|
|
|
/>;
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderTitle() {
|
|
|
|
if (!props.title) return null;
|
2021-01-23 17:51:19 +02:00
|
|
|
return <StyledTitle color={props.color}>{props.title}</StyledTitle>;
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onClick() {
|
|
|
|
if (props.disabled) return;
|
|
|
|
props.onClick();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-08-02 15:49:15 +02:00
|
|
|
<StyledButton
|
|
|
|
ref={ref}
|
|
|
|
fontSize={props.fontSize}
|
|
|
|
isSquare={props.isSquare}
|
|
|
|
size={props.size}
|
|
|
|
style={props.style}
|
|
|
|
disabled={props.disabled}
|
|
|
|
title={props.tooltip}
|
|
|
|
className={props.className}
|
|
|
|
iconOnly={iconOnly}
|
|
|
|
onClick={onClick}
|
|
|
|
|
|
|
|
aria-disabled={props.disabled}
|
|
|
|
aria-expanded={props['aria-expanded']}
|
|
|
|
aria-controls={props['aria-controls']}
|
|
|
|
>
|
2020-09-15 15:01:07 +02:00
|
|
|
{renderIcon()}
|
|
|
|
{renderTitle()}
|
|
|
|
</StyledButton>
|
|
|
|
);
|
2021-11-15 19:19:51 +02:00
|
|
|
});
|
2021-01-20 00:58:09 +02:00
|
|
|
|
|
|
|
export default styled(Button)`${space}`;
|