A brand color
import { Banner } from "@senvenli/react/feedback";
import { checkBrandColor, ColorPicker, formatRatio } from "@senvenli/react/color";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";
// The picker picks; checkBrandColor runs the theme engine's contrast check on what was picked, while dragging.
export default function Example() {
const [color, setColor] = useState("#1E63D0");
const checks = checkBrandColor(color);
const failing = checks ? (["light", "dark"] as const).filter((theme) => !checks[theme].passes) : [];
return (
<Stack space="space-200">
<ColorPicker label="Brand color" value={color} onChange={setColor} />
{checks && failing.length > 0 ? (
<Banner tone="warning" title="Low contrast on the primary button">
{failing.map((theme) => `${theme === "light" ? "Light" : "Dark"} theme: ${formatRatio(checks[theme].ratio, "en")}; the theme uses ${checks[theme].fix?.hex ?? "a darker tone"} for fills.`).join(" ")}
</Banner>
) : (
<Banner tone="success" title="Text on this color passes WCAG AA in both themes" />
)}
</Stack>
);
}import { ColorPicker } from "@senvenli/react/color";Examples
With an alpha slider
For a color that may be translucent, such as an overlay.import { ColorPicker } from "@senvenli/react/color";
export default function Example() {
// With an alpha channel the hex has eight digits: "#0F766ECC".
return <ColorPicker label="Highlight color" defaultValue="#0F766ECC" channels={["hue", "alpha"]} />;
}Save on release
`onChangeEnd` fires once per drag or commit: the place to save.import { useState } from "react";
import { ColorPicker } from "@senvenli/react/color";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
export default function Example() {
const [color, setColor] = useState("#7C3AED");
const [saved, setSaved] = useState(color);
return (
<Stack space="space-150">
<ColorPicker label="Accent color" value={color} onChange={setColor} onChangeEnd={setSaved} />
<Text size="body-small" tone="subtle">Last saved: {saved}</Text>
</Stack>
);
}Guidelines
When to use it
- Choosing a brand or theme color, where contrast in both themes matters.
Use something else when
- Presets cover the choice. Color swatch picker
- Space is tight and people know the hex. Color field
Accessibility
Keyboard
| Keys | What happens |
|---|---|
| Arrow keys | Move the area thumb or a slider by one step. |
| Page up / down | Move by a larger step. |
Built in
- The label names the area, each slider and the hex field.
- The preview announces the contrast ratio and what the theme uses when a tone fails.
You provide
- A label.
API reference
ColorPicker
| Prop | Type | Default | Description |
|---|---|---|---|
labelRequired | React.ReactNode | – | "Brand color"; names the area, the sliders and the hex field for screen readers. |
value | string | – | The hex ("#1E63D0"); "#1E63D0CC" with an alpha channel. |
defaultValue | string | DEFAULT_PRIMARY | |
onChange | (value: string) => void | – | While dragging and typing: the preview and its contrast check follow every move. |
onChangeEnd | (value: string) => void | – | On release (and when the hex field commits): save here, not on every move. |
channels | readonly ("hue" | "alpha")[] | ["hue"] | The sliders under the area: hue always, alpha when the color may be translucent. |
labelRequiredReact.ReactNode"Brand color"; names the area, the sliders and the hex field for screen readers.
valuestringThe hex ("#1E63D0"); "#1E63D0CC" with an alpha channel.
defaultValuestringDefaultDEFAULT_PRIMARYonChange(value: string) => voidWhile dragging and typing: the preview and its contrast check follow every move.
onChangeEnd(value: string) => voidOn release (and when the hex field commits): save here, not on every move.
channelsreadonly ("hue" | "alpha")[]Default["hue"]The sliders under the area: hue always, alpha when the color may be translucent.