Search documentation

Search pages, components, blocks, styles and colours.

Get started
Skip to content

Color Picker

PreviousNext

A color picker with a saturation area, hue and alpha sliders, HEX, RGB, HSL and OKLCH fields, swatches and the eyedropper.

Drag in the area to set saturation and brightness, and use the sliders for hue and alpha (transparency); each shows its value beside it. Pick a format to type or scrub its channels: drag a field's letter sideways to change its value. Use it inline as a panel, or behind a trigger with ColorPickerPopover.

Color Picker is part of the Fabricator style. It's composed from Slider, Select, Input Group, Popover and Tooltip, so every control looks and behaves like the rest of your fields. It's adapted from the Color Picker of Fluid Functionalism (MIT).

Installation

bunx --bun fabricator-ui@latest add color-picker

Usage

import { ColorPicker, ColorPickerPopover } from "@/components/ui/color-picker"
<ColorPicker defaultValue="#6b97ff" />
 
<ColorPickerPopover defaultValue="#6b97ff" triggerLabel="Fill" />

Composition

ColorPicker
├── area (saturation and brightness)
├── hue and alpha sliders, each with its value
├── one row: format select, channel fields, eyedropper
└── swatches (optional)
 
ColorPickerPopover
├── trigger (tile, hex value, label) and optional remove button
└── ColorPicker, in a popover

ColorSwatch and ColorTile are exported too, to show a color elsewhere with the same checker behind translucent colors.

Examples

Popover

ColorPickerPopover shows the color as a tile and its hex value. triggerLabel adds a label beside it, before the tile or after it with triggerLabelPosition="right".

Swatches

swatches adds preset colors below the fields. Any CSS color works, named ones too, and a { color, label } swatch names the color in a tooltip and to screen readers. The swatch matching the current color is framed.

OKLCH

defaultFormat picks the format the fields show and onValueChange writes. In OKLCH the hue you type is kept while you change lightness and chroma, even through gray.

Controlled

Control the color with value and onValueChange. The value comes back in the selected format, and switching the format writes the same color again in the new one.

Current: rgb(107, 151, 255)

Removable

triggerShowRemove adds a remove button beside the trigger, which calls onTriggerRemove without opening the picker.

Sizes

size="sm" and size="lg" set the compact and spacious density on the trigger and the panel. Without it, both follow the nearest density.

Without the eyedropper

The eyedropper uses the browser's EyeDropper, which only Chromium browsers have; elsewhere the button doesn't render. hideEyedropper hides it everywhere.

What it reads

value, defaultValue and the hex field read:

  • hex with 3, 4, 6 or 8 digits, with or without #
  • rgb() and rgba(), with commas, spaces or a / before alpha
  • hsl() and hsla()
  • oklch(), with lightness as a percent or a fraction

The hex field and swatches also take named colors (tomato). A value that can't be read is ignored and the color stays as it was.

API Reference

ColorPicker

Every div prop, plus:

PropTypeDefault
valuestring-
defaultValuestring"#6b97ff"
onValueChange(value: string, color: ParsedColor) => void-
format"hex" | "rgb" | "hsl" | "oklch"-
defaultFormat"hex" | "rgb" | "hsl" | "oklch""hex"
onFormatChange(format: ColorFormat) => void-
swatches(string | { color: string; label?: string })[]-
hideEyedropperbooleanfalse
size"sm" | "default" | "lg""default"

onValueChange also hands over the color in every format: hex, rgb, hsl and oklch strings, the rounded r, g and b channels, and h, s, v and a.

ColorPickerPopover

Every ColorPicker prop, plus:

PropTypeDefault
triggerLabelstring-
triggerLabelPosition"left" | "right""left"
triggerShowValuebooleantrue
triggerShowRemovebooleanfalse
onTriggerRemove() => void-
triggerClassNamestring-
openboolean-
defaultOpenboolean-
onOpenChange(open: boolean) => void-

Data attributes

AttributeElement
data-slot="color-picker"the panel
data-slot="color-picker-area"the saturation and brightness area
data-slot="color-picker-area-thumb"the area's thumb
data-slot="color-picker-sliders"the hue and alpha sliders
data-slot="color-picker-hue"the hue slider and its value
data-slot="color-picker-alpha"the alpha slider and its value
data-slot="color-picker-format"the format select's trigger
data-slot="color-picker-eyedropper"the eyedropper button
data-slot="color-picker-fields"the channel fields
data-slot="color-picker-field"one field
data-slot="color-picker-swatches"the swatches
data-slot="color-picker-swatch"one swatch
data-slot="color-picker-trigger"the popover's trigger
data-slot="color-picker-remove"the remove button
data-slot="color-tile"a color tile

Accessibility

  • The area is a slider named "Color" that reports saturation and brightness as its value text. Left and right arrows change saturation, up and down brightness, by 1% (10% with Shift). Home and End jump to the saturation ends, Page Up and Page Down to the brightness ends.
  • The hue and alpha sliders are Sliders, named "Hue" and "Alpha", with the Slider's keyboard. The alpha slider is the only alpha control, in percent. The value beside each slider is hidden from screen readers, which hear it from the slider.
  • Each channel field is a text input named after its channel. Up and down arrows step it (ten times further with Shift), Enter or leaving the field commits typed text, and Escape puts the last value back. Hue fields wrap past 360.
  • Swatches are toggle buttons: the selected one reports aria-pressed, and a labelled swatch is named by its label.
  • The popover's trigger is a button that reports aria-expanded. Focus moves into the panel when it opens and back to the trigger when it closes. With a changed value in a field, the first Escape reverts the field and the second closes the popover.
  • The eyedropper is a button named "Pick a color from the screen". Pressing Escape during a pick cancels it.