Listbox Examples
The examples below assume that you have imported the component and set up the theme.
Basic
Let users pick one option from a list that stays on the page. Each button with a value is an option, value on the listbox sets the starting pick, and disabled on a button keeps the keyboard and clicks off it.
<div class="listbox-basic"> <label class="field-label" for="basic-fruit">Fruit</label>
<sv-listbox id="basic-fruit" value="banana"> <button value="apple">Apple</button> <button value="banana">Banana</button> <button value="cherry">Cherry</button> <button disabled value="grape">Grape</button> <button value="mango">Mango</button> <button value="peach">Peach</button> </sv-listbox></div>
<style> .listbox-basic { display: grid; gap: 0.5em; inline-size: min(100%, 16rem); margin-inline: auto; color: var(--sv-gray-2);
.field-label { font-weight: 600; } }</style>Groups
Sort the options under headings, with a second line of text in each option. A role="group" element named by aria-labelledby holds each set of options, and data-sv-listbox-label keeps the typeahead on the city name.
Alt+ArrowDown and Alt+ArrowUp jump between the groups.
<div class="listbox-groups"> <span id="groups-timezone-label" class="field-label">Time zone</span>
<sv-listbox aria-labelledby="groups-timezone-label" value="europe/berlin"> <div aria-labelledby="groups-europe" role="group"> <span id="groups-europe" class="heading">Europe</span> <button value="europe/lisbon"> <span data-sv-listbox-label>Lisbon</span> <small>UTC+0</small> </button> <button value="europe/berlin"> <span data-sv-listbox-label>Berlin</span> <small>UTC+1</small> </button> <button value="europe/athens"> <span data-sv-listbox-label>Athens</span> <small>UTC+2</small> </button> </div>
<div aria-labelledby="groups-asia" role="group"> <span id="groups-asia" class="heading">Asia</span> <button value="asia/dubai"> <span data-sv-listbox-label>Dubai</span> <small>UTC+4</small> </button> <button value="asia/kolkata"> <span data-sv-listbox-label>Kolkata</span> <small>UTC+5:30</small> </button> <button value="asia/tokyo"> <span data-sv-listbox-label>Tokyo</span> <small>UTC+9</small> </button> </div> </sv-listbox></div>
<style> .listbox-groups { display: grid; gap: 0.5em; inline-size: min(100%, 18rem); margin-inline: auto; color: var(--sv-gray-2);
.field-label { font-weight: 600; }
sv-listbox { max-block-size: 14rem; }
[role="group"] { display: grid; gap: 0.125em; }
.heading { padding: 0.5em 0.5em 0.25em; font-size: 0.75em; font-weight: 600; line-height: 1.25; color: var(--sv-gray-3); text-transform: uppercase; }
[role="option"] { display: flex; align-items: baseline; justify-content: space-between; padding: 0.5em; font: inherit; line-height: 1.25; color: inherit; cursor: pointer; background: none; border: none; border-radius: var(--sv-bdr-rad-sm);
&[data-sv-active] { background-color: var(--sv-gray-5); }
&[aria-selected="true"] { color: var(--sv-accent-content); background-color: var(--sv-accent); } }
sv-listbox:state(--keyboard-active) [data-sv-active] { outline: solid 2px currentcolor; outline-offset: -2px; }
small { opacity: 0.7; } }</style>Filter
Filter a long list with a search field above it. input-element makes the field filter the list and move through the matches with its arrow keys, and data-sv-listbox-empty shows a message when nothing matches.
No country matches.
<div class="listbox-filter"> <label class="field-label" for="filter-search">Country</label> <input id="filter-search" type="text" class="search" autocomplete="off" placeholder="Search" />
<sv-listbox aria-label="Countries" input-element="#filter-search"> <button value="ar">Argentina</button> <button value="au">Australia</button> <button value="br">Brazil</button> <button value="ca">Canada</button> <button value="de">Germany</button> <button value="in">India</button> <button value="jp">Japan</button> <button value="mx">Mexico</button> <button value="nl">Netherlands</button> <button value="ng">Nigeria</button> <button value="pt">Portugal</button> <button value="za">South Africa</button> <p class="empty" data-sv-listbox-empty>No country matches.</p> </sv-listbox></div>
<style> .listbox-filter { display: grid; gap: 0.5em; inline-size: min(100%, 18rem); margin-inline: auto; color: var(--sv-gray-2);
.field-label { font-weight: 600; }
.search { min-inline-size: 0; padding: 0.5em 0.75em; font: inherit; color: inherit; background-color: var(--sv-gray-6); border: solid 1px var(--sv-gray-5); border-radius: var(--sv-bdr-rad-md); }
sv-listbox { block-size: 14rem; }
.empty { padding: 0.5em; margin: 0; color: var(--sv-gray-3); } }</style>Horizontal
Put the options in a row, like a size picker. horizontal lays the options out in a row and moves through them with the left and right arrow keys.
<div class="listbox-horizontal"> <span id="horizontal-size-label" class="field-label">Size</span>
<sv-listbox aria-labelledby="horizontal-size-label" horizontal value="m"> <button value="xs">XS</button> <button value="s">S</button> <button value="m">M</button> <button value="l">L</button> <button disabled value="xl">XL</button> </sv-listbox></div>
<style> .listbox-horizontal { display: grid; gap: 0.5em; justify-items: center; color: var(--sv-gray-2);
.field-label { font-weight: 600; }
sv-listbox { gap: 0.25em; max-inline-size: 100%; }
[role="option"] { min-inline-size: 2.5em; text-align: center; } }</style>Grid
Lay out the options in rows and columns, like an emoji picker. grid moves the up and down arrow keys to the nearest option in the next row, and aria-label on each option names the emoji for screen readers and typeahead.
<div class="listbox-grid"> <span id="grid-reaction-label" class="field-label">Reaction</span>
<sv-listbox aria-labelledby="grid-reaction-label" grid> <button aria-label="Thumbs up" value="thumbs-up">👍</button> <button aria-label="Heart" value="heart">❤️</button> <button aria-label="Laugh" value="laugh">😂</button> <button aria-label="Wow" value="wow">😮</button> <button aria-label="Sad" value="sad">😢</button> <button aria-label="Party" value="party">🎉</button> <button aria-label="Fire" value="fire">🔥</button> <button aria-label="Eyes" value="eyes">👀</button> <button aria-label="Rocket" value="rocket">🚀</button> <button aria-label="Clap" value="clap">👏</button> <button aria-label="Thinking" value="thinking">🤔</button> <button aria-label="Check" value="check">✅</button> </sv-listbox></div>
<style> .listbox-grid { display: grid; gap: 0.5em; justify-items: center; color: var(--sv-gray-2);
.field-label { font-weight: 600; }
sv-listbox { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.25em; }
[role="option"] { padding: 0.25em; font-size: 1.5em; line-height: 1; text-align: center; } }</style>Form
Submit several picks with a form, and require at least one. name submits one entry per selected value, required blocks an empty submit, and aria-selected="true" marks the options that a reset returns to.
<form class="listbox-form"> <label class="field-label" for="form-days">Delivery days</label>
<sv-listbox id="form-days" name="days" required selection="multiple"> <button value="mon">Monday</button> <button aria-selected="true" value="tue">Tuesday</button> <button value="wed">Wednesday</button> <button aria-selected="true" value="thu">Thursday</button> <button value="fri">Friday</button> </sv-listbox>
<div class="actions"> <button type="reset" class="btn">Reset</button> <button type="submit" class="btn btn-primary">Submit</button> </div>
<output class="result"></output></form>
<script type="module"> const form = document.querySelector(".listbox-form"); const result = form.querySelector(".result");
form.addEventListener("submit", event => { event.preventDefault(); result.textContent = `days: ${new FormData(form).getAll("days").join(", ")}`; });
form.addEventListener("reset", () => { result.textContent = ""; });</script>
<style> .listbox-form { display: grid; gap: 0.5em; inline-size: min(100%, 16rem); margin-inline: auto; color: var(--sv-gray-2);
.field-label { font-weight: 600; }
.actions { display: flex; gap: 0.5em; justify-content: end; }
.result { min-block-size: 1.5em; font-family: monospace; } }</style>