Pick one color from a few, such as a label's, with the arrow keys or a tap.
- 2.1 kB JS 2.9 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 10 tests
Previewcustomer-reported
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { sx } from "./ui/uicontext/components/Sx.ts"import { SwatchPicker } from "./ui/uicontext/components/SwatchPicker.ts"import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ card: { display: "grid", gap: space.lg, inlineSize: "min(100%, 22rem)", padding: space.lg, borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.panel }, preview: { display: "flex", alignItems: "center", gap: space.sm, fontSize: font.sm }, label: { display: "inline-flex", alignItems: "center", gap: space.xs, paddingBlock: 2, paddingInline: space.sm, borderRadius: corner.pill, borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, fontWeight: 500 }, dot: { inlineSize: 8, blockSize: 8, borderRadius: corner.pill },})const colors = [ { value: "#6b7280", name: "Gray" }, { value: "#dc2626", name: "Red" }, { value: "#ea580c", name: "Orange" }, { value: "#ca8a04", name: "Yellow" }, { value: "#16a34a", name: "Green" }, { value: "#0891b2", name: "Teal" }, { value: "#2563eb", name: "Blue" }, { value: "#7c3aed", name: "Violet" }, { value: "#db2777", name: "Pink" },]const Message = UiSchema.TaggedEnum({ Picked: { color: Schema.String } })// a label's color in an issue tracker; the arrow keys move between swatches and chooseexport const LabelColor = Html.component({ name: "LabelColor", model: Schema.Struct({ color: Schema.String }), message: Message,}).make({ init: () => ({ color: "#2563eb" }), update: { Picked: ($, { color }) => $.evolve({ color: () => color }) }, view: (h, model) => h.div.attrs(sx(styles.card)).children( SwatchPicker(h, { name: "label-color", label: "Label color", swatches: colors, value: model.color, onChange: (c) => Message.Picked({ color: c ?? "" }) }), h.p.attrs(sx(styles.preview)).children( "Preview", h.span.attrs(sx(styles.label)).children(h.span.attrs({ "aria-hidden": "true", ...sx(styles.dot) }).style({ backgroundColor: model.color }), "customer-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.
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { sx } from "./ui/uicontext/components/Sx.ts"import { SwatchPicker } from "./ui/uicontext/components/SwatchPicker.ts"import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ wrap: { display: "flex", flexWrap: "wrap", alignItems: "flex-start", gap: space.lg }, note: { inlineSize: 160, minBlockSize: 120, padding: space.md, borderRadius: corner.control, borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, fontSize: font.sm, lineHeight: 1.4, color: "#1f2937" }, plain: { backgroundColor: color.panel, color: color.fg },})const fills = [ { value: "#fef08a", name: "Lemon" }, { value: "#bbf7d0", name: "Mint" }, { value: "#bae6fd", name: "Sky" }, { value: "#fbcfe8", name: "Blush" }, { value: "#1f2937", name: "Charcoal" },]const Message = UiSchema.TaggedEnum({ Picked: { fill: Schema.NullOr(Schema.String) } })// a whiteboard note's fill: large square swatches, and No fill firstexport const NoteFill = Html.component({ name: "NoteFill", model: Schema.Struct({ fill: Schema.NullOr(Schema.String) }), message: Message,}).make({ init: () => ({ fill: "#fef08a" }), update: { Picked: ($, { fill }) => $.evolve({ fill: () => fill }) }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( h.div .attrs(sx(styles.note, model.fill === null && styles.plain)) .style(model.fill === null ? {} : { backgroundColor: model.fill, color: model.fill === "#1f2937" ? "#f9fafb" : "#1f2937" }) .children("Ask Wei about the Q1 freight rates before Friday."), SwatchPicker(h, { name: "note-fill", label: "Fill", swatches: fills, value: model.fill, none: "No fill", size: "lg", shape: "square", onChange: (fill) => Message.Picked({ fill }) }), ),})API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
namerequired | string | — | The radios' shared name, unique on the page. |
labelrequired | string | — | What's being colored: "Label color". |
hideLabel | boolean | — | Hides the label, for a picker whose purpose shows around it. Screen readers still hear it. |
swatchesrequired | Swatch[] | — | The colors, in order. |
valuerequired | string | null | — | The chosen color, or null for none. |
onChangerequired | (value: string | null) => Message | — | Called with the color chosen, or null for none. |
none | string | — | Adds a first swatch for no color, named this: "No color". |
size | "sm" | "md" | "lg" | md | 20, 28 or 36px; md by default. |
shape | "round" | "square" | round | round by default. |
style | Style | — | — |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/swatch-picker