{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "field-phone-country",
  "type": "registry:component",
  "title": "Phone Field with Country",
  "description": "A phone input with a searchable country picker and a per-country mask whose remaining digits show as faint ghost characters. Pasting a number that starts with a dial code switches the country on its own.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "forms",
    "phone",
    "mask",
    "country",
    "e164",
    "input",
    "verification"
  ],
  "docs": "https://hyperblur.dev/effects/field-phone-country",
  "files": [
    {
      "path": "registry/hyperblur/field-phone-country.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/field-phone-country.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface FieldPhoneCountryOption {\n  code: string;\n  name: string;\n  dial: string;\n  mask: string;\n  colors: [string, string, string];\n}\n\nexport interface FieldPhoneCountryValue {\n  country: string;\n  dial: string;\n  national: string;\n  e164: string;\n  complete: boolean;\n}\n\nexport interface FieldPhoneCountryProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n  label?: string;\n  accent?: string;\n  countries?: FieldPhoneCountryOption[];\n  defaultCountry?: string;\n  defaultValue?: string;\n  hint?: string;\n  disabled?: boolean;\n  onChange?: (value: FieldPhoneCountryValue) => void;\n}\n\nexport const fieldPhoneCountries: FieldPhoneCountryOption[] = [\n  { code: \"US\", name: \"United States\", dial: \"+1\", mask: \"(###) ###-####\", colors: [\"#3c3b6e\", \"#ffffff\", \"#b22234\"] },\n  { code: \"CA\", name: \"Canada\", dial: \"+1\", mask: \"(###) ###-####\", colors: [\"#d52b1e\", \"#ffffff\", \"#d52b1e\"] },\n  { code: \"BR\", name: \"Brazil\", dial: \"+55\", mask: \"(##) #####-####\", colors: [\"#009c3b\", \"#ffdf00\", \"#002776\"] },\n  { code: \"GB\", name: \"United Kingdom\", dial: \"+44\", mask: \"#### ######\", colors: [\"#012169\", \"#ffffff\", \"#c8102e\"] },\n  { code: \"DE\", name: \"Germany\", dial: \"+49\", mask: \"#### #######\", colors: [\"#141414\", \"#dd0000\", \"#ffce00\"] },\n  { code: \"FR\", name: \"France\", dial: \"+33\", mask: \"# ## ## ## ##\", colors: [\"#002395\", \"#ffffff\", \"#ed2939\"] },\n  { code: \"ES\", name: \"Spain\", dial: \"+34\", mask: \"### ## ## ##\", colors: [\"#aa151b\", \"#f1bf00\", \"#aa151b\"] },\n  { code: \"PT\", name: \"Portugal\", dial: \"+351\", mask: \"### ### ###\", colors: [\"#006600\", \"#006600\", \"#ff0000\"] },\n  { code: \"MX\", name: \"Mexico\", dial: \"+52\", mask: \"## #### ####\", colors: [\"#006847\", \"#ffffff\", \"#ce1126\"] },\n  { code: \"IN\", name: \"India\", dial: \"+91\", mask: \"##### #####\", colors: [\"#ff9933\", \"#ffffff\", \"#138808\"] },\n  { code: \"JP\", name: \"Japan\", dial: \"+81\", mask: \"##-####-####\", colors: [\"#ffffff\", \"#bc002d\", \"#ffffff\"] },\n  { code: \"AU\", name: \"Australia\", dial: \"+61\", mask: \"### ### ###\", colors: [\"#00008b\", \"#ffffff\", \"#00008b\"] },\n];\n\nconst css = `\n.hb-xg2 {\n  --hb-xg2-c: #2f7bff;\n  position: relative;\n  box-sizing: border-box;\n  width: 100%;\n  font-family: inherit;\n  color: #ededed;\n}\n.hb-xg2 *, .hb-xg2 *::before, .hb-xg2 *::after { box-sizing: border-box; }\n.hb-xg2-label { display: block; margin: 0 0 7px; font-size: 12.5px; font-weight: 500; color: #a1a1a1; }\n.hb-xg2-box {\n  display: flex;\n  align-items: stretch;\n  height: 46px;\n  border: 1px solid #262626;\n  border-radius: 12px;\n  background: linear-gradient(180deg, #161616, #0f0f0f);\n  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.045), 0 1px 2px rgba(0, 0, 0, 0.5);\n  transition: border-color 0.2s ease, box-shadow 0.25s ease;\n}\n.hb-xg2-box:hover { border-color: #333; }\n.hb-xg2-box:focus-within {\n  border-color: var(--hb-xg2-c);\n  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.045), 0 0 0 4px color-mix(in srgb, var(--hb-xg2-c) 16%, transparent);\n}\n.hb-xg2[data-disabled=\"true\"] { opacity: 0.5; pointer-events: none; }\n.hb-xg2-trigger {\n  display: flex;\n  align-items: center;\n  gap: 7px;\n  flex: none;\n  margin: 0;\n  padding: 0 10px 0 12px;\n  border: 0;\n  border-right: 1px solid #222;\n  border-radius: 11px 0 0 11px;\n  background: transparent;\n  color: #ededed;\n  font: inherit;\n  font-size: 13.5px;\n  cursor: pointer;\n  transition: background 0.2s ease;\n}\n.hb-xg2-trigger:hover { background: rgba(255, 255, 255, 0.03); }\n.hb-xg2-trigger:focus-visible { outline: 2px solid var(--hb-xg2-c); outline-offset: -2px; }\n.hb-xg2-flag {\n  position: relative;\n  width: 20px;\n  height: 14px;\n  flex: none;\n  border-radius: 3px;\n  overflow: hidden;\n  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.14), 0 1px 2px rgba(0, 0, 0, 0.5);\n}\n.hb-xg2-flag::after { content: \"\"; position: absolute; inset: 0; background: linear-gradient(180deg, rgba(255, 255, 255, 0.22), transparent 55%); }\n.hb-xg2-dial { color: #a1a1a1; font-variant-numeric: tabular-nums; }\n.hb-xg2-chev { width: 10px; height: 10px; color: #6f6f6f; transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1); }\n.hb-xg2[data-open=\"true\"] .hb-xg2-chev { transform: rotate(180deg); }\n.hb-xg2-wrap { position: relative; flex: 1; min-width: 0; }\n.hb-xg2-in, .hb-xg2-ghost {\n  position: absolute;\n  inset: 0;\n  margin: 0;\n  padding: 0 40px 0 12px;\n  font: inherit;\n  font-size: 14.5px;\n  letter-spacing: 0.015em;\n  font-variant-numeric: tabular-nums;\n  line-height: 44px;\n  white-space: pre;\n}\n.hb-xg2-in {\n  width: 100%;\n  border: 0;\n  outline: 0;\n  background: transparent;\n  color: #ededed;\n  caret-color: var(--hb-xg2-c);\n}\n.hb-xg2-ghost { pointer-events: none; overflow: hidden; color: transparent; }\n.hb-xg2-ghost b { font-weight: inherit; color: #474747; transition: color 0.2s ease; }\n.hb-xg2-wrap:focus-within .hb-xg2-ghost b { color: #5a5a5a; }\n.hb-xg2-mark {\n  position: absolute;\n  right: 12px;\n  top: 50%;\n  width: 18px;\n  height: 18px;\n  margin-top: -9px;\n  display: grid;\n  place-items: center;\n  border-radius: 50%;\n  background: #1f8f55;\n  color: #fff;\n  transform: scale(0.4);\n  opacity: 0;\n  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s ease;\n}\n.hb-xg2-mark svg { width: 10px; height: 10px; }\n.hb-xg2[data-complete=\"true\"] .hb-xg2-mark { transform: scale(1); opacity: 1; }\n.hb-xg2-help { display: flex; justify-content: space-between; gap: 8px; margin-top: 8px; font-size: 12px; color: #8f8f8f; }\n.hb-xg2-help span:last-child { font-variant-numeric: tabular-nums; color: #6f6f6f; }\n.hb-xg2[data-complete=\"true\"] .hb-xg2-help span:first-child { color: #5fd38d; }\n.hb-xg2-pop {\n  position: absolute;\n  z-index: 30;\n  left: 0;\n  right: 0;\n  top: calc(100% + 6px);\n  padding: 6px;\n  border: 1px solid #2a2a2a;\n  border-radius: 14px;\n  background: rgba(20, 20, 20, 0.96);\n  backdrop-filter: blur(14px);\n  -webkit-backdrop-filter: blur(14px);\n  box-shadow: 0 24px 48px -16px rgba(0, 0, 0, 0.8), inset 0 1px 0 rgba(255, 255, 255, 0.05);\n  transform-origin: top left;\n  animation: hb-xg2-in 0.22s cubic-bezier(0.22, 1, 0.36, 1);\n}\n.hb-xg2-search {\n  width: 100%;\n  height: 34px;\n  margin: 0 0 4px;\n  padding: 0 10px;\n  border: 1px solid #262626;\n  border-radius: 9px;\n  background: #0d0d0d;\n  color: #ededed;\n  font: inherit;\n  font-size: 13px;\n  outline: 0;\n}\n.hb-xg2-search:focus { border-color: color-mix(in srgb, var(--hb-xg2-c) 60%, #262626); }\n.hb-xg2-list { max-height: 196px; margin: 0; padding: 0; list-style: none; overflow-y: auto; scrollbar-width: thin; }\n.hb-xg2-opt {\n  display: flex;\n  align-items: center;\n  gap: 9px;\n  height: 34px;\n  padding: 0 9px;\n  border-radius: 8px;\n  font-size: 13px;\n  cursor: pointer;\n}\n.hb-xg2-opt[data-active=\"true\"] { background: rgba(255, 255, 255, 0.06); }\n.hb-xg2-opt[aria-selected=\"true\"] .hb-xg2-oname::after { content: \"\"; display: inline-block; width: 5px; height: 5px; margin-left: 7px; border-radius: 50%; background: var(--hb-xg2-c); vertical-align: middle; }\n.hb-xg2-oname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }\n.hb-xg2-odial { color: #8f8f8f; font-variant-numeric: tabular-nums; }\n.hb-xg2-empty { padding: 12px 9px; font-size: 12.5px; color: #8f8f8f; }\n@keyframes hb-xg2-in { from { opacity: 0; transform: translateY(-4px) scale(0.98); } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-xg2 *, .hb-xg2-pop { transition: none !important; animation: none !important; }\n}\n`;\n\nfunction applyMask(digits: string, mask: string) {\n  let out = \"\";\n  let i = 0;\n  for (const ch of mask) {\n    if (i >= digits.length) break;\n    if (ch === \"#\") {\n      out += digits[i];\n      i++;\n    } else out += ch;\n  }\n  return out;\n}\n\nfunction slotsOf(mask: string) {\n  return mask.split(\"\").filter((c) => c === \"#\").length;\n}\n\nfunction flagStyle(c: [string, string, string]): React.CSSProperties {\n  return { background: `linear-gradient(180deg, ${c[0]} 0 33.4%, ${c[1]} 33.4% 66.6%, ${c[2]} 66.6% 100%)` };\n}\n\nexport function FieldPhoneCountry({\n  label = \"Phone number\",\n  accent = \"#2f7bff\",\n  countries = fieldPhoneCountries,\n  defaultCountry = \"US\",\n  defaultValue = \"\",\n  hint = \"We'll text a code to confirm it.\",\n  disabled = false,\n  onChange,\n  className = \"\",\n  style,\n  ...rest\n}: FieldPhoneCountryProps) {\n  const id = React.useId();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const inputRef = React.useRef<HTMLInputElement>(null);\n  const searchRef = React.useRef<HTMLInputElement>(null);\n  const triggerRef = React.useRef<HTMLButtonElement>(null);\n  const [code, setCode] = React.useState(defaultCountry);\n  const country = countries.find((c) => c.code === code) ?? countries[0];\n  const [digits, setDigits] = React.useState(defaultValue.replace(/\\D/g, \"\").slice(0, slotsOf(country.mask)));\n  const [open, setOpen] = React.useState(false);\n  const [query, setQuery] = React.useState(\"\");\n  const [active, setActive] = React.useState(0);\n\n  const slots = slotsOf(country.mask);\n  const formatted = applyMask(digits, country.mask);\n  const ghost = country.mask.slice(formatted.length).replace(/#/g, \"0\");\n  const complete = digits.length === slots;\n\n  const q = query.trim().toLowerCase();\n  const filtered = countries.filter(\n    (c) => !q || c.name.toLowerCase().includes(q) || c.code.toLowerCase() === q || c.dial.includes(q.replace(/^\\+?/, \"+\")),\n  );\n\n  function emit(d: string, c: FieldPhoneCountryOption) {\n    onChange?.({\n      country: c.code,\n      dial: c.dial,\n      national: d,\n      e164: d ? `${c.dial}${d}` : \"\",\n      complete: d.length === slotsOf(c.mask),\n    });\n  }\n\n  React.useEffect(() => {\n    if (!open) return;\n    function down(e: PointerEvent) {\n      if (!rootRef.current?.contains(e.target as Node)) setOpen(false);\n    }\n    document.addEventListener(\"pointerdown\", down);\n    searchRef.current?.focus();\n    return () => document.removeEventListener(\"pointerdown\", down);\n  }, [open]);\n\n  function pick(c: FieldPhoneCountryOption) {\n    setCode(c.code);\n    const d = digits.slice(0, slotsOf(c.mask));\n    setDigits(d);\n    emit(d, c);\n    setOpen(false);\n    setQuery(\"\");\n    requestAnimationFrame(() => inputRef.current?.focus());\n  }\n\n  function onInput(e: React.ChangeEvent<HTMLInputElement>) {\n    const el = e.target;\n    let raw = el.value;\n    let c = country;\n    if (raw.trim().startsWith(\"+\")) {\n      const plain = raw.replace(/[^\\d+]/g, \"\");\n      const match = [...countries]\n        .sort((a, b) => b.dial.length - a.dial.length)\n        .find((x) => plain.startsWith(x.dial));\n      if (match) {\n        c = match.dial === country.dial ? country : match;\n        raw = plain.slice(match.dial.length);\n        setCode(c.code);\n      }\n    }\n    const caret = el.selectionStart ?? el.value.length;\n    const before = el.value.slice(0, caret).replace(/\\D/g, \"\").length;\n    const d = raw.replace(/\\D/g, \"\").slice(0, slotsOf(c.mask));\n    setDigits(d);\n    emit(d, c);\n    const f = applyMask(d, c.mask);\n    requestAnimationFrame(() => {\n      const target = Math.min(before, d.length);\n      let seen = 0;\n      let pos = target === 0 ? 0 : f.length;\n      for (let i = 0; i < f.length && target > 0; i++) {\n        if (/\\d/.test(f[i])) seen++;\n        if (seen === target) {\n          pos = i + 1;\n          break;\n        }\n      }\n      if (document.activeElement === inputRef.current) inputRef.current?.setSelectionRange(pos, pos);\n    });\n  }\n\n  function onSearchKey(e: React.KeyboardEvent<HTMLInputElement>) {\n    if (e.key === \"ArrowDown\") {\n      e.preventDefault();\n      setActive((a) => Math.min(filtered.length - 1, a + 1));\n    } else if (e.key === \"ArrowUp\") {\n      e.preventDefault();\n      setActive((a) => Math.max(0, a - 1));\n    } else if (e.key === \"Enter\") {\n      e.preventDefault();\n      if (filtered[active]) pick(filtered[active]);\n    } else if (e.key === \"Escape\") {\n      e.preventDefault();\n      setOpen(false);\n      triggerRef.current?.focus();\n    } else if (e.key === \"Tab\") {\n      setOpen(false);\n    }\n  }\n\n  React.useEffect(() => {\n    if (!open) return;\n    document.getElementById(`${id}-o-${active}`)?.scrollIntoView({ block: \"nearest\" });\n  }, [active, open, id]);\n\n  const vars = { \"--hb-xg2-c\": accent, ...style } as React.CSSProperties;\n\n  return (\n    <div\n      ref={rootRef}\n      className={`hb-xg2 ${className}`}\n      style={vars}\n      data-open={open}\n      data-complete={complete}\n      data-disabled={disabled}\n      {...rest}\n    >\n      <style href=\"hb-field-phone-country\" precedence=\"default\">{css}</style>\n      <label className=\"hb-xg2-label\" htmlFor={`${id}-in`}>\n        {label}\n      </label>\n      <div className=\"hb-xg2-box\">\n        <button\n          ref={triggerRef}\n          type=\"button\"\n          className=\"hb-xg2-trigger\"\n          aria-haspopup=\"listbox\"\n          aria-expanded={open}\n          aria-label={`Country: ${country.name} ${country.dial}`}\n          disabled={disabled}\n          onClick={() => {\n            setActive(Math.max(0, countries.indexOf(country)));\n            setOpen((o) => !o);\n          }}\n          onKeyDown={(e) => {\n            if (e.key === \"ArrowDown\" && !open) {\n              e.preventDefault();\n              setActive(Math.max(0, countries.indexOf(country)));\n              setOpen(true);\n            }\n          }}\n        >\n          <span className=\"hb-xg2-flag\" style={flagStyle(country.colors)} aria-hidden=\"true\" />\n          <span className=\"hb-xg2-dial\">{country.dial}</span>\n          <svg className=\"hb-xg2-chev\" viewBox=\"0 0 10 10\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" aria-hidden=\"true\">\n            <path d=\"M2.5 4l2.5 2.5L7.5 4\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n          </svg>\n        </button>\n        <div className=\"hb-xg2-wrap\">\n          <span className=\"hb-xg2-ghost\" aria-hidden=\"true\">\n            {formatted}\n            <b>{ghost}</b>\n          </span>\n          <input\n            ref={inputRef}\n            id={`${id}-in`}\n            className=\"hb-xg2-in\"\n            type=\"tel\"\n            inputMode=\"tel\"\n            autoComplete=\"tel-national\"\n            value={formatted}\n            onChange={onInput}\n            disabled={disabled}\n            aria-describedby={`${id}-help`}\n          />\n          <span className=\"hb-xg2-mark\" aria-hidden=\"true\">\n            <svg viewBox=\"0 0 10 10\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\">\n              <path d=\"M2.2 5.2l1.9 1.9L7.9 3\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n            </svg>\n          </span>\n        </div>\n      </div>\n      <div className=\"hb-xg2-help\" id={`${id}-help`} aria-live=\"polite\">\n        <span>{complete ? `Valid ${country.name} number` : hint}</span>\n        <span>\n          {digits.length}/{slots}\n        </span>\n      </div>\n      {open && (\n        <div className=\"hb-xg2-pop\">\n          <input\n            ref={searchRef}\n            className=\"hb-xg2-search\"\n            placeholder=\"Search country or code\"\n            value={query}\n            role=\"combobox\"\n            aria-expanded=\"true\"\n            aria-controls={`${id}-list`}\n            aria-activedescendant={filtered[active] ? `${id}-o-${active}` : undefined}\n            aria-label=\"Search countries\"\n            onChange={(e) => {\n              setQuery(e.target.value);\n              setActive(0);\n            }}\n            onKeyDown={onSearchKey}\n          />\n          {filtered.length ? (\n            <ul className=\"hb-xg2-list\" role=\"listbox\" id={`${id}-list`} aria-label=\"Countries\">\n              {filtered.map((c, i) => (\n                <li\n                  key={c.code}\n                  id={`${id}-o-${i}`}\n                  role=\"option\"\n                  aria-selected={c.code === country.code}\n                  data-active={i === active}\n                  className=\"hb-xg2-opt\"\n                  onPointerEnter={() => setActive(i)}\n                  onClick={() => pick(c)}\n                >\n                  <span className=\"hb-xg2-flag\" style={flagStyle(c.colors)} aria-hidden=\"true\" />\n                  <span className=\"hb-xg2-oname\">{c.name}</span>\n                  <span className=\"hb-xg2-odial\">{c.dial}</span>\n                </li>\n              ))}\n            </ul>\n          ) : (\n            <div className=\"hb-xg2-empty\">No country matches “{query}”.</div>\n          )}\n        </div>\n      )}\n    </div>\n  );\n}\n"
    }
  ]
}