Color Picker
Lets the user choose a color with a color area, sliders, swatches, or a typed value.
Usage
import { ColorPicker } from '@leverui/components';
<ColorPicker.Root>
<ColorPicker.HiddenInput />
<ColorPicker.Label />
<ColorPicker.Control>
<ColorPicker.Input />
<ColorPicker.Trigger />
</ColorPicker.Control>
<ColorPicker.Positioner>
<ColorPicker.Content>
<ColorPicker.Area />
<ColorPicker.EyeDropper />
<ColorPicker.Sliders />
<ColorPicker.SwatchGroup>
<ColorPicker.SwatchTrigger>
<ColorPicker.Swatch />
</ColorPicker.SwatchTrigger>
</ColorPicker.SwatchGroup>
</ColorPicker.Content>
</ColorPicker.Positioner>
</ColorPicker.Root>
Shortcuts
ColorPicker.ChannelSlider
<ColorPicker.ChannelSlider />
Same as:
<ColorPicker.ChannelSlider>
<ColorPicker.TransparencyGrid />
<ColorPicker.ChannelSliderTrack />
<ColorPicker.ChannelSliderThumb />
</ColorPicker.ChannelSlider>
ColorPicker.Sliders
Same as:
<Stack>
<ColorPicker.ChannelSlider channel="hue" />
<ColorPicker.ChannelSlider channel="alpha" />
</Stack>
ColorPicker.Area
<ColorPicker.Area>
<ColorPicker.AreaThumb />
<ColorPicker.AreaBackground />
</ColorPicker.Area>
Same as:
<ColorPicker.Area />
ColorPicker.EyeDropper
Same as:
<ColorPicker.EyeDropperTrigger
asChild={(getProps) => (
<IconButton {...getProps()}>
<PipetteIcon />
</IconButton>
)}
/>
Examples
Sizes
Use the size prop to change the size of the color picker.
Variants
Use the variant prop to change the visual style of the color picker. Values can be either outline or subtle.
Input Only
Combine the ColorPicker.ValueSwatch and the ColorPicker.EyeDropper on the InputGroup to render a color picker that contains only an input.
Swatch Only
Use the ColorPicker.SwatchGroup and ColorPicker.SwatchTrigger to render only the color swatches.
Trigger Only
Compose the color picker to initially show only a trigger using the ColorPicker.ValueSwatch and ColorPicker.ValueText.
Trigger Inside Input
Compose the color picker to trigger in input using the InputGroup and ColorPickerInput.
Controlled
Use the value and onValueChange props to control the state of the color picker.
Store
An alternative way to control the color picker is to use the RootProvider component and the useColorPicker store hook.
This way you can access the color picker state and methods from outside the color picker.
Change End
Use the onValueChangeEnd to listen to when the user finishes selecting a color, rather than while they are scrubbing or dragging through the color area.
onChangeEnd: #EB5E41Channel Slider
Combine the ColorPickerChannelSliders and the format prop to add the different color channels to the color picker.
Hook Form
Here's an example of how to integrate the color picker with a form, holding the value in a Solid signal.
Inline
Here's an example of how to display an inline version of the color picker.
Open From Dialog
To use the color picker within a dialog, avoid portalling the ColorPicker.Positioner to the document's body.
Disabled
Pass the disabled prop to disable the color picker.
Channel Input
Use the ChannelFormat.Select and ColorPicker.ChannelInput to create a color picker that allows users to select their preferred channel.
Fit Content
Pass the data-fit-content attribute to the ColorPicker.Trigger to make it fit the content.
ReadOnly
Use the readOnly prop to make the color picker component read-only.
Save Swatch
Here's an example of how to save a selected color as a swatch.
Swatches
Here's an example of how to combine the color picker with pre-defined swatches.
Swatch and Input
Here's how to compose a swatch with an input.
Swatch and Trigger
Here's how to compose a swatch with a trigger.
Guide
Getting the hex code
The value object's toString() takes the format you want.
<ColorPicker.Root
onValueChange={(details) => {
console.log(details.value.toString('hex')); // "#ff0000"
console.log(details.value.toString('hexa')); // "#ff0000ff" (with alpha)
console.log(details.value.toString('rgb')); // "rgb(255, 0, 0)"
console.log(details.value.toString('css')); // CSS color string
}}
>
{/* ... */}
</ColorPicker.Root>
From the store:
const picker = useColorPicker(); // or useColorPickerContext()
const hexValue = picker().value.toString('hex'); // "#ff0000"
From a parseColor color:
import { parseColor } from '@leverui/components';
const color = parseColor('#ff0000');
console.log(color.toString('hex')); // "#ff0000"
console.log(color.toString('rgba')); // "rgba(255, 0, 0, 1)"
Props
Root
| Prop | Default | Type | Description |
|---|---|---|---|
defaultFormat | 'rgba' | ColorFormat | Starting color format. |
defaultValue | #000000 | Color | Starting color. |
openAutoFocus | true | boolean | Move focus into the picker when it opens. |
size | md | '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | Picker size. Responsive. |
variant | outline | 'outline' | 'subtle' | Picker style. Responsive. |
as | div | Native tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the picker's props. |
unstyled | — | boolean | Turn off the Lever UI styles. |
closeOnSelect | false | boolean | Close when the user picks a swatch. |
defaultOpen | — | boolean | Start open. |
disabled | — | boolean | Disable the picker. |
format | — | ColorFormat | Color format, when you control it. |
id | — | string | Unique id for the picker. |
ids | — | Element ID map | Ids of the elements inside the picker. |
immediate | — | boolean | Whether present changes apply right away or are deferred. |
initialFocusEl | — | () => HTMLElement | null | Returns the element to focus on open. |
inline | — | boolean | Show the picker inline. |
invalid | — | boolean | Mark the picker invalid. |
lazyMount | false | boolean | Render the content only once the picker first opens. |
name | — | string | Form input name. |
onExitComplete | — | VoidFunction | Called when the closing animation ends. |
onFocusOutside | — | (event: FocusOutsideEvent) => void | Called when focus moves outside the picker. |
onFormatChange | — | (details: FormatChangeDetails) => void | Called when the color format changes. |
onInteractOutside | — | (event: InteractOutsideEvent) => void | Called on interaction outside the picker. |
onOpenChange | — | (details: OpenChangeDetails) => void | Called when the picker opens or closes. |
onPointerDownOutside | — | (event: PointerDownOutsideEvent) => void | Called on pointer down outside the picker. |
onValueChange | — | (details: ValueChangeDetails) => void | Called on every value change, including during a drag. |
onValueChangeEnd | — | (details: ValueChangeDetails) => void | Called when the user stops dragging. |
open | — | boolean | Open state, when you control it. |
positioning | — | PositioningOptions | Popup placement options. |
present | — | boolean | Whether the content is present, when you control it. |
readOnly | — | boolean | Make the picker read-only. |
required | — | boolean | Mark the picker required. |
skipAnimationOnMount | false | boolean | Skip the animation on the first render. |
unmountOnExit | false | boolean | Remove the content from the DOM on close. |
value | — | Color | Color value, when you control it. |
To hold the state yourself, create it with ColorPicker.useColorPicker and
pass the returned accessor to ColorPicker.RootProvider.
ColorPicker.PropsProvider sets reactive default props for the roots inside
it. ColorPicker.Context and useColorPickerContext read the nearest color
picker's state.
With Root and RootProvider, size and variant also reach content in a
portal.
getColorChannels(format) returns the editable channel names for rgba,
hsla, hsba, or hexa.
ColorPicker.EyeDropper uses the browser's eye dropper API when there is one.
Give its icon button an accessible name, such as aria-label.
Customize with CSS
Part classes: .leverui-color-picker__<slot>. Sliders uses
.leverui-color-picker__sliders. Variables:
--leverui-color-picker-root-gap--leverui-color-picker-trigger-heightand--leverui-color-picker-focus-color--leverui-color-picker-content-width,--leverui-color-picker-content-background, and--leverui-color-picker-z-index--leverui-color-picker-area-height,--leverui-color-picker-thumb-size, and--leverui-color-picker-slider-height--leverui-color-picker-swatch-size
Inline styles set the live gradients, thumb positions, and swatch colors. A
style you pass is merged with them.
Explorer
Click a part name to highlight it in the preview.
Component Anatomy
Hover to highlight, click to select parts