2023-04-12 17:55:24 +02:00
|
|
|
import React from 'react';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
import {
|
|
|
|
AlignItems,
|
|
|
|
BackgroundColor,
|
|
|
|
BorderRadius,
|
2023-07-13 22:22:40 +02:00
|
|
|
Display,
|
2023-04-12 17:55:24 +02:00
|
|
|
IconColor,
|
|
|
|
JustifyContent,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
|
|
|
|
import Box from '../../ui/box';
|
|
|
|
import { Icon, IconSize } from '../icon';
|
|
|
|
|
|
|
|
import { ButtonIconSize, ButtonIconProps } from './button-icon.types';
|
|
|
|
|
|
|
|
const buttonIconSizeToIconSize: Record<ButtonIconSize, IconSize> = {
|
|
|
|
[ButtonIconSize.Sm]: IconSize.Sm,
|
2023-07-13 22:22:40 +02:00
|
|
|
[ButtonIconSize.Md]: IconSize.Md,
|
2023-04-12 17:55:24 +02:00
|
|
|
[ButtonIconSize.Lg]: IconSize.Lg,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ButtonIcon = React.forwardRef(
|
|
|
|
(
|
|
|
|
{
|
|
|
|
ariaLabel,
|
|
|
|
as = 'button',
|
|
|
|
className = '',
|
|
|
|
color = IconColor.iconDefault,
|
|
|
|
href,
|
|
|
|
size = ButtonIconSize.Lg,
|
|
|
|
iconName,
|
|
|
|
disabled,
|
|
|
|
iconProps,
|
|
|
|
...props
|
|
|
|
}: ButtonIconProps,
|
|
|
|
ref: React.Ref<HTMLElement>,
|
|
|
|
) => {
|
|
|
|
const Tag = href ? 'a' : as;
|
|
|
|
const isDisabled = disabled && Tag === 'button';
|
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
aria-label={ariaLabel}
|
|
|
|
as={Tag}
|
|
|
|
className={classnames(
|
|
|
|
'mm-button-icon',
|
|
|
|
`mm-button-icon--size-${String(size)}`,
|
|
|
|
{
|
|
|
|
'mm-button-icon--disabled': Boolean(disabled),
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
color={color}
|
|
|
|
{...(isDisabled ? { disabled: true } : {})} // only allow disabled attribute to be passed down to the Box when the as prop is equal to a button element
|
2023-07-13 22:22:40 +02:00
|
|
|
display={Display.InlineFlex}
|
2023-04-12 17:55:24 +02:00
|
|
|
justifyContent={JustifyContent.center}
|
|
|
|
alignItems={AlignItems.center}
|
|
|
|
borderRadius={BorderRadius.LG}
|
|
|
|
backgroundColor={BackgroundColor.transparent}
|
|
|
|
{...(href ? { href } : {})}
|
|
|
|
ref={ref}
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
name={iconName}
|
|
|
|
size={buttonIconSizeToIconSize[size]}
|
|
|
|
{...iconProps}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|