Data inputExtension

Color Picker

Choose a color in one field, from a popup with a color area, hue slider and swatches, or type a hex value such as #2563EB.

Class nameTypeDescription
color-pickerplf:color-pickerComponentWraps the native input and the controls
color-picker-previewplf:color-picker-previewPartThe current color in the trigger
color-picker-panelplf:color-picker-panelPartThe Popover panel
color-picker-editorplf:color-picker-editorPartThe color area and hue slider's box the script adds, or an empty one the page places
color-picker-swatchesplf:color-picker-swatchesPartThe swatch grid
color-picker-swatchplf:color-picker-swatchPartA swatch button
color-picker-footerplf:color-picker-footerPartThe eyedropper and the buttons
color-picker-errorplf:color-picker-errorPartThe error message the script adds

Default

Give every swatch a hex value and an aria-label such as “Red”, so the color is never its only label.

Disabled

Add disabled to the native input; the text field and preview button follow it.

Live preview

The script shows each applied color from plf:color-picker:change, as a theme editor does.

Accent preview

JavaScript

Color Picker needs JavaScript and is an extension: the Full script and Full CSS leave it out. The Full script (pelatform-ui.min.js) and the extension scripts after it start every component on page load, with no code of your own; with Core and single files, the last line starts them. For markup you add later, see Dynamic content.

<link rel="stylesheet" href="pelatform-ui.min.css">
<link rel="stylesheet" href="extensions/color-picker.min.css">
<link rel="stylesheet" href="icons/icons.min.css">
<script src="pelatform-ui.min.js" defer></script>
<script src="extensions/color-picker.min.js" defer></script>
<link rel="stylesheet" href="pelatform-ui.prefixed.min.css">
<link rel="stylesheet" href="extensions/color-picker.prefixed.min.css">
<link rel="stylesheet" href="icons/icons.prefixed.min.css">
<script src="pelatform-ui.min.js" defer></script>
<script src="extensions/color-picker.min.js" defer></script>

With ESM imports, import ColorPicker from @pelatform/ui/extensions/color-picker and call use(ColorPicker) before init().

Hooks

Attribute Purpose
data-plf-color-picker The color-picker wrapper
<input type="color"> The named native input, which keeps the value and works without the script; its field, the wrapper’s child around it, hides once the text field works
data-plf-popover Holds the field, its first child, and the panel, written hidden. The field holds the preview button, the Popover’s trigger, and the text field: an unnamed, labeled text input, beside the button in one group such as an Input Group. Unless written, the script gives it the placeholder #RRGGBB and no autofill
data-plf-color-picker-editor Optional; on an empty color-picker-editor, places the color area, hue slider and comparison there instead of at the start of the panel
data-plf-area-label, data-plf-hue-label On the wrapper: translated names; Saturation and brightness and Hue by default
<button value="#2563eb"> A swatch: a named button with a hex value in the panel; all swatches share one parent, the swatch grid
data-plf-color-picker-apply The <button type="button"> that commits the draft
data-plf-color-picker-eyedropper Optional <button type="button"> that picks a color from the screen into the draft; hidden where the browser has no EyeDropper API
Method Behavior
PLF.ColorPicker.getOrCreateInstance(element) Returns the instance, creating it for valid markup
PLF.ColorPicker.getInstance(element) Returns the instance or null
instance.getState() Returns {value, draft, open} as lowercase #rrggbb
instance.setValue(value) Sets #rgb or #rrggbb, with or without #; returns false when invalid, unchanged or canceled
instance.open(), instance.close() Open or close the popup; return false when nothing changes
instance.destroy() Removes the generated controls and error, restores the native field, and leaves a Popover instance it did not create
Event When
plf:color-picker:before-change Before a new value; cancelable
plf:color-picker:change After the new value is kept

Each event’s detail:

Property Value
instance The Color Picker instance
reason trigger or api
trigger The control that set the color, such as a swatch, the apply button or the text field, or null from a method
previousValue The color before, as lowercase #rrggbb
value The new color, as lowercase #rrggbb

The native input also fires input and change.

Keyboard

Key Action
Alt + Arrow Down In the field, opens the popup with focus on the color area
Enter In the field, applies the typed hex; in the color area or hue slider, applies the draft
Escape In the field, restores the current value; in the popup, closes it and discards the draft
Arrow keys In the color area, change saturation and brightness by 1%, or 10% with Shift; in the swatches, move through the grid
Page Up, Page Down In the color area, change brightness by 10%
Home, End In the color area, set the lowest or highest saturation; in the swatches, move to the first or last
Enter, Space On a swatch, apply it and close the popup
Variable Default
--plf-color-picker-width max-content, the width of the swatch grid
--plf-color-picker-columns 8
--plf-color-picker-swatch-size 24px
--plf-color-picker-area-height 9rem (144px); down to 6rem (96px) in a panel near the edge of a short window

Built with Pelatform UIv0.1.0

Components

Getting started

Foundations

Components

Actions

Data display

Navigation

Feedback

Data input

Layout

Advanced

Integrations

Blocks

Application

Authentication

Icons