{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "form-address",
  "type": "registry:component",
  "title": "Address Form",
  "description": "A shipping address form that adapts labels, postal masks and line order to the country. A parcel label above it prints the address as you type and stamps it once saved.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "forms",
    "form",
    "address",
    "shipping",
    "checkout",
    "postal code",
    "international"
  ],
  "docs": "https://hyperblur.dev/effects/form-address",
  "files": [
    {
      "path": "registry/hyperblur/form-address.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/form-address.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface FormAddressValues {\n  country: string;\n  name: string;\n  line1: string;\n  line2: string;\n  city: string;\n  region: string;\n  postal: string;\n}\n\nexport interface FormAddressProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onSubmit\" | \"onChange\"> {\n  defaultValues?: Partial<FormAddressValues>;\n  showLabel?: boolean;\n  sender?: string;\n  submitLabel?: string;\n  accent?: string;\n  delay?: number;\n  onChange?: (values: FormAddressValues) => void;\n  onSubmit?: (values: FormAddressValues) => void | Promise<void>;\n}\n\ninterface CountryRule {\n  code: string;\n  name: string;\n  region: string | null;\n  regionHint: string;\n  postal: string;\n  postalHint: string;\n  test: RegExp;\n  mask: (raw: string) => string;\n  lines: (v: FormAddressValues) => string[];\n}\n\nconst digits = (s: string, n: number) => s.replace(/\\D/g, \"\").slice(0, n);\n\nconst countries: CountryRule[] = [\n  {\n    code: \"US\", name: \"United States\", region: \"State\", regionHint: \"CA\", postal: \"ZIP code\", postalHint: \"94107\",\n    test: /^\\d{5}(-\\d{4})?$/,\n    mask: (r) => { const d = digits(r, 9); return d.length > 5 ? `${d.slice(0, 5)}-${d.slice(5)}` : d; },\n    lines: (v) => [`${v.city}${v.city && v.region ? \", \" : \"\"}${v.region.toUpperCase()} ${v.postal}`.trim()],\n  },\n  {\n    code: \"CA\", name: \"Canada\", region: \"Province\", regionHint: \"ON\", postal: \"Postal code\", postalHint: \"M5V 2T6\",\n    test: /^[A-Z]\\d[A-Z] \\d[A-Z]\\d$/,\n    mask: (r) => { const s = r.toUpperCase().replace(/[^A-Z0-9]/g, \"\").slice(0, 6); return s.length > 3 ? `${s.slice(0, 3)} ${s.slice(3)}` : s; },\n    lines: (v) => [`${v.city} ${v.region.toUpperCase()}  ${v.postal}`.trim()],\n  },\n  {\n    code: \"GB\", name: \"United Kingdom\", region: null, regionHint: \"\", postal: \"Postcode\", postalHint: \"EC1V 9HX\",\n    test: /^[A-Z]{1,2}\\d[A-Z\\d]? \\d[A-Z]{2}$/,\n    mask: (r) => { const s = r.toUpperCase().replace(/[^A-Z0-9]/g, \"\").slice(0, 7); return s.length > 4 ? `${s.slice(0, s.length - 3)} ${s.slice(-3)}` : s; },\n    lines: (v) => [v.city.toUpperCase(), v.postal],\n  },\n  {\n    code: \"DE\", name: \"Germany\", region: null, regionHint: \"\", postal: \"PLZ\", postalHint: \"10115\",\n    test: /^\\d{5}$/,\n    mask: (r) => digits(r, 5),\n    lines: (v) => [`${v.postal} ${v.city}`.trim()],\n  },\n  {\n    code: \"BR\", name: \"Brazil\", region: \"State\", regionHint: \"SP\", postal: \"CEP\", postalHint: \"01310-100\",\n    test: /^\\d{5}-\\d{3}$/,\n    mask: (r) => { const d = digits(r, 8); return d.length > 5 ? `${d.slice(0, 5)}-${d.slice(5)}` : d; },\n    lines: (v) => [`${v.city}${v.region ? ` - ${v.region.toUpperCase()}` : \"\"}`, v.postal ? `CEP ${v.postal}` : \"\"],\n  },\n  {\n    code: \"AU\", name: \"Australia\", region: \"State\", regionHint: \"NSW\", postal: \"Postcode\", postalHint: \"2000\",\n    test: /^\\d{4}$/,\n    mask: (r) => digits(r, 4),\n    lines: (v) => [`${v.city.toUpperCase()} ${v.region.toUpperCase()} ${v.postal}`.trim()],\n  },\n];\n\nconst css = `\n.hb-xh5 {\n  --hb-xh5-a: #f5c542;\n  box-sizing: border-box;\n  width: 100%;\n  max-width: 440px;\n  margin: 0 auto;\n  font-family: inherit;\n  color: #ededed;\n  container-type: inline-size;\n}\n.hb-xh5 *, .hb-xh5 *::before, .hb-xh5 *::after { box-sizing: border-box; }\n.hb-xh5-card { padding: 16px; border: 1px solid #262626; border-radius: 18px; background: #101010; box-shadow: inset 0 1px 0 rgba(255,255,255,0.04), 0 28px 56px -28px rgba(0,0,0,0.85); }\n.hb-xh5-label {\n  position: relative;\n  display: grid;\n  grid-template-columns: 1fr auto;\n  gap: 10px 14px;\n  padding: 14px 14px 12px;\n  margin-bottom: 16px;\n  border-radius: 12px;\n  background: linear-gradient(180deg, #f6f3ea, #ece7da);\n  color: #1b1b1b;\n  box-shadow: 0 1px 0 rgba(255,255,255,0.6) inset, 0 10px 24px -12px rgba(0,0,0,0.8);\n  overflow: hidden;\n}\n.hb-xh5-label::before { content: \"\"; position: absolute; inset: 0 auto 0 0; width: 6px; background: repeating-linear-gradient(180deg, var(--hb-xh5-a) 0 8px, #1b1b1b 8px 16px); }\n.hb-xh5-from { grid-column: 1 / -1; display: flex; justify-content: space-between; padding-left: 4px; font-family: var(--font-mono, ui-monospace, monospace); font-size: 9.5px; letter-spacing: 0.06em; text-transform: uppercase; color: #6d675a; }\n.hb-xh5-to { min-width: 0; padding-left: 4px; font-family: var(--font-mono, ui-monospace, monospace); font-size: 12px; line-height: 1.45; }\n.hb-xh5-to b { display: block; font-family: var(--font-sans, inherit); font-size: 13.5px; font-weight: 700; letter-spacing: -0.01em; }\n.hb-xh5-to span { display: block; min-height: 1.45em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }\n.hb-xh5-to span[data-empty=\"true\"] { color: #b3ac9c; }\n.hb-xh5-to span[data-fresh=\"true\"] { animation: hb-xh5-ink 0.5s ease-out; }\n.hb-xh5-side { display: grid; align-content: space-between; justify-items: end; gap: 8px; }\n.hb-xh5-cc { display: grid; place-items: center; min-width: 38px; height: 26px; padding: 0 6px; border: 2px solid #1b1b1b; border-radius: 4px; font-family: var(--font-mono, ui-monospace, monospace); font-size: 13px; font-weight: 700; }\n.hb-xh5-bars { display: flex; align-items: flex-end; gap: 1px; height: 28px; }\n.hb-xh5-bars i { display: block; background: #1b1b1b; }\n.hb-xh5-stamp { position: absolute; right: 64px; top: 22px; padding: 3px 8px; border: 2px solid #1f9d55; border-radius: 4px; color: #1f9d55; font-family: var(--font-mono, ui-monospace, monospace); font-size: 11px; font-weight: 700; letter-spacing: 0.12em; transform: rotate(-12deg); opacity: 0.85; animation: hb-xh5-stamp 0.45s cubic-bezier(0.34, 1.56, 0.64, 1) both; }\n.hb-xh5-form { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; }\n.hb-xh5-f { grid-column: span 6; display: grid; gap: 5px; min-width: 0; }\n.hb-xh5-f[data-w=\"3\"] { grid-column: span 3; }\n.hb-xh5-f[data-w=\"2\"] { grid-column: span 2; }\n.hb-xh5-f[data-w=\"4\"] { grid-column: span 4; }\n@container (max-width: 340px) { .hb-xh5-f[data-w] { grid-column: span 6; } }\n.hb-xh5-l { display: flex; justify-content: space-between; font-size: 11.5px; font-weight: 500; color: #a8a8a8; }\n.hb-xh5-l small { font-weight: 400; color: #646464; }\n.hb-xh5-in, .hb-xh5-sel { width: 100%; height: 38px; padding: 0 11px; border: 1px solid #272727; border-radius: 9px; background: #0b0b0b; color: #ededed; font: inherit; font-size: 13.5px; outline: none; transition: border-color 0.18s ease, box-shadow 0.18s ease; }\n.hb-xh5-in::placeholder { color: #4f4f4f; }\n.hb-xh5-in:hover, .hb-xh5-sel:hover { border-color: #353535; }\n.hb-xh5-in:focus, .hb-xh5-sel:focus { border-color: var(--hb-xh5-a); box-shadow: 0 0 0 3px color-mix(in srgb, var(--hb-xh5-a) 18%, transparent); }\n.hb-xh5-in[aria-invalid=\"true\"] { border-color: #f05260; box-shadow: 0 0 0 3px rgba(240,82,96,0.12); }\n.hb-xh5-in[data-ok=\"true\"] { border-color: color-mix(in srgb, #3ddc84 50%, #272727); }\n.hb-xh5-mono { font-family: var(--font-mono, ui-monospace, monospace); letter-spacing: 0.04em; }\n.hb-xh5-selw { position: relative; }\n.hb-xh5-sel { appearance: none; padding-right: 32px; cursor: pointer; }\n.hb-xh5-selw svg { position: absolute; right: 11px; top: 50%; width: 12px; height: 12px; transform: translateY(-50%); color: #7a7a7a; pointer-events: none; }\n.hb-xh5-e { margin: 0; font-size: 11.5px; color: #ff8a95; animation: hb-xh5-in 0.2s ease-out; }\n.hb-xh5-foot { grid-column: span 6; display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 4px; }\n.hb-xh5-chk { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: #737373; transition: color 0.25s ease; }\n.hb-xh5-chk[data-ok=\"true\"] { color: #5fe39b; }\n.hb-xh5-chk svg { width: 14px; height: 14px; }\n.hb-xh5-save { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-width: 132px; height: 38px; padding: 0 16px; border: 0; border-radius: 10px; background: var(--hb-xh5-a); color: #1a1405; font: inherit; font-size: 13.5px; font-weight: 600; cursor: pointer; box-shadow: inset 0 1px 0 rgba(255,255,255,0.45), inset 0 -2px 0 rgba(0,0,0,0.12), 0 8px 20px -10px var(--hb-xh5-a); transition: transform 0.15s cubic-bezier(0.3, 0.7, 0.4, 1), filter 0.2s ease; }\n.hb-xh5-save:hover { filter: brightness(1.06); }\n.hb-xh5-save:active { transform: scale(0.97); }\n.hb-xh5-save:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }\n.hb-xh5-save[data-s=\"saved\"] { background: #3ddc84; color: #052012; }\n.hb-xh5-spin { width: 14px; height: 14px; border-radius: 50%; border: 2px solid rgba(26,20,5,0.25); border-top-color: #1a1405; animation: hb-xh5-rot 0.7s linear infinite; }\n.hb-xh5-save svg { width: 15px; height: 15px; }\n@keyframes hb-xh5-in { from { opacity: 0; transform: translateY(-3px); } to { opacity: 1; transform: none; } }\n@keyframes hb-xh5-ink { from { color: #8d7a3a; } to { color: inherit; } }\n@keyframes hb-xh5-rot { to { transform: rotate(360deg); } }\n@keyframes hb-xh5-stamp { from { opacity: 0; transform: rotate(-12deg) scale(2.2); } to { opacity: 0.85; transform: rotate(-12deg) scale(1); } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-xh5 *, .hb-xh5 *::before, .hb-xh5 *::after { animation: none !important; transition: none !important; }\n}\n`;\n\nfunction hashText(text: string) {\n  let h = 2166136261;\n  for (let i = 0; i < text.length; i++) {\n    h ^= text.charCodeAt(i);\n    h = Math.imul(h, 16777619);\n  }\n  return h >>> 0;\n}\n\nexport function FormAddress({\n  defaultValues,\n  showLabel = true,\n  sender = \"Haven Goods · Portland, OR\",\n  submitLabel = \"Save address\",\n  accent = \"#f5c542\",\n  delay = 900,\n  onChange,\n  onSubmit,\n  className,\n  style,\n  ...rest\n}: FormAddressProps) {\n  const uid = React.useId();\n  const [v, setV] = React.useState<FormAddressValues>({\n    country: \"US\", name: \"\", line1: \"\", line2: \"\", city: \"\", region: \"\", postal: \"\",\n    ...defaultValues,\n  });\n  const [touched, setTouched] = React.useState<Record<string, boolean>>({});\n  const [status, setStatus] = React.useState<\"idle\" | \"saving\" | \"saved\">(\"idle\");\n  const [fresh, setFresh] = React.useState(\"\");\n  const refs = React.useRef<Record<string, HTMLInputElement | null>>({});\n  const rule = countries.find((c) => c.code === v.country) ?? countries[0];\n\n  const update = (k: keyof FormAddressValues, raw: string) => {\n    let value = raw;\n    if (k === \"postal\") value = rule.mask(raw);\n    if (k === \"region\") value = raw.toUpperCase().replace(/[^A-Z]/g, \"\").slice(0, 3);\n    if (k === \"country\") {\n      const r = countries.find((c) => c.code === raw) ?? countries[0];\n      const next = { ...v, country: raw, region: r.region ? v.region : \"\", postal: r.mask(v.postal) };\n      setV(next);\n      onChange?.(next);\n      setStatus(\"idle\");\n      return;\n    }\n    const next = { ...v, [k]: value };\n    setV(next);\n    setFresh(k);\n    setStatus(\"idle\");\n    onChange?.(next);\n  };\n\n  const errors: Record<string, string> = {};\n  if (v.name.trim().length < 2) errors.name = \"Add the recipient's name.\";\n  if (v.line1.trim().length < 4) errors.line1 = \"Add a street address.\";\n  if (v.city.trim().length < 2) errors.city = \"Add a city.\";\n  if (rule.region && v.region.length < 2) errors.region = `Use the ${rule.region.toLowerCase()} code.`;\n  if (!rule.test.test(v.postal)) errors.postal = v.postal ? `Format: ${rule.postalHint}` : `Add the ${rule.postal}.`;\n  const show = (k: string) => (touched[k] ? errors[k] : undefined);\n  const complete = !Object.keys(errors).length;\n\n  const submit = async (e: React.FormEvent) => {\n    e.preventDefault();\n    if (status === \"saving\") return;\n    if (!complete) {\n      setTouched({ name: true, line1: true, city: true, region: true, postal: true });\n      const first = [\"name\", \"line1\", \"city\", \"region\", \"postal\"].find((k) => errors[k]);\n      if (first) refs.current[first]?.focus();\n      return;\n    }\n    setStatus(\"saving\");\n    try {\n      await Promise.all([Promise.resolve(onSubmit?.(v)), new Promise((r) => window.setTimeout(r, delay))]);\n      setStatus(\"saved\");\n    } catch {\n      setStatus(\"idle\");\n    }\n  };\n\n  const tail = rule.lines(v);\n  const bars = Array.from({ length: 30 }, (_, i) => {\n    const h = hashText(`${v.postal}${v.line1}${i}`);\n    return { w: (h % 3) + 1, h: 18 + (h % 11) };\n  });\n\n  const field = (k: keyof FormAddressValues, label: string, placeholder: string, w?: string, opts?: { auto?: string; mono?: boolean; optional?: boolean }) => (\n    <div className=\"hb-xh5-f\" data-w={w}>\n      <label className=\"hb-xh5-l\" htmlFor={`${uid}-${k}`}>\n        {label}\n        {opts?.optional ? <small>Optional</small> : null}\n      </label>\n      <input\n        ref={(el) => { refs.current[k] = el; }}\n        id={`${uid}-${k}`}\n        className={`hb-xh5-in${opts?.mono ? \" hb-xh5-mono\" : \"\"}`}\n        placeholder={placeholder}\n        autoComplete={opts?.auto}\n        value={v[k]}\n        onChange={(e) => update(k, e.target.value)}\n        onBlur={() => setTouched((t) => ({ ...t, [k]: true }))}\n        aria-invalid={show(k) ? true : undefined}\n        aria-describedby={show(k) ? `${uid}-${k}-e` : undefined}\n        data-ok={k === \"postal\" && rule.test.test(v.postal) ? true : undefined}\n      />\n      {show(k) ? <p className=\"hb-xh5-e\" id={`${uid}-${k}-e`}>{show(k)}</p> : null}\n    </div>\n  );\n\n  return (\n    <div className={[\"hb-xh5\", className].filter(Boolean).join(\" \")} style={{ ...style, \"--hb-xh5-a\": accent } as React.CSSProperties} {...rest}>\n      <style href=\"hb-form-address\" precedence=\"default\">{css}</style>\n      <div className=\"hb-xh5-card\">\n        {showLabel ? (\n          <div className=\"hb-xh5-label\" aria-label=\"Shipping label preview\" role=\"img\">\n            <div className=\"hb-xh5-from\"><span>From: {sender}</span><span>Ground</span></div>\n            <div className=\"hb-xh5-to\">\n              <b>{v.name.trim() || \"Recipient name\"}</b>\n              <span data-empty={!v.line1} data-fresh={fresh === \"line1\"} key={`a${v.line1.length > 0}`}>{v.line1 || \"Street address\"}</span>\n              {v.line2 ? <span>{v.line2}</span> : null}\n              {tail.map((l, i) => (\n                <span key={i} data-empty={!l.replace(/[\\s,-]/g, \"\")}>{l.replace(/[\\s,-]/g, \"\") ? l : i === 0 ? `City, ${rule.region ?? rule.postal}` : rule.postal}</span>\n              ))}\n            </div>\n            <div className=\"hb-xh5-side\">\n              <span className=\"hb-xh5-cc\">{rule.code}</span>\n              <span className=\"hb-xh5-bars\" aria-hidden=\"true\">\n                {bars.map((b, i) => <i key={i} style={{ width: b.w, height: b.h }} />)}\n              </span>\n            </div>\n            {status === \"saved\" ? <span className=\"hb-xh5-stamp\">Verified</span> : null}\n          </div>\n        ) : null}\n        <form className=\"hb-xh5-form\" onSubmit={submit} noValidate>\n          <div className=\"hb-xh5-f\" data-w=\"3\">\n            <label className=\"hb-xh5-l\" htmlFor={`${uid}-country`}>Country</label>\n            <div className=\"hb-xh5-selw\">\n              <select id={`${uid}-country`} className=\"hb-xh5-sel\" value={v.country} autoComplete=\"country\" onChange={(e) => update(\"country\", e.target.value)}>\n                {countries.map((c) => <option key={c.code} value={c.code}>{c.name}</option>)}\n              </select>\n              <svg viewBox=\"0 0 12 12\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\"><path d=\"m3 4.5 3 3 3-3\" /></svg>\n            </div>\n          </div>\n          {field(\"name\", \"Full name\", \"Jordan Alvarez\", \"3\", { auto: \"name\" })}\n          {field(\"line1\", \"Street address\", \"2150 Market Street\", undefined, { auto: \"address-line1\" })}\n          {field(\"line2\", \"Apartment, suite\", \"Unit 4B\", undefined, { auto: \"address-line2\", optional: true })}\n          {field(\"city\", \"City\", \"San Francisco\", rule.region ? \"2\" : \"4\", { auto: \"address-level2\" })}\n          {rule.region ? field(\"region\", rule.region, rule.regionHint, \"2\", { auto: \"address-level1\", mono: true }) : null}\n          {field(\"postal\", rule.postal, rule.postalHint, \"2\", { auto: \"postal-code\", mono: true })}\n          <div className=\"hb-xh5-foot\">\n            <span className=\"hb-xh5-chk\" data-ok={complete} aria-live=\"polite\">\n              <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                <path d=\"M8 1.5 2.5 4v4c0 3.2 2.4 5.6 5.5 6.5 3.1-.9 5.5-3.3 5.5-6.5V4L8 1.5Z\" />\n                {complete ? <path d=\"m5.5 8 1.8 1.8L10.8 6.3\" /> : null}\n              </svg>\n              {complete ? \"Looks deliverable\" : \"We check the format as you type\"}\n            </span>\n            <button type=\"submit\" className=\"hb-xh5-save\" data-s={status} aria-busy={status === \"saving\"}>\n              {status === \"saving\" ? <><span className=\"hb-xh5-spin\" aria-hidden=\"true\" />Saving</> : null}\n              {status === \"saved\" ? (\n                <>\n                  <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\"><path d=\"m3.5 8.5 3 3 6-7\" /></svg>\n                  Saved\n                </>\n              ) : null}\n              {status === \"idle\" ? submitLabel : null}\n            </button>\n          </div>\n        </form>\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}