Fylgja Color Picker
A color picker lets people type a color or pick one visually. The text field holds
the value and accepts any CSS color, including an empty one. Beside it, a native
<input type="color"> shows that color as a swatch and opens the system picker,
which writes a hex value back into the field. A small <color-field> element keeps the two in sync,
so you get the platform’s own picker without building one.
Preview
Type a color such as tomato, oklch(70% 0.15 150), or var(--brand), or click
the swatch.
<div class="field">
<label for="brand-color">Brand color</label>
<color-field class="input-group">
<input type="text" id="brand-color" name="brand" value="var(--brand)" autocomplete="off" spellcheck="false" aria-describedby="brand-color-hint" />
<input type="color" class="flex-none" value="#6f31ed" aria-label="Pick the brand color" style="--block-size: auto" />
</color-field>
<small id="brand-color-hint" class="block">Any CSS color, like #6d28d9, oklch(50% 0.2 290), or var(--brand).</small>
</div><div class="field">
<label for="brand-color">Brand color</label>
<div class="input-group" x-data="{ value: '#6f31ed' }" :data-empty="!value">
<input type="text" id="brand-color" name="brand" x-model="value" autocomplete="off" spellcheck="false" aria-describedby="brand-color-hint" />
<input type="color" class="flex-none" aria-label="Pick the brand color" style="--block-size: auto" :value="value" @input="value = $event.target.value" />
</div>
<small id="brand-color-hint" class="block">Any CSS color, like #6d28d9 or oklch(50% 0.2 290).</small>
</div>[data-empty] > input[type="color"] {
background: repeating-linear-gradient(-45deg, #0000 0 6px, #8884 0 12px) content-box;
}
[data-empty] > input[type="color"]::-webkit-color-swatch {
opacity: 0;
}
[data-empty] > input[type="color"]::-moz-color-swatch {
opacity: 0;
}
The <color-field> script
class ColorField extends HTMLElement {
/**
* The text field, read on use so the element works even when it is defined before its children are parsed.
* @returns {HTMLInputElement}
*/
get text() {
return this.querySelector("input:not([type=color])");
}
/** @returns {HTMLInputElement} */
get picker() {
return this.querySelector("input[type=color]");
}
connectedCallback() {
this.addEventListener("input", this);
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () => this.sync(), { once: true });
} else {
this.sync();
}
}
/**
* Writes a picked color into the text field, or shows a typed color in the swatch.
* @param {InputEvent} event
* @returns {void}
*/
handleEvent(event) {
if (event.target === this.picker) {
this.text.value = this.picker.value;
this.text.dispatchEvent(new Event("input", { bubbles: true }));
return;
}
this.sync();
}
/**
* Shows the text value in the picker, or marks the field empty when there is no color to show.
* @returns {void}
*/
sync() {
const picker = this.picker;
// Resolving through a color property lets var() and other references reach the picker as a real color.
// accent-color, since form controls transition color and would report the old value.
picker.style.accentColor = this.text.value.trim();
const isColor = picker.style.accentColor !== "";
if (isColor) picker.value = getComputedStyle(picker).accentColor;
picker.style.accentColor = "";
this.toggleAttribute("data-empty", !isColor);
}
}
customElements.define("color-field", ColorField);
Usage
- Wrap the text field and the swatch in
<color-field>with theinput-groupclass, and load the script once per page. - Put the
nameon the text field only. It holds the real value, while the picker is just a way to fill it in. - Add
flex-noneto the color input so it keeps its square size, and set--block-size: autoso it stretches to the height of the text field. - Give the color input a hex
valueclose to the text value, so the swatch is right before the script runs. The preview uses#6f31ed, the default Fylgja brand color. - A color input can never be empty. While the text field has no color, the script
sets
data-empty, and the CSS hides the native swatch behind stripes.
Anatomy
<color-field>: the custom element that shows the text value in the color input and writes picked colors back.- Text field: the submitted value, any CSS color or none.
- Color input: the swatch, opening the native picker. It has no
name, so only the text field is submitted. data-empty: set while the text field holds no color, to show the stripes.- Hint (optional): a
<small>below the control with the accepted formats or what an empty value falls back to.
Accessibility
- The
<label for>names the text field. The color input has no visible text, so give it anaria-labelsuch as “Pick the brand color”. - Both controls are in the tab order. Keyboard users can type a value directly, or press Enter or Space on the color input to open the picker. The input group draws the focus ring around both.
- Picking a color fires an
inputevent on the text field, so other scripts that listen to it see the change. - Do not show a color only as a swatch. The text value is what a screen reader reads.
Variants
Native swatch only
When a value is always required and hex is enough, the plain color input does the job with no script. Fylgja base styles it as a rounded swatch.
<label for="accent-color">Accent color</label>
<input type="color" id="accent-color" name="accent" value="#6d28d9" />Examples
Theme settings
A group of optional theme colors. Empty fields show the stripes and fall back to the theme default from the hint, and each field can hold its own color format.
<fieldset>
<legend>Surfaces and text</legend>
<div class="field">
<label for="theme-bg">Background</label>
<color-field class="input-group">
<input type="text" id="theme-bg" name="bg" autocomplete="off" spellcheck="false" aria-describedby="theme-bg-hint" />
<input type="color" class="flex-none" aria-label="Pick the background color" style="--block-size: auto" />
</color-field>
<small id="theme-bg-hint" class="block">When empty, follows Gray 50.</small>
</div>
<div class="field">
<label for="theme-surface">Surface</label>
<color-field class="input-group">
<input type="text" id="theme-surface" name="surface" value="oklch(98% 0.01 290)" autocomplete="off" spellcheck="false" aria-describedby="theme-surface-hint" />
<input type="color" class="flex-none" aria-label="Pick the surface color" style="--block-size: auto" />
</color-field>
<small id="theme-surface-hint" class="block">Cards, panels, and form fields. When empty, #fff.</small>
</div>
<div class="field">
<label for="theme-text">Text</label>
<color-field class="input-group">
<input type="text" id="theme-text" name="text" value="#1f2937" autocomplete="off" spellcheck="false" />
<input type="color" class="flex-none" aria-label="Pick the text color" style="--block-size: auto" />
</color-field>
</div>
</fieldset>Related: Form Field, Range, Search Box.
FAQ
Why not use input type color on its own?
A color input can never be empty, it always holds a value and falls back to black. That rules it out for optional settings where empty means use the default. It also only accepts lowercase #rrggbb, so people cannot type a value like oklch(50% 0.2 290) or var(--brand), and the value is hidden behind the swatch.
How does the swatch show an oklch or var() value?
Modern browsers accept any CSS color as the value of a color input and convert it to the closest hex. The script resolves the text value through the color property first, so var() and other references reach the color input as a real color. Browsers that only accept hex show black for other formats.
What happens to an oklch or rgb value when I use the picker?
The native picker only speaks hex, so picking a color writes a hex value into the text field. A typed value stays exactly as typed until the picker is used.
Is the typed value validated?
No, the text field takes any value, since a CSS variable or a theme token is as valid a color as a hex code. If the value is not a color, the swatch shows the empty stripes. Add a pattern attribute to the text field when your form only accepts one format.
Does it work without JavaScript?
The text field still submits its value and the picker still opens, but the swatch does not update and a picked color is not written to the field. The Alpine version needs no separate script, though it does not resolve var() values.