Skip to content

Color selector

color-selector

A row of colour swatches where exactly one is chosen. Real radio inputs inside a fieldset, so forms, required state, arrow keys and screen-reader grouping all work natively, and one selection ring hops between swatches.

Install

$npxshadcnadd@forme/color-selector

Adds the component, its registry dependencies, and the token stylesheet you import once.

Usage

import {
  ColorSelector,
  ColorSelectorItem,
  ColorSelectorLabel,
  ColorSelectorList,
} from "@/components/forme/color-selector/color-selector";

<ColorSelector defaultValue="#3478f6" name="accent">
  <ColorSelectorLabel>Accent colour</ColorSelectorLabel>
  <ColorSelectorList>
    <ColorSelectorItem value="#3478f6" color="#3478f6" label="Blue" />
    <ColorSelectorItem value="#ec4899" color="#ec4899" label="Pink" />
  </ColorSelectorList>
</ColorSelector>

When to use

Reach for it

  • A fixed palette of six or so colours, where seeing them side by side is the point.
  • name on the group rather than per item: the inputs share it, so the browser submits one value and enforces required for you.
  • color as a CSS custom property when the swatches should follow the theme instead of being fixed hex values.

Use something else

  • Arbitrary colour choice — reach for a ColorPicker; a swatch row cannot express a colour it does not contain.
  • Relying on the swatch colour to carry meaning: every item still needs a label, because colour alone is not a name.

API reference

Read from the source, so it is the API of the file you install. A prop marked * is required.

ColorSelector

A row of colours where exactly one is chosen.

PropTypeDefaultDescription
valuestring—The selected value, for controlled use.
defaultValuestring""The initial value when uncontrolled. Default ""
onValueChange(value: string) => void—Called with the item's value when the selection changes.
namestring—The radio name used in form submission. Generated when omitted.
requiredbooleanfalseRequires a choice before the form submits. Default false
disabledbooleanfalseDims the whole group and stops every swatch from being picked.

Also takes every prop of <fieldset> attributes.

ColorSelectorItem

PropTypeDefaultDescription
value*string—The value reported and submitted when this colour is picked.
color*string—Any CSS colour, including a custom property.
label*string—Accessible colour name. The selection never relies on colour alone.
classNamestring—Applied to the visible swatch surface.

Also takes every prop of <input> attributes.

The other parts

  • ColorSelectorLabelTakes <legend> attributes
  • ColorSelectorListTakes <div> attributes

Accessibility

  • Native radios in a fieldset with a legend, so the group is announced, arrow keys move and select, and the browser validates required.
  • Focus lands on the visually hidden input and is drawn on the swatch with peer-focus-visible, so the ring the user sees is the control they are on.
  • Every item requires a label; the visible swatch is aria-hidden, so the name never depends on colour.

Motion

  • The selection ring is one shared element that hops along an arc, so the change reads as a move rather than a second swatch turning on.
  • The press scale is the only other motion; reduced motion removes the hop and the scale and keeps the selection itself.

Responsive behaviour

  • The list wraps, so a narrow container becomes two rows instead of overflowing.
  • size-11 swatches sit on the 44px touch target; a tighter row should keep 44px of vertical space even if the visual circle shrinks.