Components Forms
Combobox
Filter a list by typing, then submit the selected option’s value.
Usage
Pass Items with Label and Value. Up and Down highlight matches, Enter selects, and Escape restores the committed value. A native select is available before the optional module is mounted.
Register combobox with Head and pass its properties to Render. Go setup guide
Go usage
err := mono.UI.Render(w, "combobox", mono.Props{
"Items": []any{
map[string]any{
"Label": "Baskervville",
"Selected": true,
"Value": "baskervville",
},
map[string]any{
"Label": "Crimson Text",
"Value": "crimson-text",
},
map[string]any{
"Label": "Instrument Sans",
"Value": "instrument-sans",
},
map[string]any{
"Label": "Space Mono",
"Value": "space-mono",
},
map[string]any{
"Label": "Work Sans",
"Value": "work-sans",
},
},
"Label": "Typeface",
"Name": "typeface",
"Placeholder": "Find a typeface…",
})Rendered HTML
<div id="demo-combobox" data-mono="combobox" class="mu-stack">
<label for="demo-combobox-native" class="mu-label">Typeface</label>
<select class="mu-field" id="demo-combobox-native" name="typeface" >
<option value="">Find a typeface…</option>
<option value="baskervville" selected >Baskervville</option><option value="crimson-text" >Crimson Text</option><option value="instrument-sans" >Instrument Sans</option><option value="space-mono" >Space Mono</option><option value="work-sans" >Work Sans</option>
</select>
<div class="mu-combo" hidden>
<input id="demo-combobox-search" class="mu-field" role="combobox" aria-autocomplete="list" aria-controls="demo-combobox-options" aria-expanded="false" autocomplete="off" placeholder="Find a typeface…" >
<ul id="demo-combobox-options" class="mu-panel mu-list" role="listbox" aria-label="Typeface" hidden></ul>
<output class="mu-small mu-muted" role="status"></output>
</div>
</div>Go template
<div id="{{.ID}}" data-mono="combobox" class="mu-stack">
<label for="{{.ID}}-native" class="mu-label">{{.Label}}</label>
<select class="mu-field" id="{{.ID}}-native" name="{{or .Name .ID}}" {{if .Required}}required{{end}} {{if .Disabled}}disabled{{end}}>
<option value="">{{or .Placeholder "Select an option"}}</option>
{{range .Items}}<option value="{{.Value}}" {{if .Selected}}selected{{end}} {{if .Disabled}}disabled{{end}}>{{.Label}}</option>{{end}}
</select>
<div class="mu-combo" hidden>
<input id="{{.ID}}-search" class="mu-field" role="combobox" aria-autocomplete="list" aria-controls="{{.ID}}-options" aria-expanded="false" autocomplete="off" placeholder="{{or .Placeholder "Search options"}}" {{if .Disabled}}disabled{{end}}>
<ul id="{{.ID}}-options" class="mu-panel mu-list" role="listbox" aria-label="{{.Label}}" hidden></ul>
<output class="mu-small mu-muted" role="status"></output>
</div>
</div>Component CSS
[data-mono="combobox"] .mu-combo { position: relative; }
[data-mono="combobox"] input { width: 100%; }
[data-mono="combobox"] [role="listbox"] { margin: .3rem 0 0; padding: .3rem; max-height: 12rem; overflow: auto; }
[data-mono="combobox"] [role="option"] { display: flex; align-items: center; gap: .6rem; cursor: pointer; padding: .4rem .65rem; border-radius: .23rem; }
[data-mono="combobox"] [data-active="true"] { background: #171717; color: white; }JavaScript module
export function mount(root) {
const select = root.querySelector('select');
const shell = root.querySelector('.mu-combo');
const input = shell.querySelector('input');
const list = shell.querySelector('[role="listbox"]');
const status = shell.querySelector('output');
const options = [...select.options].filter(option => option.value && !option.disabled);
const required = select.required;
let matches = [];
let active = -1;
function restore() {
input.value = select.value ? select.selectedOptions[0].label : '';
}
function closeList() {
list.hidden = true;
input.setAttribute('aria-expanded', 'false');
input.removeAttribute('aria-activedescendant');
active = -1;
status.textContent = '';
restore();
}
function mark(index) {
if (!matches.length) return;
active = Math.max(0, Math.min(index, matches.length - 1));
[...list.children].forEach((el, i) => { el.dataset.active = String(i === active); });
const element = list.children[active];
input.setAttribute('aria-activedescendant', element.id);
element.scrollIntoView({ block: 'nearest' });
}
function openList(query = '') {
matches = options.filter(option => option.label.toLowerCase().includes(query.toLowerCase().trim()));
active = -1;
input.removeAttribute('aria-activedescendant');
list.replaceChildren(...matches.map((option, index) => {
const li = document.createElement('li');
li.id = root.id + '-option-' + index;
li.dataset.index = String(index);
li.className = 'mu-choice';
li.setAttribute('role', 'option');
li.setAttribute('aria-selected', String(option.value === select.value));
li.textContent = option.label;
return li;
}));
list.hidden = false;
input.setAttribute('aria-expanded', 'true');
status.textContent = matches.length ? '' : 'No matching options.';
}
function choose(index) {
if (!matches[index]) return;
select.value = matches[index].value;
select.dispatchEvent(new Event('input', { bubbles: true }));
select.dispatchEvent(new Event('change', { bubbles: true }));
closeList();
}
root.querySelector('label').htmlFor = input.id;
select.hidden = true;
if (required) input.setAttribute('aria-required', 'true');
shell.hidden = false;
restore();
input.addEventListener('click', () => openList());
input.addEventListener('input', () => openList(input.value));
input.addEventListener('keydown', event => {
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault();
if (list.hidden) openList();
mark(active < 0 ? (event.key === 'ArrowDown' ? 0 : matches.length - 1)
: active + (event.key === 'ArrowDown' ? 1 : -1));
} else if (event.key === 'Enter' && !list.hidden) {
event.preventDefault();
choose(active < 0 && matches.length === 1 ? 0 : active);
} else if (event.key === 'Escape' && !list.hidden) {
event.preventDefault();
closeList();
}
});
list.addEventListener('pointerdown', event => event.preventDefault());
list.addEventListener('click', event => {
const option = event.target.closest('[role="option"]');
if (option) choose(Number(option.dataset.index));
});
input.addEventListener('blur', () => {
if (!input.value && select.value && !required) {
select.value = '';
select.dispatchEvent(new Event('input', { bubbles: true }));
select.dispatchEvent(new Event('change', { bubbles: true }));
}
closeList();
});
select.addEventListener('invalid', event => {
event.preventDefault();
input.focus();
openList();
status.textContent = select.validationMessage;
});
select.form?.addEventListener('reset', () => setTimeout(closeList, 0));
}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="./combobox.css">
<script type="module" src="./combobox.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 comboboxExample properties (JSON)
{
"Items": [
{
"Label": "Baskervville",
"Selected": true,
"Value": "baskervville"
},
{
"Label": "Crimson Text",
"Value": "crimson-text"
},
{
"Label": "Instrument Sans",
"Value": "instrument-sans"
},
{
"Label": "Space Mono",
"Value": "space-mono"
},
{
"Label": "Work Sans",
"Value": "work-sans"
}
],
"Label": "Typeface",
"Name": "typeface",
"Placeholder": "Find a typeface…"
}