Shadcn Color Picker
A color picker built from parts you arrange yourself: a two channel area, hue and alpha sliders, a text input that reads any CSS color, number inputs per channel, preset swatches and an eye dropper. It edits in HSV, HSL, sRGB or OKLCH, writes the value as hex or any CSS color syntax, and keeps colors inside the sRGB or P3 gamut. Every part works with the keyboard and screen readers.
Installation
npx shadcn@latest add @shadcnuikit/color-picker1
This adds components/ui/color-picker.tsx, the button and input components it depends on, and a demo file. Set up the registry once as described in Shadcn CLI. Color Picker is free and installs without an API key.
Manual installation
Install the dependencies and the shadcn/ui components:
npm install @ui-sdk/react@beta lucide-react
npx shadcn@latest add button input
Copy the source into components/ui/color-picker.tsx:
"use client";
import * as React from "react";import { ColorPicker as ColorPickerPrimitive, ColorPickerArea as ColorPickerAreaPrimitive, ColorPickerAreaCanvas as ColorPickerAreaCanvasPrimitive, ColorPickerAreaThumb as ColorPickerAreaThumbPrimitive, ColorPickerChannelInput as ColorPickerChannelInputPrimitive, ColorPickerEyeDropper as ColorPickerEyeDropperPrimitive, ColorPickerInput as ColorPickerInputPrimitive, ColorPickerSlider as ColorPickerSliderPrimitive, ColorPickerSliderThumb as ColorPickerSliderThumbPrimitive, ColorPickerSwatch as ColorPickerSwatchPrimitive, ColorPickerSwatches as ColorPickerSwatchesPrimitive, ColorPickerSwatchItem as ColorPickerSwatchItemPrimitive, createColorPickerController,} from "@ui-sdk/react/color-picker";import { PipetteIcon } from "lucide-react";
import { Button } from "@/components/ui/button";import { Input } from "@/components/ui/input";import { cn } from "@/lib/utils";
const checkerboard = "bg-[repeating-conic-gradient(var(--muted)_0_25%,var(--background)_0_50%)] bg-size-[8px_8px]";
const thumb = "size-4 -translate-1/2 rtl:translate-x-1/2 rounded-full border-2 border-white bg-(--color-picker-color) shadow-sm ring-1 ring-black/10 outline-none transition-[box-shadow] focus-visible:ring-[3px] focus-visible:ring-ring/50";
type WithClassName<T> = Omit<T, "className"> & { className?: string };
function ColorPicker({ className, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerPrimitive>>) { return ( <ColorPickerPrimitive className={cn( "flex w-64 flex-col gap-3 data-disabled:pointer-events-none data-disabled:opacity-50", className, )} {...props} /> );}
function ColorPickerArea({ className, children, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerAreaPrimitive>>) { return ( <ColorPickerAreaPrimitive className={cn( "relative h-40 w-full touch-none rounded-md border bg-(image:--color-area-background) has-[[data-slot=color-picker-area-canvas]]:bg-muted", className, )} {...props} > {children ?? <ColorPickerAreaThumb />} </ColorPickerAreaPrimitive> );}
function ColorPickerAreaCanvas({ className, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerAreaCanvasPrimitive>>) { return ( <ColorPickerAreaCanvasPrimitive className={cn("absolute inset-0 size-full rounded-[inherit]", className)} {...props} /> );}
function ColorPickerAreaThumb({ className, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerAreaThumbPrimitive>>) { return ( <ColorPickerAreaThumbPrimitive className={cn(thumb, "cursor-grab active:cursor-grabbing", className)} {...props} /> );}
function ColorPickerSlider({ className, children, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerSliderPrimitive>>) { return ( <ColorPickerSliderPrimitive className={cn( "relative h-3 w-full shrink-0 touch-none rounded-full", "data-[orientation=vertical]:h-auto data-[orientation=vertical]:min-h-40 data-[orientation=vertical]:w-3", "data-[channel=alpha]:[background:var(--color-slider-background),repeating-conic-gradient(var(--muted)_0_25%,var(--background)_0_50%)_0_0/8px_8px]", "not-data-[channel=alpha]:[background:var(--color-slider-background)]", className, )} {...props} > {children ?? <ColorPickerSliderThumb />} </ColorPickerSliderPrimitive> );}
function ColorPickerSliderThumb({ className, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerSliderThumbPrimitive>>) { return ( <ColorPickerSliderThumbPrimitive className={cn( thumb, // Vertical thumbs are placed with `bottom`, so they shift down by half instead of up. "data-[orientation=horizontal]:top-1/2 data-[orientation=vertical]:inset-s-1/2 data-[orientation=vertical]:translate-y-1/2", className, )} {...props} /> );}
function ColorPickerSwatch({ className, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerSwatchPrimitive>>) { return ( <ColorPickerSwatchPrimitive className={cn( "relative size-9 shrink-0 overflow-hidden rounded-md border", checkerboard, "after:absolute after:inset-0 after:bg-(--color-picker-color) after:content-['']", className, )} {...props} /> );}
function ColorPickerInput({ className, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerInputPrimitive>>) { return ( <ColorPickerInputPrimitive render={<Input />} className={cn("font-mono text-sm", className)} {...props} /> );}
function ColorPickerChannelInput({ className, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerChannelInputPrimitive>>) { return ( <ColorPickerChannelInputPrimitive render={<Input />} className={cn("min-w-0 px-2 text-center font-mono text-sm tabular-nums", className)} {...props} /> );}
function ColorPickerEyeDropper({ className, children, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerEyeDropperPrimitive>>) { return ( <ColorPickerEyeDropperPrimitive render={<Button variant="outline" size="icon" />} className={cn("shrink-0 data-unsupported:hidden", className)} {...props} > {children ?? <PipetteIcon aria-hidden="true" />} </ColorPickerEyeDropperPrimitive> );}
function ColorPickerSwatches({ className, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerSwatchesPrimitive>>) { return ( <ColorPickerSwatchesPrimitive className={cn("flex flex-wrap gap-2", className)} {...props} /> );}
function ColorPickerSwatchItem({ className, ...props}: WithClassName<React.ComponentProps<typeof ColorPickerSwatchItemPrimitive>>) { return ( <ColorPickerSwatchItemPrimitive className={cn( "relative size-7 cursor-pointer overflow-hidden rounded-md border outline-none transition-[box-shadow,scale]", checkerboard, "after:absolute after:inset-0 after:bg-(--color-picker-color) after:content-['']", "hover:scale-110 focus-visible:ring-[3px] focus-visible:ring-ring/50", "aria-selected:ring-2 aria-selected:ring-ring aria-selected:ring-offset-2 aria-selected:ring-offset-background", className, )} {...props} /> );}
export { ColorPicker, ColorPickerArea, ColorPickerAreaCanvas, ColorPickerAreaThumb, ColorPickerChannelInput, ColorPickerEyeDropper, ColorPickerInput, ColorPickerSlider, ColorPickerSliderThumb, ColorPickerSwatch, ColorPickerSwatches, ColorPickerSwatchItem, createColorPickerController,};
Usage
import {
ColorPicker,
ColorPickerArea,
ColorPickerInput,
ColorPickerSlider,
ColorPickerSwatch,
} from "@/components/ui/color-picker";
<ColorPicker aria-label="Brand color" defaultValue="#6366f1">
<ColorPickerArea />
<ColorPickerSlider channel="hue" />
<ColorPickerSwatch />
<ColorPickerInput aria-label="Hex" />
</ColorPicker>
Give the picker a name with aria-label. The value is always a CSS color string. Without format, the default HSV space writes it as hex, like "#6366f1", or "#6366f1cc" with alpha.
Controlled
const [color, setColor] = React.useState("#6366f1");
<ColorPicker aria-label="Brand color" value={color} onValueChange={setColor}>
<ColorPickerArea />
<ColorPickerSlider channel="hue" />
</ColorPicker>;
onValueChange runs on every step of a drag. Use onValueCommit to save once the pointer lifts or a key is pressed.
In a popover
<Popover>
<PopoverTrigger asChild>
<Button variant="outline">Pick a color</Button>
</PopoverTrigger>
<PopoverContent className="w-auto">
<ColorPicker aria-label="Color" value={color} onValueChange={setColor}>
<ColorPickerArea />
<ColorPickerSlider channel="hue" />
</ColorPicker>
</PopoverContent>
</Popover>
Examples
Alpha
Add a ColorPickerSlider with channel="alpha". The checkerboard behind it shows the transparency.
Format
format sets how the value is written: hex, srgb, hsl, hwb, lab, lch, oklab or oklch. The input shows the same syntax. The starting value is kept as you pass it until the first change, so write defaultValue in the same format.
OKLCH
space="oklch" edits lightness, chroma and hue. Put a ColorPickerAreaCanvas in the area, since no CSS gradient can draw chroma by lightness.
Channel Inputs
ColorPickerChannelInput edits one channel as a number, like red from 0 to 255. With space="srgb", sliders and inputs can edit red, green and blue.
Swatches
ColorPickerSwatches holds preset ColorPickerSwatchItems. Pressing one sets the value, and the one that matches the current color is selected.
Vertical
orientation="vertical" turns a slider upright. Vertical sliders grow upward.
Disabled
More layouts built with Color Picker, such as a popover picker and presets with RGB inputs, are on the Color Picker components page.
API Reference
ColorPicker
The root. A group around the parts, plus the hidden input the form submits.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | The color, as any CSS color, like "#0ea5e9" or "oklch(0.62 0.19 259)". Pass it to control the picker. | |
defaultValue | string | black | The color at first, when uncontrolled. A form reset returns to it. |
onValueChange | (value, details) => void | Runs on every step of a drag or key. details.color is the color object, details.cancel() stops the change. | |
onValueCommit | (value, details) => void | Runs once an interaction ends: the pointer lifts, a key is pressed, a swatch is picked. Use it for saving. | |
space | "hsv", "hsl", "srgb", "oklch" | "hsv" | The space the parts edit in, which decides their channels. |
format | "hex", "srgb", "hsl", "hwb", "lab", "lch", "oklab", "oklch" | "hex" for HSV, the space otherwise | How the value is written. |
gamut | "srgb", "p3", false | "srgb" | The colors the value can hold. Colors outside it move to the closest one inside. false allows any color. |
disabled | boolean | false | Disables every part. The value is not submitted. |
readOnly | boolean | false | Keeps the parts focusable and the value submitted, but stops changes. |
required | boolean | false | Adds data-required. A picker always has a value, so it never blocks a form. |
name | string | The name the value submits under. Without one, nothing is submitted. | |
form | string | The id of a form to belong to, when the picker is outside it. | |
controller | ColorPickerController | Sets the value from anywhere. Create one with createColorPickerController(). | |
className | string | Classes for the root. |
The root has data-disabled, data-readonly, data-required and data-out-of-gamut attributes for styling.
ColorPickerArea
A surface for two channels. Renders a ColorPickerAreaThumb when it has no children.
| Prop | Type | Default | Description |
|---|---|---|---|
x | ColorChannel | saturation, red or chroma, by space | The channel from left to right. |
y | ColorChannel | brightness, lightness or green, by space | The channel from bottom to top. |
className | string | Classes for the area. |
ColorChannel is one of "hue", "saturation", "brightness", "lightness", "chroma", "red", "green", "blue" or "alpha".
A CSS gradient draws the area only in HSV and HSL with saturation across and brightness or lightness up. For any other space or axes, add a ColorPickerAreaCanvas.
ColorPickerAreaThumb
The area's draggable thumb. Use it as a child of ColorPickerArea when you add a canvas.
ColorPickerAreaCanvas
Paints the area pixel by pixel, for channels no gradient can draw, like OKLCH chroma by lightness. Colors outside the gamut stay transparent.
ColorPickerSlider
A track for one channel. Renders a ColorPickerSliderThumb when it has no children.
| Prop | Type | Default | Description |
|---|---|---|---|
channel | ColorChannel | hue, or blue in sRGB | The channel the slider edits. |
orientation | "horizontal", "vertical" | "horizontal" | The slider's axis. Vertical sliders grow upward. |
className | string | Classes for the track. |
The track has data-channel and data-orientation. The alpha track gets a checkerboard background.
ColorPickerSliderThumb
The slider's thumb, placed at --color-slider-thumb-position.
ColorPickerInput
A text input that reads any CSS color on Enter or blur. Text that isn't a color goes back to the value. It takes every Input prop.
ColorPickerChannelInput
A number input for one channel, in the units people read it in: degrees for hue, percentages, 0 to 255 for red, green and blue.
| Prop | Type | Default | Description |
|---|---|---|---|
channel | ColorChannel | hue, or blue in sRGB | The channel the input edits. |
className | string | Classes for the input. |
ColorPickerSwatch
Shows the current color over a checkerboard, as --color-picker-color.
ColorPickerEyeDropper
A button that picks a color from the screen with the browser's EyeDropper API. Where the browser has no support, it gets data-unsupported and is hidden. Give it an aria-label. Its default icon is PipetteIcon.
ColorPickerSwatches
A list of preset colors. Give it an aria-label.
ColorPickerSwatchItem
One preset.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | The color it picks, as any CSS color. | |
className | string | Classes for the swatch. |
The item has aria-selected and data-selected when its color matches the picker's.
createColorPickerController
const [controller] = React.useState(() => createColorPickerController());
<ColorPicker aria-label="Color" controller={controller} />;
controller.setValue("#22c55e");
controller.getValue();
controller.focus();
Keyboard
Area and sliders
| Key | Action |
|---|---|
ArrowLeft, ArrowRight | Moves the area thumb along x, or steps a horizontal slider. |
ArrowUp, ArrowDown | Moves the area thumb along y, or steps a slider. |
Shift + arrow | Moves ten steps. |
PageUp, PageDown | Moves a tenth of the range: y on the area, the channel on a slider. |
Home, End | Goes to the lowest or highest value: x on the area, the channel on a slider. |
Inputs and swatches
| Key | Action |
|---|---|
Enter | Reads the typed color in ColorPickerInput. |
Escape | Reverts typed text in ColorPickerInput to the value. |
ArrowLeft, ArrowRight, ArrowUp, ArrowDown | Moves between swatches. |
Home, End | Moves to the first or last swatch. |
Enter, Space | Picks the focused swatch. |
Accessibility
- The area thumb is a
slidernamed for both channels, such as "Saturation and brightness". Each slider thumb is aslidernamed for its channel, with its value in readable units. - The swatch is an
imgnamed with a description of the color. Swatches are alistboxofoptions named the same way. - Labels are English by default. Pass
aria-labelto any part to translate it. - Always name the picker with
aria-label, and nameColorPickerInputandColorPickerEyeDroppertoo.
For AI Agents
When to use
- Choosing any color: brand colors, theme editors, label and tag colors, drawing and design tools.
- Picking wide gamut or perceptual colors, with
space="oklch"andgamut="p3". - A short preset palette with the option to pick something custom, with
ColorPickerSwatches.
When not to use
- Choosing between a few fixed colors only. Render
ColorPickerSwatchesalone, or use a Radio Group. - Picking a theme token like primary or muted. Use a Select.
- A plain hex text field. Use an Input.
Agent notes
- Import from
@/components/ui/color-picker. The exports areColorPicker,ColorPickerArea,ColorPickerAreaCanvas,ColorPickerAreaThumb,ColorPickerSlider,ColorPickerSliderThumb,ColorPickerInput,ColorPickerChannelInput,ColorPickerSwatch,ColorPickerEyeDropper,ColorPickerSwatches,ColorPickerSwatchItemandcreateColorPickerController. - The value is a CSS color string, not an object.
details.colorinonValueChangeholds the parsed color for@ui-sdk/core/colorhelpers likeformatColor. valueanddefaultValueare shown as passed until the first change. Write them in the same syntax asformat, or the input switches syntax on the first drag.- To switch the format of a controlled value, convert it with
parseColorandformatColorfrom@ui-sdk/core/colorbefore passing the newformat. - Outside HSV and HSL, for example in OKLCH or sRGB, add
ColorPickerAreaCanvasandColorPickerAreaThumbinsideColorPickerArea. Without a canvas the area has no background. - Save with
onValueCommit, notonValueChange, which fires on every pixel of a drag.
Resources
- Markdown: /docs/components/color-picker.md
- Variants: Color Picker components, 6 free examples
- Install:
npx shadcn@latest add @shadcnuikit/color-picker1 - MCP: call
get_item_codewithcolor-picker1from the MCP Server - Full index for agents: llms.txt