2026-10-06 11:42:11 +07:00
|
|
|
import { forwardRef } from 'react'
|
|
|
|
|
import type { ComponentPropsWithoutRef } from 'react'
|
|
|
|
|
|
|
|
|
|
type ButtonVariant = 'primary' | 'secondary' | 'ghost'
|
|
|
|
|
type ButtonSize = 'sm' | 'md' | 'lg'
|
|
|
|
|
|
|
|
|
|
interface ButtonProps extends ComponentPropsWithoutRef<'button'> {
|
|
|
|
|
variant?: ButtonVariant
|
|
|
|
|
size?: ButtonSize
|
|
|
|
|
href?: string
|
2026-10-06 14:14:01 +07:00
|
|
|
download?: boolean
|
2026-10-06 11:42:11 +07:00
|
|
|
children: React.ReactNode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const variantClasses: Record<ButtonVariant, string> = {
|
|
|
|
|
primary: 'btn btn-primary',
|
|
|
|
|
secondary: 'btn btn-secondary',
|
|
|
|
|
ghost: 'btn btn-ghost',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sizeClasses: Record<ButtonSize, string> = {
|
|
|
|
|
sm: 'btn-sm',
|
|
|
|
|
md: '',
|
|
|
|
|
lg: 'btn-lg',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const Button = forwardRef<
|
|
|
|
|
HTMLButtonElement | HTMLAnchorElement,
|
|
|
|
|
ButtonProps
|
|
|
|
|
>(({ variant = 'primary', size = 'md', href, className = '', children, ...props }, ref) => {
|
|
|
|
|
const classes = `${variantClasses[variant]} ${sizeClasses[size]} ${className}`.trim()
|
|
|
|
|
|
|
|
|
|
if (href) {
|
|
|
|
|
return (
|
|
|
|
|
<a href={href} className={classes} ref={ref as React.Ref<HTMLAnchorElement>} {...(props as React.AnchorHTMLAttributes<HTMLAnchorElement>)}>
|
|
|
|
|
{children}
|
|
|
|
|
</a>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button className={classes} ref={ref as React.Ref<HTMLButtonElement>} {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</button>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Button.displayName = 'Button'
|