Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Color Picker for Astro

A full-featured color picker for Astro. Built with the popover component, with support for hex, hsl, rgb, and oklch - plus alpha control, swatches, and more.

Installation

bunx --bun shadcn@latest add @gnostic/color-picker

Usage

---
import {
  ColorPicker,
  ColorPickerContent,
  ColorPickerInput,
} from "@/components/ui/color-picker"
---

<ColorPicker id="canvas-background" name="background" defaultValue="#f8fafc">
  <ColorPickerInput aria-label="Canvas background" />
  <ColorPickerContent />
</ColorPicker>

ColorPicker owns the color and a hidden form field. Each child part is optional. Without a slot, the root supplies a trigger and content. ColorPickerContent supplies a panel when its slot is empty. ColorPickerPanel supplies an area, hue and optional opacity sliders, and fields when its slot is empty.

The root id moves to the main text input or trigger when the browser binds the picker. A FieldLabel can use that ID. Other HTML attributes belong on the part that needs them. Use ColorPickerInput for a text field with a swatch button, or compose ColorPickerTrigger, ColorPickerSwatch, and ColorPickerValue.

Examples

Text input

Enter a CSS color. Enter or blur applies the value. Escape restores an unfinished edit. Invalid text restores the current value. Alt+ArrowDown opens the popup.

Enter a hex code, rgb(), hsl(), oklch(), or a name such as tomato.

Inline panel and opacity

Place ColorPickerPanel directly in the root for an inline picker. Set alpha on the root to permit transparency. The root format controls form values and events. ColorPickerFields joins a compact native format selector, the channels, and optional opacity in one row. The format selector changes only the displayed channels.

Individual channels

Combine channel sliders and inputs with other primitives. Channel inputs apply on Enter or blur. Arrow keys change their value; Shift changes it by ten steps.

Swatches only

A picker does not require an area or popup. Use colors on ColorPickerSwatches, or supply named ColorPickerSwatchItem parts. Each item is a native toggle button. Arrow keys, Home, and End move focus within the group. Enter or Space selects the color.

Events and external updates

color-picker:change bubbles from the root for each changed value, including a drag. color-picker:commit bubbles when an edit ends: pointer release, a slider change, a channel key, text entry, or a swatch selection. Both events carry id, name, value, hex, oklch, and hsl.

Use change for a live preview. Use commit to save the result. The design menu uses commit to update the complete preset after an edit.

import type { ColorPickerChangeDetail } from "@/components/ui/color-picker"

const picker = document.querySelector<HTMLElement>('[data-slot="color-picker"]')!
picker.addEventListener("color-picker:change", (event) => {
    const { id, value } = (event as CustomEvent<ColorPickerChangeDetail>).detail
    console.log(id, value)
})

// Silent updates do not send another change event.
picker.dispatchEvent(
    new CustomEvent("color-picker:set", {
        detail: { value: "oklch(0.7 0.15 250)" },
    }),
)

createColorPicker(root) returns getValue(), setColor(value, { silent }), and destroy(). A repeated call returns the existing controller. Native form reset restores the initial color and field formats. Disabled fields are not submitted. readOnly prevents user edits.

Color formats and styling

The output formats are HEX, RGB, HSL, HSB, and OKLCH. Parsing supports CSS named colors, transparency, and CSS Color 4 color functions. Context-dependent browser colors are resolved only for text input. HEX, RGB, HSL, and HSB output map colors into sRGB. Use OKLCH output to retain wider color values. With alpha={false}, output is opaque.

This Astro adaptation uses the supplied ReUI color model and part structure. It uses the existing data-slot Popover for popup behavior and native range inputs for channel sliders. It has no React or Motion dependency. Popup trigger and content parts retain the data-slot names required by Popover. All picker styling uses central recipes in registry/styles/gnostic/default/color-picker.css, along with the shared Button, InputGroup, and Popover recipes.

API reference

ColorPickerProps

PropType
defaultValue?string

Initial CSS color.

Typestring
Default"#000000"
format?ColorPickerFormat

Output format for events and form submission.

TypeColorPickerFormat
Default"hex"
alpha?boolean

Permit transparency.

Typeboolean
Defaultfalse
disabled?boolean

Disable all controls.

Typeboolean
Defaultfalse
readOnly?boolean

Show the color without permitting changes.

Typeboolean
Defaultfalse
label?string

Accessible name for the picker.

Typestring
Default"Choose a color"
name?string

Name of the hidden form field.

Typestring
form?string

ID of an external form.

Typestring

ColorPickerContentProps

ColorPickerSliderProps

PropType
channel?ColorPickerChannel

Channel to change.

TypeColorPickerChannel
Default"hue"
colorSpace?ColorPickerColorSpace

Color space for the channel. Defaults to the channel's native space.

TypeColorPickerColorSpace

ColorPickerChannelInputProps

PropType
channel?ColorPickerChannel | 'hex'

Channel to change, or the hex digits.

TypeColorPickerChannel | 'hex'
Default"hex"
colorSpace?ColorPickerColorSpace

Color space for the channel. Defaults to the channel's native space.

TypeColorPickerColorSpace

ColorPickerFieldsProps

PropType
defaultFormat?ColorPickerFormat

Initial display format. Independent of the output format.

TypeColorPickerFormat
Default"hex"
formats?readonly ColorPickerFormat[]

Formats available in the menu.

Typereadonly ColorPickerFormat[]
Default["hex", "rgb", "hsl", "hsb", "oklch"]

ColorPickerSwatchesProps

PropType
colors?readonly string[]

CSS colors to show.

Typereadonly string[]
Default[]

ColorPickerSwatchItemProps

PropType
valuestring

CSS color to select.

Typestring
label?string

Accessible name. Defaults to the color value.

Typestring

ColorPickerChangeDetail

PropType
idstring

ID of the main input or trigger.

Typestring
namestring

Form field name, or an empty string.

Typestring
valuestring

Color in the root's output format.

Typestring
hexstring

Equivalent hex color, with alpha when needed.

Typestring
oklchstring

Equivalent OKLCH color. Preserves wide-gamut values.

Typestring
hsl[number, number, number]

Hue, saturation, and lightness.

Type[number, number, number]

Read the installed source for the complete prop types.

Theme and layout

The controls inherit the selected project preset. Swatches show their supplied colors. Check that the popup and channel fields fit a narrow screen and that labels stay readable in light and dark mode.