The colorpicker
input allows for advanced color selection — including alpha channel support. It supports colors in Hex
, HSLA
and RGBA
formats and can be configured to provide a preset list of colors.
Most importantly, unlike the native color
input, the colorpicker
provides a consistent, predictable, and accessible user experience across browsers and operating systems.
The colorpicker
input is fully keyboard navigable and ships with accessible markup.
The colorpicker
supports formats in Hex
, HSLA
and RGBA
formats. You can set your desired format with the format
prop and color values from the colorpicker
will be returned in that format. Your chosen format will also be the default setting for the input field(s) which show up in the colorpicker
panel.
There may be times where you wish to show one format to users as a default but return a different format. You can use the value-format
prop to specify a return value format independent of the format
prop.
You can disable the alpha channel in the colorpicker
by setting the alpha
prop to false
. This will enforce full opacity on values. Any values with an alpha channel will be converted to full opacity.
Swatches are provided using the options
prop and use the same options
API as the select
, dropdown
, autocomplete
and taglist
inputs from FormKit.
Here is a simple set of swatches using the options
prop and providing it with a flat array of color values. Your provided values can be in any supported color format (Hex
, HSLA
, and RGBA
) and will be automatically converted to the colorpicker
s speficied format
which defaults to Hex
.
The colorpicker
also supports grouped options
. This allows you to create sets of swatches within the color picker grouped under a common heading.
You can create a "swatch-only" color-picker
by using the panel-controls
and panel-format
props and setting them to false
.
When showing only swatches in pop-over mode (the default) adding the close-on-select
prop will close the pop-over when a swatch is selected.
If your provided swatches via the options
prop have label
s then you can call node.input()
with a swatch name and the associated color will be set for you.
The colorpicker
input supports the browser-native EyeDropper API. If the EyeDropper is supported in your browser and you have not disabled the EyeDropper by setting the eye-dropper
prop to false
then you will see an eyedropper button to the right of the hue and alpha sliders.
By default a colorpicker
input will allow the pasting of any valid color values while user focus is anywhere in the input. Values will be converted immediately to your desired format
.
You can disable the ability to paste a value by setting the allow-paste
prop to false
.
Prop | Type | Default | Description |
---|---|---|---|
format | string | hex | The format that the colorpicker input should show in the input preview and be the default setting for the picker panel input. Can be set to hex , hsla or rgba . |
alpha | boolean | true | When set to false the alpha channel will be disabled in the color picker. |
value-format | string | undefined | The format that should be returned from the colorpicker input regardless of the displayed format. If not specified the value of the format prop will be used. |
panel-controls | boolean | true | Used to control the display of the Lightness/Saturation gradient and the Hue and Alpha sliders. |
panel-format | boolean | true | Used to control whether the display of the color input fields and format switcher button. |
eye-dropper | boolean | true | Used to control the display of the eye-dropper control. Requires a browser that supports the EyeDropper API. If enabled in an unsupported browser the control will behave as if set to false . |
inline | boolean | false | When set to true the `colorpicker` will render its panel as an inline element rather than as a pop-over. |
options | Array/Object | [] | An object of value/label pairs or an array of strings, or an array of objects that must contain a label and value property. Supports grouped options via an array of objects that contain group and options keys where options is a nested options definition. Grouping is only supported at one level of depth. |
show-value | boolean | true | Used to control the display of the current color value in the input preview. Uses the format prop to determine which value to display. |
close-on-select | boolean | false | When set to true the colorpicker panel will close when a preset swatch option is chosen. |
allow-paste | boolean | true | Allows pasting any valid color string of Hex , HSLA , or RGBA format into a colorpicker that contains the user's focus. The pasted string will immediately be set as the value of the colorpicker and converted to the desired format. |
popover | boolean | false | Renders the input's UI panel using the browser Popover API. |
Show Universal props | |||
config | Object | {} | Configuration options to provide to the input’s node and any descendent node of this input. |
delay | Number | 20 | Number of milliseconds to debounce an input’s value before the commit hook is dispatched. |
dirtyBehavior | string | touched | Determines how the "dirty" flag of this input is set. Can be set to touched or compare — touched (the default) is more performant, but will not detect when the form is once again matching its initial state. |
errors | Array | [] | Array of strings to show as error messages on this field. |
help | String | '' | Text for help text associated with the input. |
id | String | input_{n} | The unique id of the input. Providing an id also allows the input’s node to be globally accessed. |
ignore | Boolean | false | Prevents an input from being included in any parent (group, list, form etc). Useful when using inputs for UI instead of actual values. |
index | Number | undefined | Allows an input to be inserted at the given index if the parent is a list. If the input’s value is undefined, it inherits the value from that index position. If it has a value it inserts it into the lists’s values at the given index. |
label | String | '' | Text for the label element associated with the input. |
name | String | input_{n} | The name of the input as identified in the data object. This should be unique within a group of fields. |
parent | FormKitNode | contextual | By default the parent is a wrapping group, list or form — but this props allows explicit assignment of the parent node. |
prefix-icon | String | '' | Specifies an icon to put in the prefixIcon section. |
preserve | boolean | false | Preserves the value of the input on a parent group, list, or form when the input unmounts. |
preserve-errors | boolean | false | By default errors set on inputs using setErrors are automatically cleared on input, setting this prop to true maintains the error until it is explicitly cleared. |
sections-schema | Object | {} | An object of section keys and schema partial values, where each schema partial is applied to the respective section. |
suffix-icon | String | '' | Specifies an icon to put in the suffixIcon section. |
type | String | text | The type of input to render from the library. |
validation | String, Array | [] | The validation rules to be applied to the input. |
validation-visibility | String | blur | Determines when to show an input's failing validation rules. Valid values are blur , dirty , and live . |
validation-label | String | {label prop} | Determines what label to use in validation error messages, by default it uses the label prop if available, otherwise it uses the name prop. |
validation-rules | Object | {} | Additional custom validation rules to make available to the validation prop. |
value | Any | undefined | Seeds the initial value of an input and/or its children. Not reactive. Can seed entire groups (forms) and lists.. |
You can target a specific section of an input using that section's "key", allowing you to modify that section's classes, HTML (via :sections-schema
, or content (via slots)). Read more about sections here.
The colorpicker
input's structure changes depending on the existence of the inline
prop:
Section-key | Description |
---|---|
swatchPreview | A wrapping div around the swatch preview canvas element and the displayed color value string. |
canvasSwatchPreview | A canvas element that renders the current color selection. |
valueString | A span element that contains the current color value string in the format set via the format prop. |
panel | The wrapping element for all of the colorpicker controls |
panelClose | A prefix seciton of the panel that contains a close button. Only shown on small touchscreen devices where a 'fixed position' treatment is applied. |
iconClose | An element for outputting an icon within the panelClose section. |
controlGroup | A wrapping element for all of the color control elements within the panel |
preview | A wrapping element for the current color preview. |
canvasPreview | A canvas element that renders the current color selection. |
LS | A wrapping element for the Lightness/Saturation controls |
canvasLS | A canvas element that renders the currently avilable colors given the current hue . |
controlLS | An element styled as the control for the LS canvas. |
hue | A wrapping element for the hue controls |
canvasHue | A canvas element that renders every available hue at 50% lightness. |
controlHue | An element styled as the control for the hue canvas. |
alpha | A wrapping element for the alpha controls |
canvasAlpha | A canvas element that renders the currently selected color from 0% to 100% opacity. |
controlAlpha | An element styled as the control for the alpha canvas. |
eyeDropper | An element that acts as a trigger for enabling the browser's EyeDropper API. Only displays in supported browsers. |
iconEyeDropper | An element for outputting an icon within the eyeDropper section. |
formatField | A wrapping element that contains all of the color format input fields and the format toggle control. Only the field(s) relevant to the currently selected panel format will be displayed. |
colorInputGroup | A wrapping element that wraps one or more input fields associated with a given color format. |
hexField | The input for a hex color format. Supports #RRGGBBAA format although alpha hex values are not required. |
rField | The input for the R value of an RGBA color format. |
gField | The input for the G value of an RGBA color format. |
bField | The input for the B value of an RGBA color format. |
hField | The input for the H value of an HSLA color format. |
sField | The input for the S value of an HSLA color format. |
lField | The input for the L value of an HSLA color format. |
aField | The input for the A value of an RGBA or HSLA color format. |
formatSwitcher | An element that acts as a trigger for changing the currently displayed color format within the colorpicker panel. Has no effect on the format and value-format settings of the input. |
iconSwitch | An element for outputting an icon within the formatSwitcher section. |
swatches | A wrapping element that contains all available swatches made available via the options prop. |
swatch | An element that handles the rendering of each option (or option group) provided by the options prop. |
Show Universal section keys | |
outer | The outermost wrapping element. |
wrapper | A wrapper around the label and input. |
label | The label of the input. |
prefix | Has no output by default, but allows content directly before an input element. |
prefixIcon | An element for outputting an icon before the prefix section. |
inner | A wrapper around the actual input element. |
suffix | Has no output by default, but allows content directly after an input element. |
suffixIcon | An element for outputting an icon after the suffix section. |
input | The input element itself. |
help | The element containing help text. |
messages | A wrapper around all the messages. |
message | The element (or many elements) containing a message — most often validation and error messages. |
All FormKit inputs are designed with the following accessibility considerations in mind. Help us continually improve accessibility for all by filing accessibility issues here:
Section Key | Attribute | Default | Description |
---|---|---|---|
swatchPreview | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
role | button | Indicates to assistive technologies that this element functions as a button. | |
aria-valuetext | Defines the human-readable text of the value. | ||
panel | aria-role | dialog | When layout is set to popover it adds this attribute. |
aria-modal | true | When layout is set to popover it adds this attribute. | |
aria-label | Provides an accessible name. | ||
panelClose | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when panel is closed and 0 when openned. |
aria-label | Provides an accessible name. | ||
controlGroup | role | group | Indicates to assistive technologies that this element functions as a group. |
canvasLS | aria-hidden | true | Makes this element not exposed to the accessibility API. |
controlLS | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
role | slider | Indicates to assistive technologies that this element functions as a slider. | |
aria-valuemin | Defines the minimum allowed value. | ||
aria-valuemax | Defines the maximum allowed value. | ||
aria-valuetext | Defines the human-readable text of the value. | ||
canvasHue | aria-hidden | true | Makes this element not exposed to the accessibility API. |
controlHue | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
role | slider | Indicates to assistive technologies that this element functions as a slider. | |
aria-valuemin | Defines the minimum allowed value. | ||
aria-valuemax | Defines the maximum allowed value. | ||
aria-valuenow | Defines the human-readable text of the value. | ||
canvasAlpha | aria-hidden | true | Makes this element not exposed to the accessibility API. |
controlAlpha | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
role | slider | Indicates to assistive technologies that this element functions as a slider. | |
aria-valuemin | Defines the minimum allowed value. | ||
aria-valuemax | Defines the maximum allowed value. | ||
aria-valuenow | Defines the human-readable text of the value. | ||
canvasPreview | aria-hidden | true | Makes this element not exposed to the accessibility API. |
canvasSwatchPreview | aria-hidden | true | Makes this element not exposed to the accessibility API. |
eyeDropper | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
role | button | Indicates to assistive technologies that this element functions as a button. | |
colorInputGroup | aria-role | group | Indicates to assistive technologies that this element functions as a group. |
hexField | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. | ||
rField | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. | ||
gField | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. | ||
bField | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. | ||
aField | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. | ||
hField | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. | ||
sField | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. | ||
lField | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. | ||
formatSwitcher | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
role | button | Indicates to assistive technologies that this element functions as a button. | |
aria-label | Provides an accessible name. | ||
swatch | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when disabled and 0 when enabled. |
role | button | Indicates to assistive technologies that this element functions as a button. | |
aria-label | Provides an accessible name. | ||
Show Universal section key | |||
label | label | for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. |
input | input | disabled | Disables an HTML element, preventing user interaction and signaling a non-interactive state. |
aria-describedby | Associates an element with a description, aiding screen readers. | ||
aria-required | Adds this attribute when validation is required. | ||
icon | icon | for | Whenever icon is defined as a label it links it to an input element. |
Keyboard Event | Description |
---|---|
Tab | Moves the focus to the next focusable input on the page. |
Shift + Tab | Moves the focus to the previous focusable input on the page. |