Skip to content
GitHub (opens in a new tab)

ColorPicker

Inline colour picker for theming and branding settings: saturation/brightness area, hue track, optional opacity track, hex Input, and optional preset swatches. One piece, no popover.

stableMoleculecontracts/colorpicker.json

Preview

ColorPicker

Inline colour picker: saturation and brightness area, hue, optional opacity, a hex Input, and preset swatches. The picked colour is user data, painted with inline style; all chrome uses tokens. Not ColorArea, HueSlider, or input type=color.

Master

Drag the area or hue, use the arrow keys (Shift for ×10), pick a swatch, or type a hex. Current value: #2F6FEB

Usage

The area is one 2D slider: left and right set saturation, up and down set brightness. Hue and opacity are sliders. Swatches are a radio group with one Tab stop. A full hex applies as you type; an invalid hex shows a message and keeps the last valid colour. With opacity, onChange returns #RRGGBBAA below 100%.

Do's

  • Picking a brand, theme, label, or chart colour in a settings form
  • Offering a short brand palette as presets with a free-form fallback
  • Inside Field when the colour needs a label, hint, or error

Don'ts

  • Choosing among a few named options that happen to be colours, with no free pick — use RadioGroup
  • Styling the kit's own UI — use semantic tokens, not a picked colour
  • A trigger button that opens a picker panel — not built yet; place ColorPicker inline
<ColorPicker
  value="#2F6FEB"
  onChange={setColour}
  swatches={["#2F6FEB", { value: "#0E9F6E", label: "Green" }, …]}
/>

When to use

  • Picking a brand, theme, label, or chart colour in a settings form
  • Offering a short brand palette as presets with a free-form fallback
  • Inside Field when the colour needs a label, hint, or error

When not to use

  • Choosing among a few named options that happen to be colours, with no free pick — use RadioGroup
  • Styling the kit's own UI — use semantic tokens, not a picked colour
  • A trigger button that opens a picker panel — not built yet; place ColorPicker inline

What an agent reads

import { ColorPicker } from "agentic-ds-kit";
must
  • Area is one role=slider with aria-roledescription '2D slider': ← → saturation, ↑ ↓ brightness, 1 per press, Shift ×10; PageUp/PageDown brightness ±10; Home/End saturation 0/100. aria-valuetext 'Saturation 80%, brightness 60%'
  • Hue is role=slider 0 to 359: ← → (or ↑ ↓) 1 degree, Shift ×10, PageUp/PageDown ±10, Home/End. aria-valuetext '210 degrees, blue'
  • Opacity (showAlpha) is role=slider 0 to 100 with the same keys. aria-valuetext '80%'
  • Pointer: click or drag the area and tracks; the control takes focus so keys continue from there
  • Swatches are role=radiogroup of role=radio buttons with aria-checked, one Tab stop (roving tabindex). Arrows move and select, Home/End jump. Name is the swatch label or its hex
  • Hex Input uses kit Input with '#' in the start slot. A complete 6 or 8 digit hex applies while typing; 3 or 4 digit short forms apply on blur or Enter. 3/6 digits keep the current opacity; 4/8 digits set it. A swatch sets full opacity
  • Invalid hex sets aria-invalid on the Input, shows invalidMessage (role=alert) linked by aria-describedby, and keeps the last valid colour
  • Inside Field, Field label htmlFor matches id; Field describedBy and error land on the hex Input
  • disabled removes every part from the Tab order and blocks pointer and keys
  • Colour is never the only cue: the selected swatch has a ring, and every value has text (hex, valuetext, percent)
defaults
  • showAlpha false
  • disabled false
  • error false
  • ariaLabel Colour
  • invalidMessage Enter a hex colour like 3366FF.
stops if
  • Do not invent ColorArea, HueSlider, AlphaSlider, ColorSwatch, ColorInput, or SwatchPicker cousins — use ColorPicker props
  • Do not use a native input type=color
  • Do not add a colour library; hex/HSV conversion lives in the component
  • Do not use a picked colour for kit chrome; it is content
  • Do not put raw hex or rgb()/hsl() in CSS; the picked colour arrives through inline style only
  • Do not wrap ColorPicker in a Popover or Drawer as a picker trigger without a plan task

From contracts/colorpicker.json: must is the a11y requirements, defaults the prop defaults, stops if the doNot rules where the agent asks instead of inventing a cousin.

Props

NameTypeRequiredDefaultNotes
valuestringNo—Controlled hex. #RRGGBB, or #RRGGBBAA when showAlpha and opacity is below 100%. Accepts #RGB / #RGBA / #RRGGBB / #RRGGBBAA, case-insensitive.
defaultValuestringNo—Uncontrolled starting hex. With neither value nor defaultValue the picker starts at black.
onChangefunctionNo—(hex: string) => void. Uppercase #RRGGBB, or #RRGGBBAA with showAlpha below 100% opacity. Fires on every committed change (drag, keys, swatch, valid hex).
swatchesarrayNo—(string | { value, label? })[]. Preset colours shown as a radio group. The label is the accessible name; without it the hex is read out.
showAlphabooleanNofalseAdds an opacity track over a checkerboard and a percent readout.
disabledbooleanNofalse—
idstringNo—Hex Input id. Inside Field, pass the same value as htmlFor so the Field label names the hex Input.
describedBystringNo—Set by Field (hint or error id).
errorbooleanNofalseSet by Field when it has an error. Danger border on the hex Input.
ariaLabelstringNoColourNames the group and prefixes the slider names: '{ariaLabel} saturation and brightness', '{ariaLabel} hue', '{ariaLabel} opacity', '{ariaLabel} presets'. Use the Field label text, e.g. 'Brand colour'.
invalidMessagestringNoEnter a hex colour like 3366FF.Shown under the hex Input while the typed text is not a valid hex. The last valid colour is kept.
  • Component
    →

    RadioGroup

    Choose exactly one option from a small visible set.