Skip to main content
Kobalte

Color Picker

Since v2.1.0@kobalte/core/color-pickerSource

Combines color controls into one picker that shares a single value, optionally inside a popover.

Import

import { ColorPicker } from "@kobalte/core/color-picker";
// or
import { Root, Label, Trigger, Content, ValueSwatch, ... } from "@kobalte/core/color-picker";

Features

  • Shares one color between the existing color components. A ColorArea, ColorWheel, ColorSlider, ColorField, ColorChannelField, ColorSwatch or ColorSwatchPicker placed inside a picker with no value of its own reads and writes the picker's color.
  • Renders in a popover with Trigger and Content, or inline without them.
  • Fires onChange while the color changes and onChangeEnd when an interaction ends, keyboard steps included.
  • Display formats (hex, rgb, hsl, hsb) with a native select or a cycling button. Changing the format never converts the stored color.
  • An eye dropper button where the browser supports the EyeDropper API.
  • Submits the color with a form through a hidden input, and restores the default color on form reset.
  • Disabled and read-only state propagate to every control inside the picker.

Anatomy

The color picker consists of:

  • ColorPicker: The root container. It provides the color to the controls inside it.
  • ColorPicker.Label: The label of the picker. It labels the trigger and the content.
  • ColorPicker.Trigger: The button that opens the popover.
  • ColorPicker.Portal: Portals its children into the body.
  • ColorPicker.Content: The popover that holds the controls.
  • ColorPicker.Arrow: An optional arrow pointing at the trigger.
  • ColorPicker.CloseButton: An optional button that closes the popover.
  • ColorPicker.ValueSwatch: A swatch of the current color.
  • ColorPicker.ValueText: The current color as text, in the current format.
  • ColorPicker.FormatSelect: A native select that changes the display format.
  • ColorPicker.FormatTrigger: A button that moves to the next display format.
  • ColorPicker.EyeDropperTrigger: A button that picks a color from the screen.
  • ColorPicker.HiddenInput: A hidden input that submits the color with a form.
  • ColorPicker.Description: A description of the picker.
  • ColorPicker.ErrorMessage: An error message for the picker.
<ColorPicker>
<ColorPicker.Label />
<ColorPicker.Trigger>
<ColorPicker.ValueSwatch />
<ColorPicker.ValueText />
</ColorPicker.Trigger>
<ColorPicker.HiddenInput />
<ColorPicker.Portal>
<ColorPicker.Content>
<ColorPicker.Arrow />
<ColorArea />
<ColorSlider channel="hue" />
<ColorPicker.FormatSelect />
<ColorPicker.EyeDropperTrigger />
<ColorSwatchPicker />
</ColorPicker.Content>
</ColorPicker.Portal>
<ColorPicker.Description />
<ColorPicker.ErrorMessage />
</ColorPicker>

Example

Fill
import { ColorArea } from "@kobalte/core/color-area";
import { ColorChannelField } from "@kobalte/core/color-channel-field";
import { ColorField } from "@kobalte/core/color-field";
import { ColorPicker, useColorPickerContext } from "@kobalte/core/color-picker";
import { ColorSlider } from "@kobalte/core/color-slider";
import { ColorSwatch } from "@kobalte/core/color-swatch";
import { ColorSwatchPicker } from "@kobalte/core/color-swatch-picker";
import { type Color, parseColor } from "@kobalte/core/colors";
import { createSignal, For } from "solid-js";
import style from "./color-picker.module.css";
const PRESETS = [
"#ef4444",
"#f97316",
"#eab308",
"#22c55e",
"#06b6d4",
"#3b82f6",
"#8b5cf6",
"#ec4899",
];
function Area() {
return (
<ColorArea class={style.area} xChannel="saturation" yChannel="brightness">
<ColorArea.Background class={style["area-background"]}>
<ColorArea.Thumb class={style["area-thumb"]}>
<ColorArea.HiddenInputX />
<ColorArea.HiddenInputY />
</ColorArea.Thumb>
</ColorArea.Background>
</ColorArea>
);
}
function ChannelSlider(props: { channel: "hue" | "alpha" }) {
return (
<ColorSlider class={style.slider} channel={props.channel}>
<ColorSlider.Track class={style["slider-track"]}>
<ColorSlider.Thumb class={style["slider-thumb"]}>
<ColorSlider.Input />
</ColorSlider.Thumb>
</ColorSlider.Track>
</ColorSlider>
);
}
function Swatches() {
return (
<ColorSwatchPicker class={style.swatches} aria-label="Presets">
<For each={PRESETS}>
{(color) => (
<ColorSwatchPicker.Item class={style["swatch-item"]} value={color}>
<ColorSwatch class={style.swatch} />
</ColorSwatchPicker.Item>
)}
</For>
</ColorSwatchPicker>
);
}
function Trigger() {
return (
<ColorPicker.Trigger class={style.trigger}>
<ColorPicker.ValueSwatch class={style["value-swatch"]} />
<ColorPicker.ValueText />
</ColorPicker.Trigger>
);
}
export function BasicExample() {
return (
<ColorPicker class={style.root}>
<ColorPicker.Label class={style.label}>Fill</ColorPicker.Label>
<Trigger />
<ColorPicker.Portal>
<ColorPicker.Content class={style.content}>
<Area />
<ChannelSlider channel="hue" />
<ChannelSlider channel="alpha" />
<ColorField class={style.field}>
<ColorField.Input class={style.input} aria-label="Hex color" />
</ColorField>
</ColorPicker.Content>
</ColorPicker.Portal>
</ColorPicker>
);
}
.root {
display: inline-flex;
flex-direction: column;
gap: 6px;
}
.inline {
display: flex;
flex-direction: column;
gap: 12px;
width: 220px;
}
.label {
color: hsl(240 6% 10%);
font-size: 14px;
font-weight: 500;
text-transform: capitalize;
user-select: none;
}
.trigger {
display: inline-flex;
align-items: center;
gap: 8px;
height: 40px;
padding: 0 12px 0 6px;
border-radius: 6px;
border: 1px solid hsl(240 6% 90%);
background-color: white;
color: hsl(240 4% 16%);
font-size: 14px;
font-variant-numeric: tabular-nums;
outline: none;
}
.trigger:focus-visible {
outline: 2px solid hsl(200 98% 39%);
outline-offset: 2px;
}
.trigger[data-disabled] {
opacity: 0.5;
}
/*
* The swatch paints its color as a background-color, which a background-image
* would hide. Repaint the color as the top image layer over a checkerboard, so
* transparent colors show it. The custom properties are inherited from the
* picker root and the swatch picker item.
*/
.value-swatch,
.swatch {
flex-shrink: 0;
width: 28px;
height: 28px;
border-radius: 4px;
background-image:
linear-gradient(var(--color), var(--color)),
repeating-conic-gradient(#e4e4e7 0% 25%, white 0% 50%);
background-size:
auto,
12px 12px;
}
.value-swatch {
--color: var(--kb-color-picker-value);
}
.swatch {
--color: var(--kb-color-swatch-picker-item-color);
}
.content {
display: flex;
flex-direction: column;
gap: 12px;
width: 220px;
padding: 12px;
border-radius: 8px;
border: 1px solid hsl(240 6% 90%);
background-color: white;
box-shadow:
0 10px 15px -3px rgb(0 0 0 / 0.1),
0 4px 6px -4px rgb(0 0 0 / 0.1);
outline: none;
z-index: 50;
}
.area {
position: relative;
user-select: none;
touch-action: none;
}
.area-background {
position: relative;
width: 100%;
height: 150px;
border-radius: 6px;
}
.area-thumb,
.slider-thumb {
display: block;
width: 16px;
height: 16px;
border-radius: 9999px;
border: 2px solid white;
box-shadow: 0 0 0 1px rgb(0 0 0 / 0.3);
background: var(--kb-color-current);
}
.area-thumb:focus-visible,
.slider-thumb:focus-visible {
outline: 2px solid hsl(200 98% 39%);
outline-offset: 2px;
}
.slider {
position: relative;
user-select: none;
touch-action: none;
}
.slider-track {
position: relative;
height: 16px;
border-radius: 6px;
}
.field,
.channel {
display: flex;
flex-direction: column;
gap: 4px;
min-width: 0;
}
.channels {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
}
.input,
.select {
width: 100%;
min-width: 0;
box-sizing: border-box;
padding: 4px 8px;
border-radius: 6px;
border: 1px solid hsl(240 6% 90%);
background-color: white;
color: hsl(240 4% 16%);
font-size: 14px;
outline: none;
}
.input:focus-visible,
.select:focus-visible {
outline: 2px solid hsl(200 98% 39%);
outline-offset: -1px;
}
.row {
display: flex;
align-items: center;
gap: 8px;
}
.button {
appearance: none;
height: 32px;
padding: 0 12px;
border-radius: 6px;
border: 1px solid hsl(240 6% 90%);
background-color: white;
color: hsl(240 4% 16%);
font-size: 14px;
white-space: nowrap;
}
.button:focus-visible {
outline: 2px solid hsl(200 98% 39%);
outline-offset: 2px;
}
.swatches {
display: grid;
grid-template-columns: repeat(4, 28px);
gap: 8px;
outline: none;
}
.swatch-item {
border-radius: 6px;
outline: none;
}
.swatch-item[data-highlighted],
.swatch-item:focus-visible {
outline: 2px solid hsl(200 98% 39%);
outline-offset: 2px;
}
.swatch-item[data-selected] {
box-shadow:
0 0 0 2px white,
0 0 0 4px hsl(240 4% 16%);
}
.form {
display: flex;
flex-direction: column;
gap: 16px;
}
.output {
font-size: 14px;
margin-top: 16px;
margin-bottom: 0;
}

Usage

Default value

Pass an uncontrolled color with defaultValue. Prefer an hsb or hsl color: an rgb color loses its hue when its saturation reaches 0.

Fill
<ColorPicker defaultValue={parseColor("hsb(200, 80%, 90%)")}>
<ColorPicker.Label>Fill</ColorPicker.Label>
<ColorPicker.Trigger>
<ColorPicker.ValueSwatch />
<ColorPicker.ValueText />
</ColorPicker.Trigger>
<ColorPicker.Portal>
<ColorPicker.Content>
<ColorArea>...</ColorArea>
<ColorSlider channel="hue">...</ColorSlider>
</ColorPicker.Content>
</ColorPicker.Portal>
</ColorPicker>

Controlled value

Use value and onChange to control the color. onChangeEnd fires once when a drag ends, a key is pressed, a swatch is picked or a field is committed, which suits saving the color.

Fill

Color: hsb(280 70% 80%)

const [value, setValue] = createSignal(parseColor("hsb(280, 70%, 80%)"));
<ColorPicker value={value()} onChange={setValue}>
...
</ColorPicker>
<p>Color: {value().toString("hsb")}</p>

Binding controls

Any color component inside the picker with no value of its own reads and writes the picker's color. Give a component its own value to keep it independent. A defaultValue on a bound component is ignored; set it on the picker instead.

Formats

ColorPicker.ValueText shows the color in the current format, with an alpha channel added when the color is not opaque. Change the format with ColorPicker.FormatSelect, ColorPicker.FormatTrigger, or the format, defaultFormat and onFormatChange props.

Changing the format does not convert the stored color. useColorPickerContext().colorSpace() gives the color space of the current format, so you can render channel fields that match it.

Fill
function ChannelFields() {
const context = useColorPickerContext();
return (
<For each={parseColor("#000").toFormat(context.colorSpace()).getColorChannels()}>
{(channel) => (
<ColorChannelField channel={channel} colorSpace={context.colorSpace()}>
<ColorChannelField.Label>{channel}</ColorChannelField.Label>
<ColorChannelField.Input />
</ColorChannelField>
)}
</For>
);
}
<ColorPicker defaultFormat="rgb">
...
<ColorPicker.Content>
<ColorPicker.FormatSelect />
<ColorPicker.FormatTrigger>Next format</ColorPicker.FormatTrigger>
<ChannelFields />
</ColorPicker.Content>
</ColorPicker>

Swatches

A ColorSwatchPicker inside the picker selects the picker's color. Set closeOnSelect to close the popover when a swatch is picked.

Fill
<ColorPicker defaultValue={parseColor("#3b82f6")} closeOnSelect>
...
<ColorPicker.Content>
<ColorSwatchPicker aria-label="Presets">
<For each={PRESETS}>
{(color) => (
<ColorSwatchPicker.Item value={color}>
<ColorSwatch />
</ColorSwatchPicker.Item>
)}
</For>
</ColorSwatchPicker>
</ColorPicker.Content>
</ColorPicker>

Inline

Leave out Trigger and Content to render the controls inline.

ColorPicker.EyeDropperTrigger renders only once the browser is known to support the EyeDropper API, after hydration. Set forceMount to render a disabled button with data-unsupported instead.

Fill
#36B389
<ColorPicker defaultValue={parseColor("hsb(160, 70%, 70%)")}>
<ColorPicker.Label>Fill</ColorPicker.Label>
<ColorArea>...</ColorArea>
<ColorSlider channel="hue">...</ColorSlider>
<ColorPicker.ValueSwatch />
<ColorPicker.ValueText />
<ColorPicker.EyeDropperTrigger>Pick</ColorPicker.EyeDropperTrigger>
</ColorPicker>

HTML forms

Set name and render ColorPicker.HiddenInput to submit the color. It is submitted as hex, or hexa when the color is not opaque, whatever the display format. Set the input's format prop to change that. A form reset restores defaultValue and, when the format is uncontrolled, defaultFormat.

Fill
<form onSubmit={onSubmit}>
<ColorPicker name="fill" defaultValue={parseColor("hsb(20, 90%, 95%)")}>
<ColorPicker.Label>Fill</ColorPicker.Label>
<ColorPicker.Trigger>...</ColorPicker.Trigger>
<ColorPicker.HiddenInput />
...
</ColorPicker>
<button type="reset">Reset</button>
<button type="submit">Submit</button>
</form>

API Reference

ColorPicker

ColorPicker is equivalent to the Root import from @kobalte/core/color-picker.

PropDescription
valueColor
The controlled color.
defaultValueColor
The color when initially rendered. Defaults to hsb(0, 100%, 100%).
onChange(value: Color) => void
Event handler called when the color changes.
onChangeEnd(value: Color) => void
Event handler called when an interaction that changes the color ends.
format"hex" | "rgb" | "hsl" | "hsb"
The controlled display format.
defaultFormat"hex" | "rgb" | "hsl" | "hsb"
The display format when initially rendered. Defaults to "hex".
onFormatChange(format: ColorPickerFormat) => void
Event handler called when the display format changes.
closeOnSelectboolean
Whether picking a color in a ColorSwatchPicker closes the popover. Defaults to false.
openboolean
The controlled open state of the popover.
defaultOpenboolean
The open state of the popover when initially rendered.
onOpenChange(isOpen: boolean) => void
Event handler called when the open state of the popover changes.
modalboolean
Whether the popover is modal. Defaults to false.
placementPlacement
The placement of the popover. Defaults to "bottom-start".
gutternumber
The distance between the popover and the trigger. Defaults to 8.
idstring
A unique identifier for the component. The id is used to generate id attributes for nested components. If no id prop is provided, a generated id will be used.
namestring
The name submitted by ColorPicker.HiddenInput.
validationState'valid' | 'invalid'
Whether the picker should display its "valid" or "invalid" visual styling.
requiredboolean
Whether the user must pick a color before the owning form can be submitted.
disabledboolean
Whether the picker and every control inside it is disabled.
readOnlyboolean
Whether the picker and every control inside it is read only.
translationsColorPickerIntlTranslations
Localization strings.

The root also accepts the positioning props of Popover: shift, flip, slide, overlap, sameWidth, fitViewport, hideWhenDetached, detachedPadding, arrowPadding, overflowPadding, getAnchorRect, preventScroll and forceMount.

Data attributeDescription
data-validPresent when the picker is valid.
data-invalidPresent when the picker is invalid.
data-requiredPresent when the picker is required.
data-disabledPresent when the picker is disabled.
data-readonlyPresent when the picker is read only.

The root sets the --kb-color-picker-value CSS custom property to the current color.

ColorPicker.Label, ColorPicker.Trigger, ColorPicker.Description and ColorPicker.ErrorMessage share the same data attributes. ColorPicker.Trigger also has data-expanded while the popover is open.

ColorPicker.Content, ColorPicker.Arrow, ColorPicker.CloseButton and ColorPicker.Portal

These take the props of the matching Popover parts.

ColorPicker.ValueSwatch

PropDescription
respectAlphaboolean
Whether the swatch shows the alpha channel. Defaults to true.
colorNamestring
A localized accessible name for the color. Defaults to a generated color name.

ColorPicker.ValueText

PropDescription
formatColorFormat | "css"
The format of the text. Defaults to the picker's format, with alpha when the color has it.

ColorPicker.FormatSelect

PropDescription
formatsColorPickerFormat[]
The formats to choose from. Defaults to all four. Ignored when it has children.

ColorPicker.FormatTrigger

PropDescription
formatsColorPickerFormat[]
The formats to cycle through. Defaults to all four.
Data attributeDescription
data-formatThe current format.

ColorPicker.EyeDropperTrigger

PropDescription
forceMountboolean
Render a disabled button when the EyeDropper API is unsupported, instead of rendering nothing.
Data attributeDescription
data-unsupportedPresent when the EyeDropper API is unsupported.

ColorPicker.HiddenInput

PropDescription
formatColorFormat
The format of the submitted value. Defaults to "hex", or "hexa" when the color is not opaque.

Rendered elements

ComponentDefault rendered element
ColorPickerdiv
ColorPicker.Labelspan
ColorPicker.Triggerbutton
ColorPicker.PortalPortal
ColorPicker.Contentdiv
ColorPicker.Arrowdiv
ColorPicker.CloseButtonbutton
ColorPicker.ValueSwatchdiv
ColorPicker.ValueTextspan
ColorPicker.FormatSelectselect
ColorPicker.FormatTriggerbutton
ColorPicker.EyeDropperTriggerbutton
ColorPicker.HiddenInputinput
ColorPicker.Descriptiondiv
ColorPicker.ErrorMessagediv

Accessibility

Keyboard Interactions

KeyDescription
Space / EnterWhen focus is on the trigger, opens the popover and moves focus into it.
EscCloses the popover and moves focus back to the trigger.
EnterWhen focus is on a bound ColorField, commits the typed color.

The controls inside the picker keep their own keyboard interactions.

The trigger is named by the color, together with the label when there is one ("Fill, vibrant red"). Without a label it reads "Select color, vibrant red".

Last updated: 12/4/26, 3:58 PM