Skip to content

SwatchPicker

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

NameTypeRequiredDefaultDescription
itemsreadonly SwatchItem[]No

The swatches to display, in order (a readonly array is accepted; it is never modified): rendered as ColorSwatch elements, before children.

valuestringNo

Controlled selected swatch value ('' = nothing selected).

defaultValuestringNo''

Initial selected value for uncontrolled usage (also what a form reset restores).

onValueChange(value: string) => voidNo

Called with the new value when the selection changes (not when the selected swatch is chosen again).

childrenReact.ReactNodeNo

Swatches rendered directly, in order, after items (e.g. ColorSwatch): read the picker's context, so they may be wrapped in a Tooltip or another element that forwards its own props. SwatchPicker.Row elements start rows; in the grid layout every run of other children (and items, with the children that follow them) renders in a row of its own. A SwatchPicker.Row must be a direct child or inside a Fragment: a wrapper component around a Row lands in an implicit row, and nested rows are reported in development.

layout'row' | 'grid'No'row'

'row': a radio group whose arrow keys select (and wrap). 'grid': rows of cells (SwatchPicker.Row) whose arrow keys move focus in two dimensions without selecting; Space, Enter and a click select. Changing it while mounted remounts the swatches, so focus falls back to the page.

focusMode'tab' | 'arrow'No'arrow'

'arrow': one tab stop and arrow keys, with the radio group (row layout) or grid (grid layout) semantics. 'tab': every enabled swatch is its own tab stop and the arrow keys do nothing, so the picker is a role="group" of toggle buttons (aria-pressed, the selected one pressed) in both layouts; rows have no role, and Space, Enter and a click select (pressing the selected swatch again keeps it selected). The required hint (labels.required) applies, as the group cannot carry aria-required.

labelsSwatchPickerLabelsNo

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 size overrides this.

spacing'small' | 'medium'No'medium'

Gap between swatches (and between rows): 4px or 8px. At 'small' the focus outline's offset shrinks from 4px to 2px, so it does not cover the neighbour.

shape'circular' | 'square' | 'rounded'No'circular'

Shape of each swatch button (a swatch's own shape overrides this).

namestringNo

Form field name. With a name, the selected swatch's value is submitted with the form (nothing while no swatch is selected).

requiredbooleanNo

A swatch must be selected before the form can be submitted (native validation). The row layout says so with aria-required; the grid layout and tab mode, whose roles do not allow it, with the hidden labels.required text in the description (which a consumer aria-required alone also shows).

formstringNo

Id of the form the picker belongs to, when it is rendered outside that form.

refReact.Ref<HTMLDivElement>No

Ref to the root element: role="radiogroup", role="grid" in the grid layout, or role="group" in tab mode.

Any other prop is passed to the element it renders (HTMLDivElement).

Parts

  • SwatchPicker.Row

Stable class names

PartClass
rootwave-swatch-picker
swatchwave-swatch-picker__swatch