# Color field

A hex color typed with a live swatch; a malformed value stays as typed with an example of a valid one.

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

## Example: A hex color

Typed with or without the #; the swatch follows as you type.

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

export default function Example() {
  return <ColorField label="Brand color" description="Used for buttons and links." defaultValue="#1E63D0" />;
}
```

## Examples

### Controlled

`onChange` gives the normalized hex, or null when the field is emptied.

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

export default function Example() {
  const [color, setColor] = useState<string | null>("#0F766E");
  return (
    <Stack space="space-100">
      <ColorField label="Label color" value={color} onChange={setColor} isRequired />
      <Text size="body-small" tone="subtle">{color ? `Saved as ${color}` : "No color chosen"}</Text>
    </Stack>
  );
}
```

### A single channel

The field types one channel's number instead of the hex.

```tsx
import { useState } from "react";
import { ColorField, ColorSwatch } from "@senvenli/react/color";
import { Stack } from "@senvenli/react/layout";

export default function Example() {
  const [color, setColor] = useState<string | null>("#1E63D0");
  return (
    <Stack direction="horizontal" space="space-150" align="end">
      {/* Both fields edit the same hex: each one reads and writes its own channel of it. */}
      <ColorField label="Hue" channel="hue" value={color} onChange={setColor} />
      <ColorField label="Saturation" channel="saturation" value={color} onChange={setColor} />
      {color && <ColorSwatch color={color} name="Result" />}
    </Stack>
  );
}
```

## Guidelines

### When to use it

- Typing a known color in compact settings.

### Use something else when

- [color-picker](https://design.senvenli.com/docs/components/color-picker.md): People need to pick a color by eye.
- [color-swatch-picker](https://design.senvenli.com/docs/components/color-swatch-picker.md): A fixed set of colors is enough.

## Accessibility

Keyboard:

- Arrow up / down: Steps the value (the channel, on a single-channel field).

Built in:

- A malformed hex says what a valid one looks like.
- Required fields show "Fill in this field."

You provide:

- A visible label.

## API reference

### ColorField

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| label | `React.ReactNode` |  | yes |  |
| description | `React.ReactNode` |  | no |  |
| errorMessage | `React.ReactNode` |  | no | Shown while the field is invalid; a malformed hex says what a valid one looks like by itself. |
| value | `string` |  | no | "#1E63D0" (also "1e63d0" and "#16D"); `null` for none. |
| defaultValue | `string` |  | no |  |
| onChange | `(value: string \| null) => void` |  | no | The normalised hex ("#1E63D0"), or `null` when emptied. |
| channel | `ColorChannel` |  | no | `hex` by default; a single channel ("hue", "saturation", "red"…) types that channel's number instead. |
| isRequired | `boolean` |  | no |  |
| requiredValidation | `RequiredValidation` |  | no | Who says an empty required ColorField is missing: inside a DS Form, `field` (default) shows "Fill in this field." on blur and on submit, which it then blocks; `app` only marks the field. See {@link RequiredValidation}. The app's `isInvalid` (`true` with its `errorMessage`, or `false`) overrides the check, as on TextField. Disabled, empty and required, it still blocks a DS Form's submit and cannot take focus (as TextField). |
| isDisabled | `boolean` |  | no |  |
| isReadOnly | `boolean` |  | no |  |
| isInvalid | `boolean` |  | no |  |
| name | `string` |  | no |  |
| id | `string` |  | no |  |
| aria-label | `string` |  | no |  |
| aria-labelledby | `string` |  | no |  |

### 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)
