Color Picker

Lets the user choose a color with a color area, sliders, swatches, or a typed value.

Usage

Code
import { ColorPicker } from '@leverui/components';
Code
<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

Code
<ColorPicker.ChannelSlider />

Same as:

Code
<ColorPicker.ChannelSlider>
  <ColorPicker.TransparencyGrid />
  <ColorPicker.ChannelSliderTrack />
  <ColorPicker.ChannelSliderThumb />
</ColorPicker.ChannelSlider>

ColorPicker.Sliders

Same as:

Code
<Stack>
  <ColorPicker.ChannelSlider channel="hue" />
  <ColorPicker.ChannelSlider channel="alpha" />
</Stack>

ColorPicker.Area

Code
<ColorPicker.Area>
  <ColorPicker.AreaThumb />
  <ColorPicker.AreaBackground />
</ColorPicker.Area>

Same as:

Code
<ColorPicker.Area />

ColorPicker.EyeDropper

Same as:

Code
<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: #EB5E41

Channel 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.

Code
<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:

Code
const picker = useColorPicker(); // or useColorPickerContext()
const hexValue = picker().value.toString('hex'); // "#ff0000"

From a parseColor color:

Code
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

PropDefaultTypeDescription
defaultFormat'rgba'ColorFormatStarting color format.
defaultValue#000000ColorStarting color.
openAutoFocustruebooleanMove focus into the picker when it opens.
sizemd'2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'Picker size. Responsive.
variantoutline'outline' | 'subtle'Picker style. Responsive.
asdivNative tagHTML tag to render.
asChild—Solid render functionRender your own element with the picker's props.
unstyled—booleanTurn off the Lever UI styles.
closeOnSelectfalsebooleanClose when the user picks a swatch.
defaultOpen—booleanStart open.
disabled—booleanDisable the picker.
format—ColorFormatColor format, when you control it.
id—stringUnique id for the picker.
ids—Element ID mapIds of the elements inside the picker.
immediate—booleanWhether present changes apply right away or are deferred.
initialFocusEl—() => HTMLElement | nullReturns the element to focus on open.
inline—booleanShow the picker inline.
invalid—booleanMark the picker invalid.
lazyMountfalsebooleanRender the content only once the picker first opens.
name—stringForm input name.
onExitComplete—VoidFunctionCalled when the closing animation ends.
onFocusOutside—(event: FocusOutsideEvent) => voidCalled when focus moves outside the picker.
onFormatChange—(details: FormatChangeDetails) => voidCalled when the color format changes.
onInteractOutside—(event: InteractOutsideEvent) => voidCalled on interaction outside the picker.
onOpenChange—(details: OpenChangeDetails) => voidCalled when the picker opens or closes.
onPointerDownOutside—(event: PointerDownOutsideEvent) => voidCalled on pointer down outside the picker.
onValueChange—(details: ValueChangeDetails) => voidCalled on every value change, including during a drag.
onValueChangeEnd—(details: ValueChangeDetails) => voidCalled when the user stops dragging.
open—booleanOpen state, when you control it.
positioning—PositioningOptionsPopup placement options.
present—booleanWhether the content is present, when you control it.
readOnly—booleanMake the picker read-only.
required—booleanMark the picker required.
skipAnimationOnMountfalsebooleanSkip the animation on the first render.
unmountOnExitfalsebooleanRemove the content from the DOM on close.
value—ColorColor 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-height and --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