Skip to main content
Kobalte

Color Swatch Picker

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

A list of color swatches the user can pick one color from.

Import

import { ColorSwatchPicker } from "@kobalte/core/color-swatch-picker";
// or
import { 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 #f00 swatch.
  • A ColorSwatch inside 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.

PropDescription
valueColor
The controlled selected color.
defaultValueColor
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".
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.
validationState'valid' | 'invalid'
Whether the swatch picker should display its "valid" or "invalid" visual styling.
disabledboolean
Whether the swatch picker is disabled.
readOnlyboolean
Whether the swatch picker is read only: swatches can be focused but not picked.
Data attributeDescription
data-layoutThe layout, grid or stack.
data-validPresent when the swatch picker is valid.
data-invalidPresent when the swatch picker is invalid.
data-disabledPresent when the swatch picker is disabled.
data-readonlyPresent when the swatch picker is read only.

ColorSwatchPicker.Item

PropDescription
valueColor | string
The color of the item. A string is parsed with parseColor.
colorNamestring
The accessible name of the color. Defaults to a generated color name.
disabledboolean
Whether the item is disabled.
Data attributeDescription
data-selectedPresent when the item is selected.
data-disabledPresent when the item is disabled.
data-highlightedPresent when the item has keyboard focus.

The item sets the --kb-color-swatch-picker-item-color CSS custom property to its color.

ColorSwatchPicker.ItemIndicator

PropDescription
forceMountboolean
Used to force mounting when more control is needed, for example with animations.

Rendered elements

ComponentDefault rendered element
ColorSwatchPickerdiv
ColorSwatchPicker.Itemdiv
ColorSwatchPicker.ItemIndicatorspan

Accessibility

Keyboard Interactions

KeyDescription
TabMoves focus to the selected item, or the first enabled item.
ArrowRight / ArrowLeftIn a grid, moves focus to the next or previous item. Does not wrap.
ArrowDown / ArrowUpMoves focus to the closest item in the next or previous row, or the next or previous item in a stack.
Home / EndMoves focus to the first or last enabled item.
Space / EnterPicks the focused color.

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