# Color picker

> A round swatch that morphs into a full color picker. Installed as source with the shadcn CLI.

Source: https://cloudlet.dev/docs/components/color-picker

`ColorPicker` is a swatch that opens into the picker; `ColorPickerPanel` is the same picker in the flow of the page, for forms and sidebars. Both edit a color given as a CSS string and give back a string in the format you allow.

## Preview

Live preview at https://cloudlet.dev/docs/components/color-picker#preview: the picker as a swatch or as an inline panel, the value its `onChange` gives, and controls for the component, the output format and opacity. The code they make:

```tsx
"use client";

import { useState } from "react";
import { ColorPickerPanel } from "@/components/ui/color-picker/color-picker";

export function AccentColor() {
  const [color, setColor] = useState("oklch(51.9% 0.203 263.2)");
  return <ColorPickerPanel color={color} onChange={setColor} defaultFormat="oklch" alpha />;
}
```

- Component: `ColorPicker` (the swatch that opens the picker) or `ColorPickerPanel` (the picker inline).
- `defaultFormat`: `hex` (the default), `rgb`, `hsl`, `oklch`; the starting color is written in it.
- `alpha`: off by default; on, the picker shows the opacity slider.

A prop at its default is left out; with every default the code is `<ColorPicker color={color} onChange={setColor} />`.

## Installation

The color picker is a shadcn registry item, not an npm package: the CLI copies its source into your project, where it is yours to change.

```sh title="Terminal"
npx shadcn@latest add https://cloudlet.dev/r/color-picker.json
```

It adds six files under your `ui` and `lib` aliases and nothing to `package.json`:

| File | What it holds |
| --- | --- |
| `components/ui/color-picker/color-picker.tsx` | `ColorPicker`, `ColorPickerPanel` and their types |
| `components/ui/color-picker/color-picker.css` | The styles, imported by the component itself |
| `components/ui/color-picker/color.ts` | Parsing, conversions and the canonical output |
| `components/ui/color-picker/morph.ts` | The springs of the swatch-to-panel morph |
| `components/ui/color-picker/color-picker-agent.ts` | `useColorPickerAgent`, which connects the picker to the Cloudlet agent bridge; nothing runs until you import it |
| `lib/spring.ts` | The spring easing both use |

The stylesheet reads the shadcn tokens `--background`, `--foreground`, `--border`, `--ring`, `--primary` with `--primary-foreground`, `--radius` and, if your theme has it, `--muted`. It resets its own buttons, so it does not rely on Tailwind's preflight.

## Usage

Pass `color` and `onChange` and the picker is controlled, like an input. `onChange` receives the value in the current format on every change, and once more when the reader switches the format, so your state, the field and a hidden input always agree.

```tsx title="accent-field.tsx"
"use client";

import { useState } from "react";
import { ColorPicker } from "@/components/ui/color-picker/color-picker";

export function AccentField() {
  const [accent, setAccent] = useState("#245cdb");
  return <ColorPicker color={accent} onChange={setAccent} labels={{ open: "Pick accent" }} />;
}
```

Leave `color` out and give `defaultColor` to let the picker own its value. With `name`, a hidden input carries the value in a native form, so a server action or a plain `<form>` receives it:

```tsx title="theme-form.tsx"
import { ColorPickerPanel } from "@/components/ui/color-picker/color-picker";

export function ThemeForm({ action }: { action: (data: FormData) => void }) {
  return (
    <form action={action}>
      <ColorPickerPanel defaultColor="oklch(70% 0.19 48)" defaultFormat="oklch" name="accent" alpha />
      <button type="submit">Save</button>
    </form>
  );
}
```

### Styling

The corners follow your `--radius` the way shadcn's own components do: the panel a step above it, the value field and the format switch at it, the area and the buttons a step below; the swatch, the sliders and their handles stay round. The checked format is a `--primary` pill with `--primary-foreground` text, like a primary button, and the copy check turns `--primary`. A theme without these tokens gets the picker's own look: 1rem corners, a background pill and the foreground.

Everything is plain CSS in `color-picker.css`; change it there, or override a class in your own stylesheet:

```css title="globals.css"
.color-picker-panel {
  width: 18rem;
  border-radius: 1rem;
}
```

### Placement and limits

The panel opens down and to the right of the swatch and flips up and/or left when it would cross the edge of the viewport. A swatch in the middle of a narrow screen has room on neither side: there the panel also moves along the edge until it is inside, 8 px from it, and its own swatch moves back by as much, so the panel still grows out of the swatch you pressed. It is not portalled, so it inherits the theme of its subtree and works inside an iframe — and an ancestor with `overflow: hidden` clips it. Give that container `overflow: visible`, or place the picker outside it.

> **Not inside a paragraph.** The picker's root is a `<div>`, so it cannot sit inside a `<p>`: the browser would move it out and React reports a hydration error. Put it in a `<div>` instead.

### Reduced motion

Under `prefers-reduced-motion: reduce` the swatch opens and closes in a single frame, without the morph, and the format switch and the copy check stop animating. An app with its own setting can ask for the same with `data-reduced-motion` on any ancestor:

```tsx title="settings.tsx"
import type { ReactNode } from "react";

export function MotionSetting({ reduce, children }: { reduce: boolean; children: ReactNode }) {
  return <div data-reduced-motion={reduce ? "" : undefined}>{children}</div>;
}
```

## Examples

### Inline panel

`ColorPickerPanel` is the picker without the swatch, always open.

```tsx title="panel.tsx"
"use client";

import { useState } from "react";
import { ColorPickerPanel } from "@/components/ui/color-picker/color-picker";

export function InlinePanel() {
  const [color, setColor] = useState("#245cdb");
  return <ColorPickerPanel color={color} onChange={setColor} labels={{ value: "Accent color" }} />;
}
```

### One format

A single entry in `formats` fixes the output and hides the format switch.

```tsx title="one-format.tsx"
import { ColorPicker } from "@/components/ui/color-picker/color-picker";

export function HexOnly() {
  return <ColorPicker defaultColor="#245cdb" formats={["hex"]} labels={{ open: "Pick a hex color" }} />;
}
```

### Opacity

`alpha` adds the opacity slider; the value carries opacity only below 1.

```tsx title="opacity.tsx"
import { ColorPickerPanel } from "@/components/ui/color-picker/color-picker";

export function WithOpacity() {
  return <ColorPickerPanel defaultColor="rgb(36 92 219 / 0.6)" defaultFormat="rgb" alpha />;
}
```

### Format in your state

`format` with `onFormatChange` keeps the reader's choice of a format where your app can save it.

```tsx title="format.tsx"
"use client";

import { useState } from "react";
import { ColorPickerPanel, type ColorFormat } from "@/components/ui/color-picker/color-picker";

export function SavedFormat() {
  const [color, setColor] = useState("oklch(51.9% 0.203 263.2)");
  const [format, setFormat] = useState<ColorFormat>("oklch");
  return (
    <div className="grid justify-items-center gap-3">
      <ColorPickerPanel color={color} onChange={setColor} format={format} onFormatChange={setFormat} />
      <p className="text-sm">Saved format: {format}</p>
    </div>
  );
}
```

### Swatch from a CSS variable

`swatch` paints the button with any CSS color, here the variable the picker's value is written to.

```tsx title="swatch.tsx"
"use client";

import { useState, type CSSProperties } from "react";
import { ColorPicker } from "@/components/ui/color-picker/color-picker";

export function AccentVariable() {
  const [accent, setAccent] = useState("#245cdb");
  return (
    <div style={{ "--accent": accent } as CSSProperties}>
      <ColorPicker color={accent} onChange={setAccent} swatch="var(--accent)" labels={{ open: "Pick accent" }} />
    </div>
  );
}
```

### Translated labels

A full `labels` object: every name and announcement in Spanish.

```tsx title="labels.tsx"
import { ColorPicker, type ColorPickerLabels } from "@/components/ui/color-picker/color-picker";

const labels: ColorPickerLabels = {
  open: "Elegir color",
  panel: "Selector de color",
  close: "Cerrar",
  area: "Saturación y brillo",
  saturation: "Saturación",
  brightness: "Brillo",
  hue: "Tono",
  alpha: "Opacidad",
  value: "Valor del color",
  format: "Formato",
  eyedropper: "Tomar de la pantalla",
  copy: "Copiar",
  copied: "Copiado",
};

export function SpanishPicker() {
  return <ColorPicker defaultColor="#245cdb" labels={labels} />;
}
```

## Agent

`color-picker-agent.ts` connects the picker to the [agent bridge](https://cloudlet.dev/docs/agents), experimental for now. `useColorPickerAgent(agent, props, options)` takes the picker's props and returns them wrapped; spread them on `ColorPicker` or `ColorPickerPanel`:

```tsx title="accent-agent.tsx"
"use client";

import { createAgent } from "@cloudlet/agent";
import { useState } from "react";
import { ColorPicker } from "@/components/ui/color-picker/color-picker";
import { useColorPickerAgent } from "@/components/ui/color-picker/color-picker-agent";

const agent = createAgent();

export function AccentAgent() {
  const [accent, setAccent] = useState("#245cdb");
  const picker = useColorPickerAgent(agent, { color: accent, onChange: setAccent }, { name: "accent", label: "Accent color" });
  return <ColorPicker {...picker} />;
}
```

The picker is one `ColorPickerEntry` in the snapshot: `ref` (its name), `component: 'color-picker'`, `label`, `value` as the field shows it, `format`, `formats` and `alpha`. Its one verb is `set [color]`: hex, `rgb()`, `hsl()` or `oklch()`, written in the picker's format, without opacity unless `alpha` is on. It runs unless the bridge's `allow` says no, and answers with the color the picker shows. `ColorPickerAgentOptions` are `name` (default `color-picker`; a second picker on one agent needs its own) and `label` (default `labels.open`). `COLOR_PICKER_VERBS` is the verb's text for a model. With `agent` null or undefined the props come back as they are. Each change reaches `agent.subscribe` under the key `color`, with its source.

- **The answer is what the picker shows.** A step reads the picker after React commits the app's update. An app that sets the value later, after a server answers or in a transition, reads as not taking it: `The app did not take "#ff0000": "Accent color" is still "#245cdb".` Other refusals: a color it cannot read (`… is not a color "Accent color" can read. Use hex, rgb(), hsl() or oklch().`) and wrong arguments (`"set" on "accent" takes a color.`).
- **One author per commit.** The color adapters on one agent share who started a change: `user` through the picker, `agent` through a step, `app` otherwise. A value your app passes on in a later commit, from an effect, is `app`. React 19 may merge a deferred update of your app into the next synchronous commit; then that value is named after the person's or the step's change in the same commit.
- **Format changed by the app.** After your app itself changes a controlled `format`, the snapshot's `value` can differ from the field in the last digit until the next change.
- **Connecting later.** An uncontrolled picker whose `agent` goes from null to a bridge starts again from `defaultColor`.

The file also exports what the palette's adapter shares with it, which you do not need to call: copies of the bridge's protocol types (`AgentEntryShape`, `AgentSignalShape`, `AgentValueShape`, `AgentPermissionShape`, `AgentStepCallShape`, `AgentStepNamesShape`, `AgentStepCheckShape`, `AgentRunContextShape`, `AgentRunOutcomeShape`, `AgentSummonOutcomeShape`, `AgentChangeShape`, `AgentVerbsShape`, `AgentComponentShape`, `AgentConnectorShape`, `ChangeSourceShape`), so the file needs no `@cloudlet/agent` import, and the helpers `colorTexts`, `writeColor`, `commitNow`, `useChangeAuthor` with `ChangeAuthor`, and `useAgentPort` with `AgentPortMethods`.

## API Reference

### Props

`ColorPickerProps` for `ColorPicker`, `ColorPickerPanelProps` for `ColorPickerPanel`; every prop is optional.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `color` | `string` | — | The color to show (controlled): hex, `rgb()`, `hsl()` or `oklch()`. A value that does not parse, such as `var(--accent)`, keeps the last valid color. |
| `defaultColor` | `string` | `#ffffff` | The initial color when the picker owns its value. |
| `onChange` | `(value: string) => void` | — | Called with the value in `format` on every change and when the format switches. |
| `format` | `ColorFormat` | — | The output format (controlled); pair it with `onFormatChange`. Changing it from your code does not call `onChange`, see [Formats](#formats). |
| `defaultFormat` | `ColorFormat` | the first of `formats` | The initial format when the picker owns it. |
| `onFormatChange` | `(format: ColorFormat) => void` | — | Called when the reader picks another format. |
| `formats` | `readonly ColorFormat[]` | all four | The formats the switch offers, in order. A single entry hides the switch. |
| `alpha` | `boolean` | `false` | Shows the opacity slider and the checkerboard. Without it, alpha is dropped from input and output. |
| `name` | `string` | — | Renders `<input type="hidden">` with this name and the current value. |
| `labels` | `Partial<ColorPickerLabels>` | `DEFAULT_LABELS` | Names and announcements; anything left out keeps its English default. |
| `swatch` | `string` | the current value | `ColorPicker` only: any CSS color for the swatch button, such as a `var()` the picker cannot parse. |
| `className` | `string` | — | `ColorPickerPanel` only: added to the panel. |

### Formats

`ColorFormat` is `'hex' | 'rgb' | 'hsl' | 'oklch'`. The picker reads `#rgb`, `#rgba`, `#rrggbb` and `#rrggbbaa`, `rgb()` and `rgba()` in the old and the new syntax, `hsl()` and `hsla()`, and `oklch()` with lightness as a percentage or a fraction (`none` counts as 0). It writes one canonical form per format: lowercase hex, space-separated channels, oklch lightness, chroma and hue rounded to at most one, three and one decimals, and opacity only below 1. For `#ff8000`:

| Format | Output | With opacity below 1 |
| --- | --- | --- |
| `hex` | `#ff8000` | `#ff800080` |
| `rgb` | `rgb(255 128 0)` | `rgb(255 128 0 / 0.5)` |
| `hsl` | `hsl(30 100% 50%)` | `hsl(30 100% 50% / 0.5)` |
| `oklch` | `oklch(73.2% 0.186 53)` | `oklch(73.2% 0.186 53 / 0.5)` |

When the reader switches the format, `onChange` gets the same color in the new one. When your code changes `format`, it does not: the field and the hidden input show the new format at once, and `onChange` reports it from the next change of the color. If you store the value and switch the format from outside, rewrite the stored value yourself.

The area edits saturation and brightness in sRGB. An `oklch()` color outside sRGB shows as given on the swatch; the first move of a handle keeps its lightness and hue and lowers the chroma until it fits.

### Eyedropper and copy

The eyedropper button appears only where the browser has the EyeDropper API, Chromium browsers today; elsewhere it is not rendered rather than shown disabled. Cancelling the eyedropper changes nothing. The copy button writes the value to the clipboard, turns into a check for a moment and announces `labels.copied`; a blocked clipboard leaves everything as it was.

### Keyboard and screen readers

| Part | Role | Keys |
| --- | --- | --- |
| Swatch | button, `aria-expanded` | Enter or Space opens the picker |
| Picker | dialog named by `labels.panel` | focus starts in the area; Escape closes and returns focus to the swatch; a press outside, or Tab out of the panel, closes without moving focus |
| Area | slider, "Saturation 40%, Brightness 80%" | ← → saturation, ↑ ↓ brightness, by 1%; with Shift by 10% |
| Hue, opacity | sliders, "Hue 48°", "Opacity 50%" | ← → by 1° or 1%, Shift by 10, Home and End to the ends |
| Value field | text field named by `labels.value` | typing applies every color that parses; Escape reverts, a second Escape closes |
| Format switch | radio group | arrows move between formats |

Tab moves through the swatch that closes the panel, the value field, the area, the sliders, the format switch, the eyedropper and copy, in that order; past copy, focus leaves the picker and the panel closes, so it never covers what Tab reaches next. `ColorPickerPanel` has no closing swatch and starts at the value field. In forced-colors mode the handles, the area and the switch draw borders in system colors, so every part stays findable without gradients.

The sliders are thin to look at but take a press anywhere in the gaps around them: up to the area's edge, down to the row of the format switch and the tools, and halfway to the other slider when there are two, so each is at least 24 px tall to press and a press meant for one never moves its neighbour or the area.

### Labels

Every name and spoken word comes from `labels`; pass the ones you translate and the rest keep these defaults (`DEFAULT_LABELS`):

| Key | Default | Where |
| --- | --- | --- |
| `open` | `Pick color` | the swatch button |
| `panel` | `Color picker` | the dialog |
| `close` | `Close` | the swatch inside the panel |
| `area` | `Saturation and brightness` | the area's name |
| `saturation`, `brightness` | `Saturation`, `Brightness` | the area's value, "Saturation 40%, Brightness 80%" |
| `hue` | `Hue` | the hue slider's name and value, "Hue 48°" |
| `alpha` | `Opacity` | the opacity slider's name and value, "Opacity 50%" |
| `value` | `Color value` | the value field |
| `format` | `Format` | the format switch |
| `eyedropper` | `Pick from screen` | the eyedropper button |
| `copy`, `copied` | `Copy`, `Copied` | the copy button and its announcement |
