ColorPicker
A color picker whose value is a hex string (#rrggbb, or #rrggbbaa with an alpha byte), grouped as a named role="group".
- **Default layout** (no children): a color area (saturation and brightness) and a hue slider, then a preview and the hex field, preset swatches and, with showOpacity, an opacity row whose slider is an AlphaSlider (0–100 %). Dragging to gray, white or black keeps the hue.
- **Your own layout** (children): ColorArea, ColorSlider and AlphaSlider, which bind to the picker while they have neither value nor defaultValue, and the parts ColorPicker.HexField, ColorPicker.Swatches and ColorPicker.Preview.
- **Hex field**: the text is a draft. Complete 6- and 8-digit values apply while typing; text that can never be a hex color is flagged at once (aria-invalid plus the hexError description). Leaving the field or pressing Enter applies a short #rgb (keeping the current opacity) or #rgba, then shows the current value again; other text stays flagged until it is corrected, cleared, or the value changes from elsewhere. Enter still submits the surrounding form, with the color just applied.
- **Presets** render as a SwatchPicker radiogroup ("Preset colors"): one tab stop, arrow keys move and pick, and picking keeps the current opacity.
- **Naming**: aria-label/aria-labelledby name the group; inside a Field its label does, and the Field's hint and error describe the group. The invalid state (aria-invalid, from the Field or the prop) is set on the hex field, since role="group" does not support it, and a consumer aria-errormessage goes there too. A consumer aria-required makes the hidden form input required instead. The built-in texts are localised with labels.
- **Forms**: with name the color is submitted with the form; a form reset restores defaultValue and reports it through onValueChange only when the color changes.
React Server Components cannot dot into a client module: import the flat names ColorPickerHexField, ColorPickerSwatches and ColorPickerPreview there; ColorPicker.HexField etc. work in client files.
Examples
Default
export const Default: Story = {};With Opacity
export const WithOpacity: Story = {
args: {
showOpacity: true,
defaultValue: '#0f6cbdcc', // wave-allow-color: fixture
},
};Custom Presets
export const CustomPresets: Story = {
args: {
presets: [
{ color: '#ff6b6b', label: 'Coral' }, // wave-allow-color: fixture
{ color: '#ffa94d', label: 'Tangerine' }, // wave-allow-color: fixture
{ color: '#ffd43b', label: 'Sunflower' }, // wave-allow-color: fixture
{ color: '#69db7c', label: 'Mint' }, // wave-allow-color: fixture
{ color: '#4dabf7', label: 'Sky' }, // wave-allow-color: fixture
{ color: '#9775fa', label: 'Lavender' }, // wave-allow-color: fixture
],
defaultValue: '#ff6b6b', // wave-allow-color: fixture
},
};No Presets
export const NoPresets: Story = {
args: {
presets: [],
defaultValue: '#242424', // wave-allow-color: fixture
},
};Controlled
Color: #0f6cbd
export const Controlled: Story = {
args: {
showOpacity: true,
},
render: function ControlledStory(args) {
const [color, setColor] = useState('#0f6cbd'); // wave-allow-color: fixture
return (
<div className="flex flex-col gap-2">
<p className="flex items-center gap-2 text-wave-body-1 text-wave-foreground">
<span
aria-hidden="true"
className="inline-block h-4 w-4 rounded border border-wave-border"
style={{ backgroundColor: color }}
/>
Color: {color}
</p>
<ColorPicker
{...args}
value={color}
onValueChange={(next) => {
setColor(next);
args.onValueChange?.(next);
}}
/>
</div>
);
},
};Composed
export const Composed: Story = {
render: (args) => (
<ColorPicker {...args}>
<ColorArea />
<ColorSlider />
<AlphaSlider />
</ColorPicker>
),
};Custom Layout
export const CustomLayout: Story = {
render: (args) => (
<ColorPicker {...args}>
<div className="flex gap-3">
<ColorArea className="h-48" />
<ColorSlider orientation="vertical" className="h-48" />
<AlphaSlider orientation="vertical" className="h-48" />
</div>
<div className="flex items-start gap-2">
<ColorPicker.Preview />
<ColorPicker.HexField />
</div>
<ColorPicker.Swatches />
</ColorPicker>
),
};Rgb And Hsl Fields
export const RgbAndHslFields: Story = {
render: function RgbAndHslStory(args) {
const [color, setColor] = useState('#0f6cbd'); // wave-allow-color: fixture
const rgb = [1, 3, 5].map((index) => parseInt(color.slice(index, index + 2), 16));
const hsl = hsvToHsl(hexToHsv(color) ?? { h: 0, s: 0, v: 0 });
const setRgb = (channel: number, next: number) => {
const bytes = rgb.map((value, index) => (index === channel ? next : value));
setColor(`#${bytes.map((value) => value.toString(16).padStart(2, '0')).join('')}`);
};
const setHsl = (key: 'h' | 's' | 'l', next: number) =>
setColor(hsvToHex(hslToHsv({ ...hsl, [key]: key === 'h' ? next : next / 100 })));
return (
<div className="flex flex-col gap-3">
<ColorPicker
{...args}
value={color}
onValueChange={(next) => {
setColor(next);
args.onValueChange?.(next);
}}
/>
<fieldset className="flex gap-2">
<legend className="text-wave-caption-1 text-wave-muted-foreground">RGB</legend>
{['Red', 'Green', 'Blue'].map((label, index) => (
<SpinButton
key={label}
aria-label={label}
min={0}
max={255}
value={rgb[index]}
onValueChange={(next) => setRgb(index, next)}
/>
))}
</fieldset>
<fieldset className="flex gap-2">
<legend className="text-wave-caption-1 text-wave-muted-foreground">HSL</legend>
<SpinButton
aria-label="Hue"
min={0}
max={360}
value={Math.round(hsl.h)}
onValueChange={(next) => setHsl('h', next)}
/>
<SpinButton
aria-label="Saturation"
min={0}
max={100}
value={Math.round(hsl.s * 100)}
onValueChange={(next) => setHsl('s', next)}
/>
<SpinButton
aria-label="Lightness"
min={0}
max={100}
value={Math.round(hsl.l * 100)}
onValueChange={(next) => setHsl('l', next)}
/>
</fieldset>
</div>
);
},
};In Popover
export const InPopover: Story = {
render: function InPopoverStory(args) {
const [color, setColor] = useState('#0f6cbd'); // wave-allow-color: fixture
const [draft, setDraft] = useState(color);
const [open, setOpen] = useState(false);
return (
<Popover
open={open}
onOpenChange={(next) => {
setOpen(next);
if (next) setDraft(color);
}}
>
<Popover.Trigger>
<Button
icon={
<span
aria-hidden="true"
className="inline-block size-4 rounded border border-wave-border"
style={{ backgroundColor: color }}
/>
}
>
Accent color
</Button>
</Popover.Trigger>
<Popover.Content title="Accent color">
<div className="flex flex-col gap-3">
<ColorPicker
{...args}
value={draft}
onValueChange={(next) => {
setDraft(next);
args.onValueChange?.(next);
}}
/>
<div className="flex justify-end gap-2">
<Button onClick={() => setOpen(false)}>Cancel</Button>
<Button
appearance="primary"
onClick={() => {
setColor(draft);
setOpen(false);
}}
>
Apply
</Button>
</div>
</div>
</Popover.Content>
</Popover>
);
},
};Square
export const Square: Story = {
args: { shape: 'square', showOpacity: true },
};Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
value | string | No | Controlled hex color: | |
defaultValue | string | No | '#0f6cbd' | Initial hex color for uncontrolled usage (also what a form reset restores). Like every edit it is shown, submitted and reported as a lowercase |
onValueChange | (color: string) => void | No | Called with the new color when it changes: a lowercase | |
presets | ReadonlyArray<string | ColorPickerPreset> | No | Blue, Red, Green, Yellow, Purple, Teal, Pink and Black (Fluent brand colors) | Preset colors shown as quick-select swatches. Pass |
showOpacity | boolean | No | false | Whether the default layout shows the opacity row: the caption, an alpha slider (0–100 %, named by |
labels | ColorPickerLabels | No | Texts of the picker's own parts and of the color parts bound to it, for localisation. | |
shape | 'square' | 'rounded' | No | 'rounded' |
|
children | React.ReactNode | No | Your own layout: | |
name | string | No | Form field name. With a name, the current color is submitted with the form. | |
required | boolean | No | Marks the hidden form input as required (a color is always chosen, so it never blocks). A consumer | |
form | string | No | Id of the form the picker belongs to, when it is rendered outside that form. | |
ref | React.Ref<HTMLDivElement> | No | Ref to the root | |
slotProps | { opacityLabel?: SlotPart<'span'>; opacityValue?: SlotPart<'span'> } | No | Props for the elements the picker draws itself, by part, merged into each part's own: your classes after the part's (yours win a conflict), your styles over its styles, your handlers and attributes added, your ref attached. A part takes no |
Any other prop is passed to the element it renders (HTMLDivElement).
Parts
opacityLabelopacityValueColorPicker.HexFieldColorPicker.SwatchesColorPicker.Preview
Stable class names
| Part | Class |
|---|---|
root | wave-color-picker |
opacityLabel | wave-color-picker__opacity-label |
opacityValue | wave-color-picker__opacity-value |