feat(ui): Button with variants + sizes + disabled state
This commit is contained in:
1 parent
86f65767de
commit
fe5adcd249
2 files changed
+63
-2
No files matched your search
@@ -1,3 +1,46 @@
|
|||||||
export default function Button() {
|
import type { ButtonHTMLAttributes, ReactNode } from 'react'
|
||||||
return <div>Button placeholder</div>
|
|
||||||
|
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
||||||
|
variant?: 'primary' | 'secondary' | 'ghost' | 'danger'
|
||||||
|
size?: 'sm' | 'md' | 'lg'
|
||||||
|
children?: ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
const VARIANTS: Record<NonNullable<ButtonProps['variant']>, string> = {
|
||||||
|
primary:
|
||||||
|
'bg-[var(--color-accent)] text-white hover:bg-[var(--color-accent-strong)] focus-visible:ring-[var(--color-accent)]',
|
||||||
|
secondary:
|
||||||
|
'bg-[var(--color-surface)] text-[var(--color-ink)] border border-[var(--color-border)] hover:bg-[var(--color-surface-strong)]',
|
||||||
|
ghost:
|
||||||
|
'bg-transparent text-[var(--color-ink)] hover:bg-[var(--color-surface)]',
|
||||||
|
danger:
|
||||||
|
'bg-[var(--color-danger)] text-white hover:bg-[var(--color-danger-strong)]',
|
||||||
|
}
|
||||||
|
|
||||||
|
const SIZES: Record<NonNullable<ButtonProps['size']>, string> = {
|
||||||
|
sm: 'h-8 px-3 text-sm rounded-md',
|
||||||
|
md: 'h-10 px-4 text-base rounded-lg',
|
||||||
|
lg: 'h-12 px-6 text-base rounded-lg',
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Button({
|
||||||
|
variant = 'primary',
|
||||||
|
size = 'md',
|
||||||
|
className = '',
|
||||||
|
children,
|
||||||
|
...rest
|
||||||
|
}: ButtonProps) {
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
className={[
|
||||||
|
'inline-flex items-center justify-center font-medium transition disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2',
|
||||||
|
VARIANTS[variant],
|
||||||
|
SIZES[size],
|
||||||
|
className,
|
||||||
|
].join(' ')}
|
||||||
|
{...rest}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { describe, it, expect, vi } from 'vitest'
|
||||||
|
import { render, screen } from '@testing-library/react'
|
||||||
|
import userEvent from '@testing-library/user-event'
|
||||||
|
import Button from '@/components/ui/Button'
|
||||||
|
|
||||||
|
describe('Button', () => {
|
||||||
|
it('renders children and handles clicks', async () => {
|
||||||
|
const onClick = vi.fn()
|
||||||
|
render(<Button onClick={onClick}>Save</Button>)
|
||||||
|
await userEvent.click(screen.getByRole('button', { name: 'Save' }))
|
||||||
|
expect(onClick).toHaveBeenCalledOnce()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('respects disabled', () => {
|
||||||
|
render(<Button disabled>Save</Button>)
|
||||||
|
expect(screen.getByRole('button')).toBeDisabled()
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in new issue
Block a user