Web · Components
ColorPicker
Color picker.
Live example
Preview
Code
Events
- Events appear here when you interact with the preview.
Inline, OKLCH and opacity
Theme colors: a chosen var(--ui-*) changes with the theme
Readable text color (WCAG contrast)
ColorSwatch: for showing in tables and lists
Import
Color picker. Clicking the color button in the box (or the text field) opens a panel with a saturation/brightness area, hue and alpha sliders, swatches and an eyedropper; a color can also be typed.
- Formats: the value is returned in the
formatformat (hex,rgb,hsl,oklch). The text field understands any CSS color (#f00,rgb(…),hsl(…),oklch(…),red,var(--ui-primary)); the format button changes the display. - Theme:
swatches="theme"offers the theme colors; a selectedvar(--ui-*)value is returned as is, so the color changes with the theme. - Keyboard: the area with arrow keys (horizontal saturation, vertical brightness), sliders with ←/→, Home/End; Shift takes 10× steps. ↓ in the box opens the panel.
- Hue is kept: going down to gray or black doesn't lose the hue; coming back returns to the same hue.
- Mobile: the panel is a bottom sheet; the page doesn't scroll while dragging with a finger, and vertical scrolling works on the sliders. Closes by pulling down and with the back button.
- Form: the value is submitted with
name;requiredworks and it returns to the initial color when the form resets.
To use it with a label, description and error message see ColorField; to only display a color see ColorSwatch.
Examples
ColorPicker with its label, description and error message. Takes all ColorPicker props (format, alpha, swatches, recent, inline…).
- With
requiredthe form can't be submitted without a color; an asterisk appears on the label. - Clicking the label focuses the text field.
- Server errors come from
erroror from the nearestForm'sstatebyname; they clear when the color changes.
Examples
Shows a color in a small box. Transparent colors are drawn over a checkerboard; an empty value is shown with a "no color" stripe. A server component.
Examples
resolveCssColor
functionResolves any color text the browser understands: names (rebeccapurple), theme variables (var(--ui-primary)), color-mix(), lab()… Variables are resolved where the scope element is (e.g. inside data-theme). parseColor is tried first; only it works on the server.
COLOR_FORMATS
constantSupported formats, in picker order.
rgbToHsv
functionRGB → HSV. Grays return hue 0; to keep the hue see keepHue.
hsvToRgb
functionHSV → RGB (fractional channels; rounded when written).
rgbToHsl
functionRGB → HSL (h 0–360, s/l 0–1).
hslToRgb
functionHSL → RGB.
rgbToOklch
functionRGB → OKLCH (l 0–1, c ~0–0.4, h 0–360).
oklchToRgb
functionOKLCH → RGB.
keepHue
functionKeeps the hue (and saturation on black) from the previous value when the new HSV value loses it. A picker dragged to gray or black does not jump to red.
parseColor
functionParses a color string. Supported forms: #rgb, #rgba, #rrggbb, #rrggbbaa (with or without #), rgb()/rgba(), hsl()/hsla(), hwb(), oklch(), oklab(), color(srgb …) (comma and space separated, with percentage and angle units) and transparent.
For forms the browser has to resolve, such as names (red) and theme variables (var(--ui-primary)), see resolveCssColor.
Examples
isColor
functionIs the string a valid color? See parseColor.
detectColorFormat
functionFormat the string is written in; undefined when unknown.
formatColor
functionWrites a color in the requested format. See ColorFormat.
Examples
convertColor
functionConverts a string to another format; undefined when it cannot be parsed.
Examples
luminance
functionWCAG relative luminance (0 black – 1 white).
contrastRatio
functionWCAG contrast ratio of two colors (1–21). At least 4.5 is recommended for normal text, 3 for large text. 1 when a color cannot be parsed.
Examples
readableColor
functionA readable text color on a color background: "#000000" or "#ffffff" (whichever has the higher contrast).
THEME_SWATCHES
constantTheme colors: swatches="theme" shows this list. Since the values are var(--ui-*), the selected color is that theme's color in every theme.
DEFAULT_SWATCHES
constantDefault swatches: the 500 shades of the Tailwind palette, white and black. Shown when swatches is omitted.
defaultColorNames
constantTurkish swatch names.
enColorNames
constantEnglish swatch names.
Rgba
interfacesRGB color: channels 0–255 (may be fractional), alpha 0–1. See parseColor, formatColor.
| Name | Type | Description |
|---|---|---|
r | number | Red, 0–255. |
g | number | Green, 0–255. |
b | number | Blue, 0–255. |
a | number | Alpha: 0 fully transparent, 1 fully opaque. |
Hsva
interfaceHSV (hue, saturation, value) color: the color picker's internal model. h 0–360, s and v 0–1, a 0–1.
| Name | Type | Description |
|---|---|---|
h | number | Hue, 0–360 degrees. |
s | number | Saturation, 0–1. |
v | number | Value (brightness), 0–1. |
a | number | Alpha, 0–1. |
ColorFormat
typeFormat of a color string.
"hex":#3b82f6(with alpha#3b82f680)"rgb":rgb(59 130 246)(with alphargb(59 130 246 / 0.5))"hsl":hsl(217 91% 60%)"oklch":oklch(0.623 0.188 259.8): perceptually even steps; the library's themes use this format.
FormatColorOptions
interfaceformatColor options.
| Name | Type | Default | Description |
|---|---|---|---|
alpha? | boolean | "auto" | "auto" | Write the alpha? "auto" writes it only when the color is transparent; false drops the alpha. |
ColorFieldProps
typeProps of the ColorField component.
Extends: Omit<FieldOwnProps, "classNames"> & Omit<Omit<PickerBaseProps, "icon" | "classNames" | "placeholder">, "className" | "classNames">
| Name | Type | Default | Description |
|---|---|---|---|
value? | string | Color in controlled use (any CSS color); "" for no color. | |
defaultValue? | string | Initial color in uncontrolled use. | |
onValueChange? | (value: string) => void | Called when the color changes; on every step while dragging. The value is in the format format; names and var(--ui-*) theme variables coming from a swatch or typing are returned as is. | |
onValueCommit? | (value: string) => void | Called when a selection completes: when a drag ends, on a key press, when a swatch or the eyedropper is used, and when typed color is confirmed. Use this to save to the server. | |
format? | ColorFormat | "hex" | Format of the returned value. See ColorFormat. Even if the user sees another format in the text field, the value is returned in this format. |
alpha? | boolean | false | Show an alpha slider and include alpha in the value? |
formats? | readonly ColorFormat[] | ["hex", "rgb", "hsl", "oklch"] | Formats the format button in the text field cycles through. With a single format the button is hidden. |
swatches? | readonly (string | ColorSwatchOption)[] | "theme" | false | Swatches. An array of strings or { value, label }; "theme" shows the theme colors (THEME_SWATCHES, var(--ui-*)); false hides them. Defaults to DEFAULT_SWATCHES. | |
recent? | boolean | string | false | Remember recently chosen colors (at most 8, stored in the browser). true uses a list shared by all pickers; a string keeps a separate list under that key (e.g. "brand-colors"). |
eyeDropper? | boolean | true | Pick color from screen (eyedropper) button. Only visible in supporting browsers (Chromium desktop). |
inline? | boolean | false | Show the picker inline on the page instead of a dropdown (panel always open). For settings pages and side panels. |
placeholder? | string | Text shown in the text field while empty. Defaults to a format example like #000000. | |
classNames? | FieldClassNames & ColorPickerClassNames | Classes added to the inner parts; control goes to the picker's container. |
ColorPickerClassNames
interfaceClasses that can be given to the inner parts of ColorPicker.
| Name | Type | Description |
|---|---|---|
box? | string | The field's visible box (color button and text field). |
input? | string | Text field in the box or the panel. |
content? | string | The open panel (popover / mobile bottom sheet). |
panel? | string | The picker itself (area, sliders, swatches). |
area? | string | Saturation/brightness area (e.g. h-56 for height). |
swatches? | string | Grid of swatches. |
ColorPickerProps
interfaceProps of the ColorPicker component.
Extends: Omit<PickerBaseProps, "icon" | "classNames" | "placeholder">
| Name | Type | Default | Description |
|---|---|---|---|
value? | string | Color in controlled use (any CSS color); "" for no color. | |
defaultValue? | string | Initial color in uncontrolled use. | |
onValueChange? | (value: string) => void | Called when the color changes; on every step while dragging. The value is in the format format; names and var(--ui-*) theme variables coming from a swatch or typing are returned as is. | |
onValueCommit? | (value: string) => void | Called when a selection completes: when a drag ends, on a key press, when a swatch or the eyedropper is used, and when typed color is confirmed. Use this to save to the server. | |
format? | ColorFormat | "hex" | Format of the returned value. See ColorFormat. Even if the user sees another format in the text field, the value is returned in this format. |
alpha? | boolean | false | Show an alpha slider and include alpha in the value? |
formats? | readonly ColorFormat[] | ["hex", "rgb", "hsl", "oklch"] | Formats the format button in the text field cycles through. With a single format the button is hidden. |
swatches? | readonly (string | ColorSwatchOption)[] | "theme" | false | Swatches. An array of strings or { value, label }; "theme" shows the theme colors (THEME_SWATCHES, var(--ui-*)); false hides them. Defaults to DEFAULT_SWATCHES. | |
recent? | boolean | string | false | Remember recently chosen colors (at most 8, stored in the browser). true uses a list shared by all pickers; a string keeps a separate list under that key (e.g. "brand-colors"). |
eyeDropper? | boolean | true | Pick color from screen (eyedropper) button. Only visible in supporting browsers (Chromium desktop). |
inline? | boolean | false | Show the picker inline on the page instead of a dropdown (panel always open). For settings pages and side panels. |
placeholder? | string | Text shown in the text field while empty. Defaults to a format example like #000000. | |
classNames? | ColorPickerClassNames | Classes added to the inner parts. |
ColorSwatchOption
interfaceA color in a swatch list.
| Name | Type | Description |
|---|---|---|
value | string | CSS color text: #3b82f6, oklch(0.62 0.19 260), rebeccapurple or a theme variable var(--ui-primary). Returned as is on click; theme variables follow the new color when the theme changes. |
label? | string | Screen reader and tooltip name. Defaults to name's translation, then value. |
name? | ColorName | A built-in color name; its text comes from the dictionary's ui.colorNames (used by THEME_SWATCHES and DEFAULT_SWATCHES). |
ColorSwatchProps
interfaceProps of the ColorSwatch component.
Extends: Omit<ComponentProps<"span">, "color">
| Name | Type | Default | Description |
|---|---|---|---|
color | string | undefined | CSS color to show (any format and var(--ui-*)). When empty, a cross-striped "no color" box is drawn. | |
size? | "sm" | "md" | "lg" | "md"` (20 px) | Size. |
shape? | "rounded" | "circle" | "square" | "rounded" | Corner shape. |
label? | string | Screen reader name. When omitted it is treated as decorative (aria-hidden); no name is needed when the color's name is written next to it. | |
effect? | EffectProp | Effect or effects of the box (e.g. "shine", "lift"). Defaults to UIProvider appearance.defaults.colorSwatch.effect. See BuiltinEffect. |
ColorNameMessages
interfaceNames of the built-in ColorPicker swatches: screen reader names and tooltips.
| Name | Type | Default | Description |
|---|---|---|---|
primary | string | "Birincil" | |
secondary | string | "İkincil" | |
accent | string | "Vurgu" | |
success | string | "Başarı" | |
warning | string | "Uyarı" | |
destructive | string | "Tehlike" | |
foreground | string | "Ön plan" | |
mutedForeground | string | "Soluk yazı" | |
border | string | "Kenarlık" | |
background | string | "Arka plan" | |
red | string | "Kırmızı" | |
orange | string | "Turuncu" | |
amber | string | "Kehribar" | |
yellow | string | "Sarı" | |
lime | string | "Limon" | |
green | string | "Yeşil" | |
teal | string | "Deniz yeşili" | |
cyan | string | "Camgöbeği" | |
blue | string | "Mavi" | |
indigo | string | "Çivit" | |
violet | string | "Mor" | |
fuchsia | string | "Fuşya" | |
pink | string | "Pembe" | |
stone | string | "Taş" | |
white | string | "Beyaz" | |
black | string | "Siyah" |
ColorName
typeKey of a built-in swatch name.