Interactive
Combobox
Filter as you type over a native select or an input with a datalist, following the WAI-ARIA combobox pattern. Matches are highlighted, accents are ignored and option groups are kept. Needs monsoon.js.
Needs monsoon.js. The styles work on their own, and the behavior comes from the optional interactive layer: add <script src="https://cdn.jsdelivr.net/npm/monsooncss/dist/monsoon.js" defer></script> or import 'monsooncss/js'. See the interactive layer.
Example
<div class="grid gap-4 max-w-xs">
<div class="combobox" data-m-combobox>
<select class="select" aria-label="Time zone">
<option value="">Pick a time zone</option>
<optgroup label="Americas">
<option value="America/Sao_Paulo">São Paulo</option>
<option value="America/New_York">New York</option>
<option value="America/Mexico_City">Mexico City</option>
<option value="America/Vancouver">Vancouver</option>
</optgroup>
<optgroup label="Europe">
<option value="Europe/Lisbon">Lisbon</option>
<option value="Europe/Berlin">Berlin</option>
<option value="Europe/Zurich">Zürich</option>
</optgroup>
<optgroup label="Asia">
<option value="Asia/Tokyo">Tokyo</option>
<option value="Asia/Singapore">Singapore</option>
</optgroup>
</select>
</div>
<div class="combobox" data-m-combobox>
<input class="input" list="cb-languages" placeholder="Favorite language" aria-label="Favorite language">
<datalist id="cb-languages"><option value="JavaScript"><option value="TypeScript"><option value="Python"><option value="Rust"><option value="Go"><option value="Elixir"></datalist>
</div>
</div>JavaScript API
Attribute
data-m-combobox
Options
| Option | Description |
|---|---|
data-m-combobox-placeholder | |
data-m-combobox-empty="No results" | |
data-m-combobox-async (ask for items with an event) |
Methods
| Method | Description |
|---|---|
Monsoon.combobox(el).open() | |
Monsoon.combobox(el).close() | |
Monsoon.combobox(el).value | |
Monsoon.combobox(el).refresh() |
Events
| Event | Description |
|---|---|
monsoon:change { value, label } | |
monsoon:combobox-search { query, set(items) } with data-m-combobox-async |
Classes
7 classes. Click a name to copy it. Utilities on the same element always win over component defaults, so any of them can be adjusted with plain utility classes.
| Class | Type | What it does |
|---|---|---|
| Base | Base class of the combobox. On a <div> in the example. Sets block display, width, padding and transform.CSS |
|
| Modifier | Modifier, combined with the combobox class. Not used in the example above. A hook for state rules such as .combobox > .combobox-input.CSS |
|
| Modifier | Modifier, combined with the combobox class. Not used in the example above. Sets absolute position, offsets, grid display, alignment, width and text color.CSS |
|
| Modifier | Modifier, combined with the combobox class. Not used in the example above. Sets fixed position, offsets, stacking order, margin, padding and overflow, and --tone and --tone-fg.CSS |
|
| Modifier | Modifier, combined with the combobox class. Not used in the example above. Sets flex display, alignment, gap, padding, corner radius and pointer cursor.CSS |
|
| Modifier | Modifier, combined with the combobox class. Not used in the example above. Sets padding, text size, font weight and text color.CSS |
|
| Modifier | Modifier, combined with the combobox class. Not used in the example above. Sets padding, text style and text color.CSS |
Usage
The styles ship inside the same stylesheet as the utilities, and the behavior comes from the optional monsoon.js script, which enhances any element with a matching data-m attribute, including markup added later.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/monsooncss">
<script src="https://cdn.jsdelivr.net/npm/monsooncss/dist/monsoon.js" defer></script>
<!-- Combobox: start from the base class, then add modifiers -->
<div class="grid gap-4 max-w-xs">
<div class="combobox" data-m-combobox>
<select class="select" aria-label="Time zone">
<option value="">Pick a time zone</option>
<optgroup label="Americas">
<option value="America/Sao_Paulo">São Paulo</option>
<option value="America/New_York">New York</option>
<option value="America/Mexico_City">Mexico City</option>
<option value="America/Vancouver">Vancouver</option>
</optgroup>
<optgroup label="Europe">
<option value="Europe/Lisbon">Lisbon</option>
<option value="Europe/Berlin">Berlin</option>
<option value="Europe/Zurich">Zürich</option>
<!-- the full example is in the Code tab above -->Theme it with --seed on any ancestor, for example style="--seed: #0d9488", and force a scheme with class="dark" or class="light" on a wrapper.
Accessibility
- Give every control a visible
labellinked withfor, or anaria-labelwhen the purpose is obvious from context. Link help and error text witharia-describedby; invalid styles use:user-invalid, so they appear only after the user interacts. - Every interactive part shows a visible focus ring with
:focus-visible, and tone colors pair with-foregroundtokens that keep text at 4.5:1 or better in light and dark mode.