2 ColorSwatches
L4stIdi0t edited this page 2026-11-17 10:55:14 +01:00

ColorSwatches

A palette of named colours as a grid of equal squares, and one more square for any colour at all.

Every cell is the same size, the same border and the same selected ring — including the custom one, which is a native colour input laid over a swatch rather than the browser's own rounded well. It is a listbox: one tab stop, arrows move in two dimensions, Home/End go to the ends, and Enter or Space chooses. Focus does not choose, because a colour change is often expensive (a re-render, a re-slice) and arrowing past six colours to reach the seventh should not pay for six. The name of the colour under the pointer or the focus is shown once, under the grid — a palette of identical-looking greys is unreadable without it.

<ColorSwatches colors={palette} bind:value aria-label="Filament color" />

Forms · two-way: bind:value · submits in a form with name · keyboard contract

<script lang="ts">
	import { ColorSwatches } from '@l4st/driftwood';
</script>
Light Dark
color-swatches color-swatches in dark mode

Props

Prop Type Default What it does
colors required Swatch[] — In render order. Two with the same value are told apart by name.
value bind: string — The chosen colour. One not in colors shows on the custom square.
columns number 8 Squares per row. Every square is the same size whatever the count.
custom boolean true The square that opens the system colour picker.
customLabel string 'Custom color' The custom square's accessible name.
showName boolean true The name of the colour under the pointer or focus, else the chosen one, under the grid.
name string — Submits the chosen value with the form.
class string — Merged with the component's own classes, last — so yours win.
classes Classes<ColorSwatchesPart> — Classes for the parts inside: body (the grid), swatch (each colour square; the chosen one has aria-selected), icon (the check on the chosen square), custom (the custom-colour square; carries data-selected when the value is a custom colour), customIcon (its plus), label (the name under the grid). Merged last, so they win.
aria-label string — Names the palette, e.g. "Filament color".
onValueChange (value: string) => void — Fires once per choice: a click, Enter or Space, or the colour picker closing.

Types

export type Swatch = {
	/** What is chosen: a colour string, usually `#rrggbb`. */
	value: string;
	/** Said by a screen reader, shown on hover and under the grid. */
	name: string;
	/** CSS background when it is not just `value` — a gradient for a mixed spool, say. */
	fill?: string;
};
export type ColorSwatchesPart = 'body' | 'swatch' | 'icon' | 'custom' | 'customIcon' | 'label';

Keyboard

Key Does
← / → / ↑ / ↓ move through the grid, a row at a time up and down — without choosing
Home / End first / last colour
Enter / Space choose the focused colour
Tab on to the custom square, which opens the system colour picker

Example

The markup behind the screenshot above, lifted from src/routes/shots/ — so it compiles, and it is exactly what produced the image.

<ColorSwatches
	colors={[
		{ value: '#f2f2f2', name: 'Jade White' },
		{ value: '#111111', name: 'Black' },
		{ value: '#8e9089', name: 'Gray' },
		{ value: '#c12e1f', name: 'Red' },
		{ value: '#ff6a13', name: 'Orange' },
		{ value: '#f4ee2a', name: 'Yellow' },
		{ value: '#00ae42', name: 'Bambu Green' },
		{ value: '#0a2989', name: 'Blue' },
		{ value: '#5e43b7', name: 'Purple' },
		{ value: '#f55a74', name: 'Pink' },
		{ value: '#9d432c', name: 'Brown' }
	]}
	value="#00ae42"
	columns={6}
	aria-label="Filament color"
/>

Forms · Field · Input · FileInput · FileList · Textarea · CodeEditor · Combobox · RadioGroup · NumberInput · InlineEdit · Label · Select · MultiSelect · DatePicker · TagsInput · Checkbox · Switch · Slider


← All components · Getting started · Theming · source