Skip to content

ColorPicker

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

Opacity
80%
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

Opacity
100%
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

RGB
HSL
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

Opacity
100%
export const Square: Story = {
  args: { shape: 'square', showOpacity: true },
};

Props

NameTypeRequiredDefaultDescription
valuestringNo

Controlled hex color: #rrggbb, or #rrggbbaa with an alpha byte (e.g. '#0f6cbd80' for 50% opacity). Other formats are not supported (development warning).

defaultValuestringNo'#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 #rrggbb, or #rrggbbaa when it is not fully opaque ('#ABC' becomes '#aabbcc').

onValueChange(color: string) => voidNo

Called with the new color when it changes: a lowercase #rrggbb, or #rrggbbaa when the color is not fully opaque.

presetsReadonlyArray<string | ColorPickerPreset>NoBlue, Red, Green, Yellow, Purple, Teal, Pink and Black (Fluent brand colors)

Preset colors shown as quick-select swatches. Pass { color, label } objects so screen reader users hear a name; a plain hex string is announced by its hex code. Non-hex presets are skipped (development warning). Picking a preset keeps the current opacity, so a preset's alpha digits are ignored. With an alpha below f/ff (#rgba, #rrggbbaa) there is a development warning, and a string preset is announced by the #rrggbb color it applies; an opaque alpha (#rgbf, #rrggbbff) changes nothing, and the string stays the name as written. With children, the presets feed only ColorPicker.Swatches.

showOpacitybooleanNofalse

Whether the default layout shows the opacity row: the caption, an alpha slider (0–100 %, named by labels.opacity) and the percentage. The opacity is the alpha byte of value. Does nothing with children.

labelsColorPickerLabelsNo

Texts of the picker's own parts and of the color parts bound to it, for localisation.

shape'square' | 'rounded'No'rounded'

'square' removes the corner radius of the preview and of the area and the rails of the color parts bound to it, including those of the default layout.

childrenReact.ReactNodeNo

Your own layout: ColorArea, ColorSlider and AlphaSlider (bound to the picker while they have neither value nor defaultValue) and the parts ColorPicker.HexField, ColorPicker.Swatches and ColorPicker.Preview. Without children the picker renders its default layout. With children, showOpacity does nothing and presets feed only ColorPicker.Swatches.

namestringNo

Form field name. With a name, the current color is submitted with the form.

requiredbooleanNo

Marks the hidden form input as required (a color is always chosen, so it never blocks). A consumer aria-required has the same effect: role="group" does not support it, so it is not rendered on the group.

formstringNo

Id of the form the picker belongs to, when it is rendered outside that form.

refReact.Ref<HTMLDivElement>No

Ref to the root role="group" element.

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 children. The parts carry no attribute of their own, so none is locked. Both are drawn only by the default layout with showOpacity and are ignored with children; the color area, the sliders, the hex field and the swatches of the default layout are components of their own, whose props it does not pass on (write your own layout to reach them).

Any other prop is passed to the element it renders (HTMLDivElement).

Parts

  • opacityLabel
  • opacityValue
  • ColorPicker.HexField
  • ColorPicker.Swatches
  • ColorPicker.Preview

Stable class names

PartClass
rootwave-color-picker
opacityLabelwave-color-picker__opacity-label
opacityValuewave-color-picker__opacity-value