Skip to content

Combobox Examples

The examples below assume that you have imported the components and set up the theme.

Basic

Make a searchable single-select field. Each button with a value is an option, and data-sv-combobox-no-results shows when a query matches nothing.

No framework matches your search

--opened
Behavior
none turns off the built-in matching, for a list that a script fills.
Shows the clear button while there is a value.
State
<div class="combobox-basic">
<label for="framework">Framework</label>
<sv-combobox id="framework" placeholder="Type to search">
<button type="button" value="astro">Astro</button>
<button type="button" value="preact">Preact</button>
<button type="button" value="react">React</button>
<button type="button" value="solid">SolidJS</button>
<button type="button" disabled value="staticbolt">Staticbolt</button>
<button type="button" value="svelte">Svelte</button>
<button type="button" value="vue">Vue</button>
<p class="empty" data-sv-combobox-no-results>No framework matches your search</p>
</sv-combobox>
</div>
<style>
.combobox-basic {
display: grid;
gap: 0.5em;
inline-size: min(100%, 22rem);
margin-inline: auto;
color: var(--sv-gray-2);
label {
justify-self: start;
cursor: pointer;
}
.empty {
padding: 0.5em;
margin: 0;
color: var(--sv-gray-3);
}
}
</style>

Multiple

Let the user pick several options as chips. multiple shows each pick as a chip, and hide-selected takes it out of the list.

Every topic is picked

No topic matches your search

Behavior
Takes the selected options out of the list.
Shows the clear button, which removes every chip.
<div class="combobox-multiple">
<label for="topics">Topics</label>
<sv-combobox id="topics" clearable hide-selected multiple placeholder="Type to search">
<button type="button" aria-selected="true" value="a11y">Accessibility</button>
<button type="button" value="animation">Animation</button>
<button type="button" aria-selected="true" value="css">CSS</button>
<button type="button" value="forms">Forms</button>
<button type="button" value="testing">Testing</button>
<p class="message" data-sv-combobox-empty>Every topic is picked</p>
<p class="message" data-sv-combobox-no-results>No topic matches your search</p>
</sv-combobox>
</div>
<style>
.combobox-multiple {
display: grid;
gap: 0.5em;
inline-size: min(100%, 22rem);
margin-inline: auto;
color: var(--sv-gray-2);
label {
justify-self: start;
cursor: pointer;
}
.message {
padding: 0.5em;
margin: 0;
color: var(--sv-gray-3);
}
}
</style>

Rich Options

Show more than a label in each option, like an avatar and an email, and search the email too. data-sv-combobox-label marks the part that the field and the chips show, and a filter function matches the whole option text.

No one in your contacts matches

<div class="combobox-rich-options">
<label for="recipients">To</label>
<sv-combobox id="recipients" class="recipients" multiple name="to" placeholder="Add people">
<button type="button" class="contact" aria-selected="true" value="maya@example.com">
<sv-avatar aria-hidden="true" colorful name="Maya Chen"></sv-avatar>
<span data-sv-combobox-label>Maya Chen</span>
<span class="email">maya@example.com</span>
</button>
<button type="button" class="contact" value="omar@example.com">
<sv-avatar aria-hidden="true" colorful name="Omar Haddad"></sv-avatar>
<span data-sv-combobox-label>Omar Haddad</span>
<span class="email">omar@example.com</span>
</button>
<button type="button" class="contact" value="lena@example.com">
<sv-avatar aria-hidden="true" colorful name="Lena Fischer"></sv-avatar>
<span data-sv-combobox-label>Lena Fischer</span>
<span class="email">lena@example.com</span>
</button>
<button type="button" class="contact" value="ravi@example.com">
<sv-avatar aria-hidden="true" colorful name="Ravi Patel"></sv-avatar>
<span data-sv-combobox-label>Ravi Patel</span>
<span class="email">ravi@example.com</span>
</button>
<p class="empty" data-sv-combobox-no-results>No one in your contacts matches</p>
</sv-combobox>
</div>
<script type="application/x-typescript" module>
const recipients = document.querySelector<Combobox>(".recipients")!;
recipients.filter = (option, query) =>
option.textContent.toLowerCase().includes(query.toLowerCase());
</script>
<style>
.combobox-rich-options {
display: grid;
gap: 0.5em;
inline-size: min(100%, 22rem);
margin-inline: auto;
color: var(--sv-gray-2);
label {
justify-self: start;
cursor: pointer;
}
.contact {
display: grid;
grid-template-columns: auto 1fr;
column-gap: 0.75em;
align-items: center;
text-align: start;
}
sv-avatar {
grid-row: span 2;
font-size: 0.8em;
}
.email {
font-size: 0.8em;
}
.empty {
padding: 0.5em;
margin: 0;
color: var(--sv-gray-3);
}
}
</style>

Groups

Group the options under headings, like cities by region. A role="group" element with aria-labelledby names each group, and a :has() rule hides a group with no visible option. Grouped options get no default styling, and a CSS border divides the groups because a listbox allows no role="separator".

Americas
Europe
Asia

No city matches your search

<div class="combobox-groups">
<label for="timezone">Timezone</label>
<sv-combobox id="timezone" placeholder="Search city">
<div class="group" aria-labelledby="tz-americas" role="group">
<span id="tz-americas" class="group-label">Americas</span>
<button type="button" value="America/New_York">New York</button>
<button type="button" value="America/Chicago">Chicago</button>
<button type="button" value="America/Sao_Paulo">São Paulo</button>
</div>
<div class="group" aria-labelledby="tz-europe" role="group">
<span id="tz-europe" class="group-label">Europe</span>
<button type="button" value="Europe/London">London</button>
<button type="button" value="Europe/Paris">Paris</button>
<button type="button" value="Europe/Berlin">Berlin</button>
</div>
<div class="group" aria-labelledby="tz-asia" role="group">
<span id="tz-asia" class="group-label">Asia</span>
<button type="button" value="Asia/Dubai">Dubai</button>
<button type="button" value="Asia/Singapore">Singapore</button>
<button type="button" value="Asia/Tokyo">Tokyo</button>
</div>
<p class="empty" data-sv-combobox-no-results>No city matches your search</p>
</sv-combobox>
</div>
<style>
.combobox-groups {
display: grid;
gap: 0.5em;
inline-size: min(100%, 22rem);
margin-inline: auto;
color: var(--sv-gray-2);
label {
justify-self: start;
cursor: pointer;
}
.group {
display: grid;
gap: 0.25em;
&:not(:has(button:not([hidden]))) {
display: none;
}
&:has(button:not([hidden])) ~ & {
padding-block-start: 0.25em;
border-block-start: solid var(--sv-bdr-sz-sm) var(--sv-gray-5);
}
}
.group-label {
padding: 0.5em 0.75em 0.25em;
font-size: 0.8em;
font-weight: 600;
color: var(--sv-gray-3);
text-transform: uppercase;
letter-spacing: 0.05em;
}
.group button {
padding: 0.5em 0.75em;
font: inherit;
line-height: normal;
color: var(--sv-gray-2);
text-align: start;
cursor: pointer;
background-color: transparent;
border: none;
border-radius: var(--sv-bdr-rad-sm);
&[data-sv-active] {
outline: auto;
}
&[aria-selected="true"] {
color: var(--sv-accent-content);
background-color: var(--sv-accent);
}
@media (hover: hover) and (pointer: fine) {
&:not([aria-selected="true"]):hover {
background-color: var(--sv-gray-5);
}
}
}
sv-combobox::part(content) {
gap: 0.25em;
max-block-size: min(var(--sv-flyout-available-height), 18rem);
}
.empty {
padding: 0.5em;
margin: 0;
color: var(--sv-gray-3);
}
}
</style>

Custom Values

Make a tag input with suggestions. allow-custom turns the typed text into a chip on Enter, and data-sv-combobox-custom offers it in the list.

Selection
<div class="combobox-custom-values">
<label for="labels">Labels</label>
<sv-combobox
id="labels"
allow-custom
hide-selected
multiple
placeholder="Type a label and press Enter"
>
<button type="button" value="bug">bug</button>
<button type="button" value="docs">docs</button>
<button type="button" value="feature">feature</button>
<button type="button" value="question">question</button>
<button type="button" data-sv-combobox-custom>
Add
<q data-sv-combobox-query></q>
</button>
</sv-combobox>
</div>
<style>
.combobox-custom-values {
display: grid;
gap: 0.5em;
inline-size: min(100%, 22rem);
margin-inline: auto;
color: var(--sv-gray-2);
label {
justify-self: start;
cursor: pointer;
}
}
</style>