Color Picker
<sl-color-picker> | SlColorPicker
Color pickers allow the user to select a color.
<sl-color-picker label="Select a color"></sl-color-picker>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/sl-color-picker';
const App = () => <SlColorPicker label="Select a color" />;
This component works with standard <form>
elements. Please refer to the section on
form controls to learn more about form submission and
client-side validation.
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>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/sl-color-picker';
const App = () => <SlColorPicker 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>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/sl-color-picker';
const App = () => <SlColorPicker opacity 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>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/sl-color-picker';
const App = () => (
<>
<SlColorPicker format="hex" value="#4a90e2" />
<SlColorPicker format="rgb" value="rgb(80, 227, 194)" />
<SlColorPicker format="hsl" value="hsl(290, 87%, 47%)" />
<SlColorPicker format="hsv" value="hsv(55, 89%, 97%)" />
</>
);
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>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/sl-color-picker';
const App = () => (
<SlColorPicker
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>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/sl-color-picker';
const App = () => (
<>
<SlColorPicker size="small" label="Select a color" />
<SlColorPicker size="medium" label="Select a color" />
<SlColorPicker 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>
import SlColorPicker from '@shoelace-style/shoelace/dist/react/sl-color-picker';
const App = () => <SlColorPicker inline label="Select a color" />;
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component from the CDN using a script tag:
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.6.0/cdn/components/color-picker/color-picker.js"></script>
To import this component from the CDN using a JavaScript import:
import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.6.0/cdn/components/color-picker/color-picker.js';
To import this component using a bundler:
import '@shoelace-style/shoelace/dist/components/color-picker/color-picker.js';
To import this component as a React component:
import SlColorPicker from '@shoelace-style/shoelace/dist/react/color-picker';
Slots
Name | Description |
---|---|
label
|
The color picker’s form label. Alternatively, you can use the label attribute. |
Learn more about using slots.
Properties
Name | Description | Reflects | Type | Default |
---|---|---|---|---|
value
|
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.
|
string
|
''
|
|
defaultValue
|
The default value of the form control. Primarily used for resetting the form control. |
string
|
''
|
|
label
|
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.
|
string
|
''
|
|
format
|
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. |
'hex' | 'rgb' | 'hsl' | 'hsv'
|
'hex'
|
|
inline
|
Renders the color picker inline rather than in a dropdown. |
|
boolean
|
false
|
size
|
Determines the size of the color picker’s trigger. This has no effect on inline color pickers. |
|
'small' | 'medium' | 'large'
|
'medium'
|
noFormatToggle
no-format-toggle
|
Removes the button that lets users toggle between format. |
boolean
|
false
|
|
name
|
The name of the form control, submitted as a name/value pair with form data. |
string
|
''
|
|
disabled
|
Disables the color picker. |
|
boolean
|
false
|
hoist
|
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.
|
boolean
|
false
|
|
opacity
|
Shows the opacity slider. Enabling this will cause the formatted value to be HEXA, RGBA, or HSLA. |
boolean
|
false
|
|
uppercase
|
By default, values are lowercase. With this attribute, values will be uppercase instead. |
boolean
|
false
|
|
swatches
|
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.
|
string | string[]
|
''
|
|
form
|
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.
|
|
string
|
''
|
required
|
Makes the color picker a required field. |
|
boolean
|
false
|
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.
Events
Name | React Event | Description | Event Detail |
---|---|---|---|
sl-blur |
onSlBlur |
Emitted when the color picker loses focus. | - |
sl-change |
onSlChange |
Emitted when the color picker’s value changes. | - |
sl-focus |
onSlFocus |
Emitted when the color picker receives focus. | - |
sl-input |
onSlInput |
Emitted when the color picker receives input. | - |
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 | Description | Arguments |
---|---|---|
focus() |
Sets focus on the color picker. |
options: FocusOptions
|
blur() |
Removes focus from the color picker. | - |
getFormattedValue() |
Returns the current value as a string in the specified format. |
format: 'hex' | 'hexa' | 'rgb' | 'rgba' | 'hsl' | 'hsla' | 'hsv' | 'hsva'
|
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() |
Sets a custom validation message. Pass an empty string to restore validity. |
message: string
|
Learn more about methods.
Custom Properties
Name | Description | Default |
---|---|---|
--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.
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>