# Swatch Picker

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

Page: https://uicontext.com/docs/components/swatch-picker

Group: Primitives.

## When to use

Pick one color from a few, such as a label's, with the arrow keys or a tap. Use [Color Dot](https://uicontext.com/docs/components/color-dot.md) instead when people only see the color, not choose it.

## Usage

```ts
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

Each example is a complete file. Its imports point where the CLI installs the source.

### A label's color

The example the page opens with: Swatch Picker in “A label's color”.

```ts title="src/demo.ts" source="www/src/examples/swatch-picker/demo.ts"
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 choose
export 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"),
			),
		),
})
```

### Square, with No fill

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

```ts title="src/notes.ts" source="www/src/examples/swatch-picker/notes.ts"
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 first
export 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 |
| --- | --- | --- | --- |
| `name` (required) | `string` | — | The radios' shared name, unique on the page. |
| `label` (required) | `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. |
| `swatches` (required) | `Swatch[]` | — | The colors, in order. |
| `value` (required) | `string \| null` | — | The chosen color, or null for none. |
| `onChange` (required) | `(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

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

With npm: `npx shadcn@latest add checkfu/uicontext/swatch-picker`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/swatch-picker`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/swatch-picker`.

It writes these files:

- `src/ui/uicontext/components/SwatchPicker.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/touch.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/SwatchPicker.ts](https://github.com/checkfu/uicontext/blob/main/src/SwatchPicker.ts)
- [src/touch.stylex.ts](https://github.com/checkfu/uicontext/blob/main/src/touch.stylex.ts)

## Used in

- [SaaS dashboard](https://uicontext.com/blocks/saas-dashboard.md)
- [Workflow builder](https://uicontext.com/blocks/automation-builder.md)
- [Team chat](https://uicontext.com/blocks/team-chat.md)
- [Whiteboard](https://uicontext.com/blocks/whiteboard.md)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
