Loading component…
Loading component…
A swatch button that opens a color card
To reuse this local React demo, copy all files from the Code tab into the same folder, then import ColorPicker from ./color-picker.
import { useState } from "react";
import { ColorPicker } from "@/components/ui/color-picker";
const PRESETS = [
{ value: "#0190fe", label: "Blue" },
{ value: "#13b3a0", label: "Teal" },
{ value: "#e8b000", label: "Amber" },
{ value: "#a651f0", label: "Purple" },
];
export function AgentColor({ onSave }: { onSave: (hex: string) => void }) {
const [color, setColor] = useState("#0190fe");
return (
<ColorPicker
label="Agent color"
value={color}
onValueChange={setColor}
onOpenChange={(open) => !open && onSave(color)}
presets={PRESETS}
align="end"
/>
);
}| Name | Type | Default | Description |
|---|---|---|---|
| value | string | The color as lowercase hex – "#0190fe", or "#0190fecc" when alpha is on and opacity is below 100%. From outside it also takes #rgb and rgb() | |
| defaultValue | string | "#0190fe" | The starting color when value isn't controlled |
| onValueChange | (value: string) => void | Called on every change, while dragging too | |
| alpha | boolean | false | Shows the opacity slider and its percentage; without it, opacity in a value is dropped |
| presets | (string \| { value: string; label: string })[] | Round swatches under the sliders; a label is read out and shown as the tooltip (a plain string reads as its hex) | |
| open | boolean | ColorPicker only – whether the card is open; leave it out to let the picker handle it | |
| defaultOpen | boolean | false | ColorPicker only – open from the first render, without taking the focus |
| onOpenChange | (open: boolean) => void | ColorPicker only – called when the button, a press outside, Escape or tabbing away opens or closes it | |
| label | string | "Color" | ColorPicker only – what the color is for; names the button ("Agent color: #0190FE") and the card |
| showValue | boolean | true | ColorPicker only – the hex next to the swatch; off: a square swatch button |
| size | "sm" \| "md" | "md" | ColorPicker only – a 28 or 32px high button; bigger on touch screens |
| side | "bottom" \| "top" | "bottom" | ColorPicker only – where the card opens; it flips when there is more room on the other side |
| align | "start" \| "end" | "start" | ColorPicker only – which edges line up; switches when it would stick out |
| name | string | ColorPicker only – sends the hex with a form (a hidden input) | |
| ...button / div props | ComponentProps<"button"> \| ComponentProps<"div"> | ColorPicker: disabled, aria-label, id, ref … go to the button. ColorPickerPanel: className, style, aria-label … go to its box |
Dragging captures the pointer, so you can drag past the card's edge and the thumb stays at the end
The hex is set in a fixed-width face, so the button doesn't change size while you drag
The card lives in the top layer, follows its button on scroll and resize, and stays fully on screen; 264px wide, it fits a 320px phone
The sliders always run left to right, also in right-to-left pages – a color scale isn't text
Touch screens get a taller area, bigger thumbs and swatches, and 16px in the hex field so iOS doesn't zoom in
Dark mode built in: switches when an ancestor (usually <html>) has the class dark or data-theme="dark"
@import "tailwindcss"; in your main CSS file.