import './Button.scss';
import { AnchorHTMLAttributes, ButtonHTMLAttributes, forwardRef, FunctionComponent, SVGProps } from 'react';
const sparkClassNames = {
button: 'spark-button spark-focus-visible spark-focus-visible-self spark-focus-visible-snap',
buttonSizePrefix: 'spark-button-size-',
buttonVariantPrefix: 'spark-button-',
disabledButton: 'spark-button-disabled',
buttonStartSlot: 'spark-button-start-slot',
buttonContent: 'spark-button-content',
buttonOnly: 'spark-button-icon-only',
};
type ButtonVariant = 'action' | 'primary' | 'secondary' | 'ghost';
type ButtonSize = 'l' | 'm' | 's';
type AsElementProps =
| (ButtonHTMLAttributes & { as?: 'button' })
| (AnchorHTMLAttributes & { as?: 'link' });
type ButtonProps = {
text?: string;
variant?: ButtonVariant;
size?: ButtonSize;
disabled?: boolean;
value?: string;
onClick?: () => void;
icon?: FunctionComponent>;
className?: string;
} & AsElementProps;
export const Button = forwardRef(function Button(
{ text, as = 'button', variant = 'primary', size = 'm', disabled = false, onClick, icon, className, ...props },
ref
): JSX.Element {
const sizeClassName = `${sparkClassNames.buttonSizePrefix}${size}`;
const variantClassName = `${sparkClassNames.buttonVariantPrefix}${variant}`;
const classNames = [sparkClassNames.button, sizeClassName, variantClassName];
if (disabled) {
classNames.push(sparkClassNames.disabledButton);
}
if (!text && icon) {
classNames.push(sparkClassNames.buttonOnly);
}
if (className) {
classNames.push(className);
}
const buttonContent = (
<>
{icon && {icon({ className: 'button-icon' })}}
{text}
>
);
if (as === 'link') {
return (
).href}
target="_blank"
rel="noreferrer"
aria-label={text}
>
{buttonContent}
);
}
return (
);
});