62 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

import type { CSSProperties } from 'react'
import React from 'react'
import { type VariantProps, cva } from 'class-variance-authority'
2023-05-15 08:51:32 +08:00
import Spinner from '../spinner'
import classNames from '@/utils/classnames'
2023-05-15 08:51:32 +08:00
const buttonVariants = cva(
2024-06-21 14:17:45 +08:00
'btn disabled:btn-disabled',
{
variants: {
variant: {
2024-06-21 14:17:45 +08:00
'primary': 'btn-primary',
'warning': 'btn-warning',
'secondary': 'btn-secondary',
'secondary-accent': 'btn-secondary-accent',
'ghost': 'btn-ghost',
2024-07-24 12:50:48 +08:00
'ghost-accent': 'btn-ghost-accent',
'tertiary': 'btn-tertiary',
2024-06-21 14:17:45 +08:00
},
size: {
small: 'btn-small',
medium: 'btn-medium',
large: 'btn-large',
},
},
defaultVariants: {
2024-06-21 14:17:45 +08:00
variant: 'secondary',
size: 'medium',
},
},
)
2023-05-15 08:51:32 +08:00
export type ButtonProps = {
2024-07-24 12:50:48 +08:00
destructive?: boolean
loading?: boolean
styleCss?: CSSProperties
} & React.ButtonHTMLAttributes<HTMLButtonElement> & VariantProps<typeof buttonVariants>
2023-05-15 08:51:32 +08:00
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
2024-07-24 12:50:48 +08:00
({ className, variant, size, destructive, loading, styleCss, children, ...props }, ref) => {
return (
<button
type='button'
2024-07-24 12:50:48 +08:00
className={classNames(
buttonVariants({ variant, size, className }),
destructive && 'btn-destructive',
)}
ref={ref}
style={styleCss}
{...props}
>
{children}
{loading && <Spinner loading={loading} className='!text-white !h-3 !w-3 !border-2 !ml-1' />}
</button>
)
},
)
Button.displayName = 'Button'
2023-05-15 08:51:32 +08:00
export default Button
export { Button, buttonVariants }