2024-07-10 16:44:39 +00:00
|
|
|
import { memo } from 'react';
|
|
|
|
import { classNames } from '~/utils/classNames';
|
|
|
|
|
2024-07-12 15:25:41 +00:00
|
|
|
type IconSize = 'sm' | 'md' | 'xl' | 'xxl';
|
2024-07-10 16:44:39 +00:00
|
|
|
|
|
|
|
interface BaseIconButtonProps {
|
|
|
|
size?: IconSize;
|
|
|
|
className?: string;
|
|
|
|
iconClassName?: string;
|
|
|
|
disabledClassName?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
onClick?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
type IconButtonWithoutChildrenProps = {
|
|
|
|
icon: string;
|
|
|
|
children?: undefined;
|
|
|
|
} & BaseIconButtonProps;
|
|
|
|
|
|
|
|
type IconButtonWithChildrenProps = {
|
|
|
|
icon?: undefined;
|
|
|
|
children: string | JSX.Element | JSX.Element[];
|
|
|
|
} & BaseIconButtonProps;
|
|
|
|
|
|
|
|
type IconButtonProps = IconButtonWithoutChildrenProps | IconButtonWithChildrenProps;
|
|
|
|
|
|
|
|
export const IconButton = memo(
|
|
|
|
({
|
|
|
|
icon,
|
|
|
|
size = 'xl',
|
|
|
|
className,
|
|
|
|
iconClassName,
|
|
|
|
disabledClassName,
|
|
|
|
disabled = false,
|
|
|
|
onClick,
|
|
|
|
children,
|
|
|
|
}: IconButtonProps) => {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className={classNames(
|
2024-07-11 19:45:08 +00:00
|
|
|
'flex items-center text-gray-600 bg-transparent enabled:hover:text-gray-900 rounded-md p-1 enabled:hover:bg-gray-200/80 disabled:cursor-not-allowed',
|
2024-07-10 16:44:39 +00:00
|
|
|
{
|
|
|
|
[classNames('opacity-30', disabledClassName)]: disabled,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
disabled={disabled}
|
|
|
|
onClick={(event) => {
|
|
|
|
if (disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick?.(event);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children ? children : <div className={classNames(icon, getIconSize(size), iconClassName)}></div>}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
function getIconSize(size: IconSize) {
|
|
|
|
if (size === 'sm') {
|
|
|
|
return 'text-sm';
|
|
|
|
} else if (size === 'md') {
|
|
|
|
return 'text-md';
|
2024-07-12 15:25:41 +00:00
|
|
|
} else if (size === 'xl') {
|
2024-07-10 16:44:39 +00:00
|
|
|
return 'text-xl';
|
2024-07-12 15:25:41 +00:00
|
|
|
} else {
|
|
|
|
return 'text-2xl';
|
2024-07-10 16:44:39 +00:00
|
|
|
}
|
|
|
|
}
|