import { ColorField } from "@senvenli/react/color";
export default function Example() {
return <ColorField label="Brand color" description="Used for buttons and links." defaultValue="#1E63D0" />;
}import { ColorField } from "@senvenli/react/color";Examples
Controlled
`onChange` gives the normalized hex, or null when the field is emptied.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.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
- People need to pick a color by eye. Color picker
- A fixed set of colors is enough. Color swatch picker
Accessibility
Keyboard
| Keys | What happens |
|---|---|
| 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 | Description |
|---|---|---|---|
labelRequired | React.ReactNode | – | |
description | React.ReactNode | – | |
errorMessage | React.ReactNode | – | Shown while the field is invalid; a malformed hex says what a valid one looks like by itself. |
value | string | – | "#1E63D0" (also "1e63d0" and "#16D"); |
defaultValue | string | – | |
onChange | (value: string | null) => void | – | The normalised hex ("#1E63D0"), or |
channel | ColorChannel | – |
|
isRequired | boolean | – | |
requiredValidation | RequiredValidation | – | Who says an empty required ColorField is missing: inside a DS Form, |
isDisabled | boolean | – | |
isReadOnly | boolean | – | |
isInvalid | boolean | – | |
name | string | – | |
id | string | – | |
aria-label | string | – | |
aria-labelledby | string | – |
labelRequiredReact.ReactNodedescriptionReact.ReactNodeerrorMessageReact.ReactNodeShown while the field is invalid; a malformed hex says what a valid one looks like by itself.
valuestring"#1E63D0" (also "1e63d0" and "#16D");
nullfor none.defaultValuestringonChange(value: string | null) => voidThe normalised hex ("#1E63D0"), or
nullwhen emptied.channelColorChannelhexby default; a single channel ("hue", "saturation", "red"…) types that channel's number instead.isRequiredbooleanrequiredValidationRequiredValidationWho 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;apponly marks the field. See {@link RequiredValidation}. The app'sisInvalid(truewith itserrorMessage, orfalse) overrides the check, as on TextField. Disabled, empty and required, it still blocks a DS Form's submit and cannot take focus (as TextField).isDisabledbooleanisReadOnlybooleanisInvalidbooleannamestringidstringaria-labelstringaria-labelledbystring