data-color
Display a styled color swatch with hex code for color inputs. Clicking opens the native color picker while maintaining form value.
Overview
The data-color attribute replaces the browser's default color input with a styled display showing a circular color swatch and the hex code. Clicking the display opens the native color picker. The underlying input remains functional for form submission.
How It Works
Add data-color to any <input type="color">. The init script:
- Wraps the input in a
.color-wrappercontainer - Creates a
.color-displaybutton containing a.color-swatchspan and a.color-hexspan - Sets the swatch background color to the current input value
- Displays the hex code as text (e.g.,
#6366f1) - Hides the native color input visually while keeping it in the DOM
- Adds a click handler to the display button that opens the native picker
- Listens for
inputevents to update the swatch and hex text in real time - Sets
data-color-initto prevent double-binding
The native color input remains in the DOM and participates in form submission. The display is a visual layer on top.
Attributes
| Attribute | Type | Description |
|---|---|---|
data-color |
boolean | Enables the color swatch enhancement on a color input. |
data-color-init |
boolean | Set automatically to prevent double-binding. Do not set manually. |
value |
string | Standard HTML attribute. The hex color value (e.g., #6366f1). Sets the initial swatch color. |
DOM Structure
The init script transforms the input into a richer structure. Here is what the enhanced DOM looks like:
The .color-display button acts as the visual trigger. Clicking it programmatically opens the hidden native color input's picker dialog.
Multiple Colors
Use multiple data-color inputs for theme or palette builders. Each operates independently.
Color Palette Form
Combine multiple color inputs in a form for a complete palette builder. Each input submits its hex value with the form.
Live Updates
As the user drags through the native color picker, the swatch background and hex text update in real time via the input event. The change event fires when the picker closes with a final selection.
| Event | Target | Description |
|---|---|---|
input |
The <input> |
Fired continuously as the user drags through the picker. Swatch and hex text update in real time. |
change |
The <input> |
Fired once when the picker closes with a final color selection. |
Styling
The display button, swatch, and hex text are styled via CSS classes. All styles are gated on [data-color-init] so the native color input renders normally without JavaScript.
The swatch uses border-radius: var(--radius-round) for a circular shape. The hex text uses the monospace font stack for consistent character widths.
Dynamic Elements
Inputs added to the DOM after page load are automatically enhanced via a MutationObserver. No manual initialization is needed.
Accessibility
- The native
<input type="color">remains in the DOM, preserving form submission and validation - The display button is focusable and activates the native picker on click or keyboard activation
- The hex code is visible text, readable by screen readers alongside the label
- Color value changes are reflected immediately in both the visual swatch and the text hex code
- Works with
<form-field>for label association and validation feedback - Without JavaScript, the native color input renders normally (progressive enhancement)