Yugen UI

ColorPicker

ColorPicker picks any color: a saturation / brightness area, hue and opacity bars, and HEX / RGB / HSL inputs.

Drag or use the arrow keys on the color area (left / right: saturation, up / down: brightness, Shift ×10); the hue and opacity bars are sliders with full keyboard support. The inputs switch between HEX, RGB and HSL; typing applies as soon as the value is valid, and up / down arrows step each channel. The button that picks a color from the screen only shows when the browser has the EyeDropper API (Chrome, Edge). swatches shows preset colors with SwatchPicker, and the one matching the current color is marked. The value is hex ("#rrggbb", or "#rrggbbaa" with alpha below 100% opacity); onValueChange also gets rgb / hsl / hsv. The color tracks always run left → right, RTL included. Put it in a Popover for a color field.
Selected #c2347a

Import

Install from npm, copy the source into your project with @yugen-ui/cli or the shadcn CLI, or import directly when working in this repo.

TSX
// npm install yugen-uiimport { ColorPicker, parseColor, type ColorPickerChangeData } from "yugen-ui";

Variants

Each tile is a standalone example: hover to copy, or press Code to see the source.

Selected #0f6cbd99

Opacity (alpha)

Adds an opacity bar and input; the value becomes 8-digit hex.

In a Popover

A button showing the current color opens the ColorPicker.

Usage

Do
  • Add swatches with your brand colors for quick, consistent picks.
  • Show the color code next to the button that opens the picker, not just a swatch.
  • Check contrast when a color people pick is used for text.
Don't
  • Don't use ColorPicker for a few fixed colors. Use SwatchPicker.
  • Don't turn on alpha where the color can't be transparent.

Props

Prop · TypeDefaultDescription
value / defaultValuestringDefault: —Selected color: hex, rgb() or hsl(). defaultValue is black by default.
onValueChange(value: string, data: ColorPickerChangeData) => voidDefault: —value is hex; data holds hex, rgb, hsl and hsv.
alphabooleanDefault: falseShows the opacity bar and input.
swatches(string | { color: string; label?: string })[]Default: —Preset colors, shown with SwatchPicker.
defaultFormat"hex" | "rgb" | "hsl"Default: "hex"Initial format of the inputs.
eyeDropperbooleanDefault: trueButton that picks a color from the screen, where the browser supports it.
disabledbooleanDefault: falseDisables the whole picker.
refRef<HTMLDivElement>Default: —Points to the outer element.
parseColor(input)(input: string) => RgbaColor | nullDefault: —Reads hex, rgb() and hsl() into { r, g, b, a }.

Messages

Text built into the component: labels, aria-labels, screen reader announcements.

English by default. Change it with the messages prop of YugenProvider, by the namespace below; messagesVi has the Vietnamese set. See Interface language.

messages.colorPicker

Key · TypeDefault (English)messagesVi
areastring

aria-label của vùng chọn độ bão hoà / độ sáng.

Default (English)"Saturation and brightness"messagesVi"Độ bão hoà và độ sáng"
areaValue(saturation: number, brightness: number) => string

Giá trị đọc cho trình đọc màn hình (phần trăm).

Default (English)(saturation, brightness) => `Saturation ${saturation}%, brightness ${brightness}%`messagesVi(saturation, brightness) => `Độ bão hoà ${saturation}%, độ sáng ${brightness}%`
huestringDefault (English)"Hue"messagesVi"Sắc độ"
alphastring

Thanh và ô độ đục (alpha).

Default (English)"Opacity"messagesVi"Độ đục"
formatstring

aria-label ô chọn định dạng (HEX / RGB / HSL).

Default (English)"Color format"messagesVi"Định dạng màu"
hexstringDefault (English)"Hex"messagesVi"Hex"
redstringDefault (English)"Red"messagesVi"Đỏ"
greenstringDefault (English)"Green"messagesVi"Xanh lá"
bluestringDefault (English)"Blue"messagesVi"Xanh dương"
saturationstringDefault (English)"Saturation"messagesVi"Bão hoà"
lightnessstringDefault (English)"Lightness"messagesVi"Độ sáng"
eyeDropperstring

Nút lấy màu trên màn hình (EyeDropper).

Default (English)"Pick a color from the screen"messagesVi"Lấy màu trên màn hình"
swatchesstring

Nhãn nhóm màu có sẵn (swatches).

Default (English)"Preset colors"messagesVi"Màu có sẵn"