import { ColorPicker } from "@/components/ui/color-picker";
Color Picker
A versatile color picker component that allows users to select colors visually or by entering values in HEX, RGB, or HSL formats.
Playground
Customize the Color Picker properties to see different variations.
Customize
Installation
pnpm dlx shadcn@latest add https://shadcn-ui-variants.vercel.app/r/color-picker.json
Overview
Color Picker is a versatile color picker component built upon the lightweight library react-colorful
. It allows users to select colors visually or by entering values in HEX
, RGB
, or HSL
formats. It supports alpha channel selection, preset color palettes, and customizable input formats. Built as a popover for easy integration.
Basic Usage
<ColorPicker />
Examples
Controlled Component
Manage the color state outside the component.
Current color: #10B981
Uncontrolled Component with Default Color
Set an initial color without controlling its state externally.
Alpha Channel
Enable the alpha channel for transparency selection.
When withAlpha
is true, HEX values will include the alpha component (e.g., #RRGGBBAA
). RGB and HSL inputs will also show an "Alpha" field.
Customizing Input Formats (showFormat
)
Control which color model input fields (HEX, RGB, HSL) are available.
If only one format is enabled (e.g., showFormat={{ hex: true }}
), the toggle group for format selection will be hidden. If showFormat
results in no formats being explicitly enabled, "HEX" will be shown as a fallback.
Preset Colors (presetColors
)
Provide an array of HEX strings for quick color selection.
Custom Trigger
Replace the default button trigger with a custom element.
"Add New Color" Action (onAddColor
)
Utilize the onAddColor
callback for custom actions.
The "Add New Color" button appears at the bottom of the popover if onAddColor
is provided.
Form Integration
Theme customization
Color Utilities
Alongside the ColorPicker component, several utility functions for color conversion are also exported. These can be useful for manual color manipulations elsewhere in your application.
rgbaToHex()
Converts RGBA (Red, Green, Blue, Alpha) color values to a HEX string.
Signature:
function rgbaToHex(r: number, g: number, b: number, a?: number): string;
Parameters:
r: number
The red color component (0-255).g: number
The green color component (0-255).b: number
The blue color component (0-255).a?: number
(optional) The alpha (opacity) component (0-1). Defaults to1
(fully opaque).
Returns:
(string)
: An uppercase HEX color string with a leading#
.- If alpha is
1
or undefined, a 6-digit HEX string is returned (e.g.,"#RRGGBB"
). - If alpha is less than
1
, an 8-digit HEX string is returned (e.g.,"#RRGGBBAA"
).
Example:
import { rgbaToHex } from "@/components/ui/color-picker";
const hexColorOpaque = rgbaToHex(255, 165, 0); // Returns "#FFA500"
const hexColorTransparent = rgbaToHex(75, 0, 130, 0.5); // Returns "#4B008280" (Indigo with 50% alpha)
hexToRgba()
Converts a HEX color string to an RGBA object.
Signature:
function hexToRgba(hex: string | null | undefined): RgbaColor | null;
Parameters:
hex: string | null | undefined
The HEX color string. It can be 3-digit (#RGB
), 4-digit (#RGBA
), 6-digit (#RRGGBB
), or 8-digit (#RRGGBBAA
). The leading#
is optional. The function is case-insensitive.
Returns:
(RgbaColor | null)
: An object representing the RGBA color, ornull
if the input HEX string is invalid. TheRgbaColor
object has the following structure:interface RgbaColor { r: number; // 0-255 g: number; // 0-255 b: number; // 0-255 a: number; // 0-1 }
Example:
import { hexToRgba } from "@/components/ui/color-picker";
const rgbaColor1 = hexToRgba("#FF0000"); // Returns { r: 255, g: 0, b: 0, a: 1 }
const rgbaColor2 = hexToRgba("00FF0080"); // Returns { r: 0, g: 255, b: 0, a: 0.5 } (approx)
const rgbaColor3 = hexToRgba("#F0A"); // Returns { r: 255, g: 0, b: 170, a: 1 }
const invalidColor = hexToRgba("#12345"); // Returns null
hexToHsla()
Converts a HEX color string to an HSLA (Hue, Saturation, Lightness, Alpha) object.
Signature:
function hexToHsla(hex: string | null | undefined): HslaColor | null;
Parameters:
hex: string | null | undefined
The HEX color string. It can be 3, 4, 6, or 8 digits, with or without a leading#
. Case-insensitive.
Returns:
(HslaColor | null)
: An object representing the HSLA color, ornull
if the input HEX string is invalid. TheHslaColor
object has the following structure:interface HslaColor { h: number; // 0-360 (degrees) s: number; // 0-100 (percentage) l: number; // 0-100 (percentage) a: number; // 0-1 (alpha) }
Example:
import { hexToHsla } from "@/components/ui/color-picker";
const hslaColor1 = hexToHsla("#00FF00"); // Returns approx. { h: 120, s: 100, l: 50, a: 1 }
const hslaColor2 = hexToHsla("#4B008280"); // Returns approx. { h: 275, s: 100, l: 25, a: 0.5 }
hslaToHex()
Converts HSLA (Hue, Saturation, Lightness, Alpha) color values to a HEX string.
Signature:
function hslaToHex(h: number, s: number, l: number, a?: number): string;
Parameters:
h: number
The hue component (0-360 degrees).s: number
The saturation component (0-100, as a percentage).l: number
The lightness component (0-100, as a percentage).a?: number
(optional) The alpha (opacity) component (0-1). Defaults to1
(fully opaque).
Returns:
(string)
: An uppercase HEX color string with a leading#
.- If alpha is
1
or undefined, a 6-digit HEX string is returned (e.g.,"#RRGGBB"
). - If alpha is less than
1
, an 8-digit HEX string is returned (e.g.,"#RRGGBBAA"
).
Example:
import { hslaToHex } from "@/components/ui/color-picker";
const hexFromHsl1 = hslaToHex(240, 100, 50); // Returns "#0000FF" (Blue)
const hexFromHsl2 = hslaToHex(0, 100, 50, 0.75); // Returns "#FF0000BF" (Red with 75% alpha)
Accessibility
- Label Association: Use the
id
prop in conjunction with a<Label htmlFor={id}>
to ensure proper accessibility for screen readers and keyboard navigation in forms. - Keyboard Navigation:
- The
PopoverTrigger
(default button or custom trigger) is focusable and can be activated usingSpace
orEnter
. - Once the popover is open, focus is managed within it. Navigation between the color selection canvas, input fields, toggle group, and buttons follows standard keyboard patterns, largely inherited from the underlying components (
Popover
,Input
,Button
,ToggleGroup
). - Color swatches from
defaultColors
are implemented asbutton
elements, making them focusable and activatable withEnter
orSpace
.
- The
- ARIA Attributes: Default ARIA attributes are applied by the underlying components to enhance accessibility. The
aria-label
on format toggle buttons anddefaultColor
swatch buttons provide descriptive names for assistive technologies.
ColorPicker Props
Prop | Type | Default |
---|---|---|
className | string | — |
value | string | — |
onChange | function | — |
onAddColor | function | — |
trigger | React.ReactNode | — |
popoverSide | enum | "bottom" |
popoverAlign | enum | "center" |
defaultColor | string | — |
presetColors | string[] | — |
withAlpha | boolean | false |
showFormat | ColorPickerFormatOption | { hex: true, rgb: true, hsl: true } |
selectorTitle | React.ReactNode | — |