<cs-color-picker>
StableFormsSince 0.1

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('cs-color-picker');
picker.swatches = [
  { color: '#d0021b', label: 'Red' },
  { color: '#f5a623', label: 'Orange' },
  { color: '#417505', label: 'Green' },
  { color: '#4a90e2', label: 'Blue' },
];

To offer a fully transparent option, include the transparent keyword as a swatch. It renders with a checkerboard pattern, and selecting it sets the value to black with zero alpha, e.g. #00000000.

Transparent swatches require the opacity attribute. Without it, the alpha channel is discarded and selecting the swatch yields opaque black.

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.

Brand preview

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 this component, use one of the following snippets.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/color-picker/color-picker.js';
import './cornerstone/components/color-picker/color-picker.js';
import CsColorPicker from '@cruglobal/cornerstone-components/react/color-picker/index.js';

Slots

Learn more about using slots.

NameDescription
hintThe color picker's form hint. Alternatively, you can use the hint attribute.
labelThe color picker's form label. Alternatively, you can use the label attribute.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
defaultValuevalueThe default value of the form control. Primarily used for resetting the form control.string | nullYes
disableddisabledDisables the color picker.booleanfalse
formBy 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.HTMLFormElement | null
formatformatThe 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'
hinthintThe color picker's hint. If you need to display HTML, use the hint slot instead.string''
labellabelThe 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''
namenameThe name of the form control, submitted as a name/value pair with form data.string | nullnullYes
opacityopacityShows the opacity slider. Enabling this will cause the formatted value to be HEXA, RGBA, or HSLA.booleanfalse
openopenIndicates 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.booleanfalseYes
placementplacementThe 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.'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'right' | 'right-start' | 'right-end' | 'left' | 'left-start' | 'left-end''bottom-start'Yes
requiredrequiredMakes the color picker a required field.booleanfalseYes
sizesizeDetermines the size of the color picker's trigger'xs' | 's' | 'm' | 'l' | 'xl''m'Yes
ssrHintssr-hintOnly 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.booleanfalseYes
ssrLabelssr-labelOnly 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.booleanfalseYes
swatchesswatchesOne 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.string | string[] | CsColorPickerSwatch[]''
uppercaseuppercaseBy default, values are lowercase. With this attribute, values will be uppercase instead.booleanfalse
validationTargetOverride this to change where constraint validation popups are anchored.undefined | HTMLElement
validatorsValidators 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.Validator[][]
valueThe 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.
withoutFormatTogglewithout-format-toggleRemoves the button that lets users toggle between format.booleanfalse

Methods

Learn more about methods.

NameDescriptionArguments
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: unknown
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.

NameDescription
blurEmitted when the color picker loses focus.
changeEmitted when the color picker's value changes.
cs-after-hide
cs-after-show
cs-hide
cs-invalidEmitted when the form control has been checked for validity and its constraints aren't satisfied.
cs-show
focusEmitted when the color picker receives focus.
inputEmitted when the color picker receives input.

CSS Custom Properties

Learn more about CSS custom properties.

NameDescriptionDefault
--grid-handle-sizeThe size of the color grid's handle.
--grid-heightThe height of the color grid.
--grid-widthThe width of the color grid.
--slider-handle-sizeThe diameter of the slider's handle.
--slider-heightThe height of the hue and alpha sliders.

CSS Parts

Learn more about CSS parts.

NameDescriptionCSS selector
color-pickerThe dropdown panel that holds the grid, sliders, and swatches.::part(color-picker)
eyedropper-buttonThe eye dropper button.::part(eyedropper-button)
eyedropper-button__buttonThe eye dropper button's exported button part.::part(eyedropper-button__button)
eyedropper-button__caretThe eye dropper button's exported caret part.::part(eyedropper-button__caret)
eyedropper-button__endThe eye dropper button's exported end part.::part(eyedropper-button__end)
eyedropper-button__labelThe eye dropper button's exported label part.::part(eyedropper-button__label)
eyedropper-button__startThe eye dropper button's exported start part.::part(eyedropper-button__start)
form-controlThe form control that wraps the label, input, and hint.::part(form-control)
form-control-inputThe color picker's trigger button.::part(form-control-input)
form-control-labelThe label.::part(form-control-label)
format-buttonThe format button.::part(format-button)
format-button__buttonThe format button's exported button part.::part(format-button__button)
format-button__caretThe format button's exported caret part.::part(format-button__caret)
format-button__endThe format button's exported end part.::part(format-button__end)
format-button__labelThe format button's exported label part.::part(format-button__label)
format-button__startThe format button's exported start part.::part(format-button__start)
gridThe color grid.::part(grid)
grid-handleThe color grid's handle.::part(grid-handle)
hintThe hint's wrapper.::part(hint)
hue-sliderThe hue slider.::part(hue-slider)
hue-slider-handleThe hue slider's handle.::part(hue-slider-handle)
inputThe text input.::part(input)
opacity-sliderThe opacity slider.::part(opacity-slider)
opacity-slider-handleThe opacity slider's handle.::part(opacity-slider-handle)
previewThe preview color.::part(preview)
sliderHue and opacity sliders.::part(slider)
slider-handleHue and opacity slider handles.::part(slider-handle)
swatchEach individual swatch.::part(swatch)
swatchesThe container that holds the swatches.::part(swatches)
triggerThe color picker's dropdown trigger.::part(trigger)
trigger-containerThe container that wraps the color picker's trigger.::part(trigger-container)

Dependencies

This component automatically imports the following elements. Sub-dependencies, if any exist, are included in this list.