# Color swatch picker

Preset colors as named swatches, optionally ending with a button that opens a color picker for a custom color.

- Family: color
- Import: `import { ColorSwatchPicker } from "@senvenli/react/color";`
- Page: https://design.senvenli.com/docs/components/color-swatch-picker
- JSON: https://design.senvenli.com/docs/components/color-swatch-picker.json

## Example: Preset colors

One choice from a few named colors; the caption names the selected one.

```tsx
import { ColorSwatchPicker } from "@senvenli/react/color";

const PRESETS = [
  { value: "#1E63D0", name: "Ocean" },
  { value: "#0F766E", name: "Pine" },
  { value: "#7C3AED", name: "Plum" },
  { value: "#BE123C", name: "Berry" },
  { value: "#B45309", name: "Amber" },
];

export default function Example() {
  return <ColorSwatchPicker label="Label color" items={PRESETS} defaultValue="#0F766E" />;
}
```

## Examples

### With a custom color

The last button opens a color picker for a color outside the presets.

```tsx
import { useState } from "react";
import { ColorSwatchPicker } from "@senvenli/react/color";

const PRESETS = [
  { value: "#1E63D0", name: "Ocean" },
  { value: "#0F766E", name: "Pine" },
  { value: "#7C3AED", name: "Plum" },
];

export default function Example() {
  const [color, setColor] = useState("#1E63D0");
  return (
    <ColorSwatchPicker
      label="Theme color"
      items={PRESETS}
      value={color}
      onChange={setColor}
      allowsCustom
      description="Used for buttons and links."
    />
  );
}
```

## Guidelines

### When to use it

- Choosing from a small set of preset colors.

### Use something else when

- [color-picker](https://design.senvenli.com/docs/components/color-picker.md): Any color is allowed and presets are a shortcut at most.

## Accessibility

Keyboard:

- Arrow keys: Move between swatches.

Built in:

- Each swatch is named; the caption names the selection.

You provide:

- A label and a name for each preset.

## API reference

### ColorSwatchPicker

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| label | `React.ReactNode` |  | yes | "Theme presets". |
| items | `readonly ColorSwatchItem[]` |  | yes | The preset colors; check them with checkBrandColor so that only a custom color can fail. |
| value | `string` |  | no | The chosen color, controlled. `null` or `""`: none chosen, so no swatch shows a check (a theme with no brand color). |
| defaultValue | `string` |  | no |  |
| onChange | `(value: string) => void` |  | no |  |
| allowsCustom | `boolean` |  | no | Ends with a button that opens ColorPicker for a custom color. |
| description | `React.ReactNode` |  | no | Under the swatches, after "Ocean · selected". |

### Tokens it reads

`--ds-background-neutral-pressed`, `--ds-border`, `--ds-border-focused`, `--ds-radius-full`, `--ds-radius-large`, `--ds-radius-medium`, `--ds-radius-small`, `--ds-shadow-raised`, `--ds-space-050`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-400`, `--ds-surface-raised`, `--font-ds-mono`

## Related

- [color-picker](https://design.senvenli.com/docs/components/color-picker.md)
- [color-swatch](https://design.senvenli.com/docs/components/color-swatch.md)
