Components Forms
Select
Choose a single option from a compact list that supports keyboard navigation and type-ahead.
Usage
Items accepts Label, Value, Selected, and Disabled. Use arrow keys or type the start of a label, then press Enter to commit. Escape keeps the existing value. The named select provides the form value and the HTML-only fallback.
Register select with Head and pass its properties to Render. Go setup guide
Go usage
err := mono.UI.Render(w, "select", mono.Props{
"Items": []any{
map[string]any{
"Label": "Gallery",
"Value": "gallery",
},
map[string]any{
"Label": "Journal",
"Selected": true,
"Value": "journal",
},
map[string]any{
"Label": "Newsletter",
"Value": "newsletter",
},
map[string]any{
"Disabled": true,
"Label": "Press archive",
"Value": "archive",
},
},
"Label": "Publishing channel",
"Name": "channel",
"Placeholder": "Choose a channel",
})Rendered HTML
<div id="demo-select" data-mono="select" class="mu-stack">
<label id="demo-select-label" for="demo-select-native" class="mu-label">Publishing channel</label>
<select id="demo-select-native" name="channel" class="mu-field" >
<option value="">Choose a channel</option>
<option value="gallery" >Gallery</option><option value="journal" selected >Journal</option><option value="newsletter" >Newsletter</option><option value="archive" disabled>Press archive</option>
</select>
<div data-select-ui hidden>
<button type="button" id="demo-select-trigger" class="mu-field" role="combobox" aria-haspopup="listbox" aria-labelledby="demo-select-label" aria-expanded="false" aria-controls="demo-select-list" ><span data-selected></span><span class="mu-chevron mu-chevron-down" aria-hidden="true"></span></button>
<ul id="demo-select-list" role="listbox" class="mu-list mu-panel" aria-labelledby="demo-select-label" hidden></ul>
<output class="mu-small" role="status"></output>
</div>
</div>Go template
<div id="{{.ID}}" data-mono="select" class="mu-stack">
<label id="{{.ID}}-label" for="{{.ID}}-native" class="mu-label">{{.Label}}</label>
<select id="{{.ID}}-native" name="{{or .Name .ID}}" class="mu-field" {{if .Required}}required{{end}} {{if .Disabled}}disabled{{end}}>
{{if .Placeholder}}<option value="">{{.Placeholder}}</option>{{end}}
{{range .Items}}<option value="{{.Value}}" {{if .Selected}}selected{{end}} {{if .Disabled}}disabled{{end}}>{{.Label}}</option>{{end}}
</select>
<div data-select-ui hidden>
<button type="button" id="{{.ID}}-trigger" class="mu-field" role="combobox" aria-haspopup="listbox" aria-labelledby="{{.ID}}-label" aria-expanded="false" aria-controls="{{.ID}}-list" {{if .Required}}aria-required="true"{{end}} {{if .Disabled}}disabled{{end}}><span data-selected></span><span class="mu-chevron mu-chevron-down" aria-hidden="true"></span></button>
<ul id="{{.ID}}-list" role="listbox" class="mu-list mu-panel" aria-labelledby="{{.ID}}-label" hidden></ul>
<output class="mu-small" role="status"></output>
</div>
</div>Component CSS
[data-mono="select"] [role="combobox"] { display: flex; align-items: center; justify-content: space-between; gap: .6rem; width: 100%; text-align: start; cursor: pointer; }
[data-mono="select"] [role="listbox"] { margin: .3rem 0 0; padding: .25rem; max-height: 12rem; overflow: auto; }
[data-mono="select"] [role="option"] { display: flex; align-items: center; gap: .6rem; cursor: pointer; border-radius: .25rem; padding: .45rem .65rem; }
[data-mono="select"] [role="option"][aria-disabled="true"] { opacity: .45; }
[data-mono="select"] [role="option"][data-active="true"] { background: #171717; color: white; }JavaScript module
export function mount(root) {
const native = root.querySelector('select');
const shell = root.querySelector('[data-select-ui]');
const trigger = shell.querySelector('button');
const list = shell.querySelector('[role="listbox"]');
const options = [...native.options];
const enabled = options.map((option, i) => option.disabled ? -1 : i).filter(i => i >= 0);
let active = -1, typed = '', lastKey = 0;
const nodes = options.map((option, i) => {
const li = document.createElement('li');
li.className = 'mu-choice';
li.setAttribute('role', 'option');
li.setAttribute('aria-disabled', String(option.disabled));
li.textContent = option.label;
li.id = root.id + '-choice-' + i;
li.dataset.index = String(i);
list.append(li);
return li;
});
function sync() {
trigger.querySelector('[data-selected]').textContent = native.selectedOptions[0]?.label ?? '';
nodes.forEach((li, i) => li.setAttribute('aria-selected', String(native.selectedIndex === i)));
}
function mark(i) {
if (!enabled.includes(i)) return;
active = i;
nodes.forEach((li, n) => { li.dataset.active = String(n === i); });
trigger.setAttribute('aria-activedescendant', nodes[i].id);
nodes[i].scrollIntoView({ block: 'nearest' });
}
function open() {
list.hidden = false;
trigger.setAttribute('aria-expanded', 'true');
mark(enabled.includes(native.selectedIndex) ? native.selectedIndex : enabled[0]);
}
function close(commit) {
if (commit && active !== -1 && active !== native.selectedIndex) {
native.selectedIndex = active;
native.dispatchEvent(new Event('input', { bubbles: true }));
native.dispatchEvent(new Event('change', { bubbles: true }));
}
list.hidden = true;
trigger.setAttribute('aria-expanded', 'false');
trigger.removeAttribute('aria-activedescendant');
typed = '';
active = -1;
sync();
}
function typeAhead(character) {
const now = performance.now();
const key = character.toLowerCase();
typed = now - lastKey > 650 ? key : typed + key;
lastKey = now;
const prefix = [...typed].every(letter => letter === key) ? key : typed;
const start = prefix.length === 1 ? enabled.indexOf(active) + 1 : enabled.indexOf(active);
for (let n = 0; n < enabled.length; n++) {
const index = enabled[(start + n + enabled.length) % enabled.length];
if (options[index].label.toLowerCase().startsWith(prefix)) { mark(index); break; }
}
}
trigger.addEventListener('click', () => list.hidden ? open() : close(false));
trigger.addEventListener('keydown', event => {
const key = event.key;
if (key === 'Tab') { if (!list.hidden) close(true); return; }
if (key === 'Escape') { if (!list.hidden) { event.preventDefault(); event.stopPropagation(); close(false); } return; }
if (event.altKey || event.ctrlKey || event.metaKey) return;
if (key === ' ' || key === 'Enter') {
event.preventDefault();
if (list.hidden) open(); else close(true);
return;
}
if (['ArrowUp', 'ArrowDown', 'Home', 'End'].includes(key)) {
event.preventDefault();
if (list.hidden) open();
const at = enabled.indexOf(active);
const to = key === 'Home' ? 0 : key === 'End' ? enabled.length - 1 : at + (key === 'ArrowDown' ? 1 : -1);
mark(enabled[Math.max(0, Math.min(to, enabled.length - 1))]);
} else if (key.length === 1) {
event.preventDefault();
if (list.hidden) open();
typeAhead(key);
}
});
list.addEventListener('pointerdown', event => event.preventDefault());
list.addEventListener('click', event => {
const item = event.target.closest('[role="option"]');
if (!item) return;
const i = Number(item.dataset.index);
if (enabled.includes(i)) { mark(i); close(true); }
});
trigger.addEventListener('blur', () => { if (!list.hidden) close(false); });
native.addEventListener('change', () => { sync(); shell.querySelector('output').textContent = ''; });
native.addEventListener('invalid', event => {
event.preventDefault();
shell.querySelector('output').textContent = native.validationMessage;
trigger.focus();
open();
});
native.form?.addEventListener('reset', () => setTimeout(() => close(false), 0));
root.querySelector('label').htmlFor = trigger.id;
native.hidden = true;
shell.hidden = false;
sync();
}Use the HTML in a static site
Copy the rendered HTML above. Download these assets into the same folder as your page, then add the tags below to its head.
Shared CSS Component CSS Ready-to-use module
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./select.css">
<script type="module" src="./select.js"></script>Copy into your project
Run this command from the Mono checkout. Set -out to the component directory in your application.
go run ./cmd/mono-ui add -out ./components selectExample properties (JSON)
{
"Items": [
{
"Label": "Gallery",
"Value": "gallery"
},
{
"Label": "Journal",
"Selected": true,
"Value": "journal"
},
{
"Label": "Newsletter",
"Value": "newsletter"
},
{
"Disabled": true,
"Label": "Press archive",
"Value": "archive"
}
],
"Label": "Publishing channel",
"Name": "channel",
"Placeholder": "Choose a channel"
}