Color palette
A row of up to four color pickers with add and remove. Installed as source with the shadcn CLI.
On this page
ColorPalette keeps a set of one to four colors in a row of swatches. Every swatch is a ColorPicker; a button adds a color, Delete or a small × removes one. It edits an array of CSS strings the way the picker edits one.
Preview
"use client";
import { useState } from "react";
import { ColorPalette } from "@/components/ui/color-palette/color-palette";
export function BrandColors() {
const [colors, setColors] = useState([
"#245cdb",
"#f2b84b",
"#2f8f9d",
]);
return <ColorPalette colors={colors} onChange={setColors} />;
}Installation
The palette is its own registry item. It lists the color picker as a registry dependency, so the CLI adds the picker's files too if your project does not have them yet:
npx shadcn@latest add https://cloudlet.dev/r/color-palette.json| File | What it holds |
|---|---|
components/ui/color-palette/color-palette.tsx | ColorPalette, its props and labels |
components/ui/color-palette/color-palette.css | The styles, imported by the component itself |
components/ui/color-palette/color-palette-agent.ts | useColorPaletteAgent, which connects the palette to the Cloudlet agent bridge; nothing runs until you import it |
Usage
Pass colors and onChange to control it. onChange receives every color in the current format, in the reader's order, after any change, addition or removal; switching the format in one picker switches it for the whole palette and re-emits all of them. Its second argument, a ColorPaletteChange, says which color it was: { type: "set" | "add" | "remove", index }, or { type: "format" } when every color was rewritten; a plain setter can ignore it. With name, the palette renders one hidden input per color, so FormData.getAll() gives back the array:
"use client";
import { useState } from "react";
import { ColorPalette } from "@/components/ui/color-palette/color-palette";
export function BrandPalette() {
const [colors, setColors] = useState<string[]>(["#245cdb", "#f2b84b"]);
return (
<form action={(data) => console.log(data.getAll("brand"))}>
<ColorPalette colors={colors} onChange={setColors} max={3} name="brand" labels={{ group: "Brand palette" }} />
<button type="submit">Save</button>
</form>
);
}Each swatch belongs to its color, not to its place. When your code removes, inserts or reorders colors itself, the other swatches keep theirs: a picker the reader has open stays on the color it was opened for, and the picker of a removed color closes with it. The palette tells colors apart by their value, whatever format each is written in (rgb(255 0 0) and #ff0000 are one color), so of two identical colors it cannot know which one your code removed; either way the row shows the same colors.
A new color starts from the last one, turned around the OKLCH hue wheel by a full turn divided by max (a square of four at the default), and its picker opens at once. A grey has no hue to turn, so the new color steps in lightness instead.
On touch screens, which have no hover, the × stays in view on every swatch. Under prefers-reduced-motion: reduce or inside data-reduced-motion, swatches appear and go without their springs.
Examples
In a form
name on a palette that owns its colors: Save shows what FormData.getAll() receives.
"use client";
import { useState } from "react";
import { ColorPalette } from "@/components/ui/color-palette/color-palette";
export function BrandForm() {
const [sent, setSent] = useState<string[]>([]);
return (
<form className="grid justify-items-center gap-3" action={(data) => setSent(data.getAll("brand").map(String))}>
<ColorPalette defaultColors={["#245cdb", "#f2b84b"]} max={3} name="brand" labels={{ group: "Brand colors" }} />
<button type="submit" className="rounded-md border px-3 py-1.5 text-sm">
Save
</button>
<output className="text-sm">{sent.join(", ")}</output>
</form>
);
}Colors in OKLCH
defaultColors in any syntax the picker reads, and defaultFormat for what the palette gives back.
import { ColorPalette } from "@/components/ui/color-palette/color-palette";
export function OklchPalette() {
return <ColorPalette defaultColors={["oklch(51.9% 0.203 263.2)", "oklch(81.7% 0.14 80.1)"]} defaultFormat="oklch" />;
}Opacity
alpha adds the opacity slider to every picker.
import { ColorPalette } from "@/components/ui/color-palette/color-palette";
export function TranslucentPalette() {
return <ColorPalette defaultColors={["rgb(36 92 219 / 0.6)", "rgb(242 184 75)"]} defaultFormat="rgb" alpha />;
}Agent
color-palette-agent.ts connects the palette to the agent bridge, experimental for now. useColorPaletteAgent(agent, props, options) wraps the palette's props:
"use client";
import { createAgent } from "@cloudlet/agent";
import { useState } from "react";
import { ColorPalette } from "@/components/ui/color-palette/color-palette";
import { useColorPaletteAgent } from "@/components/ui/color-palette/color-palette-agent";
const agent = createAgent();
export function BrandAgent() {
const [colors, setColors] = useState<readonly string[]>(["#245cdb", "#ff9500"]);
const palette = useColorPaletteAgent(agent, { colors, onChange: setColors }, { name: "brand", label: "Brand palette" });
return <ColorPalette {...palette} />;
}The palette is one ColorPaletteEntry: label, colors as it shows them, max, format, formats and alpha. Its verbs, a ColorPaletteAction, are set [index, color], add [color] while there are fewer than max, and remove [index] while more than one is left; indexes count from 0, and each answers with the colors after it. ColorPaletteAgentOptions are name (default color-palette) and label (default labels.group); COLOR_PALETTE_VERBS is the verbs' text for a model. Each change reaches agent.subscribe under the key colors, and a step hands your onChange the same ColorPaletteChange as the reader's edits. Refusals name the palette: There is no color 5 in "Brand palette": it has colors 0–1., … already holds 4 colors, its most., … holds only one color, its least. Set it instead. The answer, the author of a change and the format follow the same rules as the picker's adapter, with whom the palette shares one author on the same agent.
API Reference
Props
ColorPaletteProps; every prop is optional. ColorFormat and the syntaxes a color may be written in are the picker's: Formats.
| Prop | Type | Default | Description |
|---|---|---|---|
colors | readonly string[] | — | The colors (controlled): one to max strings in hex, rgb(), hsl() or oklch(). An empty array shows one default color. |
defaultColors | readonly string[] | — | The initial colors when the palette owns them. |
onChange | (colors: string[], change: ColorPaletteChange) => void | — | Called with every color in format on any change, addition or removal; change names the color, see Usage. |
max | number | 4 | The most colors the palette holds; the add button is gone at this count. |
format | ColorFormat | — | The output format of every color (controlled); pair it with onFormatChange. |
defaultFormat | ColorFormat | the first of formats | The initial format when the palette owns it. |
onFormatChange | (format: ColorFormat) => void | — | Called when the reader picks another format in any picker. |
formats | readonly ColorFormat[] | all four | The formats every picker offers. A single entry hides the switch. |
alpha | boolean | false | Shows the opacity slider in every picker. |
name | string | — | Renders one <input type="hidden"> per color under this name. |
labels | Partial<ColorPaletteLabels> | DEFAULT_PALETTE_LABELS | Names and announcements; anything left out keeps its English default. |
className | string | — | Added to the palette's row. |
Keyboard
The palette is one Tab stop: ← and → move between the swatches and the add button, Home and End jump to the ends, Enter or Space opens the focused swatch's picker, and Delete or Backspace removes its color while more than one is left. After a removal, focus moves to the neighbouring swatch and a screen reader hears labels.removed; after an addition, focus is already in the new color's picker. Each swatch says its place and value, "Color 2 of 3, #2f8f9d". In forced-colors mode the swatches, the add button and the × draw borders in system colors. With a picker open, the keys are the picker's.
Labels
| Key | Default | Where |
|---|---|---|
group | Palette | the group's name |
color | Color {n} of {total} | each swatch's name, before its value |
add | Add color | the add button |
remove | Remove color | the × on a swatch |
removed | Color removed | the announcement after a removal |
picker | {} | labels of every swatch's picker (its open is ignored: the swatch is named by color) |