The <ColorPicker/> component is built from small parts so you only render what you need. The sliders build on top of the bits-ui <Slider/> component.
Use the following composition to build a ColorPicker:
FormatSelect, ChannelInputs and AlphaInput are optional.
Wrap a Popover with <ColorPicker.Root/> and put the parts in <Popover.Content/>, so the picker keeps its state while the popover is closed. The trigger here paints its swatch from value, so the select leaves out hsb, which isn't a CSS color.
This example also adds a <ColorPicker.FormatSelect/> and joins <ColorPicker.ChannelInputs/> and <ColorPicker.AlphaInput/> into one row with the Button Group component. ChannelInputs shows one field per channel for rgb, hsl and hsb, and a single text input for hex. The grid-cols-4 class gives every number field the same width, and in a grid the hex input spans the three channel columns on its own.
While an AlphaInput is shown, the hex input leaves the alpha out. Typing a color without an alpha (e.g. #ff0000) keeps the current alpha; typing one with an alpha (e.g. #ff000080) sets it.
value accepts hex, rgb(), hsl() or hsb() and is written back in the chosen format. format is bindable, so a <ColorPicker.FormatSelect/> can change it. Pass formats to the select to offer fewer options.
hsb() isn't a CSS color, so browsers ignore it in a style. Use hex, rgb or hsl when you put value straight into CSS.
Holds the color state and provides it to the other parts.
The saturation and brightness square. Screen readers get one slider per axis. Arrow keys move by 1 (Shift by 10), Home/End set saturation, PageUp/PageDown change brightness by 10.
A slider for the hue, from 0 to 360.
| Prop | Type | Default |
|---|---|---|
ref? $bindable | HTMLSpanElement | null |
class? | string | - |
thumbLabel? | string | Hue |
A slider for the transparency, from 0 to 100%.
| Prop | Type | Default |
|---|---|---|
ref? $bindable | HTMLSpanElement | null |
class? | string | - |
thumbLabel? | string | Alpha |
A text input for the color. Applies on Enter or blur, reverts invalid values and cancels on Escape. While an `AlphaInput` is shown it leaves the alpha out, and a typed color without an alpha keeps the current one.
| Prop | Type | Default |
|---|---|---|
ref? $bindable | HTMLInputElement | null |
class? | string | - |
A select that changes the bound `format` of the root (HEX, RGB, HSL or HSB).
| Prop | Type | Default |
|---|---|---|
ref? $bindable | HTMLButtonElement | null |
class? | string | - |
formats? | ColorFormat[] | ['hex', 'rgb', 'hsl', 'hsb'] |
A text input for the transparency as a percentage. Arrow keys change it by 1 (Shift by 10). Applies on Enter or blur, reverts invalid values and cancels on Escape.
| Prop | Type | Default |
|---|---|---|
ref? $bindable | HTMLInputElement | null |
class? | string | - |