Color
Color Swatch Picker
One colour of a palette of swatches: a listbox whose arrows move along and between its rows.
npx shadcn@latest add @fragiola/color-swatch-pickerNo colour variants. The colour is the palette class the element carries: the
palettes row above changes nothing but className="palette-*". A new colour is a
new palette (Writing a palette) — the component never
changes.
A color swatch picker chooses one colour from a palette. It is a listbox whose options take the
focus: Tab enters at the chosen option (else the first), Left and Right move along a row and wrap
(swapped in RTL), Up and Down move to the nearest option of the row above or below (the rows are
the ones your layout wraps into), Home and End go to the ends, and Space, Enter or a press choose.
A focus move never chooses. An option is chosen when its colour is the value's, whatever the
space. name submits the chosen colour as hex.
Each Item takes a value (its colour); its content is a Color Swatch
of that value unless you give one, and the swatch's name is the option's name, so name it
(aria-label="Ocean"). Name the list with aria-label or aria-labelledby.
ColorSwatchPicker.Template.Simple renders an option per colour of colors ({ value, label }
names one). Inside a Color Picker, a swatch picker with neither value
nor defaultValue binds to the picker's colour.
The look
It composes the color family and the foundation:
- the Root is
color.swatchPicker, a wrapping row with a 4px gap; - an Item is
color.swatchItem, a 2px frame around its swatch. The primitive marks the chosen optiondata-selected; the item addsdata-checkedfrom its state, so the chosen one is the foundation'schecked(aringframe: "on" isring, on every palette), the others take thehoverfill, and the focus is the standalone ring outside it.
Bases
Primitives-backed. Base UI has no swatch picker, so this is one shared file
(ui/color-swatch-picker.tsx) that wraps Fragiola Primitives' ColorSwatchPicker on both
bases, and the item depends on @fragiola/[email protected] on both. It follows the direction
of the base's DirectionProvider on Base UI too, and dir="rtl" without one.
On Base UI, the registry's Field is Base UI's, and a Fragiola Primitives control does not join
it: name the list with aria-labelledby (the label's id) or aria-label, as the example does,
and pass name and disabled to the Root. On Fragiola Primitives the picker is also a Field
control.
Parts
| Part | Source |
|---|---|
Root | primitive ColorSwatchPicker.Root + color.swatchPicker() |
Item | primitive ColorSwatchPicker.Item + color.swatchItem() (data-checked from its state); a ColorSwatch of its value by default |
Template.Simple | an option per colour of colors |