feat(design): add Modernist UI primitives (Button, Tag, StatusTag, Field, Card, table styles)
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import type { ButtonHTMLAttributes } from 'react'
|
||||
import { cx } from './cx'
|
||||
|
||||
type Variant = 'primary' | 'secondary' | 'ghost' | 'icon'
|
||||
|
||||
const BASE =
|
||||
'inline-flex items-center gap-1.5 text-left font-extrabold text-sm leading-tight ' +
|
||||
'border border-transparent cursor-pointer transition-colors ' +
|
||||
'disabled:opacity-45 disabled:cursor-not-allowed'
|
||||
|
||||
const VARIANTS: Record<Variant, string> = {
|
||||
primary: 'justify-start bg-accent text-ground px-3.5 py-2 hover:bg-accent-600 active:bg-accent-700',
|
||||
secondary: 'justify-start border-divider text-ink px-3.5 py-2 hover:bg-ink/7 active:bg-ink/15',
|
||||
ghost: 'justify-start text-accent-700 px-1 py-2 hover:bg-accent/10 active:bg-accent/20',
|
||||
icon: 'justify-center w-9 h-9 p-0 text-ink hover:bg-ink/7 active:bg-ink/15',
|
||||
}
|
||||
|
||||
type Props = ButtonHTMLAttributes<HTMLButtonElement> & {
|
||||
variant?: Variant
|
||||
block?: boolean
|
||||
}
|
||||
|
||||
export function Button({ variant = 'primary', block = false, className, type = 'button', ...rest }: Props) {
|
||||
return <button type={type} className={cx(BASE, VARIANTS[variant], block && 'w-full', className)} {...rest} />
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
export function Card({
|
||||
kicker,
|
||||
title,
|
||||
meta,
|
||||
children,
|
||||
}: {
|
||||
kicker?: ReactNode
|
||||
title?: ReactNode
|
||||
meta?: ReactNode
|
||||
children?: ReactNode
|
||||
}) {
|
||||
return (
|
||||
<div className="flex flex-col gap-2 p-3 bg-surface">
|
||||
{kicker && <span className="text-[10px] tracking-[0.1em] uppercase text-accent-700">{kicker}</span>}
|
||||
{title && <h3 className="text-[17px] leading-tight font-extrabold">{title}</h3>}
|
||||
{children && <div className="flex-1 text-[13px] opacity-80">{children}</div>}
|
||||
{meta && <div className="flex items-center gap-1.5 text-[11px] text-ink/50">{meta}</div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export const cx = (...parts: Array<string | false | null | undefined>): string =>
|
||||
parts.filter(Boolean).join(' ')
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { InputHTMLAttributes, ReactNode, SelectHTMLAttributes, TextareaHTMLAttributes } from 'react'
|
||||
import { cx } from './cx'
|
||||
|
||||
export const INPUT_CLASS =
|
||||
'w-full min-h-9 px-2.5 py-1.5 text-sm text-ink caret-accent bg-surface border border-divider ' +
|
||||
'hover:border-ink/45 focus-visible:border-accent focus-visible:outline-offset-0 ' +
|
||||
'disabled:opacity-45 disabled:cursor-not-allowed'
|
||||
|
||||
export function Field({ label, htmlFor, children }: { label: string; htmlFor: string; children: ReactNode }) {
|
||||
return (
|
||||
<div>
|
||||
<label htmlFor={htmlFor} className="block text-xs mb-[5px] text-ink/70">
|
||||
{label}
|
||||
</label>
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function Input({ className, ...rest }: InputHTMLAttributes<HTMLInputElement>) {
|
||||
return <input className={cx(INPUT_CLASS, className)} {...rest} />
|
||||
}
|
||||
|
||||
export function Textarea({ className, ...rest }: TextareaHTMLAttributes<HTMLTextAreaElement>) {
|
||||
return <textarea className={cx(INPUT_CLASS, 'min-h-[90px] resize-y', className)} {...rest} />
|
||||
}
|
||||
|
||||
export function Select({ className, ...rest }: SelectHTMLAttributes<HTMLSelectElement>) {
|
||||
return <select className={cx(INPUT_CLASS, className)} {...rest} />
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import {
|
||||
AlarmClock,
|
||||
BadgeCheck,
|
||||
Circle,
|
||||
CircleAlert,
|
||||
CircleCheck,
|
||||
ClipboardList,
|
||||
ListChecks,
|
||||
Loader,
|
||||
RotateCcw,
|
||||
Search,
|
||||
type LucideIcon,
|
||||
} from 'lucide-react'
|
||||
import { Tag, type TagVariant } from './tag'
|
||||
|
||||
const TONES: Record<string, TagVariant> = {
|
||||
reported: 'accent',
|
||||
triaged: 'outline',
|
||||
investigating: 'outline',
|
||||
capa_pending: 'outline',
|
||||
verification: 'outline',
|
||||
closed: 'neutral',
|
||||
open: 'accent',
|
||||
in_progress: 'outline',
|
||||
overdue: 'accent',
|
||||
pending_verification: 'outline',
|
||||
verified: 'neutral',
|
||||
reopened: 'accent',
|
||||
}
|
||||
|
||||
const ICONS: Record<string, LucideIcon> = {
|
||||
reported: CircleAlert,
|
||||
triaged: ListChecks,
|
||||
investigating: Search,
|
||||
capa_pending: ClipboardList,
|
||||
verification: BadgeCheck,
|
||||
closed: CircleCheck,
|
||||
open: Circle,
|
||||
in_progress: Loader,
|
||||
overdue: AlarmClock,
|
||||
pending_verification: BadgeCheck,
|
||||
verified: CircleCheck,
|
||||
reopened: RotateCcw,
|
||||
}
|
||||
|
||||
export function statusTone(status: string): TagVariant {
|
||||
return TONES[status] ?? 'neutral'
|
||||
}
|
||||
|
||||
function humanize(value: string): string {
|
||||
return value
|
||||
.split('_')
|
||||
.map(w => (w === 'capa' ? 'CAPA' : w.charAt(0).toUpperCase() + w.slice(1)))
|
||||
.join(' ')
|
||||
}
|
||||
|
||||
export function StatusTag({ status, children }: { status: string; children?: ReactNode }) {
|
||||
const Icon = ICONS[status]
|
||||
return (
|
||||
<Tag variant={statusTone(status)}>
|
||||
{Icon && <Icon size={12} strokeWidth={2} aria-hidden />}
|
||||
{children ?? humanize(status)}
|
||||
</Tag>
|
||||
)
|
||||
}
|
||||
|
||||
const PRIORITY_TONES: Record<string, TagVariant> = { high: 'accent', med: 'outline', low: 'neutral' }
|
||||
|
||||
export function PriorityTag({ priority, children }: { priority: string; children?: ReactNode }) {
|
||||
return <Tag variant={PRIORITY_TONES[priority] ?? 'neutral'}>{children ?? humanize(priority)}</Tag>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export const TABLE = 'w-full border-collapse text-sm'
|
||||
export const TH =
|
||||
'text-left text-[11px] uppercase tracking-[0.08em] text-ink/60 px-2 py-2 border-b-2 border-divider'
|
||||
export const TR = 'hover:bg-ink/4'
|
||||
export const TD = 'px-2 py-2 border-b border-divider'
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import { cx } from './cx'
|
||||
|
||||
export type TagVariant = 'accent' | 'neutral' | 'outline'
|
||||
|
||||
const VARIANTS: Record<TagVariant, string> = {
|
||||
accent: 'bg-accent-100 text-accent-800',
|
||||
neutral: 'bg-neutral-100 text-neutral-800',
|
||||
outline: 'border border-accent text-accent-700',
|
||||
}
|
||||
|
||||
export function Tag({ variant, children, className }: { variant: TagVariant; children: ReactNode; className?: string }) {
|
||||
return (
|
||||
<span
|
||||
className={cx(
|
||||
'inline-flex items-center gap-1 text-[11px] font-medium tracking-wide px-2.5 py-[3px] whitespace-nowrap',
|
||||
VARIANTS[variant],
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -708,15 +708,15 @@ import { cx } from './cx'
|
||||
type Variant = 'primary' | 'secondary' | 'ghost' | 'icon'
|
||||
|
||||
const BASE =
|
||||
'inline-flex items-center justify-start gap-1.5 text-left font-extrabold text-sm leading-tight ' +
|
||||
'inline-flex items-center gap-1.5 text-left font-extrabold text-sm leading-tight ' +
|
||||
'border border-transparent cursor-pointer transition-colors ' +
|
||||
'disabled:opacity-45 disabled:cursor-not-allowed'
|
||||
|
||||
const VARIANTS: Record<Variant, string> = {
|
||||
primary: 'bg-accent text-ground px-3.5 py-2 hover:bg-accent-600 active:bg-accent-700',
|
||||
secondary: 'border-divider text-ink px-3.5 py-2 hover:bg-ink/7 active:bg-ink/15',
|
||||
ghost: 'text-accent px-1 py-2 hover:bg-accent/10 active:bg-accent/20',
|
||||
icon: 'w-9 h-9 p-0 !justify-center text-ink hover:bg-ink/7 active:bg-ink/15',
|
||||
primary: 'justify-start bg-accent text-ground px-3.5 py-2 hover:bg-accent-600 active:bg-accent-700',
|
||||
secondary: 'justify-start border-divider text-ink px-3.5 py-2 hover:bg-ink/7 active:bg-ink/15',
|
||||
ghost: 'justify-start text-accent-700 px-1 py-2 hover:bg-accent/10 active:bg-accent/20',
|
||||
icon: 'justify-center w-9 h-9 p-0 text-ink hover:bg-ink/7 active:bg-ink/15',
|
||||
}
|
||||
|
||||
type Props = ButtonHTMLAttributes<HTMLButtonElement> & {
|
||||
@@ -725,20 +725,10 @@ type Props = ButtonHTMLAttributes<HTMLButtonElement> & {
|
||||
}
|
||||
|
||||
export function Button({ variant = 'primary', block = false, className, type = 'button', ...rest }: Props) {
|
||||
const isIcon = variant === 'icon'
|
||||
return (
|
||||
<button
|
||||
type={type}
|
||||
className={cx(BASE, VARIANTS[variant], block && 'w-full', className)}
|
||||
{...rest}
|
||||
data-icon={isIcon || undefined}
|
||||
/>
|
||||
)
|
||||
return <button type={type} className={cx(BASE, VARIANTS[variant], block && 'w-full', className)} {...rest} />
|
||||
}
|
||||
```
|
||||
|
||||
Because the icon variant must be centered while `BASE` carries `justify-start`, the icon variant sets `!justify-center`. The test asserts the icon button's class list contains `justify-center`, which `!justify-center` satisfies as a substring.
|
||||
|
||||
`components/ui/tag.tsx`:
|
||||
|
||||
```tsx
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Tag } from '@/components/ui/tag'
|
||||
import { StatusTag, PriorityTag, statusTone } from '@/components/ui/status-tag'
|
||||
import { Field, Input } from '@/components/ui/field'
|
||||
import { Card } from '@/components/ui/card'
|
||||
import { TABLE, TH, TR, TD } from '@/components/ui/table-styles'
|
||||
|
||||
describe('Button', () => {
|
||||
it('renders a primary accent button that is type=button and flush left', () => {
|
||||
render(<Button variant="primary">Save</Button>)
|
||||
const btn = screen.getByRole('button', { name: 'Save' })
|
||||
expect(btn.getAttribute('type')).toBe('button')
|
||||
expect(btn.className).toContain('bg-accent')
|
||||
expect(btn.className).toContain('justify-start')
|
||||
expect(btn.className).not.toContain('justify-center')
|
||||
})
|
||||
|
||||
it('block buttons span the row and stay flush left', () => {
|
||||
render(<Button block>Go</Button>)
|
||||
const btn = screen.getByRole('button', { name: 'Go' })
|
||||
expect(btn.className).toContain('w-full')
|
||||
expect(btn.className).toContain('justify-start')
|
||||
})
|
||||
|
||||
it('icon buttons are square and centered', () => {
|
||||
render(<Button variant="icon" aria-label="Close" />)
|
||||
const btn = screen.getByRole('button', { name: 'Close' })
|
||||
expect(btn.className).toContain('w-9')
|
||||
expect(btn.className).toContain('justify-center')
|
||||
expect(btn.className).not.toContain('justify-start')
|
||||
})
|
||||
|
||||
it('never uses a rounded class', () => {
|
||||
render(<Button variant="secondary">x</Button>)
|
||||
expect(screen.getByRole('button').className).not.toMatch(/rounded/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Tag', () => {
|
||||
it.each([
|
||||
['accent', 'bg-accent-100'],
|
||||
['neutral', 'bg-neutral-100'],
|
||||
['outline', 'border-accent'],
|
||||
] as const)('%s variant carries %s', (variant, cls) => {
|
||||
render(<Tag variant={variant}>Label</Tag>)
|
||||
expect(screen.getByText('Label').className).toContain(cls)
|
||||
})
|
||||
})
|
||||
|
||||
describe('statusTone', () => {
|
||||
it.each([
|
||||
['reported', 'accent'],
|
||||
['triaged', 'outline'],
|
||||
['investigating', 'outline'],
|
||||
['capa_pending', 'outline'],
|
||||
['verification', 'outline'],
|
||||
['closed', 'neutral'],
|
||||
['open', 'accent'],
|
||||
['in_progress', 'outline'],
|
||||
['overdue', 'accent'],
|
||||
['pending_verification', 'outline'],
|
||||
['verified', 'neutral'],
|
||||
['reopened', 'accent'],
|
||||
['something_else', 'neutral'],
|
||||
])('%s maps to %s', (status, tone) => {
|
||||
expect(statusTone(status)).toBe(tone)
|
||||
})
|
||||
})
|
||||
|
||||
describe('StatusTag', () => {
|
||||
it('shows a humanized text label plus an icon, never color alone', () => {
|
||||
const { container } = render(<StatusTag status="capa_pending" />)
|
||||
expect(screen.getByText('CAPA Pending')).toBeTruthy()
|
||||
expect(container.querySelector('svg')).not.toBeNull()
|
||||
})
|
||||
|
||||
it('accepts a label override', () => {
|
||||
render(<StatusTag status="overdue">Late</StatusTag>)
|
||||
expect(screen.getByText('Late')).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
describe('PriorityTag', () => {
|
||||
it.each([
|
||||
['high', 'bg-accent-100'],
|
||||
['med', 'border-accent'],
|
||||
['low', 'bg-neutral-100'],
|
||||
])('%s priority uses %s', (priority, cls) => {
|
||||
const { container } = render(<PriorityTag priority={priority} />)
|
||||
expect(container.firstElementChild?.className).toContain(cls)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Field and Input', () => {
|
||||
it('associates the label with the input', () => {
|
||||
render(
|
||||
<Field label="Email" htmlFor="email">
|
||||
<Input id="email" />
|
||||
</Field>,
|
||||
)
|
||||
const input = screen.getByLabelText('Email')
|
||||
expect(input.className).toContain('bg-surface')
|
||||
expect(input.className).toContain('border-divider')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Card', () => {
|
||||
it('renders kicker, title, body and meta on the surface fill', () => {
|
||||
const { container } = render(
|
||||
<Card kicker="Open" title="Forklift" meta="Due Friday">
|
||||
Body text
|
||||
</Card>,
|
||||
)
|
||||
expect(screen.getByText('Open')).toBeTruthy()
|
||||
expect(screen.getByText('Forklift')).toBeTruthy()
|
||||
expect(screen.getByText('Body text')).toBeTruthy()
|
||||
expect(screen.getByText('Due Friday')).toBeTruthy()
|
||||
expect(container.firstElementChild?.className).toContain('bg-surface')
|
||||
})
|
||||
})
|
||||
|
||||
describe('table styles', () => {
|
||||
it('uses a 2px header rule and 1px row rules', () => {
|
||||
expect(TH).toContain('border-b-2')
|
||||
expect(TH).toContain('border-divider')
|
||||
expect(TD).toContain('border-b')
|
||||
expect(TR).toContain('hover:bg-ink/4')
|
||||
expect(TABLE).toContain('w-full')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user