Skip to main content
Since Shoelace 2.0 Code stable Pattern Tentative Figma Needed

Color Picker

sl-color-picker

Color pickers allow the user to select a color.

<sl-color-picker label="Select a color"></sl-color-picker>
sl-color-picker label="Select a color"

Examples

Initial Value

Use the value attribute to set an initial value for the color picker.

<sl-color-picker value="#4a90e2" label="Select a color"></sl-color-picker>
sl-color-picker value="#4a90e2" label="Select a color"

Opacity

Use the opacity attribute to enable the opacity slider. When this is enabled, the value will be displayed as HEXA, RGBA, HSLA, or HSVA based on format.

<sl-color-picker value="#f5a623ff" opacity label="Select a color"></sl-color-picker>
sl-color-picker value="#f5a623ff" opacity=true label="Select a color"

Formats

Set the color picker’s format with the format attribute. Valid options include hex, rgb, hsl, and hsv. Note that the color picker’s input will accept any parsable format (including CSS color names) regardless of this option.

To prevent users from toggling the format themselves, add the no-format-toggle attribute.

<sl-color-picker format="hex" value="#4a90e2" label="Select a color"></sl-color-picker>
<sl-color-picker format="rgb" value="rgb(80, 227, 194)" label="Select a color"></sl-color-picker>
<sl-color-picker format="hsl" value="hsl(290, 87%, 47%)" label="Select a color"></sl-color-picker>
<sl-color-picker format="hsv" value="hsv(55, 89%, 97%)" label="Select a color"></sl-color-picker>
sl-color-picker format="hex" value="#4a90e2" label="Select a color"
sl-color-picker format="rgb" value="rgb(80, 227, 194)" label="Select a color"
sl-color-picker format="hsl" value="hsl(290, 87%, 47%)" label="Select a color"
sl-color-picker format="hsv" value="hsv(55, 89%, 97%)" label="Select a color"

Swatches

Use the swatches attribute to add convenient presets to the color picker. Any format the color picker can parse is acceptable (including CSS color names), but each value must be separated by a semicolon (;). Alternatively, you can pass an array of color values to this property using JavaScript.

<sl-color-picker
  label="Select a color"
  swatches="
    #d0021b; #f5a623; #f8e71c; #8b572a; #7ed321; #417505; #bd10e0; #9013fe;
    #4a90e2; #50e3c2; #b8e986; #000; #444; #888; #ccc; #fff;
  "
></sl-color-picker>
sl-color-picker label="Select a color" swatches=` #d0021b; #f5a623; #f8e71c; #8b572a; #7ed321; #417505; #bd10e0; #9013fe; #4a90e2; #50e3c2; #b8e986; #000; #444; #888; #ccc; #fff; `

Sizes

Use the size attribute to change the color picker’s trigger size.

<sl-color-picker size="small" label="Select a color"></sl-color-picker>
<sl-color-picker size="medium" label="Select a color"></sl-color-picker>
<sl-color-picker size="large" label="Select a color"></sl-color-picker>
sl-color-picker size="small" label="Select a color"
sl-color-picker size="medium" label="Select a color"
sl-color-picker size="large" label="Select a color"

Inline

The color picker can be rendered inline instead of in a dropdown using the inline attribute.

<sl-color-picker inline label="Select a color"></sl-color-picker>
sl-color-picker inline=true label="Select a color"

Component Props

Property Default Details
value ''

string

The current value of the color picker. The value’s format will vary based the format attribute. To get the value in a specific format, use the getFormattedValue() method. The value is submitted as a name/value pair with form data.

defaultValue ''

string

The default value of the form control. Primarily used for resetting the form control.

label ''

string

The color picker’s label. This will not be displayed, but it will be announced by assistive devices. If you need to display HTML, you can use the label slot` instead.

format 'hex'

'hex' | 'rgb' | 'hsl' | 'hsv'

The format to use. If opacity is enabled, these will translate to HEXA, RGBA, HSLA, and HSVA respectively. The color picker will accept user input in any format (including CSS color names) and convert it to the desired format.

inline false

boolean

Renders the color picker inline rather than in a dropdown.

size 'medium'

'small' | 'medium' | 'large'

Determines the size of the color picker’s trigger. This has no effect on inline color pickers.

noFormatToggle
no-format-toggle
false

boolean

Removes the button that lets users toggle between format.

name ''

string

The name of the form control, submitted as a name/value pair with form data.

disabled false

boolean

Disables the color picker.

hoist false

boolean

Enable this option to prevent the panel from being clipped when the component is placed inside a container with overflow: auto|scroll. Hoisting uses a fixed positioning strategy that works in many, but not all, scenarios.

opacity false

boolean

Shows the opacity slider. Enabling this will cause the formatted value to be HEXA, RGBA, or HSLA.

uppercase false

boolean

By default, values are lowercase. With this attribute, values will be uppercase instead.

swatches ''

string | string[]

One or more predefined color swatches to display as presets in the color picker. Can include any format the color picker can parse, including HEX(A), RGB(A), HSL(A), HSV(A), and CSS color names. Each color must be separated by a semicolon (;). Alternatively, you can pass an array of color values to this property using JavaScript.

form ''

string

By default, form controls are associated with the nearest containing <form> element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work.

required false

boolean

Makes the color picker a required field.

validity

Gets the validity state object

validationMessage

Gets the validation message

updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Slots

Name Details
label The color picker’s form label. Alternatively, you can use the label attribute.

Learn more about using slots.

Events

Name Name Name React Event Details
sl-blur sl-blur sl-blur onSlBlur

Emitted when the color picker loses focus.

sl-change sl-change sl-change onSlChange

Emitted when the color picker’s value changes.

sl-focus sl-focus sl-focus onSlFocus

Emitted when the color picker receives focus.

sl-input sl-input sl-input onSlInput

Emitted when the color picker receives input.

sl-invalid sl-invalid sl-invalid onSlInvalid

Emitted when the form control has been checked for validity and its constraints aren’t satisfied.

Learn more about events.

Methods

Name Details
focus()

options: FocusOptions

Sets focus on the color picker.

blur()

Removes focus from the color picker.

getFormattedValue()

format: 'hex' | 'hexa' | 'rgb' | 'rgba' | 'hsl' | 'hsla' | 'hsv' | 'hsva'

Returns the current value as a string in the specified format.

checkValidity()

Checks for validity but does not show a validation message. Returns true when valid and false when invalid.

getForm()

Gets the associated form, if one exists.

reportValidity()

Checks for validity and shows the browser’s validation message if the control is invalid.

setCustomValidity()

message: string

Sets a custom validation message. Pass an empty string to restore validity.

Learn more about methods.

Custom Properties

Name Details
--grid-width

The width of the color grid.

--grid-height

The height of the color grid.

--grid-handle-size

The size of the color grid’s handle.

--slider-height

The height of the hue and alpha sliders.

--slider-handle-size

The diameter of the slider’s handle.

--swatch-size

The size of each predefined color swatch.

Learn more about customizing CSS custom properties.

CSS Parts

Name Description
base The component’s base wrapper.
trigger The color picker’s dropdown trigger.
swatches The container that holds the swatches.
swatch Each individual swatch.
grid The color grid.
grid-handle The color grid’s handle.
slider Hue and opacity sliders.
slider-handle Hue and opacity slider handles.
hue-slider The hue slider.
hue-slider-handle The hue slider’s handle.
opacity-slider The opacity slider.
opacity-slider-handle The opacity slider’s handle.
preview The preview color.
input The text input.
eye-dropper-button The eye dropper button.
eye-dropper-button__base The eye dropper button’s exported button part.
eye-dropper-button__prefix The eye dropper button’s exported prefix part.
eye-dropper-button__label The eye dropper button’s exported label part.
eye-dropper-button__suffix The eye dropper button’s exported suffix part.
eye-dropper-button__caret The eye dropper button’s exported caret part.
format-button The format button.
format-button__base The format button’s exported button part.
format-button__prefix The format button’s exported prefix part.
format-button__label The format button’s exported label part.
format-button__suffix The format button’s exported suffix part.
format-button__caret The format button’s exported caret part.

Learn more about customizing CSS parts.

Dependencies

This component automatically imports the following dependencies.

  • <sl-button>
  • <sl-button-group>
  • <sl-dropdown>
  • <sl-icon>
  • <sl-input>
  • <sl-popup>
  • <sl-spinner>
  • <sl-visually-hidden>