All blocks
Country Picker
Searchable country picker with flags, Intl-localized names, and ISO 3166-1 values — built on the accessible combobox pattern.
Built withCombobox
Afghanistan
Åland Islands
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua & Barbuda
Argentina
Armenia
Aruba
Australia
Austria
Azerbaijan
Bahamas
Bahrain
Bangladesh
Barbados
Belarus
Belgium
Belize
Benin
Bermuda
Bhutan
Bolivia
Bosnia & Herzegovina
Botswana
Bouvet Island
Brazil
British Indian Ocean Territory
British Virgin Islands
Brunei
Bulgaria
Burkina Faso
Burundi
Cambodia
Cameroon
Canada
Cape Verde
Caribbean Netherlands
Cayman Islands
Central African Republic
Chad
Chile
China
Christmas Island
Cocos (Keeling) Islands
Colombia
Comoros
Congo - Brazzaville
Congo - Kinshasa
Cook Islands
Costa Rica
Côte d’Ivoire
Croatia
Cuba
Curaçao
Cyprus
Czechia
Denmark
Djibouti
Dominica
Dominican Republic
Ecuador
Egypt
El Salvador
Equatorial Guinea
Eritrea
Estonia
Eswatini
Ethiopia
Falkland Islands
Faroe Islands
Fiji
Finland
France
French Guiana
French Polynesia
French Southern Territories
Gabon
Gambia
Georgia
Germany
Ghana
Gibraltar
Greece
Greenland
Grenada
Guadeloupe
Guam
Guatemala
Guernsey
Guinea
Guinea-Bissau
Guyana
Haiti
Heard & McDonald Islands
Honduras
Hong Kong SAR China
Hungary
Iceland
India
Indonesia
Iran
Iraq
Ireland
Isle of Man
Israel
Italy
Jamaica
Japan
Jersey
Jordan
Kazakhstan
Kenya
Kiribati
Kosovo
Kuwait
Kyrgyzstan
Laos
Latvia
Lebanon
Lesotho
Liberia
Libya
Liechtenstein
Lithuania
Luxembourg
Macao SAR China
Madagascar
Malawi
Malaysia
Maldives
Mali
Malta
Marshall Islands
Martinique
Mauritania
Mauritius
Mayotte
Mexico
Micronesia
Moldova
Monaco
Mongolia
Montenegro
Montserrat
Morocco
Mozambique
Myanmar (Burma)
Namibia
Nauru
Nepal
Netherlands
New Caledonia
New Zealand
Nicaragua
Niger
Nigeria
Niue
Norfolk Island
North Korea
North Macedonia
Northern Mariana Islands
Norway
Oman
Pakistan
Palau
Palestinian Territories
Panama
Papua New Guinea
Paraguay
Peru
Philippines
Pitcairn Islands
Poland
Portugal
Puerto Rico
Qatar
Réunion
Romania
Russia
Rwanda
Samoa
San Marino
São Tomé & Príncipe
Saudi Arabia
Senegal
Serbia
Seychelles
Sierra Leone
Singapore
Sint Maarten
Slovakia
Slovenia
Solomon Islands
Somalia
South Africa
South Georgia & South Sandwich Islands
South Korea
South Sudan
Spain
Sri Lanka
St. Barthélemy
St. Helena
St. Kitts & Nevis
St. Lucia
St. Martin
St. Pierre & Miquelon
St. Vincent & Grenadines
Sudan
Suriname
Svalbard & Jan Mayen
Sweden
Switzerland
Syria
Taiwan
Tajikistan
Tanzania
Thailand
Timor-Leste
Togo
Tokelau
Tonga
Trinidad & Tobago
Tunisia
Türkiye
Turkmenistan
Turks & Caicos Islands
Tuvalu
U.S. Outlying Islands
U.S. Virgin Islands
Uganda
Ukraine
United Arab Emirates
United Kingdom
United States
Uruguay
Uzbekistan
Vanuatu
Vatican City
Venezuela
Vietnam
Wallis & Futuna
Western Sahara
Yemen
Zambia
Zimbabwe
Install
1npx stylexblocks add country-pickerSource
components/blocks/ark-ui/country-picker.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177import * as stylex from "@stylexjs/stylex";import { useMemo, useState } from "react";import { useListCollection } from "@ark-ui/react/collection";import { useFilter } from "@ark-ui/react/locale";import { Combobox, type ComboboxVariantProps } from "#components/ui/ark-ui/combobox.tsx";import type { StyleOverrideProps } from "#lib/recipe-props.ts";
/** * ISO 3166-1 alpha-2 codes (plus XK for Kosovo, which most products need). * Display names and sort order come from the platform via Intl, so the list * localizes itself and never goes stale. */export const countryCodes = [ "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "XK", "YE", "YT", "ZA", "ZM", "ZW",];
export interface Country { /** ISO 3166-1 alpha-2 code — what your backend receives. */ value: string; /** Localized display name from Intl.DisplayNames. */ label: string;}
/** Localized, collator-sorted country list; `locale` defaults to the runtime's. */export function getCountries(locale?: string): Country[] { const displayNames = new Intl.DisplayNames(locale ? [locale] : undefined, { type: "region" }); const collator = new Intl.Collator(locale); return countryCodes .map((value) => ({ value, label: displayNames.of(value) ?? value })) .sort((a, b) => collator.compare(a.label, b.label));}
/** "NL" → 🇳🇱 via regional indicator symbols; falls back to letters on Windows. */function flagEmoji(code: string): string { return String.fromCodePoint(...[...code].map((c) => 0x1f1a5 + c.charCodeAt(0)));}
const styles = stylex.create({ flag: { flexShrink: 0, marginInlineEnd: "0.5rem", },});
export interface CountryPickerProps extends ComboboxVariantProps, StyleOverrideProps { label?: string; /** Trigger text while nothing is selected. */ placeholder?: string; searchPlaceholder?: string; /** ISO 3166-1 alpha-2 code of the initial selection. */ defaultValue?: string; /** ISO 3166-1 alpha-2 code of the controlled selection. */ value?: string; onValueChange?: (code: string) => void; /** Posts the selected code with native form submits via a hidden input. */ name?: string; /** Display-name locale; defaults to the runtime's locale. */ locale?: string; /** Override the offered countries, e.g. only shipping destinations. */ items?: Country[]; disabled?: boolean;}
/** * CountryPicker — country picker with a select-style trigger and a search * input inside the popup. The whole thing is one combobox machine, not a * Select with an input bolted in: opening moves focus to the panel's input, * which carries the ARIA combobox wiring (aria-expanded, aria-controls, * aria-activedescendant), so filtering stays screen-reader-accessible. * Speaks ISO 3166-1 alpha-2 on the wire; names localize through Intl. */export function CountryPicker({ label = "Country", placeholder = "Select country", searchPlaceholder = "Search countries…", defaultValue, value, onValueChange, name, locale, items, disabled, variant = "outline", size = "md", palette, style,}: CountryPickerProps) { const countries = useMemo(() => items ?? getCountries(locale), [items, locale]); const [inner, setInner] = useState(defaultValue ?? ""); const selected = value ?? inner; const selectedCountry = countries.find((c) => c.value === selected);
const { contains } = useFilter({ sensitivity: "base" }); const { collection, filter, reset } = useListCollection({ initialItems: countries, itemToString: (item: Country) => item.label, itemToValue: (item: Country) => item.value, // match the localized name loosely, or the ISO code exactly filter: (itemText, filterText, item) => contains(itemText, filterText) || item.value.toLowerCase() === filterText.toLowerCase(), });
return ( <Combobox.Root collection={collection} defaultValue={defaultValue ? [defaultValue] : undefined} value={value !== undefined ? (value ? [value] : []) : undefined} onValueChange={(details) => { const code = details.value[0] ?? ""; setInner(code); onValueChange?.(code); }} onInputValueChange={(details) => filter(details.inputValue)} // the search is transient per open — a fresh panel shows every country onOpenChange={(details) => { if (details.open) reset(); }} selectionBehavior="clear" disabled={disabled} variant={variant} size={size} palette={palette} style={style} > <Combobox.Label>{label}</Combobox.Label> <Combobox.Control> <Combobox.FieldTrigger> <Combobox.FieldValue placeholder={placeholder}> {selectedCountry ? ( <> <span {...stylex.props(styles.flag)} aria-hidden="true"> {flagEmoji(selectedCountry.value)} </span> {selectedCountry.label} </> ) : null} </Combobox.FieldValue> </Combobox.FieldTrigger> </Combobox.Control> <Combobox.Positioner> <Combobox.Panel> <Combobox.Search placeholder={searchPlaceholder} /> <Combobox.PanelList> {collection.items.map((country) => ( <Combobox.Item key={country.value} item={country}> <span {...stylex.props(styles.flag)} aria-hidden="true"> {flagEmoji(country.value)} </span> <Combobox.ItemText>{country.label}</Combobox.ItemText> <Combobox.ItemIndicator /> </Combobox.Item> ))} {collection.items.length === 0 ? ( <Combobox.Empty>No countries found</Combobox.Empty> ) : null} </Combobox.PanelList> </Combobox.Panel> </Combobox.Positioner> {name ? <input type="hidden" name={name} value={selected} /> : null} </Combobox.Root> );}