How it works
The preview holds one layer: the whole preview drawn in the settled colour. A new pick adds a second layer, the whole preview drawn again in the new colour and its ink, clipped to a circle centred on the pressed swatch (measured before paint, relative to the preview) that grows to the farthest corner. Because each layer carries its own copy of the content, text changes colour exactly where the edge passes, and the old colour stays underneath until it is covered. When a layer finishes it becomes the settled one and the layers under it are dropped, so fast picks stack without a jump. The ring is one element at the top left of the grid, moved by transform to the chosen column and row with an overshoot, so it never depends on layout measured in the browser.
Use it for
- Choosing a colour from a short, fixed set: a card, a project, a label, a calendar, a theme.
- When people should see the result on the real object while they choose.
Look elsewhere when
- Any colour at all: use a full colour picker with a field and a hue control.
- More than about sixteen choices: the row becomes hard to scan; group them or use a palette grid with names.
Usage
import { useState } from 'react'
import { SwatchPicker } from './SwatchPicker.jsx'
const colors = [
{ value: '#4f46e5', label: 'Indigo' },
{ value: '#0f766e', label: 'Lagoon' },
{ value: '#f5b301', label: 'Saffron' },
{ value: '#dc3d43', label: 'Coral' },
]
export function ProjectColour({ name }) {
const [color, setColor] = useState('#4f46e5')
return (
<SwatchPicker
label="Project colour"
colors={colors}
value={color}
onChange={setColor}
ratio={3}
// Text inside inherits the ink that reads best on the chosen colour
renderPreview={() => <strong style={{ display: 'block', padding: 16 }}>{name}</strong>}
/>
)
}Props
| Prop | Type | Default | What it does |
|---|---|---|---|
colors | { value, label, ink? }[] | — | The swatches in order. value is any CSS colour; label names it for screen readers; ink sets the preview text colour, which otherwise is dark or white by contrast (worked out for #rgb and #rrggbb values, white for anything else). |
value / defaultValue / onChange | string / string / (value, color) => void | - / first colour / - | The chosen colour's value, controlled or uncontrolled. onChange also receives the colour object. |
renderPreview | (color, { index, ink }) => ReactNode | — | Draws the preview for one colour. It is called for the settled colour and again for each colour still growing over it; those copies are aria-hidden and inert, so keep controls out of the preview. ink is the text colour chosen for that colour. Without it (and without children) only the swatches show; children may be a node or the same function. |
ratio | number | 1.586 | Aspect ratio of the preview; the default is a payment card. |
columns | number | colors.length | Swatches per row. With several rows, Up and Down move by a row. |
size / gap | number / number | 32 / 12 | Swatch diameter and the space between swatches, in pixels. The hit area of each swatch reaches halfway into the gap. |
label | string | 'Colour' | Accessible name of the radio group. |
...rest | HTMLAttributes | — | Other attributes go to the root <div>. |
Accessibility
- The swatches are role="radio" buttons in a role="radiogroup" named by label, each named by its colour's label; only the chosen one is in the tab order.
- Arrow keys select the next or previous colour and move focus with it (Up and Down move by a row when there are several), Home and End go to the ends.
- Every swatch has a hairline edge, so white and pale colours stay visible on any surface, and its hit area reaches halfway into the gap.
- Keyboard focus on the chosen swatch turns the ring to the accent colour with a halo; on another swatch it draws an outline.
- The preview text uses dark or white by contrast, or a colour's own ink; the copies drawn while a colour spreads are aria-hidden and inert, so the content is read once.
- Reduced motion replaces the growing circle with a short fade and moves the ring at once.
Motion
Flood: clip-path circle from 0 to the farthest corner in 640ms cubic-bezier(0.3, 0.6, 0.1, 1), the text inside changing with it. Ring: transform 560ms cubic-bezier(0.34, 1.3, 0.5, 1). Swatches grow to 1.1 on hover and press to 0.88 with a spring.
Add it to your project
Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.
SwatchPicker.jsxswatch-picker.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.





