Cómo funciona
The picker works in OKLCH so equal steps look equal. The area runs across from grey to the most chroma sRGB can show at that lightness and hue (found by bisection, so every point is a real colour) and up from black to white in OKLab lightness; it is drawn on a small canvas the browser scales smoothly. The hue slider's track samples OKLCH hues at one lightness, so no stretch of it is wider than another. The hex value is the truth: the thumbs are placed from it, except during a drag, when the thumbs lead and the hex follows. A grey keeps the last hue. The field parses #rgb, #rrggbb with or without #, rgb() and oklch(); pasting text that contains a colour (a CSS line, a token file) takes the first colour in it.
Úsalo para
- Choosing any colour: a brand colour, a theme, a chart series, a label people define themselves.
- When people may bring a value from elsewhere: paste a hex, an rgb() or an oklch(), or pick it from the screen.
Busca otra opción cuando
- A short fixed set of colours: use the swatch picker, which shows the result as people choose.
Uso
import { useState } from 'react'
import { ColourPicker } from './ColourPicker.jsx'
const swatches = [
{ value: '#2563eb', label: 'Cobalt' },
{ value: '#0d9488', label: 'Teal' },
{ value: '#e11d48', label: 'Rose' },
]
export function BrandColour() {
const [colour, setColour] = useState('#2563eb')
return (
<ColourPicker
label="Brand colour"
value={colour}
onChange={setColour}
swatches={swatches}
storageKey="brand-colour-recent"
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
value / defaultValue / onChange | string / string / (hex) => void | - / '#2563eb' / - | The colour as #rrggbb, controlled or uncontrolled. onChange fires as the colour changes, also during a drag; the value is always lower-case #rrggbb whatever format is shown. |
onCommit | (hex) => void | — | Called once a change is finished: a drag released, a key step, a swatch or the eyedropper picked, a paste, Enter or leaving the field with a valid colour. Use it for work too heavy to repeat on every pointer move. |
swatches | (string | { value, label })[] | [] | Preset colours shown under the field; label names one for screen readers and the tooltip. Values that are not hex colours are left out. |
storageKey / recentLimit | string / number | - / 8 | Recent colours are added when the popover closes on a new colour. With storageKey they are kept in localStorage under that key; without it they last for the page. |
open / defaultOpen / onOpenChange | boolean / boolean / (open) => void | - / false / - | Whether the popover is open, controlled or uncontrolled. Opening with the trigger moves focus to the area; Escape closes and returns focus to the trigger; a press outside closes. |
inline | boolean | false | Shows the panel in the flow, without the trigger, for a side panel or a settings page. |
align | 'start' | 'end' | 'start' | Which edge of the trigger the popover lines up with. |
format / defaultFormat / onFormatChange | 'hex' | 'rgb' | 'oklch' | - / 'hex' / - | The format the field shows and copies. The field accepts any of the three whatever is shown. |
label / labels | string / object | 'Colour' / English | label names the trigger (followed by the value). labels replaces any built-in text: dialog, area, saturation, lightness, hue, field, format, eyedropper, copy, copied, recent, presets, invalid. |
disabled | boolean | false | Disables the trigger. |
...rest | HTMLAttributes | — | Other attributes go to the root <div>. |
Accesibilidad
- The trigger is a button named by label and the value, with aria-haspopup="dialog" and aria-expanded; its text is dark or white, whichever reads better on the colour.
- The area is a role="slider" with aria-roledescription="2D slider" and a value text giving saturation and lightness; Left and Right change saturation, Up and Down lightness, Shift or Page Up and Page Down by 10, Home and End to grey or full colour.
- The hue is a role="slider" from 0 to 359; arrows move by 1 degree, Shift by 10, Home and End to the ends.
- Opening moves focus to the area; Escape closes and returns focus to the trigger.
- The field is labelled, marks a value it cannot read with aria-invalid and a message, and copying is announced in a status region.
- On touch screens every button, field and slider row is 44px and the thumbs grow; dragging uses pointer capture with touch-action: none.
- The eyedropper only appears where the browser offers one. Reduced motion removes the open animation and the transitions.
Movimiento
Open: 200ms ease-out from 6px above and scale 0.96. Thumbs grow to 1.12 on focus and 1.18 while dragged in 100ms. Swatches grow to 1.1 on hover with a spring. The trigger's colour follows in 160ms.
Añádelo a tu proyecto
Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.
ColourPicker.jsxcolour-picker.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.





