Fields
Color Field
A labelled native color swatch that opens the browser's picker.
When to use it: Use it when people choose one opaque color, such as a theme accent.
Example
import { Description, Label, TextField } from "@comp0/react";
import { ColorField } from "@comp0/react";
export function Example() {
return (
<TextField as="div" defaultValue="#0d9488" className="flex max-w-xs flex-col gap-1.5">
<Label className="text-base font-medium text-zinc-900 sm:text-sm dark:text-zinc-100">
Accent color
</Label>
<ColorField
name="accent"
className="h-11 w-20 rounded border border-zinc-950/10 bg-white p-1 outline-teal-600 focus-visible:outline-2 sm:h-9 dark:border-white/10 dark:bg-zinc-900 dark:outline-teal-400"
/>
<Description className="text-base text-zinc-600 sm:text-sm dark:text-zinc-400">
Hex sRGB only; the browser picker cannot store transparency.
</Description>
</TextField>
);
}Anatomy
Dashed frames are invisible state providers; shaded shapes own real DOM. Numbered pins match the list below.
A wireframe sketch of the assembled component. Each numbered marker matches a part in the list that follows.
TextFieldOptional
Shared field brain that connects the label, the color input, help, and errors. Does not add a DOM element.
Label
Names the color for sighted and assistive users. Owns a DOM element.
ColorField
Native color input; the browser supplies the swatch and picker. Owns a DOM element.
DescriptionOptional
Hint wired via aria-describedby. Owns a DOM element.
Step by step
- 1
Add the main part
Start a TextField with a Label naming what the color is for.
- 2
Add the supporting parts
Put ColorField inside it instead of Input, with a name so the value submits.
- 3
Make the behavior clear
The native value is hex sRGB without alpha, such as "#0d9488"; say so in a Description when transparency might be expected.
Exampletsx <TextField defaultValue="#0d9488"><Label>Accent color</Label><ColorField name="accent" /></TextField>
Keyboard
- ↵
- Opens the browser color picker on the focused swatch.
- Space
- Opens the browser color picker on the focused swatch.
Forms and accessibility
Submits its hex value under name like any native input.
Accessibility checklist
- Use Label so the swatch has a name; a bare colored square says nothing.
- Show the chosen value or its meaning as visible text; the swatch alone is a color-only signal.
- Native color inputs hold opaque hex sRGB only; explain that limit in a Description when alpha or wide gamut might be expected.
API reference
import { ColorField, Description, Label, TextField } from "@comp0/react";TextField
OptionalContext onlyShared field brain that connects the label, the color input, help, and errors.
| Prop | Type | Description |
|---|---|---|
value | string | Controlled hex value such as "#0d9488". |
defaultValue | string | Initial hex value such as "#0d9488". |
onChange | (value: string) => void | Receives the next hex value. |
disabled / invalid / required | boolean | Field state inherited by the color input and announced through the label pieces. |
Label
DOM elementNames the color for sighted and assistive users.
ColorField
DOM elementNative color input; the browser supplies the swatch and picker.
| Prop | Type | Description |
|---|---|---|
name | string | Submits the hex value with a form. |
value | string | Controlled hex value when standalone. |
defaultValue | string | Initial hex value when standalone. |
onChange | (event: ChangeEvent) => void | Native change event; read event.currentTarget.value for the hex string. |
disabled / required | boolean | Overrides the surrounding field state. |
Description
OptionalDOM elementHint wired via aria-describedby.
Style hooks
Attributes that appear while a state is true. Target them with Tailwind data variants such as data-open:bg-zinc-100, or with any CSS selector.
[data-value]on ColorField
- The current hex value.
[data-invalid]on ColorField
- The field is invalid.
:disabledon ColorField
- The input is disabled.
[data-focus-visible]on ColorField
- Focused via keyboard.