import type { ButtonHTMLAttributes, AnchorHTMLAttributes, ReactNode } from 'react'
import { Icons, type IconComponent } from './Icons'
type ButtonVariant = 'primary' | 'secondary' | 'danger' | 'ghost' | 'accent'
type ButtonBaseProps = {
variant?: ButtonVariant
loading?: boolean
Icon?: IconComponent
children: ReactNode
}
type ButtonAsButton = ButtonBaseProps & ButtonHTMLAttributes & { href?: never }
type ButtonAsLink = ButtonBaseProps & AnchorHTMLAttributes & { href: string }
type ButtonProps = ButtonAsButton | ButtonAsLink
const variantClasses: Record = {
primary: 'btn-primary',
secondary: 'btn-secondary',
danger: 'btn-danger',
ghost: 'btn-ghost',
accent: 'btn-accent',
}
export function Button({
variant = 'secondary',
loading = false,
Icon,
children,
className = '',
...props
}: ButtonProps) {
const content = (
<>
{loading ? (
) : Icon ? (
) : null}
{children}
>
)
if ('href' in props && props.href) {
const { href, ...linkProps } = props
return (
{content}
)
}
const { disabled, ...buttonProps } = props as ButtonAsButton
return (
)
}