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