{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "country-select",
  "type": "registry:ui",
  "title": "Country select",
  "description": "React country select with search, emoji flags and names localised by Intl.DisplayNames. Returns ISO codes, pins top countries and submits with forms.",
  "author": "MiniDev UI <https://ui.minidev.pro>",
  "dependencies": [
    "@base-ui/react",
    "lucide-react"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "registry/ui/country-select.tsx",
      "type": "registry:ui",
      "target": "components/ui/country-select.tsx",
      "content": "\"use client\"\nimport * as React from \"react\"\nimport { Combobox as ComboboxPrimitive } from \"@base-ui/react/combobox\"\nimport { CheckIcon, ChevronsUpDownIcon } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\n\n/**\n * CountrySelect — a searchable country picker whose names come from the browser in any language, with emoji flags and no data files.\n *\n * Use it for billing, shipping and sign-up forms. The value is an ISO 3166-1 alpha-2 code (\"US\", \"DE\").\n * Search matches the start of any word in the localised or English name, or the exact two-letter code, ignoring accents.\n * Pin the countries most of your users pick with `priority`. Built on Base UI Combobox, styled like\n * Combobox; use Combobox for any other long list and PhoneInput for phone numbers. The `countries(locale)`\n * helper returns the same sorted list for your own fields, such as the country column in TaxFields.\n *\n * @example\n * const [country, setCountry] = React.useState(\"US\")\n * <FormField id=\"country\" label=\"Country\">\n *   <CountrySelect id=\"country\" name=\"country\" value={country} onValueChange={setCountry} priority={[\"US\", \"GB\", \"CA\"]} />\n * </FormField>\n */\n\n// ISO 3166-1 alpha-2, all 249 assigned codes. Names come from Intl.DisplayNames, so no other data ships.\nconst CODES =\n  \"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 YE YT ZA ZM ZW\".split(\" \")\n\ntype Country = {\n  /** ISO 3166-1 alpha-2 code, upper case. */\n  code: string\n  /** Name in the requested locale. */\n  name: string\n  /** Flag emoji built from the code's regional indicator letters. */\n  flag: string\n}\n\n/** Flag emoji for an alpha-2 code: each letter maps to its regional indicator symbol. */\nconst flagOf = (code: string) => String.fromCodePoint(...[...code].map((c) => 0x1f1e6 + c.charCodeAt(0) - 65))\n\nfunction namer(locale: string) {\n  try {\n    return new Intl.DisplayNames([locale], { type: \"region\" })\n  } catch {\n    return new Intl.DisplayNames([\"en\"], { type: \"region\" })\n  }\n}\n\n/** Every country as `{ code, name, flag }`, named in `locale` and sorted the way that language sorts. */\nfunction countries(locale = \"en\"): Country[] {\n  const names = namer(locale)\n  const collator = new Intl.Collator(locale)\n  return CODES.map((code) => ({ code, name: names.of(code) ?? code, flag: flagOf(code) })).sort((a, b) =>\n    collator.compare(a.name, b.name)\n  )\n}\n\ntype Item = { value: string; label: string; english: string; flag: string }\ntype Group = { value: string; label: string; items: Item[] }\n\n// Folds case, accents and punctuation, so \"cote d iv\" finds \"Côte d’Ivoire\" and \"turkiye\" finds \"Türkiye\".\nconst fold = (s: string) => \" \" + s.normalize(\"NFD\").replace(/\\p{M}/gu, \"\").toLowerCase().replace(/[^\\p{L}\\p{N}]+/gu, \" \").trim()\n\n// Matches at the start of a word, so \"ger\" finds Germany but not Algeria or Niger.\nconst matches = (item: Item, query: string) => {\n  const q = fold(query)\n  if (q === \" \") return true\n  return fold(item.label).includes(q) || fold(item.english).includes(q) || item.value.toLowerCase() === q.trim()\n}\n\nconst toLabel = (i: Item) => i.label\nconst toValue = (i: Item) => i.value\nconst same = (a: Item, b: Item) => a.value === b.value\n\ntype CountrySelectProps = {\n  /** Controlled selection, an ISO 3166-1 alpha-2 code such as \"US\". Leave undefined for an uncontrolled select. */\n  value?: string\n  /** Initial code for the uncontrolled select. */\n  defaultValue?: string\n  /** Called with the picked country's upper-case code. */\n  onValueChange?: (code: string) => void\n  /** BCP 47 locale for the country names and their sort order, for example \"de\" or \"ja\". */\n  locale?: string\n  /** Codes pinned in a \"Suggested\" group above the rest, in this order. They are not repeated below. */\n  priority?: string[]\n  /** Codes to leave out, for example countries you do not ship to. */\n  exclude?: string[]\n  /** Show the emoji flag before each name. */\n  showFlag?: boolean\n  /** Trigger text while nothing is selected. */\n  placeholder?: string\n  /** Placeholder of the search field inside the popup. */\n  searchPlaceholder?: string\n  /** Shown when the search matches nothing. */\n  emptyText?: string\n  /** Heading of the `priority` group. */\n  priorityLabel?: string\n  disabled?: boolean\n  /** Id for the trigger, so a `<label htmlFor>` or FormField can name it. */\n  id?: string\n  /** Submits the selected code under this name inside a `<form>`. */\n  name?: string\n  /** Requires a selection before the form submits. */\n  required?: boolean\n  /** Accessible name when there is no visible label. */\n  \"aria-label\"?: string\n  /** Classes for the trigger button (width defaults to w-64). */\n  className?: string\n}\n\nfunction CountrySelect({\n  value,\n  defaultValue,\n  onValueChange,\n  locale = \"en\",\n  priority = [],\n  exclude = [],\n  showFlag = true,\n  placeholder = \"Select country\",\n  searchPlaceholder = \"Search countries…\",\n  emptyText = \"No country found\",\n  priorityLabel = \"Suggested\",\n  disabled,\n  id,\n  name,\n  required,\n  \"aria-label\": ariaLabel,\n  className,\n}: CountrySelectProps) {\n  const [inner, setInner] = React.useState(defaultValue?.toUpperCase())\n  const current = (value ?? inner)?.toUpperCase()\n  const priorityKey = priority.join(\",\").toUpperCase()\n  const excludeKey = exclude.join(\",\").toUpperCase()\n\n  const groups = React.useMemo<Group[]>(() => {\n    const english = namer(\"en\")\n    const skip = new Set(excludeKey.split(\",\"))\n    const all = countries(locale)\n      .filter((c) => !skip.has(c.code))\n      .map((c) => ({ value: c.code, label: c.name, english: english.of(c.code) ?? c.code, flag: c.flag }))\n    const pinned = priorityKey\n      .split(\",\")\n      .map((code) => all.find((c) => c.value === code))\n      .filter((c): c is Item => Boolean(c))\n    const rest = all.filter((c) => !pinned.includes(c))\n    return pinned.length\n      ? [{ value: \"priority\", label: priorityLabel, items: pinned }, { value: \"all\", label: \"All countries\", items: rest }]\n      : [{ value: \"all\", label: \"All countries\", items: rest }]\n  }, [locale, priorityKey, excludeKey, priorityLabel])\n\n  const selected = React.useMemo(\n    () => groups.flatMap((g) => g.items).find((i) => i.value === current) ?? null,\n    [groups, current]\n  )\n  const grouped = groups.length > 1\n\n  return (\n    <div data-slot=\"country-select\">\n      <ComboboxPrimitive.Root\n        items={groups}\n        value={selected}\n        onValueChange={(next: Item | null) => {\n          if (!next) return\n          if (value === undefined) setInner(next.value)\n          onValueChange?.(next.value)\n        }}\n        filter={matches}\n        itemToStringLabel={toLabel}\n        itemToStringValue={toValue}\n        isItemEqualToValue={same}\n        disabled={disabled}\n        name={name}\n        required={required}\n        autoComplete=\"country\"\n        autoHighlight\n      >\n        <ComboboxPrimitive.Trigger\n          id={id}\n          aria-label={ariaLabel}\n          data-placeholder={selected ? undefined : \"\"}\n          className={cn(\n            \"inline-flex h-9 w-64 items-center justify-between gap-2 rounded-lg border border-border bg-surface px-3 text-sm text-fg\",\n            \"shadow-highlight outline-none transition-[border-color] duration-[70ms] ease-hairline\",\n            \"hover:border-fg-subtle focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-bg\",\n            \"data-disabled:cursor-not-allowed data-disabled:opacity-50\",\n            className\n          )}\n        >\n          <span className={cn(\"flex min-w-0 items-center gap-2\", !selected && \"text-fg-subtle\")}>\n            {selected && showFlag ? (\n              <span aria-hidden className=\"shrink-0 text-base leading-none\">\n                {selected.flag}\n              </span>\n            ) : null}\n            {/* Server and browser ICU can name a few countries differently; the browser's name wins. */}\n            <span className=\"truncate\" suppressHydrationWarning>\n              {selected?.label ?? placeholder}\n            </span>\n          </span>\n          <ChevronsUpDownIcon aria-hidden className=\"size-4 shrink-0 text-fg-muted\" />\n        </ComboboxPrimitive.Trigger>\n        <ComboboxPrimitive.Portal>\n          <ComboboxPrimitive.Positioner align=\"start\" sideOffset={6} className=\"isolate z-50 outline-none\">\n            <ComboboxPrimitive.Popup\n              data-slot=\"country-select-content\"\n              aria-label={ariaLabel ?? placeholder}\n              className={cn(\n                \"w-(--anchor-width) min-w-64 max-w-(--available-width) origin-(--transform-origin) rounded-xl border border-border bg-raised p-1 text-fg shadow-overlay outline-none\",\n                \"duration-200 ease-hairline data-closed:duration-[160ms]\",\n                \"data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95\"\n              )}\n            >\n              <ComboboxPrimitive.Input\n                aria-label=\"Search countries\"\n                placeholder={searchPlaceholder}\n                className=\"mb-1 h-8 w-full rounded-md border border-border bg-surface px-2 text-sm text-fg outline-none placeholder:text-fg-subtle focus-visible:border-accent focus-visible:shadow-[0_0_0_3px_var(--accent-soft)]\"\n              />\n              <ComboboxPrimitive.Empty className=\"px-2 py-2 text-xs text-fg-muted empty:hidden\">{emptyText}</ComboboxPrimitive.Empty>\n              <ComboboxPrimitive.List className=\"max-h-[min(18rem,var(--available-height))] overflow-auto overscroll-contain scroll-py-1\">\n                {(group: Group) => (\n                  <ComboboxPrimitive.Group\n                    key={group.value}\n                    items={group.items}\n                    className={cn(grouped && \"not-first:mt-1 not-first:border-t not-first:border-border not-first:pt-1\")}\n                  >\n                    {grouped ? (\n                      <ComboboxPrimitive.GroupLabel className=\"px-2.5 pt-1.5 pb-1 text-xs font-medium text-fg-subtle\">\n                        {group.label}\n                      </ComboboxPrimitive.GroupLabel>\n                    ) : null}\n                    <ComboboxPrimitive.Collection>\n                      {(item: Item) => (\n                        <ComboboxPrimitive.Item\n                          key={item.value}\n                          value={item}\n                          className={cn(\n                            \"flex w-full cursor-default items-center gap-2 rounded-lg px-2.5 py-1.5 text-left text-sm outline-none select-none\",\n                            \"transition-[background-color] duration-[70ms] data-highlighted:bg-sunken\"\n                          )}\n                        >\n                          <span className=\"grid size-4 shrink-0 place-items-center\">\n                            <ComboboxPrimitive.ItemIndicator>\n                              <CheckIcon aria-hidden className=\"size-4 text-accent-fg\" />\n                            </ComboboxPrimitive.ItemIndicator>\n                          </span>\n                          {showFlag ? (\n                            <span aria-hidden className=\"shrink-0 text-base leading-none\">\n                              {item.flag}\n                            </span>\n                          ) : null}\n                          <span className=\"min-w-0 flex-1 truncate\">{item.label}</span>\n                          <span aria-hidden className=\"shrink-0 font-mono text-[11px] text-fg-subtle\">\n                            {item.value}\n                          </span>\n                        </ComboboxPrimitive.Item>\n                      )}\n                    </ComboboxPrimitive.Collection>\n                  </ComboboxPrimitive.Group>\n                )}\n              </ComboboxPrimitive.List>\n            </ComboboxPrimitive.Popup>\n          </ComboboxPrimitive.Positioner>\n        </ComboboxPrimitive.Portal>\n      </ComboboxPrimitive.Root>\n    </div>\n  )\n}\n\nexport { CountrySelect, countries }\nexport type { CountrySelectProps, Country }\n"
    }
  ],
  "categories": [
    "forms"
  ],
  "docs": "import { CountrySelect } from \"@/components/ui/country-select\"\nNeeds the MiniDev tokens stylesheet (https://ui.minidev.pro/r/styles.css) imported after Tailwind.\nDocs: https://ui.minidev.pro/docs/country-select · Markdown for agents: https://ui.minidev.pro/docs/country-select.md",
  "meta": {
    "tier": "free",
    "kind": "ui",
    "category": "forms",
    "import": "@/components/ui/country-select",
    "npmImport": "minidev-ui-kit/ui/country-select",
    "exports": [
      "CountrySelect"
    ],
    "related": [
      "combobox",
      "phone-input",
      "tax-fields"
    ],
    "docsUrl": "https://ui.minidev.pro/docs/country-select",
    "markdownUrl": "https://ui.minidev.pro/docs/country-select.md",
    "keywords": [
      "shadcn country select",
      "react country dropdown with flags",
      "country picker react"
    ]
  }
}
