Preset colors
import { ColorSwatchPicker } from "@senvenli/react/color";
const PRESETS = [
{ value: "#1E63D0", name: "Ocean" },
{ value: "#0F766E", name: "Pine" },
{ value: "#7C3AED", name: "Plum" },
{ value: "#BE123C", name: "Berry" },
{ value: "#B45309", name: "Amber" },
];
export default function Example() {
return <ColorSwatchPicker label="Label color" items={PRESETS} defaultValue="#0F766E" />;
}import { ColorSwatchPicker } from "@senvenli/react/color";Examples
With a custom color
The last button opens a color picker for a color outside the presets.import { useState } from "react";
import { ColorSwatchPicker } from "@senvenli/react/color";
const PRESETS = [
{ value: "#1E63D0", name: "Ocean" },
{ value: "#0F766E", name: "Pine" },
{ value: "#7C3AED", name: "Plum" },
];
export default function Example() {
const [color, setColor] = useState("#1E63D0");
return (
<ColorSwatchPicker
label="Theme color"
items={PRESETS}
value={color}
onChange={setColor}
allowsCustom
description="Used for buttons and links."
/>
);
}Guidelines
When to use it
- Choosing from a small set of preset colors.
Use something else when
- Any color is allowed and presets are a shortcut at most. Color picker
Accessibility
Keyboard
| Keys | What happens |
|---|---|
| Arrow keys | Move between swatches. |
Built in
- Each swatch is named; the caption names the selection.
You provide
- A label and a name for each preset.
API reference
ColorSwatchPicker
| Prop | Type | Default | Description |
|---|---|---|---|
labelRequired | React.ReactNode | – | "Theme presets". |
itemsRequired | readonly ColorSwatchItem[] | – | The preset colors; check them with checkBrandColor so that only a custom color can fail. |
value | string | – | The chosen color, controlled. |
defaultValue | string | – | |
onChange | (value: string) => void | – | |
allowsCustom | boolean | – | Ends with a button that opens ColorPicker for a custom color. |
description | React.ReactNode | – | Under the swatches, after "Ocean · selected". |
labelRequiredReact.ReactNode"Theme presets".
itemsRequiredreadonly ColorSwatchItem[]The preset colors; check them with checkBrandColor so that only a custom color can fail.
valuestringThe chosen color, controlled.
nullor"": none chosen, so no swatch shows a check (a theme with no brand color).defaultValuestringonChange(value: string) => voidallowsCustombooleanEnds with a button that opens ColorPicker for a custom color.
descriptionReact.ReactNodeUnder the swatches, after "Ocean · selected".