Color Fields

Color Fields

The color of body text.

<div class="field max-w-xs">
<label for="demo-color-field-text">Text Color</label>
<div class="input-group color-field">
<span data-align="start"><input type="color" aria-label="Pick the text color" /></span>
<input type="text" id="demo-color-field-text" name="text_color" value="#595d69" />
</div>
<p>The color of body text.</p>
</div>

An empty text field sends no colour, and the picker shows none

<div class="field max-w-xs">
<label for="demo-color-field-empty">Link Color</label>
<div class="input-group color-field">
<span data-align="start"><input type="color" aria-label="Pick the link color" /></span>
<input type="text" id="demo-color-field-empty" name="link_color" placeholder="Inherits the accent color" />
</div>
</div>

A three digit value sets the picker too

<div class="field max-w-xs">
<label for="demo-color-field-short">Background Color</label>
<div class="input-group color-field">
<span data-align="start"><input type="color" aria-label="Pick the background color" /></span>
<input type="text" id="demo-color-field-short" name="background_color" value="#000" />
</div>
</div>

disabled on both inputs dims the field

<div class="field max-w-xs">
<label for="demo-color-field-disabled">Footer Color</label>
<div class="input-group color-field">
<span data-align="start"><input type="color" aria-label="Pick the footer color" disabled /></span>
<input type="text" id="demo-color-field-disabled" name="footer_color" value="#191a1f" disabled />
</div>
</div>

The form hears input when a colour is picked or typed, and sends the text field

#0a7f6e
<form class="flex max-w-xs flex-col gap-3" id="demo-color-field-form" oninput="document.getElementById('demo-color-field-result').textContent = new FormData(this).get('button_color')">
<div class="field">
<label for="demo-color-field-button">Button Color</label>
<div class="input-group color-field">
<span data-align="start"><input type="color" aria-label="Pick the button color" /></span>
<input type="text" id="demo-color-field-button" name="button_color" value="#0a7f6e" />
</div>
</div>
<pre class="bg-muted overflow-x-auto rounded-lg p-4 font-mono text-xs" id="demo-color-field-result">#0a7f6e</pre>
</form>

API Reference

Attributes

Attribute On Values Default Description
value the input[type=text] #rgb, #rrggbb, or empty empty The colour a form sends. The picker shows it.
aria-label the input[type=color] text none Names the picker for a screen reader. The text field has the visible label.
data-empty .color-field present or absent set by the script Present while the text field is empty. The picker then shows no colour.

Events

Event On Detail Description
input the input[type=text], and it bubbles none Sent when a colour is picked, as typing in the text field sends it. It is the browser's own event.
change the input[type=text], and it bubbles none Sent when the browser's colour dialog closes with a new colour. It is the browser's own event.

Keyboard

Key Description
Tab Moves from the picker to the text field. It is the browser's own.
Enter, Space On the picker, opens the browser's colour dialog. It is the browser's own.

A short value such as #000 sets the picker to its long form. The text field keeps what was typed.

A value that is not a hex colour leaves the picker as it was.

Once the script has started it, the element carries data-color-field-initialized="true" and sends tsugi:initialized. The methods and properties above are on the element itself.