Color Picker
Color pickers let users choose a color from a visual palette or by entering a value. They support HEX, RGB, HSL, and HSV formats with optional alpha channel and swatch presets.
This component works with standard <form> elements. See form controls for form submission and client-side validation.
Examples
Label
Use the label attribute to give the color picker an accessible label. For labels that contain HTML, use the label slot instead.
Hint
Add a descriptive hint with the hint attribute. For hints that contain HTML, use the hint slot instead.
Initial Value
Use the value attribute to set a starting color. The value's format follows the format attribute, but any parsable color (including CSS color names) is accepted as input.
Format
Set the color picker's format with the format attribute. Valid options are hex, rgb, hsl, and hsv. The input still accepts any parsable format regardless of this setting.
To stop people from cycling through formats themselves, add the without-format-toggle attribute.
Opacity
Use the opacity attribute to add an opacity slider. With opacity enabled, the value is shown as HEXA, RGBA, HSLA, or HSVA to match the format.
Uppercase Values
By default, values are lowercase. Add the uppercase attribute to display them in uppercase instead.
Swatches
Use the swatches attribute to offer preset colors. Any format the picker can parse works (including CSS color names), and each value must be separated by a semicolon (;).
To give swatches accessible names, set the swatches property in JavaScript to an array of { color, label } objects — each label becomes that swatch's accessible name instead of the raw color value. (Their appearance is identical, so this isn't a live example.)
const picker = document.querySelector('wa-color-picker'); picker.swatches = [ { color: '#d0021b', label: 'Red' }, { color: '#f5a623', label: 'Orange' }, { color: '#417505', label: 'Green' }, { color: '#4a90e2', label: 'Blue' }, ];
Placement
Set the placement attribute to control where the dropdown opens. The actual position may shift to keep the panel inside the viewport.
Size
Use the size attribute to change the color picker's trigger size.
Disabled
Use the disabled attribute to disable a color picker.
Applying the Selected Color
The color picker emits an input event as the user picks, so you can apply the chosen color to your UI in real time. Here, changing the color themes a preview card.
Pick a color to theme this card in real time.
API
Importing
If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.
Import this component directly from the CDN:
import 'https://ka-f.webawesome.com/webawesome@3.10.0/components/color-picker/color-picker.js';
After installing Web Awesome via npm, import this component:
import '@awesome.me/webawesome/dist/components/color-picker/color-picker.js';
If you're self-hosting Web Awesome, import this component from your server:
import './webawesome/dist/components/color-picker/color-picker.js';
To import this component for React 18 or below, use the following code:
import WaColorPicker from '@awesome.me/webawesome/dist/react/color-picker/index.js';
Slots
Learn more about using slots.
| Name | Description |
|---|---|
hint
|
The color picker's form hint. Alternatively, you can use the hint attribute. |
label
|
The color picker's form label. Alternatively, you can use the label attribute. |
Attributes & Properties
Learn more about attributes and properties.
| Name | Description | Reflects |
|---|---|---|
defaultValuevalue |
The default value of the form control. Primarily used for resetting the form control.
Type
string | null
|
|
disableddisabled |
Disables the color picker.
Type
boolean
Default
false
|
|
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
HTMLFormElement | null
|
|
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'
|
|
hinthint |
The color picker's hint. If you need to display HTML, use the
hint slot instead.
Type
string
Default
''
|
|
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
string
Default
''
|
|
namename |
The name of the form control, submitted as a name/value pair with form data.
Type
string | null
Default
null
|
|
opacityopacity |
Shows the opacity slider. Enabling this will cause the formatted value to be HEXA, RGBA, or HSLA.
Type
boolean
Default
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
boolean
Default
false
|
|
placementplacement |
The preferred placement of the color picker's popup. Note that the actual placement will vary as configured to
keep the panel inside of the viewport.
Type
'top'
| 'top-start'
| 'top-end'
| 'bottom'
| 'bottom-start'
| 'bottom-end'
| 'right'
| 'right-start'
| 'right-end'
| 'left'
| 'left-start'
| 'left-end'
Default
'bottom-start'
|
|
requiredrequired |
Makes the color picker a required field.
Type
boolean
Default
false
|
|
sizesize |
Determines the size of the color picker's trigger
Type
'xs' | 's' | 'm' | 'l' | 'xl' | 'small' | 'medium' | 'large'
Default
'm'
|
|
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 or an array of { color, label } objects to
this property using JavaScript. When using objects with labels, the label will be used for the swatch's accessible
name instead of the raw color value.
Type
string | string[] | WaColorPickerSwatch[]
Default
''
|
|
uppercaseuppercase |
By default, values are lowercase. With this attribute, values will be uppercase instead.
Type
boolean
Default
false
|
|
validationTarget |
Override this to change where constraint validation popups are anchored.
Type
undefined | HTMLElement
|
|
validators |
Validators are static because they have
observedAttributes, essentially attributes to "watch"
for changes. Whenever these attributes change, we want to be notified and update the validator.
Type
Validator[]
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. |
|
withHintwith-hint |
Only required for SSR. Set to
true if you're slotting in a hint element so the server-rendered markup
includes the hint before the component hydrates on the client.
Type
boolean
Default
false
|
|
withLabelwith-label |
Only required for SSR. Set to
true if you're slotting in a label element so the server-rendered markup
includes the label before the component hydrates on the client.
Type
boolean
Default
false
|
|
withoutFormatTogglewithout-format-toggle |
Removes the button that lets users toggle between format.
Type
boolean
Default
false
|
Methods
Learn more about methods.
| Name | Description | Arguments |
|---|---|---|
blur() |
Removes focus from the color picker. | |
focus() |
Sets focus on the color picker. |
options: FocusOptions
|
formStateRestoreCallback() |
Called when the browser is trying to restore element’s state to state in which case reason is "restore", or when the browser is trying to fulfill autofill on behalf of user in which case reason is "autocomplete". In the case of "restore", state is a string, File, or FormData object previously set as the second argument to setFormValue. |
state: string | File | FormData | null,
reason: 'autocomplete' | 'restore'
|
getFormattedValue() |
Returns the current value as a string in the specified format. |
format: 'hex' | 'hexa' | 'rgb' | 'rgba' | 'hsl' | 'hsla' | 'hsv' | 'hsva'
|
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:
|
hide() |
Hides the color picker panel | |
reportValidity() |
Checks for validity and shows the browser's validation message if the control is invalid. | |
resetValidity() |
Reset validity is a way of removing manual custom errors and native validation. | |
setCustomValidity() |
Do not use this when creating a "Validator". This is intended for end users of components. We track manually defined custom errors so we don't clear them on accident in our validators. |
message: string
|
show() |
Shows the color picker panel. |
Events
Learn more about events.
| Name | Description |
|---|---|
blur |
Emitted when the color picker loses focus. |
change |
Emitted when the color picker's value changes. |
focus |
Emitted when the color picker receives focus. |
input |
Emitted when the color picker receives input. |
wa-after-hide |
|
wa-after-show |
|
wa-hide |
|
wa-invalid |
Emitted when the form control has been checked for validity and its constraints aren't satisfied. |
wa-show |
CSS Custom Properties
Learn more about CSS custom properties.
| Name | Description |
|---|---|
--grid-handle-size |
The size of the color grid's handle.
|
--grid-height |
The height of the color grid.
|
--grid-width |
The width of the color grid.
|
--slider-handle-size |
The diameter of the slider's handle.
|
--slider-height |
The height of the hue and alpha sliders.
|
CSS Parts
Learn more about CSS parts.
| Name | Description | CSS selector |
|---|---|---|
base |
The component's base wrapper. |
::part(base)
|
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__caret |
The eye dropper button's exported caret part. |
::part(eyedropper-button__caret)
|
eyedropper-button__end |
The eye dropper button's exported end part. |
::part(eyedropper-button__end)
|
eyedropper-button__label |
The eye dropper button's exported label part. |
::part(eyedropper-button__label)
|
eyedropper-button__start |
The eye dropper button's exported start part. |
::part(eyedropper-button__start)
|
format-button |
The format button. |
::part(format-button)
|
format-button__base |
The format button's exported button part. |
::part(format-button__base)
|
format-button__caret |
The format button's exported caret part. |
::part(format-button__caret)
|
format-button__end |
The format button's exported end part. |
::part(format-button__end)
|
format-button__label |
The format button's exported label part. |
::part(format-button__label)
|
format-button__start |
The format button's exported start part. |
::part(format-button__start)
|
grid |
The color grid. |
::part(grid)
|
grid-handle |
The color grid's handle. |
::part(grid-handle)
|
hue-slider |
The hue slider. |
::part(hue-slider)
|
hue-slider-handle |
The hue slider's handle. |
::part(hue-slider-handle)
|
input |
The text input. |
::part(input)
|
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)
|
slider |
Hue and opacity sliders. |
::part(slider)
|
slider-handle |
Hue and opacity slider handles. |
::part(slider-handle)
|
swatch |
Each individual swatch. |
::part(swatch)
|
swatches |
The container that holds the swatches. |
::part(swatches)
|
trigger |
The color picker's dropdown trigger. |
::part(trigger)
|
Dependencies
This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.