# Color picker

A saturation-brightness area, hue (and alpha) sliders, a hex field and a two-theme preview of the primary button with its contrast.

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

## Example: A brand color

The picker with `checkBrandColor`: the contrast of text on it, in both themes, as it moves.

```tsx
import { Banner } from "@senvenli/react/feedback";
import { checkBrandColor, ColorPicker, formatRatio } from "@senvenli/react/color";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";

// The picker picks; checkBrandColor runs the theme engine's contrast check on what was picked, while dragging.
export default function Example() {
  const [color, setColor] = useState("#1E63D0");
  const checks = checkBrandColor(color);
  const failing = checks ? (["light", "dark"] as const).filter((theme) => !checks[theme].passes) : [];
  return (
    <Stack space="space-200">
      <ColorPicker label="Brand color" value={color} onChange={setColor} />
      {checks && failing.length > 0 ? (
        <Banner tone="warning" title="Low contrast on the primary button">
          {failing.map((theme) => `${theme === "light" ? "Light" : "Dark"} theme: ${formatRatio(checks[theme].ratio, "en")}; the theme uses ${checks[theme].fix?.hex ?? "a darker tone"} for fills.`).join(" ")}
        </Banner>
      ) : (
        <Banner tone="success" title="Text on this color passes WCAG AA in both themes" />
      )}
    </Stack>
  );
}
```

## Examples

### With an alpha slider

For a color that may be translucent, such as an overlay.

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

export default function Example() {
  // With an alpha channel the hex has eight digits: "#0F766ECC".
  return <ColorPicker label="Highlight color" defaultValue="#0F766ECC" channels={["hue", "alpha"]} />;
}
```

### Save on release

`onChangeEnd` fires once per drag or commit: the place to save.

```tsx
import { useState } from "react";
import { ColorPicker } from "@senvenli/react/color";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";

export default function Example() {
  const [color, setColor] = useState("#7C3AED");
  const [saved, setSaved] = useState(color);
  return (
    <Stack space="space-150">
      <ColorPicker label="Accent color" value={color} onChange={setColor} onChangeEnd={setSaved} />
      <Text size="body-small" tone="subtle">Last saved: {saved}</Text>
    </Stack>
  );
}
```

## Guidelines

### When to use it

- Choosing a brand or theme color, where contrast in both themes matters.

### Use something else when

- [color-swatch-picker](https://design.senvenli.com/docs/components/color-swatch-picker.md): Presets cover the choice.
- [color-field](https://design.senvenli.com/docs/components/color-field.md): Space is tight and people know the hex.

## Accessibility

Keyboard:

- Arrow keys: Move the area thumb or a slider by one step.
- Page up / down: Move by a larger step.

Built in:

- The label names the area, each slider and the hex field.
- The preview announces the contrast ratio and what the theme uses when a tone fails.

You provide:

- A label.

## API reference

### ColorPicker

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| label | `React.ReactNode` |  | yes | "Brand color"; names the area, the sliders and the hex field for screen readers. |
| value | `string` |  | no | The hex ("#1E63D0"); "#1E63D0CC" with an alpha channel. |
| defaultValue | `string` | `DEFAULT_PRIMARY` | no |  |
| onChange | `(value: string) => void` |  | no | While dragging and typing: the preview and its contrast check follow every move. |
| onChangeEnd | `(value: string) => void` |  | no | On release (and when the hex field commits): save here, not on every move. |
| channels | `readonly ("hue" \| "alpha")[]` | `["hue"]` | no | The sliders under the area: hue always, alpha when the color may be translucent. |

### 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-field](https://design.senvenli.com/docs/components/color-field.md)
- [color-swatch-picker](https://design.senvenli.com/docs/components/color-swatch-picker.md)
