SwatchPicker
A single-select set of color swatches: a radio group (the default row layout) or a grid of rows (layout="grid").
- **Row layout** (APG radio group, role="radiogroup" of role="radio" buttons): one tab stop, the selected swatch, else the first. All four arrow keys move focus and select (Left/Right mirrored under dir="rtl"), wrapping at the ends; Home/End jump to the first/last swatch.
- **Grid layout** (APG grid, role="grid" of SwatchPicker.Row rows whose cells hold the swatch buttons, the selected one aria-selected and pressed): the arrow keys move focus only. Left/Right go through the cells in reading order across rows (mirrored under dir="rtl") and stop at the grid's first and last cell; Up/Down go to the nearest enabled cell of the previous/next row; Home/End to the first/last cell of the row, Ctrl+Home/End of the grid. Space, Enter and a click select. One tab stop: the last focused cell, starting at the selected swatch, else the first. items, and every run of children that are not rows, render in a row of their own; rows do not wrap.
- **Selected indicator**: an offset ring in the foreground color (drawn on the page background, not on the swatch) plus a check glyph whose color is picked by the swatch's luminance, so the selection never relies on color alone.
- **Naming**: give the picker a name with aria-label/aria-labelledby or render it inside a Field, and give every item a label; both are reported in development when missing.
- **Forms**: with name (or required) it takes part in native forms; a form reset restores defaultValue. A required grid says so in its description (labels.required), since its role does not allow aria-required.
- **Composition**: items render as ColorSwatch elements before children. A ColorSwatch, ImageSwatch or EmptySwatch rendered directly as a child lets you wrap it, for example in a Tooltip with relationship="label" to name and describe it. A swatch rendered outside any SwatchPicker throws in development. React Server Components use the flat SwatchPickerRow name for SwatchPicker.Row.
Examples
Default
export const Default: Story = {
args: {
defaultValue: 'blue',
},
};Square
export const Square: Story = {
args: {
shape: 'square',
defaultValue: 'yellow',
},
};Large
export const Large: Story = {
args: {
size: 'large',
shape: 'rounded',
defaultValue: 'teal',
},
};Controlled
Selected: Red
export const Controlled: Story = {
render: function ControlledStory(args) {
const [value, setValue] = useState('red');
const selected = defaultItems.find((item) => item.value === value);
return (
<div className="flex flex-col gap-2">
<p className="text-wave-body-1 text-wave-foreground">
Selected: {selected?.label ?? 'none'}
</p>
<SwatchPicker
{...args}
value={value}
onValueChange={(next) => {
setValue(next);
args.onValueChange?.(next);
}}
/>
</div>
);
},
};Swatch children with tooltips
export const SwatchChildrenWithTooltips: Story = {
name: 'Swatch children with tooltips',
args: {
items: undefined,
defaultValue: 'ocean',
},
render: (args) => (
<SwatchPicker {...args}>
<Tooltip content="Ocean" relationship="label">
<ColorSwatch value="ocean" color={fixture.blue} />
</Tooltip>
<Tooltip content="Forest" relationship="label">
<ColorSwatch value="forest" color={fixture.green} />
</Tooltip>
<Tooltip content="Used for links and highlights" relationship="description">
<ColorSwatch value="berry" color={fixture.berry} aria-label="Berry" />
</Tooltip>
</SwatchPicker>
),
};Grid layout
export const GridLayout: Story = {
name: 'Grid layout',
args: {
items: undefined,
layout: 'grid',
defaultValue: 'blue',
},
render: (args) => (
<SwatchPicker {...args}>
<SwatchPicker.Row>
<ColorSwatch value="red" color={fixture.red} aria-label="Red" />
<ColorSwatch value="blue" color={fixture.blue} aria-label="Blue" />
<ColorSwatch value="green" color={fixture.green} aria-label="Green" />
</SwatchPicker.Row>
<SwatchPicker.Row>
<ColorSwatch value="yellow" color={fixture.yellow} aria-label="Yellow" />
<ColorSwatch value="purple" color={fixture.purple} aria-label="Purple" disabled />
<EmptySwatch onClick={fn()} />
</SwatchPicker.Row>
</SwatchPicker>
),
};Tab focus mode
export const TabFocusMode: Story = {
name: 'Tab focus mode',
args: {
focusMode: 'tab',
defaultValue: 'green',
},
render: (args) => (
<div className="flex flex-col gap-4">
<SwatchPicker {...args} aria-label="Accent color, row layout" />
<SwatchPicker {...args} items={undefined} layout="grid" aria-label="Accent color, grid">
<SwatchPicker.Row>
<ColorSwatch value="red" color={fixture.red} aria-label="Red" />
<ColorSwatch value="green" color={fixture.green} aria-label="Green" />
</SwatchPicker.Row>
<SwatchPicker.Row>
<ColorSwatch value="blue" color={fixture.blue} aria-label="Blue" />
</SwatchPicker.Row>
</SwatchPicker>
</div>
),
};Swatch kinds
export const SwatchKinds: Story = {
name: 'Swatch kinds',
args: {
items: undefined,
defaultValue: 'stripes',
},
render: (args) => (
<SwatchPicker {...args}>
<ColorSwatch value="red" color={fixture.red} aria-label="Red" />
<ColorSwatch
value="white"
color={fixture.white}
borderColor={fixture.edge}
aria-label="White"
/>
<ColorSwatch value="teal" color={fixture.teal} aria-label="Teal" disabled />
<ColorSwatch value="gold" color={fixture.yellow} aria-label="Gold" icon="*" />
<ColorSwatch
value="big"
color={fixture.purple}
aria-label="Large square"
size="large"
shape="square"
/>
<ImageSwatch value="stripes" src={stripes} aria-label="Stripes" />
<EmptySwatch onClick={fn()} />
</SwatchPicker>
),
};Sizes and spacing
export const SizesAndSpacing: Story = {
name: 'Sizes and spacing',
args: {
defaultValue: 'red',
},
render: (args) => (
<div className="flex flex-col gap-4">
<SwatchPicker
{...args}
size="extra-small"
spacing="small"
aria-label="Extra small, small spacing"
/>
<SwatchPicker {...args} size="small" spacing="small" aria-label="Small, small spacing" />
<SwatchPicker {...args} size="medium" aria-label="Medium, medium spacing" />
<SwatchPicker {...args} size="large" aria-label="Large, medium spacing" />
</div>
),
};Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
items | readonly SwatchItem[] | No | The swatches to display, in order (a readonly array is accepted; it is never modified): rendered as | |
value | string | No | Controlled selected swatch value ( | |
defaultValue | string | No | '' | Initial selected value for uncontrolled usage (also what a form reset restores). |
onValueChange | (value: string) => void | No | Called with the new value when the selection changes (not when the selected swatch is chosen again). | |
children | React.ReactNode | No | Swatches rendered directly, in order, after | |
layout | 'row' | 'grid' | No | 'row' |
|
focusMode | 'tab' | 'arrow' | No | 'arrow' |
|
labels | SwatchPickerLabels | No | Built-in strings, for localization. | |
size | 'small' | 'extra-small' | 'medium' | 'large' | No | 'medium' | Size of each swatch button (20, 24, 32 or 40px). A 20px swatch passes the target-size bar through its spacing; a swatch's own |
spacing | 'small' | 'medium' | No | 'medium' | Gap between swatches (and between rows): 4px or 8px. At |
shape | 'circular' | 'square' | 'rounded' | No | 'circular' | Shape of each swatch button (a swatch's own |
name | string | No | Form field name. With a name, the selected swatch's | |
required | boolean | No | A swatch must be selected before the form can be submitted (native validation). The row layout says so with | |
form | string | No | Id of the form the picker belongs to, when it is rendered outside that form. | |
ref | React.Ref<HTMLDivElement> | No | Ref to the root element: |
Any other prop is passed to the element it renders (HTMLDivElement).
Parts
SwatchPicker.Row
Stable class names
| Part | Class |
|---|---|
root | wave-swatch-picker |
swatch | wave-swatch-picker__swatch |