# Color picker

Any colour, set by eye on a field of saturation and brightness and a hue bar, or typed as a hex, with swatches to start from; one colour to choose as a round swatch, or a team's hue as a row of dots.

Live variants: https://design.arcadia.is/components/color-picker

```tsx
import { ColorPicker, Swatch, HuePicker } from "@arcadia/ui";
```

```tsx
import { useState, useRef } from "react";
import { MARKING_INKS, Swatch } from "@arcadia/ui";

function Inks() {
  const travel = useRef<object>({}).current;
  const [color, setColor] = useState(MARKING_INKS[0]!.color);
  const [other, setOther] = useState<string | null>(null);
  const known = MARKING_INKS.some((i) => i.color === color);
  return (
    <div {...sx("cmp-color-picker-row", styles.row)}>
      <Swatch label="Other color" color={other ?? color} selected={!known} travelRow={travel} onPick={(c) => { setOther(c); setColor(c); }} />
      {MARKING_INKS.map((ink) => (
        <Swatch key={ink.color} label={ink.name} color={ink.color} selected={ink.color === color} travelRow={travel} onClick={() => setColor(ink.color)} />
      ))}
    </div>
  );
}
```

Choose a hue for a label.

```tsx
import { useState } from "react";
import { HuePicker, Chip } from "@arcadia/ui";

function LabelColor() {
  const [hue, setHue] = useState<LabelHue>("chart-1");
  return (
    <div {...sx("cmp-color-picker-row", styles.row)}>
      <HuePicker value={hue} onChange={setHue} aria-label="Label color" />
      <Chip hue={hue}>Design review</Chip>
    </div>
  );
}
```

## Accessibility

A swatch is a button named by its `label`, its state `aria-pressed`, or `aria-checked` as a `radio` in a group. The wheel is a button named by its `label` too, which opens its picker as a dialog; Esc closes it, and focus comes back to the wheel. A chosen swatch is told apart by its ring, a full step of ink, and by its name. Under forced colours a swatch keeps its colour, and the chosen one's ring is an outline in the system's highlight. The hue picker is a radio group, one Tab stop, the arrows choosing, each dot named by its colour's word (Blue, Red, Violet, Amber, Pink, Green and Grey).

The picker is a group named by its `label`. The field is a `slider` described as a 2D slider, its value read as saturation and brightness in percent; the hue bar is a `slider` from 0 to 360 degrees. The hex is a field named Hex: a whole hex sets the colour as it is typed, Enter or leaving the field also reads the short `#rgb` form, and anything else goes back to the colour. The swatches are one radio group, one Tab stop, and a pointer's pick carries its ink ring to the new swatch; it lands whole from the keyboard. Under forced colours the field, the hue bar, their thumbs and the preview keep their colours, because the colour is what they say. The pipette is a button named `Pick color`, beside the hex.

- Tab: The field, the hue bar, the hex, the pipette where there is one, then the swatches
- ← + →: In the field, saturation by 1%; on the hue bar, a degree
- ↑ + ↓: In the field, brightness by 1%; on the hue bar, a degree
- Shift: With an arrow, ten steps
- Page Up + Page Down: Brightness by 10%, or the hue by 10 degrees
- Home + End: No saturation or all of it; the hue's start or end
- Esc: In a popover, close it, focus back on its swatch

## API reference

### ColorPicker

- `value` (``#${string}``, required): The colour, `#rrggbb`.
- `onChange` (`(color: `#${string}`) => void`, required): Each colour as it is set, `#rrggbb`: every step of a drag or a key, a whole hex typed, a swatch chosen.
- `swatches` (`readonly Ink[]`, default `INKS`): Colours to start from, a row of swatches under the rest. Default the marking inks; `[]` for none.
- `label` (`string`, default `"Color"`): Its name for assistive technology.
- `onEyedrop` (`() => void`): Picking a colour from the screen: a pipette beside the hex, which hands back to this instead of opening the browser's own picker.
- `rootStyle` (`RootStyle<ColorPickerKnobs>`): Typed StyleX override: layout and `--color-picker-area-h`.

Rest props go to `<div>` props.

### Swatch

- `color` (`string`): The colour it shows, `#rrggbb` or a token's `var()`. Omit for the monochrome default (a hollow ring). With `onPick`, the colour last picked (`#rrggbb`), shown in its middle while it is selected.
- `label` (`string`, required): Required: its name, which is its accessible name and its tooltip.
- `selected` (`boolean`): It is the colour chosen: an ink ring round it. Where `travelRow` joins the swatches, the ring travels from the swatch chosen before on a pointer's pick, and lands whole from the keyboard, a wrap or reduced motion; without it, no ring travels. As a `radio`, `aria-checked`; else `aria-pressed`.
- `onPick` (`(color: `#${string}`) => void`): Any other colour: a wheel of hues that opens a `ColorPicker` in a popover, handing back each colour picked.
- `onEyedrop` (`() => void`): Picking a colour from the screen: the picker's pipette closes the popover and hands back to this instead of opening the browser's own picker.
- `size` (`"sm" | "md" | "lg"`, default `"lg"`): `sm` 24, `md` 28, `lg` 32.
- `tooltip` (`false`): `false`: no tooltip, where its name already stands beside it.
- `travelRow` (`object`): The row of one choice it belongs to, so the chosen ring travels within it on a pointer pick. Give every swatch of one row the same stable object; without one, no ring travels between swatches.
- `rootStyle` (`RootStyle`): Typed StyleX override: layout.

Rest props go to `<button>` props.

### HuePicker

- `value` (`LabelHue`, required): The hue chosen.
- `onChange` (`(hue: LabelHue) => void`, required)
- `size` (`"sm" | "md" | "lg"`, default `"sm"`)
- `rootStyle` (`RootStyle`): Typed StyleX override: where the row sits.

Rest props go to `<fieldset>` props.
