Swatch Picker

View as Markdown

Pick one color from a few, such as a label's, with the arrow keys or a tap.

Label color

Previewcustomer-reported

When to use

Pick one color from a few, such as a label's, with the arrow keys or a tap. Use Color Dot instead when people only see the color, not choose it.

Usage

import { SwatchPicker } from "./ui/uicontext/components/SwatchPicker.ts"
export const Color = <M>(h: Html.H<M>, value: string | null, picked: (value: string | null) => M) =>	SwatchPicker(h, { name: "label-color", label: "Color", value, onChange: picked, swatches: [{ value: "#2563eb", name: "Blue" }, { value: "#16a34a", name: "Green" }] })

Examples

Square, with No fill

none adds a first swatch for no color; size and shape fit a toolbar or a panel.

Ask Wei about the Q1 freight rates before Friday.
Fill

API reference

Options

SwatchPicker options
NameTypeDefaultDescription
namerequiredstring—The radios' shared name, unique on the page.
labelrequiredstring—What's being colored: "Label color".
hideLabelboolean—Hides the label, for a picker whose purpose shows around it. Screen readers still hear it.
swatchesrequiredSwatch[]—The colors, in order.
valuerequiredstring | null—The chosen color, or null for none.
onChangerequired(value: string | null) => Message—Called with the color chosen, or null for none.
nonestring—Adds a first swatch for no color, named this: "No color".
size"sm" | "md" | "lg"md20, 28 or 36px; md by default.
shape"round" | "square"roundround by default.
styleStyle——

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/swatch-picker

Source