# Color picker

Lets people choose any colour, or pick one from a set of swatches.

```tsx
import { ColorField } from '@halo-ds/react';
```

## When to use it
A colour picker has three parts. Color field shows the current colour and opens the picker. Color picker is the panel: a shade area, hue and opacity sliders, typed values and preset swatches. Color swatch is the small chip used in the picker and on its own.

## Do
- Show the value next to the colour, so colour is never the only clue.
- Offer brand colours as swatches to keep designs on brand.
- Keep recent colours in the swatch row.
- Remember the format people used last.

## Don't
- Show the full picker when only a few colours are allowed.
- Offer opacity where see-through colours won't work, like for text.
- Show the eyedropper where the browser can't use it.
- Let a colour be the only way to tell things apart; add a label.

## Full picker or swatches?
Give people the full picker only when any colour is allowed.
- **Full picker**: Any colour is allowed, like a design or drawing tool. Keep swatches at the bottom for recent or brand colours. Show opacity only when see-through colours make sense.
- **Swatches only**: People pick from a fixed set, like label or theme colours. Use a row of Color swatches, without the panel. Name each colour for screen readers, like "Purple".

## Props
### ColorField props
| Prop | Type | What it does |
| --- | --- | --- |
| `size` | `"sm" \| "md" \| "lg" \| undefined` |  |
| `disabled` | `boolean \| undefined` |  |
| `invalid` | `boolean \| undefined` |  |
| `id` | `string \| undefined` |  |
| `aria-label` | `string \| undefined` | Defines a string value that labels the current element. |
| `aria-describedby` | `string \| undefined` | Identifies the element (or elements) that describes the object. |
| `aria-invalid` | `true \| undefined` | Indicates the entered value does not conform to the format expected by the application. |
| `name` | `string \| undefined` | Form field name. The text box carries the hex. |
| `required` | `boolean \| undefined` |  |
| `placeholder` | `string \| undefined` | Shown while the hex box is empty, like while retyping it. |
| `defaultValue` | `string \| undefined` |  |
| `value` | `string \| undefined` | A hex colour like "#CC1DD0", or "#CC1DD080" with opacity. |
| `onValueChange` | `((hex: string) => void) \| undefined` |  |
| `swatches` | `string[] \| undefined` | Preset colours under the picker. Figma: Show swatches. |
| `showOpacity` | `boolean \| undefined` | Figma: Show opacity. |
| `showEyedropper` | `boolean \| undefined` | Shows the eyedropper where the browser supports it. Figma: Show eyedropper. |
| `defaultFormat` | `ColorFormat \| undefined` | Which values the fields show first. Figma: Format. |
| `classNames` | `Partial<Record<ColorInputPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<ColorInputPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { ColorField } from '@halo-ds/react';

export default function Example() {
  return (
    <div style={{ width: 280 }}>
      <ColorField label="Brand colour" placeholder="#2F6BFF" defaultValue="#2F6BFF" />
    </div>
  );
}
```

### Swatches
Square or round, two sizes, and a selected state.
```tsx
import { ColorSwatch } from '@halo-ds/react';

export default function Demo() {
  return (
    <div className="demo-row">
      <ColorSwatch color="#CC1DD0" aria-label="Purple" selected />
      <ColorSwatch color="#3D7BF7" aria-label="Blue" />
      <ColorSwatch color="#1FA971" aria-label="Green" shape="circle" />
      <ColorSwatch color="#E5484D80" aria-label="Red at half opacity" size="sm" />
    </div>
  );
}
```

### Field with swatches
Saved colours under the picker, one click away.
```tsx
import { ColorField } from '@halo-ds/react';

export default function Demo() {
  return (
    <div style={{ width: 280 }}>
      <ColorField label="Brand colour" placeholder="#2F6BFF" hint="Used for buttons and links." swatches={['#CC1DD0', '#3D7BF7', '#1FA971', '#F2994A']} />
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/color-picker
