Color Picker
<wa-color-picker>
Color pickers allow the user to select a color.
<wa-color-picker label="Select a color"></wa-color-picker>
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
Jump to heading
Initial Value
Jump to heading
Use the value attribute to set an initial value for the color picker.
<wa-color-picker value="#4a90e2" label="Select a color"></wa-color-picker>
Opacity
Jump to heading
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.
<wa-color-picker value="#f5a623ff" opacity label="Select a color"></wa-color-picker>
Formats
Jump to heading
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 without-format-toggle attribute.
<div class="wa-grid" style="--min-column-size: 12ch;"> <wa-color-picker format="hex" value="#4a90e2" label="Pick a hex color"></wa-color-picker> <wa-color-picker format="rgb" value="rgb(80, 227, 194)" label="Pick an RGB color"></wa-color-picker> <wa-color-picker format="hsl" value="hsl(290, 87%, 47%)" label="Pick an HSL color"></wa-color-picker> <wa-color-picker format="hsv" value="hsv(55, 89%, 97%)" label="Pick an HSV color"></wa-color-picker> </div>
Swatches
Jump to heading
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.
<wa-color-picker label="Select a color" swatches=" #d0021b; #f5a623; #f8e71c; #8b572a; #7ed321; #417505; #bd10e0; #9013fe; #4a90e2; #50e3c2; #b8e986; #000; #444; #888; #ccc; #fff; " ></wa-color-picker>
Sizes
Jump to heading
Use the size attribute to change the color picker's trigger size.
<div class="wa-gap-m wa-align-items-baseline"> <wa-color-picker size="small" label="Select a color"></wa-color-picker> <wa-color-picker size="medium" label="Select a color"></wa-color-picker> <wa-color-picker size="large" label="Select a color"></wa-color-picker> </div>
Disabled
Jump to heading
The color picker can be rendered as disabled.
<wa-color-picker disabled label="Select a color"></wa-color-picker>
Hint
Jump to heading
Add descriptive hint to a color picker with the hint attribute. For hints that contain HTML, use the hint slot instead.
<wa-color-picker label="Select a color" hint="Choose a color with appropriate contrast!"></wa-color-picker>
Slots
Jump to heading
Learn more about using slots.
| Name | Description |
|---|---|
label
|
The color picker's form label. Alternatively, you can use the label attribute. |
hint
|
The color picker's form hint. Alternatively, you can use the hint attribute. |
Attributes & Properties
Jump to heading
Learn more about attributes and properties.
| Name | Description | Reflects | |
|---|---|---|---|
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. |
||
defaultValuevalue |
The default value of the form control. Primarily used for resetting the form control.
Type
string | null |
|
|
labellabel |
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.Type
stringDefault
'' |
||
hinthint |
The color picker's hint. If you need to display HTML, use the
hint slot instead.Type
stringDefault
'' |
||
formatformat |
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.
Type
'hex' | 'rgb' | 'hsl' | 'hsv'Default
'hex' |
||
sizesize |
Determines the size of the color picker's trigger
Type
'small' | 'medium' | 'large'Default
'medium' |
|
|
withoutFormatTogglewithout-format-toggle |
Removes the button that lets users toggle between format.
Type
booleanDefault
false |
||
namename |
The name of the form control, submitted as a name/value pair with form data.
Type
string | nullDefault
null |
|
|
disableddisabled |
Disables the color picker.
Type
booleanDefault
false |
||
openopen |
Indicates whether or not the popup is open. You can toggle this attribute to show and hide the popup, or you
can use the
show() and hide() methods and this attribute will reflect the popup's open state.Type
booleanDefault
false |
|
|
opacityopacity |
Shows the opacity slider. Enabling this will cause the formatted value to be HEXA, RGBA, or HSLA.
Type
booleanDefault
false |
||
uppercaseuppercase |
By default, values are lowercase. With this attribute, values will be uppercase instead.
Type
booleanDefault
false |
||
swatchesswatches |
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.Type
string | string[]Default
'' |
||
formform |
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.Type
nullDefault
null |
|
|
requiredrequired |
Makes the color picker a required field.
Type
booleanDefault
false |
|
Methods
Jump to heading
Learn more about methods.
| Name | Description | Arguments |
|---|---|---|
getHexString() |
Generates a hex string from HSV values. Hue must be 0-360. All other arguments must be 0-100. |
hue: number,
saturation: number,
brightness: number,
alpha:
|
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'
|
reportValidity() |
Checks for validity and shows the browser's validation message if the control is invalid. | |
show() |
Shows the color picker panel. | |
hide() |
Hides the color picker panel |
Events
Jump to heading
Learn more about events.
| Name | Description |
|---|---|
change |
Emitted when the color picker's value changes. |
input |
Emitted when the color picker receives input. |
wa-show |
|
wa-after-show |
|
wa-hide |
|
wa-after-hide |
|
blur |
Emitted when the color picker loses focus. |
focus |
Emitted when the color picker receives focus. |
wa-invalid |
Emitted when the form control has been checked for validity and its constraints aren't satisfied. |
CSS custom properties
Jump to heading
Learn more about CSS custom properties.
| Name | Description |
|---|---|
--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.
|
CSS parts
Jump to heading
Learn more about CSS parts.
| Name | Description | CSS selector |
|---|---|---|
base |
The component's base wrapper. |
::part(base)
|
trigger |
The color picker's dropdown trigger. |
::part(trigger)
|
swatches |
The container that holds the swatches. |
::part(swatches)
|
swatch |
Each individual swatch. |
::part(swatch)
|
grid |
The color grid. |
::part(grid)
|
grid-handle |
The color grid's handle. |
::part(grid-handle)
|
slider |
Hue and opacity sliders. |
::part(slider)
|
slider-handle |
Hue and opacity slider handles. |
::part(slider-handle)
|
hue-slider |
The hue slider. |
::part(hue-slider)
|
hue-slider-handle |
The hue slider's handle. |
::part(hue-slider-handle)
|
opacity-slider |
The opacity slider. |
::part(opacity-slider)
|
opacity-slider-handle |
The opacity slider's handle. |
::part(opacity-slider-handle)
|
preview |
The preview color. |
::part(preview)
|
input |
The text input. |
::part(input)
|
eyedropper-button |
The eye dropper button. |
::part(eyedropper-button)
|
eyedropper-button__base |
The eye dropper button's exported button part. |
::part(eyedropper-button__base)
|
eyedropper-button__start |
The eye dropper button's exported start part. |
::part(eyedropper-button__start)
|
eyedropper-button__label |
The eye dropper button's exported label part. |
::part(eyedropper-button__label)
|
eyedropper-button__end |
The eye dropper button's exported end part. |
::part(eyedropper-button__end)
|
eyedropper-button__caret |
The eye dropper button's exported caret part. |
::part(eyedropper-button__caret)
|
format-button |
The format button. |
::part(format-button)
|
format-button__base |
The format button's exported button part. |
::part(format-button__base)
|
format-button__start |
The format button's exported start part. |
::part(format-button__start)
|
format-button__label |
The format button's exported label part. |
::part(format-button__label)
|
format-button__end |
The format button's exported end part. |
::part(format-button__end)
|
format-button__caret |
The format button's exported caret part. |
::part(format-button__caret)
|
Dependencies
Jump to heading
This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.
Importing
Jump to heading
Autoloading components via projects is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.
Let your project code do the work! Sign up for free to use a project with your very own CDN — it's the fastest and easiest way to use Web Awesome.
To manually import this component from NPM, use the following code.
import '@awesome.me/webawesome/dist/components/color-picker/color-picker.js';
To manually import this component from React, use the following code.
import WaColorPicker from '@awesome.me/webawesome/dist/react/color-picker';