Country Picker
Build a country picker from data, with a flag beside every name and the picked flag in the field. A custom filter function matches the name or the exact country code, data-sv-combobox-label marks the name as the label, and the before slot holds the flag of the picked country.
No country matches your search
Code
The following example assumes that you have imported the components and set up the theme.
<div class="country-picker">6 collapsed lines
<label for="country">Country</label>
<sv-combobox id="country" name="country" placeholder="Search by name or code"> <img id="country-flag" class="selected-flag" alt="" hidden slot="before" /> <p class="empty" data-sv-combobox-no-results>No country matches your search</p> </sv-combobox></div>
<template id="country-option">4 collapsed lines
<button type="button" class="option"> <img alt="" loading="lazy" /> <span data-sv-combobox-label></span> </button></template>
<script type="application/x-typescript" module>46 collapsed lines
interface Country { code: string; name: string; }
const countriesResponse = await fetch( "https://cdn.jsdelivr.net/npm/country-list@2.3.0/data.json" ); const countriesData: Country[] = await countriesResponse.json();
const countryCombobox = document.querySelector<Combobox>("#country")!; const selectedFlag = document.querySelector<HTMLImageElement>("#country-flag")!; const optionTemplate = document.querySelector<HTMLTemplateElement>("#country-option")!;
function getFlagUrl(countryCode: string) { return `https://hatscripts.github.io/circle-flags/flags/${countryCode}.svg`; }
for (const country of countriesData) { const option = optionTemplate.content.firstElementChild!.cloneNode(true) as HTMLButtonElement; const countryCode = country.code.toLowerCase();
option.value = countryCode; option.querySelector("img")!.src = getFlagUrl(countryCode); option.querySelector("[data-sv-combobox-label]")!.textContent = country.name;
countryCombobox.append(option); }
// Match the name anywhere, or the exact country code countryCombobox.filter = (option, query) => { const lowerCaseQuery = query.toLowerCase(); return ( option.value === lowerCaseQuery || option.textContent!.toLowerCase().includes(lowerCaseQuery) ); };
// Show the flag of the picked country at the start of the field countryCombobox.addEventListener("valuechange", () => { const countryCode = countryCombobox.value as string; selectedFlag.hidden = !countryCode;
if (countryCode) { selectedFlag.src = getFlagUrl(countryCode); } });</script>
<style>48 collapsed lines
.country-picker { display: grid; gap: 0.5em; inline-size: min(100%, 22rem); margin-inline: auto; color: var(--sv-gray-2);
label { justify-self: start; cursor: pointer; }
.option { display: flex; gap: 0.75em; align-items: center; min-inline-size: 0;
img { flex: none; inline-size: 1.5em; block-size: 1.5em; }
span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } }
.selected-flag { flex: none; inline-size: 1.5em; block-size: 1.5em; margin-inline-start: 0.2em; }
sv-combobox::part(content) { max-block-size: min(var(--sv-flyout-available-height), 20rem); }
.empty { padding: 0.5em; margin: 0; color: var(--sv-gray-3); } }</style>