Skip to content

Color Picker

<quiet-color-picker> stable since 1.0

Color picker provides a customizable interface for selecting a color using a two-dimensional slider for luminosity and saturation and standard sliders for hue and opacity.

<quiet-color-picker 
  label="Select a color"
  value="#7578c5" 
  with-input
  with-alpha
  with-eye-dropper
  swatches="
    #09090b #71717a #ef4444 #f97316 
    #f59e0b #eab308 #84cc16 #22c55e 
    #10b981 #14b8a6 #06b6d4 #3b82f6 
    #6366f1 #a855f7 #d946ef #ec4899
  "
></quiet-color-picker>

This component is a primitive for enabling color selection. It is not a form control, so it will not submit a value to a form. See color input for a color picker suitable for using with forms.

Examples Jump to heading

Labels Jump to heading

Use the label attribute to provide an accessible label for the color picker. This won't be shown, but it will be read to assistive devices.

<quiet-color-picker label="Select a color"></quiet-color-picker>

Setting an initial value Jump to heading

Use the value attribute to provide an initial value for the color picker. You can specify values in a variety of formats as well as CSS colors names. Parsing of color values is very permissive. See the TinyColor docs for more details on acceptable color formats.

<quiet-color-picker 
  label="Select a color"
  value="#71e0f3"
>
</quiet-color-picker>

Enabling alpha Jump to heading

Add the with-alpha attribute to allow the user to adjust opacity.

<quiet-color-picker 
  label="Select a color"
  value="#ce238088" 
  with-alpha
></quiet-color-picker>

Enabling the eye dropper Jump to heading

Add the with-eye-dropper attribute to show the eye dropper button, which allows the user to select a color from anywhere on the screen.

<quiet-color-picker 
  label="Select a color"
  with-eye-dropper
></quiet-color-picker>

The EyeDropper API is only available in supportive browsers .

Enabling the color input Jump to heading

Add the with-input attribute to show the color input, a text field the user can type into.

<quiet-color-picker 
  label="Select a color"
  value="#4716ce" 
  with-input
></quiet-color-picker>

Showing swatches Jump to heading

Set the swatches attribute to one or more space-delimited hex colors or CSS color names, e.g. lightblue, to show as preset swatches below the color picker. This is useful for providing users with access to recent colors or a predefined color palette.

<quiet-color-picker
  label="Select a color"
  swatches="
    #09090b #71717a #ef4444 #f97316 
    #f59e0b #eab308 #84cc16 #22c55e 
    #10b981 #14b8a6 #06b6d4 #3b82f6 
    #6366f1 #a855f7 #d946ef #ec4899
  "
></quiet-color-picker>

Setting the format Jump to heading

Use the format attribute to set the format of the value. Valid options include hex (default), rgb, and hsl.

<quiet-color-picker 
  label="Select a color"
  value="#ffcc00"
  format="rgb"
  with-input
  id="color-picker__format"
></quiet-color-picker>

<script>
  const colorPicker = document.getElementById('color-picker__format');

  // This will output an RGB string when the color changes
  colorPicker.addEventListener('quiet-change', () => {
    console.log(colorPicker.value);
  });
</script>

You can set the color picker's value using any format and it will automatically be converted to the format specified by the format attribute.

Getting the value programmatically Jump to heading

If you need to access the value in a specific format, use the getValueAs() method. You can pass rgb (default), hsl, hex, or hex8 to get a corresponding value.

For rgb, an object with { r, g, b, a } properties will be returned where r, g, and b range from 0-255 and a (alpha) ranges from 0–1. For hsl, an object with { h, s, l, a } properties will be returned where h ranges from 0–360 and s, l, and a range from 0–1. For hex and hex8, a string in the format of #RRGGBB and #RRGGBBAA will be returned, respectively.

<quiet-color-picker 
  label="Select a color"
  value="tomato"
  format="rgb"
  id="color-picker__object"
></quiet-color-picker>

<script>
  const colorPicker = document.getElementById('color-picker__object');

  // Outputs two objects when the color changes
  colorPicker.addEventListener('quiet-change', () => {
    console.log(
      colorPicker.getValueAs('rgb'),
      colorPicker.getValueAs('hex'),
      colorPicker.getValueAs('hex8'),
      colorPicker.getValueAs('hsl')
    );
  });
</script>

Changing the size Jump to heading

Use the size attribute to change the color pickers's size.

<quiet-select label="Select a size" style="max-width: 18rem; margin-block-end: 2rem;">
  <option value="xs">Extra small</option>
  <option value="sm">Small</option>
  <option value="md">Medium</option>
  <option value="lg">Large</option>
  <option value="xl">Extra large</option>
</quiet-select>

<quiet-color-picker 
  size="xs" 
  label="Color picker sizes"
    swatches="
    #09090b #71717a #ef4444 #f97316 
    #f59e0b #eab308 #84cc16 #22c55e 
    #10b981 #14b8a6 #06b6d4 #3b82f6 
    #6366f1 #a855f7 #d946ef #ec4899
  "
  with-input
  with-alpha
  with-eye-dropper
  id="color-picker__size"
></quiet-color-picker>

<script>
  const colorPicker = document.getElementById('color-picker__size');
  const select = colorPicker.previousElementSibling;

  select.addEventListener('quiet-change', () => {
    colorPicker.size = select.value;
  });  
</script>

Disabling Jump to heading

To disable a color picker, add the disabled attribute.

<quiet-color-picker 
  label="Select a color"
  disabled
  value="#6366f1"
  with-alpha 
  with-input
  with-eye-dropper
  swatches="
    #09090b #71717a #ef4444 #f97316 
    #f59e0b #eab308 #84cc16 #22c55e 
    #10b981 #14b8a6 #06b6d4 #3b82f6 
    #6366f1 #a855f7 #d946ef #ec4899  
  "
></quiet-color-picker>

API Jump to heading

Importing Jump to heading

The autoloader is the recommended way to import components but, if you prefer to do it manually, the following code snippets will be helpful.

CDN npm

To manually import <quiet-color-picker> from the CDN, use the following code.

import 'https://cdn.jsdelivr.net/npm/@quietui/quiet-browser@1.0.0/dist/components/color-picker/color-picker.js';

To manually import <quiet-color-picker> from npm, use the following code.

import '@quietui/quiet/dist/components/color-picker/color-picker.js';

Properties Jump to heading

Color Picker has the following properties that can be set with corresponding attributes. In many cases, the attribute's name is the same as the property's name. If an attribute is different, it will be displayed after the property. Learn more about attributes and properties

Property / Attribute Description Reflects Type Default
label The color picker's label. This won't be shown, but it will be read to assistive devices so you should always include one. string
value The color picker's value. string ''
format The format to use for the color's value. 'hex'
'rgb'
'hsl'
'hex'
disabled Disables the color picker. boolean false
size The color picker's size. 'xs'
'sm'
'md'
'lg'
'xl'
'md'
swatches One or more space-delimited hex colors or CSS color names, e.g. lightblue, to show as preset swatches below the color picker. string ''
withAlpha
with-alpha
Enables the alpha slider. boolean false
withEyeDropper
with-eye-dropper
Enables the eye dropper button. Only available in supportive browsers . boolean false
withInput
with-input
Enables the color value text field. boolean false

Methods Jump to heading

Color Picker supports the following methods. You can obtain a reference to the element and call them like functions in JavaScript. Learn more about methods

Name Description Arguments
focus() Sets focus to the color picker.
blur() Removes focus from the color picker.
getValueAs() Gets the current value as a hex string, a hex8 string, an RGB object, or an HSL object. RBG objects have r, g, and b properties ranging from 0–255 and an a property (representing opacity) that ranges from 0-1. HSL objects have an h property ranging from 0-359 and s, l, and a properties ranging from 0–1. format: 'hex' <br> 'hex8' <br> 'hsl' <br> 'rgb'

Events Jump to heading

Color Picker dispatches the following custom events. You can listen to them the same way was native events. Learn more about custom events

Name Description
change
input
quiet-change Emitted when the user commits changes to the color picker's value.
quiet-input Emitted when the color picker receives input. This can fire very frequently during dragging, so avoid doing expensive operations in the handler. If you don't live feedback, use the quiet-change event instead.

CSS parts Jump to heading

Color Picker exposes internal elements that can be styled with CSS using the selectors shown below. Learn more about CSS parts

Name Description CSS selector
picker The element the wraps the color picker. ::part(picker)
color-slider The 2d color slider. ::part(color-slider)
color-slider-thumb The color slider's thumb. ::part(color-slider-thumb)
controls The container that wraps the sliders and preview. ::part(controls)
sliders The container that wraps the hue and alpha slider. ::part(sliders)
hue-slider The slider that controls the color's hue. ::part(hue-slider)
hue-slider__label The hue slider's label part. ::part(hue-slider__label)
hue-slider__description The hue slider's description part. ::part(hue-slider__description)
hue-slider__slider The hue slider's slider part. ::part(hue-slider__slider)
hue-slider__track The hue slider's track part. ::part(hue-slider__track)
hue-slider__indicator The hue slider's indicator part. ::part(hue-slider__indicator)
hue-slider__thumb The hue slider's thumb part. ::part(hue-slider__thumb)
alpha-slider The slider that controls the color's opacity. ::part(alpha-slider)
alpha-slider__label The alpha slider's label part. ::part(alpha-slider__label)
alpha-slider__description The alpha slider's description part. ::part(alpha-slider__description)
alpha-slider__slider The alpha slider's slider part. ::part(alpha-slider__slider)
alpha-slider__track The alpha slider's track part. ::part(alpha-slider__track)
alpha-slider__indicator The alpha slider's indicator part. ::part(alpha-slider__indicator)
alpha-slider__thumb The alpha slider's thumb part. ::part(alpha-slider__thumb)
eye-dropper-button The eye dropper button, a <quiet-button> element. ::part(eye-dropper-button)
preview-button The button that shows a preview of the current color, a <quiet-button> element. ::part(preview-button)
color-input The color input text field, a <quiet-text-field> element. ::part(color-input)
color-input__visual-box The element that wraps the internal text box. ::part(color-input__visual-box)
color-input__text-box The internal text box, an <input> element. ::part(color-input__text-box)
swatches The element that contains swatches. ::part(swatches)
swatch Each individual swatch. ::part(swatch)

Custom States Jump to heading

Color Picker has the following custom states. You can target them with CSS using the selectors shown below. Learn more about custom states

Name Description CSS selector
disabled Applied when the color picker is disabled. :state(disabled)
focused Applied when the color picker has focus. :state(focused)

Dependencies Jump to heading

Color Picker automatically imports the following elements. Sub-dependencies are also included in this list.

Search this website Toggle dark mode Get the code on GitHub Follow @quietui.org on Bluesky Follow @quiet_ui on X

    No results found