Every component is rebuilt on @base-ui/react with a single prop API, token-only colours and dark mode, and 28 new components are added. Components - New: Field, FeaturedTextArea, NumberInput, Combobox, CheckboxGroup, Slider, ToggleGroup/Toggle, IconButton, Dialog/Modal, AlertDialog, Drawer/Sheet, Popover, InfoTip, DropdownMenu, Tabs, Breadcrumb, Pagination, Toast (Toaster, useToast), Progress, Meter, Skeleton, CountBadge, Avatar/AvatarGroup, Kbd, Separator, Table - Rewritten: Button, Input, FeaturedInput, TextArea, Select, FeaturedSelect (real ARIA combobox), NativeSelect, Checkbox, Radio, Switch, Tooltip, Accordion, Alert, Spinner, Badge, Card - Removed TextField (use Input) and sonner (use Toast) Foundations - Semantic tokens (surfaces, text, borders, status ramps) in src/styles/tokens.css, with class-based dark mode - Visible focus rings, linked labels/descriptions/errors, alert roles by severity, keyboard support from Base UI - Fixed the Classic Blue ramp; Emerald, Amber and Teal 500 steps now carry white text at AA - Library build (per-component ES modules, types, tokens.css, exports) Quality - Vitest, Testing Library and axe tests for every component (289 tests) - scripts/check-stories.mjs renders every story in Chrome and fails on errors or axe violations, including contrast; all 423 stories pass in light and dark mode and in all seven palettes - Storybook: controls for every prop, open states for every popup, light/dark toolbar, Introduction guides, docs/CONVENTIONS.md BREAKING CHANGE: component props changed across the kit; see CHANGELOG.md.
129 lines
5.8 KiB
TypeScript
129 lines
5.8 KiB
TypeScript
import { render, screen, waitFor } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
import { expectNoA11yViolations } from '../test/a11y'
|
|
import { Button } from './Button'
|
|
import { DropdownMenu } from './DropdownMenu'
|
|
import type { DropdownMenuItemData } from './dropdown-menu.tokens'
|
|
|
|
function items(overrides: Partial<Record<string, () => void>> = {}): DropdownMenuItemData[] {
|
|
return [
|
|
{ label: 'Edit', shortcut: '⌘E', onSelect: overrides.edit },
|
|
{ label: 'Duplicate', onSelect: overrides.duplicate },
|
|
{ type: 'separator' },
|
|
{ type: 'checkbox', label: 'Show archived', defaultChecked: false },
|
|
{ type: 'radio', label: 'Sort by', defaultValue: 'name', items: [{ value: 'name', label: 'Name' }, { value: 'date', label: 'Date' }] },
|
|
{ type: 'separator' },
|
|
{ label: 'Delete', color: 'danger', onSelect: overrides.remove },
|
|
]
|
|
}
|
|
|
|
describe('DropdownMenu', () => {
|
|
it('opens from the trigger as a menu', async () => {
|
|
render(<DropdownMenu trigger={<Button>Actions</Button>} items={items()} />)
|
|
const trigger = screen.getByRole('button', { name: 'Actions' })
|
|
expect(trigger).toHaveAttribute('aria-haspopup', 'menu')
|
|
await userEvent.click(trigger)
|
|
const menu = await screen.findByRole('menu')
|
|
expect(menu).toHaveAccessibleName('Actions')
|
|
expect(screen.getByRole('menuitem', { name: /Edit/ })).toHaveTextContent('⌘E')
|
|
expect(screen.getByRole('menuitem', { name: /Edit/ }).querySelector('kbd')).toHaveTextContent('⌘E')
|
|
})
|
|
|
|
it('moves with ArrowDown and runs onSelect on Enter', async () => {
|
|
const duplicate = vi.fn()
|
|
render(<DropdownMenu trigger={<Button>Actions</Button>} items={items({ duplicate })} />)
|
|
screen.getByRole('button', { name: 'Actions' }).focus()
|
|
await userEvent.keyboard('{Enter}')
|
|
await screen.findByRole('menu')
|
|
await waitFor(() => expect(screen.getByRole('menuitem', { name: /Edit/ })).toHaveFocus())
|
|
await userEvent.keyboard('{ArrowDown}')
|
|
expect(screen.getByRole('menuitem', { name: 'Duplicate' })).toHaveFocus()
|
|
await userEvent.keyboard('{Enter}')
|
|
expect(duplicate).toHaveBeenCalledOnce()
|
|
await waitFor(() => expect(screen.queryByRole('menu')).not.toBeInTheDocument())
|
|
})
|
|
|
|
it('runs onSelect on click', async () => {
|
|
const remove = vi.fn()
|
|
render(<DropdownMenu trigger={<Button>Actions</Button>} items={items({ remove })} />)
|
|
await userEvent.click(screen.getByRole('button', { name: 'Actions' }))
|
|
await userEvent.click(await screen.findByRole('menuitem', { name: 'Delete' }))
|
|
expect(remove).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('toggles checkbox items and selects radio items', async () => {
|
|
render(<DropdownMenu trigger={<Button>Actions</Button>} items={items()} />)
|
|
await userEvent.click(screen.getByRole('button', { name: 'Actions' }))
|
|
const checkbox = await screen.findByRole('menuitemcheckbox', { name: 'Show archived' })
|
|
expect(checkbox).toHaveAttribute('aria-checked', 'false')
|
|
await userEvent.click(checkbox)
|
|
expect(checkbox).toHaveAttribute('aria-checked', 'true')
|
|
expect(screen.getByRole('menu')).toBeInTheDocument()
|
|
const date = screen.getByRole('menuitemradio', { name: 'Date' })
|
|
await userEvent.click(date)
|
|
expect(date).toHaveAttribute('aria-checked', 'true')
|
|
expect(screen.getByRole('menuitemradio', { name: 'Name' })).toHaveAttribute('aria-checked', 'false')
|
|
})
|
|
|
|
it('closes on Escape and returns focus to the trigger', async () => {
|
|
render(<DropdownMenu trigger={<Button>Actions</Button>} items={items()} />)
|
|
const trigger = screen.getByRole('button', { name: 'Actions' })
|
|
await userEvent.click(trigger)
|
|
await screen.findByRole('menu')
|
|
await userEvent.keyboard('{Escape}')
|
|
await waitFor(() => expect(screen.queryByRole('menu')).not.toBeInTheDocument())
|
|
await waitFor(() => expect(trigger).toHaveFocus())
|
|
})
|
|
|
|
it('opens a submenu with ArrowRight', async () => {
|
|
const onSelect = vi.fn()
|
|
render(
|
|
<DropdownMenu
|
|
trigger={<Button>Actions</Button>}
|
|
items={[{ type: 'submenu', label: 'Move to', items: [{ label: 'Design team', onSelect }] }]}
|
|
/>,
|
|
)
|
|
await userEvent.click(screen.getByRole('button', { name: 'Actions' }))
|
|
const sub = await screen.findByRole('menuitem', { name: 'Move to' })
|
|
expect(sub).toHaveAttribute('aria-haspopup', 'menu')
|
|
sub.focus()
|
|
await userEvent.keyboard('{ArrowRight}')
|
|
await userEvent.click(await screen.findByRole('menuitem', { name: 'Design team' }))
|
|
expect(onSelect).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('builds from compound parts', async () => {
|
|
const onSelect = vi.fn()
|
|
render(
|
|
<DropdownMenu.Root defaultOpen>
|
|
<DropdownMenu.Trigger render={<Button>Options</Button>} />
|
|
<DropdownMenu.Content>
|
|
<DropdownMenu.Group>
|
|
<DropdownMenu.Label>Account</DropdownMenu.Label>
|
|
<DropdownMenu.Item onSelect={onSelect}>Profile</DropdownMenu.Item>
|
|
</DropdownMenu.Group>
|
|
<DropdownMenu.Separator />
|
|
<DropdownMenu.CheckboxItem defaultChecked>Notifications</DropdownMenu.CheckboxItem>
|
|
</DropdownMenu.Content>
|
|
</DropdownMenu.Root>,
|
|
)
|
|
expect(await screen.findByRole('group', { name: 'Account' })).toBeInTheDocument()
|
|
expect(screen.getByRole('menuitemcheckbox', { name: 'Notifications' })).toHaveAttribute('aria-checked', 'true')
|
|
await userEvent.click(screen.getByRole('menuitem', { name: 'Profile' }))
|
|
expect(onSelect).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('has no axe violations when open', async () => {
|
|
render(
|
|
<DropdownMenu
|
|
defaultOpen
|
|
trigger={<Button>Actions</Button>}
|
|
items={[{ type: 'label', label: 'Project' }, ...items()]}
|
|
/>,
|
|
)
|
|
await screen.findByRole('menu')
|
|
await expectNoA11yViolations(document.body)
|
|
})
|
|
})
|