Simplify and generalize Button component

this allows Button to have any attributes that normal button accepts,
like autofocus, role=link, ref, etc.
This commit is contained in:
Sinclair Chen 2022-08-09 18:58:45 -07:00
parent c07daafb8d
commit c10ad31f11

View File

@ -1,10 +1,7 @@
import { ReactNode } from 'react'
import clsx from 'clsx'
export function Button(props: {
className?: string
onClick?: () => void
children?: ReactNode
export function Button(
props: {
size?: '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'
color?:
| 'green'
@ -15,17 +12,14 @@ export function Button(props: {
| 'gray'
| 'gradient'
| 'gray-white'
type?: 'button' | 'reset' | 'submit'
disabled?: boolean
}) {
} & React.ButtonHTMLAttributes<HTMLButtonElement>
) {
const {
children,
className,
onClick,
size = 'md',
color = 'indigo',
type = 'button',
disabled = false,
...buttonProps // onClick, disabled, type, etc.
} = props
const sizeClasses = {
@ -40,7 +34,6 @@ export function Button(props: {
return (
<button
type={type}
className={clsx(
'font-md items-center justify-center rounded-md border border-transparent shadow-sm hover:transition-colors disabled:cursor-not-allowed disabled:opacity-50',
sizeClasses,
@ -56,8 +49,8 @@ export function Button(props: {
'border-none bg-white text-gray-500 shadow-none hover:bg-gray-200',
className
)}
disabled={disabled}
onClick={onClick}
type="button"
{...buttonProps}
>
{children}
</button>