<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.
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>
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>
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.
<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>
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>
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>
The color picker can be rendered as disabled.
<wa-color-picker disabled label="Select a color"></wa-color-picker>
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 appropate contrast!"></wa-color-picker>
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. |
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. |
||
defaultValue value |
The default value of the form control. Primarily used for resetting the form control.
Type
string | null |
|
|
label 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.Type
string Default
'' |
||
hint hint |
The color picker's hint. If you need to display HTML, use the
hint slot instead.Type
string Default
'' |
||
format 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.
Type
'hex' | 'rgb' | 'hsl' | 'hsv' Default
'hex' |
||
size size |
Determines the size of the color picker's trigger
Type
'small' | 'medium' | 'large' | 'inherit' Default
'inherit' |
|
|
noFormatToggle no-format-toggle |
Removes the button that lets users toggle between format.
Type
boolean Default
false |
||
name name |
The name of the form control, submitted as a name/value pair with form data.
Type
string | null Default
null |
|
|
disabled disabled |
Disables the color picker.
Type
boolean Default
false |
||
hoist 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.Type
boolean Default
false |
||
opacity opacity |
Shows the opacity slider. Enabling this will cause the formatted value to be HEXA, RGBA, or HSLA.
Type
boolean Default
false |
||
uppercase uppercase |
By default, values are lowercase. With this attribute, values will be uppercase instead.
Type
boolean Default
false |
||
swatches 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.Type
string | string[] Default
'' |
||
form 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.Type
null Default
null |
|
|
required required |
Makes the color picker a required field.
Type
boolean Default
false |
|
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. |
Learn more about events.
Name | Description |
---|---|
change |
Emitted when the color picker's value changes. |
input |
Emitted when the color picker receives input. |
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. |
Learn more about CSS custom properties.
Name | Description |
---|---|
--background-color |
The color picker's background color.
|
--border-color |
The color of the color picker's borders.
|
--border-radius |
The radius of the color picker's corners.
|
--border-style |
The style of the color picker's borders.
|
--border-width |
The width of the color picker's borders.
|
--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.
|
--preview-size |
The size of the preview color.
|
--preview-border-radius |
The corners of the preview color.
|
--slider-height |
The height of the hue and alpha sliders.
|
--slider-handle-size |
The diameter of the slider's handle.
|
--spacing |
The amount of space around and between the color picker's controls.
|
--swatch-border-radius |
The corners of each predefined color swatch.
|
--swatch-size |
The size of each predefined color swatch.
|
--trigger-border-radius |
The corners of the color picker's dropdown trigger.
|
Learn more about 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. |
This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.
<wa-button>
<wa-button-group>
<wa-dropdown>
<wa-icon>
<wa-input>
<wa-popup>
<wa-spinner>
<wa-visually-hidden>
The autoloader is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.
To manually import this component from the CDN, use the following code.
import 'https://early.webawesome.com/webawesome@3.0.0-alpha.10/dist/components/color-picker/color-picker.js';