3.0.0-alpha.4 Alpha
Light Dark Light Dark System Search /

Color Picker

<wa-color-picker> Since 2.0 stable

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

Initial Value

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

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

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.

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

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.

<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

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

<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>

Disabled

The color picker can be rendered as disabled.

<wa-color-picker disabled label="Select a color"></wa-color-picker>

Help Text

Add descriptive help text to a color picker with the help-text attribute. For help texts that contain HTML, use the help-text slot instead.

<wa-color-picker label="Select a color" help-text="Choose a color with appropate contrast!"></wa-color-picker>

Slots

Name Description
label The color picker's form label. Alternatively, you can use the label attribute.
help-text The color picker's form help text. Alternatively, you can use the helpText attribute.

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 null | string
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 ''
helpText
help-text
The color picker's help text. If you need to display HTML, use the help-text 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'
Default 'medium'
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

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.

Events

Name Description
wa-blur Emitted when the color picker loses focus.
wa-change Emitted when the color picker's value changes.
wa-focus Emitted when the color picker receives focus.
wa-input Emitted when the color picker receives input.
wa-invalid Emitted when the form control has been checked for validity and its constraints aren't satisfied.

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.

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.

Dependencies

This component automatically imports the following elements. Subdependencies, if any exist, will also be included in this list.

Importing

The autoloader is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.

CDN npm React

To manually import this component from the CDN, use the following code.

import 'https://early.webawesome.com/webawesome@3.0.0-alpha.4/dist/components/color-picker/color-picker.js';
Coming soon! Coming soon!
    No results