A list of color swatches the user can pick one color from.
Import
import { ColorSwatchPicker } from "@kobalte/core/color-swatch-picker";// orimport { Root, Item, ItemIndicator } from "@kobalte/core/color-swatch-picker";Features
- Exposed to assistive technology as a listbox, with one tab stop.
- Arrow keys move in two dimensions in a grid layout, measured from the rendered positions of the swatches, or up and down in a stack layout.
- Selection does not follow focus: Enter, Space or a click picks a color.
- Matches the value against the swatches in any color space, so
hsl(0, 100%, 50%)selects the#f00swatch. - A
ColorSwatchinside an item takes the item's color. - Inside a
ColorPicker, selects the picker's color.
Anatomy
The color swatch picker consists of:
- ColorSwatchPicker: The listbox that contains the swatches.
- ColorSwatchPicker.Item: An option of the list.
- ColorSwatchPicker.ItemIndicator: Rendered only when its item is selected.
<ColorSwatchPicker> <ColorSwatchPicker.Item> <ColorSwatch /> <ColorSwatchPicker.ItemIndicator /> </ColorSwatchPicker.Item></ColorSwatchPicker>Example
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-swatch-picker.module.css";
const COLORS = [ "#ef4444", "#f97316", "#eab308", "#22c55e", "#06b6d4", "#3b82f6", "#8b5cf6", "#ec4899", "#78716c",];
function Items() { return ( <For each={COLORS}> {(color) => ( <ColorSwatchPicker.Item class={style.item} value={color}> <ColorSwatch class={style.swatch} /> <ColorSwatchPicker.ItemIndicator class={style.indicator}> ✓ </ColorSwatchPicker.ItemIndicator> </ColorSwatchPicker.Item> )} </For> );}
export function BasicExample() { return ( <ColorSwatchPicker class={style.root} aria-label="Colors"> <Items /> </ColorSwatchPicker> );}.root { display: grid; grid-template-columns: repeat(3, 32px); gap: 8px; outline: none;}
.stack { display: flex; flex-direction: column; gap: 4px; width: 160px; outline: none;}
.item { position: relative; border-radius: 6px; outline: none;}
.stack-item { display: flex; align-items: center; gap: 8px; padding: 4px; border-radius: 6px; font-size: 14px; outline: none;}
.item[data-highlighted],.stack-item[data-highlighted] { outline: 2px solid hsl(200 98% 39%); outline-offset: 2px;}
.item[data-selected] { box-shadow: 0 0 0 2px white, 0 0 0 4px hsl(240 4% 16%);}
.stack-item[data-selected] { background-color: hsl(240 5% 96%);}
.item[data-disabled],.stack-item[data-disabled] { opacity: 0.4;}
.swatch { width: 32px; height: 32px; border-radius: 6px;}
.stack-item .swatch { width: 20px; height: 20px; border-radius: 4px;}
.indicator { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: white; font-size: 16px; pointer-events: none;}
.output { font-size: 14px; margin-top: 16px; margin-bottom: 0;}Usage
Default value
<ColorSwatchPicker aria-label="Colors" defaultValue={parseColor("#22c55e")}> ...</ColorSwatchPicker>Controlled value
Selected color: #3B82F6
const [value, setValue] = createSignal(parseColor("#3b82f6"));
<ColorSwatchPicker aria-label="Colors" value={value()} onChange={setValue}> ...</ColorSwatchPicker><p>Selected color: {value().toString("hex")}</p>Stack layout
Set layout="stack" for a vertical list. Only the up and down arrows move focus.
#ef4444
#f97316
#eab308
#22c55e
<ColorSwatchPicker layout="stack" aria-label="Colors"> <For each={colors}> {(color) => ( <ColorSwatchPicker.Item value={color}> <ColorSwatch /> <span>{color}</span> </ColorSwatchPicker.Item> )} </For></ColorSwatchPicker>Disabled items
Disabled items can't be picked, and the keyboard skips them.
<ColorSwatchPicker.Item value="#f97316" disabled> <ColorSwatch /></ColorSwatchPicker.Item>API Reference
ColorSwatchPicker
ColorSwatchPicker is equivalent to the Root import from @kobalte/core/color-swatch-picker.
| Prop | Description |
|---|---|
| value | Color The controlled selected color. |
| defaultValue | Color The selected color when initially rendered. |
| onChange | (value: Color) => void Event handler called when the selected color changes. |
| layout | "grid" | "stack" How the swatches are laid out. Defaults to "grid". |
| id | string 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. |
| validationState | 'valid' | 'invalid' Whether the swatch picker should display its "valid" or "invalid" visual styling. |
| disabled | boolean Whether the swatch picker is disabled. |
| readOnly | boolean Whether the swatch picker is read only: swatches can be focused but not picked. |
| Data attribute | Description |
|---|---|
| data-layout | The layout, grid or stack. |
| data-valid | Present when the swatch picker is valid. |
| data-invalid | Present when the swatch picker is invalid. |
| data-disabled | Present when the swatch picker is disabled. |
| data-readonly | Present when the swatch picker is read only. |
ColorSwatchPicker.Item
| Prop | Description |
|---|---|
| value | Color | string The color of the item. A string is parsed with parseColor. |
| colorName | string The accessible name of the color. Defaults to a generated color name. |
| disabled | boolean Whether the item is disabled. |
| Data attribute | Description |
|---|---|
| data-selected | Present when the item is selected. |
| data-disabled | Present when the item is disabled. |
| data-highlighted | Present when the item has keyboard focus. |
The item sets the --kb-color-swatch-picker-item-color CSS custom property to its color.
ColorSwatchPicker.ItemIndicator
| Prop | Description |
|---|---|
| forceMount | boolean Used to force mounting when more control is needed, for example with animations. |
Rendered elements
| Component | Default rendered element |
|---|---|
ColorSwatchPicker | div |
ColorSwatchPicker.Item | div |
ColorSwatchPicker.ItemIndicator | span |
Accessibility
Keyboard Interactions
| Key | Description |
|---|---|
| Tab | Moves focus to the selected item, or the first enabled item. |
| ArrowRight / ArrowLeft | In a grid, moves focus to the next or previous item. Does not wrap. |
| ArrowDown / ArrowUp | Moves focus to the closest item in the next or previous row, or the next or previous item in a stack. |
| Home / End | Moves focus to the first or last enabled item. |
| Space / Enter | Picks the focused color. |
Last updated: 12/4/26, 3:58 PM