{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "field-card-number",
  "type": "registry:component",
  "title": "Card Number Field",
  "description": "A single-line card field that detects the brand, checks the number with Luhn and then folds it down to the last four digits while expiry and security code slide in. The tiny card glyph flips to its back when the security code has focus.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "forms",
    "card",
    "payment",
    "mask",
    "checkout",
    "luhn",
    "input"
  ],
  "docs": "https://hyperblur.dev/effects/field-card-number",
  "files": [
    {
      "path": "registry/hyperblur/field-card-number.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/field-card-number.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type FieldCardBrand = \"visa\" | \"mastercard\" | \"amex\" | \"discover\" | \"unknown\";\n\nexport interface FieldCardValue {\n  number: string;\n  expiry: string;\n  cvc: string;\n  brand: FieldCardBrand;\n  complete: boolean;\n}\n\nexport interface FieldCardNumberProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n  label?: string;\n  accent?: string;\n  defaultNumber?: string;\n  defaultExpiry?: string;\n  defaultCvc?: string;\n  hint?: string;\n  disabled?: boolean;\n  onChange?: (value: FieldCardValue) => void;\n  onComplete?: (value: FieldCardValue) => void;\n}\n\nconst css = `\n.hb-xg1 {\n  --hb-xg1-c: #2f7bff;\n  box-sizing: border-box;\n  width: 100%;\n  font-family: inherit;\n  color: #ededed;\n}\n.hb-xg1 *, .hb-xg1 *::before, .hb-xg1 *::after { box-sizing: border-box; }\n.hb-xg1-label {\n  display: flex;\n  align-items: center;\n  justify-content: space-between;\n  margin: 0 0 7px;\n  font-size: 12.5px;\n  font-weight: 500;\n  color: #a1a1a1;\n}\n.hb-xg1-lock { display: inline-flex; align-items: center; gap: 5px; font-size: 11px; color: #6f6f6f; }\n.hb-xg1-lock svg { width: 11px; height: 11px; }\n.hb-xg1-box {\n  position: relative;\n  display: flex;\n  align-items: center;\n  gap: 10px;\n  height: 48px;\n  padding: 0 12px;\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  overflow: hidden;\n}\n.hb-xg1-box:hover { border-color: #333; }\n.hb-xg1-box:focus-within {\n  border-color: var(--hb-xg1-c);\n  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.045), 0 0 0 4px color-mix(in srgb, var(--hb-xg1-c) 16%, transparent);\n}\n.hb-xg1[data-state=\"error\"] .hb-xg1-box {\n  border-color: #f2555a;\n  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.045), 0 0 0 4px rgba(242, 85, 90, 0.14);\n  animation: hb-xg1-shake 0.42s cubic-bezier(0.36, 0.07, 0.19, 0.97);\n}\n.hb-xg1[data-disabled=\"true\"] { opacity: 0.5; pointer-events: none; }\n.hb-xg1-glyph { flex: none; width: 30px; height: 20px; perspective: 240px; }\n.hb-xg1-card {\n  position: relative;\n  width: 100%;\n  height: 100%;\n  transform-style: preserve-3d;\n  transition: transform 0.55s cubic-bezier(0.3, 1.35, 0.5, 1);\n}\n.hb-xg1[data-flip=\"true\"] .hb-xg1-card { transform: rotateY(180deg); }\n.hb-xg1-face {\n  position: absolute;\n  inset: 0;\n  border-radius: 4px;\n  backface-visibility: hidden;\n  -webkit-backface-visibility: hidden;\n  overflow: hidden;\n  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.12), 0 2px 6px rgba(0, 0, 0, 0.5);\n}\n.hb-xg1-front { background: linear-gradient(135deg, #3a3a3a, #1d1d1d); transition: background 0.35s ease; }\n.hb-xg1[data-brand=\"visa\"] .hb-xg1-front { background: linear-gradient(135deg, #3d64ff, #1b2a8a); }\n.hb-xg1[data-brand=\"mastercard\"] .hb-xg1-front { background: linear-gradient(135deg, #ff8a3d, #b3261e); }\n.hb-xg1[data-brand=\"amex\"] .hb-xg1-front { background: linear-gradient(135deg, #4fd1c5, #1d6f8a); }\n.hb-xg1[data-brand=\"discover\"] .hb-xg1-front { background: linear-gradient(135deg, #ffb347, #c25e00); }\n.hb-xg1-chip { position: absolute; left: 4px; top: 5px; width: 7px; height: 5px; border-radius: 1.5px; background: linear-gradient(135deg, #f3d77a, #b08a2e); }\n.hb-xg1-brand {\n  position: absolute;\n  right: 3px;\n  bottom: 2px;\n  font-family: var(--font-grotesk), var(--font-sans), sans-serif;\n  font-size: 6px;\n  font-weight: 700;\n  letter-spacing: 0.04em;\n  color: rgba(255, 255, 255, 0.92);\n}\n.hb-xg1-back { transform: rotateY(180deg); background: linear-gradient(135deg, #2c2c2c, #151515); }\n.hb-xg1-stripe { position: absolute; left: 0; right: 0; top: 4px; height: 4px; background: #050505; }\n.hb-xg1-cvcbox { position: absolute; right: 3px; top: 11px; width: 10px; height: 5px; border-radius: 1px; background: #fff; box-shadow: 0 0 0 1.5px var(--hb-xg1-c); }\n.hb-xg1-num {\n  position: relative;\n  flex: 1 1 auto;\n  min-width: 0;\n  max-width: 100%;\n  height: 100%;\n  transition: max-width 0.5s cubic-bezier(0.22, 1, 0.36, 1);\n}\n.hb-xg1[data-collapsed=\"true\"] .hb-xg1-num { max-width: 84px; }\n.hb-xg1-in {\n  width: 100%;\n  height: 100%;\n  margin: 0;\n  padding: 0;\n  border: 0;\n  outline: 0;\n  background: transparent;\n  color: #ededed;\n  font: inherit;\n  font-size: 14.5px;\n  letter-spacing: 0.02em;\n  font-variant-numeric: tabular-nums;\n  caret-color: var(--hb-xg1-c);\n}\n.hb-xg1-in::placeholder { color: #565656; }\n.hb-xg1-num .hb-xg1-in { transition: opacity 0.25s ease; }\n.hb-xg1[data-collapsed=\"true\"] .hb-xg1-num .hb-xg1-in { opacity: 0; }\n.hb-xg1-last {\n  position: absolute;\n  inset: 0;\n  display: flex;\n  align-items: center;\n  gap: 5px;\n  font-size: 14.5px;\n  font-variant-numeric: tabular-nums;\n  color: #ededed;\n  white-space: nowrap;\n  cursor: text;\n  opacity: 0;\n  transform: translateX(10px);\n  pointer-events: none;\n  transition: opacity 0.3s ease, transform 0.45s cubic-bezier(0.22, 1, 0.36, 1);\n}\n.hb-xg1-last span { color: #6f6f6f; letter-spacing: 0.08em; }\n.hb-xg1[data-collapsed=\"true\"] .hb-xg1-last { opacity: 1; transform: none; pointer-events: auto; }\n.hb-xg1-rest {\n  display: flex;\n  align-items: center;\n  gap: 10px;\n  flex: none;\n  max-width: 0;\n  height: 100%;\n  opacity: 0;\n  transform: translateX(14px);\n  transition: max-width 0.5s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.3s ease 0.08s, transform 0.5s cubic-bezier(0.22, 1, 0.36, 1);\n}\n.hb-xg1[data-reveal=\"true\"] .hb-xg1-rest { max-width: 150px; opacity: 1; transform: none; }\n.hb-xg1-sep { width: 1px; height: 18px; flex: none; background: #262626; }\n.hb-xg1-exp { width: 56px; flex: none; }\n.hb-xg1-cvc { width: 42px; flex: none; }\n.hb-xg1-help {\n  display: flex;\n  align-items: center;\n  gap: 6px;\n  min-height: 18px;\n  margin-top: 8px;\n  font-size: 12.5px;\n  color: #8f8f8f;\n}\n.hb-xg1-help svg { width: 14px; height: 14px; flex: none; }\n.hb-xg1[data-state=\"error\"] .hb-xg1-help { color: #ff6b6f; }\n.hb-xg1[data-state=\"ok\"] .hb-xg1-help { color: #5fd38d; }\n.hb-xg1-tick { stroke-dasharray: 20; stroke-dashoffset: 20; animation: hb-xg1-draw 0.45s cubic-bezier(0.65, 0, 0.35, 1) forwards 0.1s; }\n.hb-xg1-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }\n@keyframes hb-xg1-shake {\n  20%, 60% { transform: translateX(-4px); }\n  40%, 80% { transform: translateX(4px); }\n}\n@keyframes hb-xg1-draw { to { stroke-dashoffset: 0; } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-xg1 *, .hb-xg1 *::before { transition: none !important; animation: none !important; }\n  .hb-xg1[data-state=\"error\"] .hb-xg1-box { animation: none; }\n  .hb-xg1-tick { stroke-dashoffset: 0; }\n}\n`;\n\nconst brandLabel: Record<FieldCardBrand, string> = {\n  visa: \"VISA\",\n  mastercard: \"MC\",\n  amex: \"AMEX\",\n  discover: \"DISC\",\n  unknown: \"\",\n};\n\nconst brandName: Record<FieldCardBrand, string> = {\n  visa: \"Visa\",\n  mastercard: \"Mastercard\",\n  amex: \"Amex\",\n  discover: \"Discover\",\n  unknown: \"Card\",\n};\n\nfunction brandOf(d: string): FieldCardBrand {\n  if (/^4/.test(d)) return \"visa\";\n  if (/^3[47]/.test(d)) return \"amex\";\n  if (/^(5[1-5]|2[2-7])/.test(d)) return \"mastercard\";\n  if (/^(6011|65|64[4-9])/.test(d)) return \"discover\";\n  return \"unknown\";\n}\n\nfunction groupsOf(brand: FieldCardBrand) {\n  return brand === \"amex\" ? [4, 6, 5] : [4, 4, 4, 4];\n}\n\nfunction lengthOf(brand: FieldCardBrand) {\n  return brand === \"amex\" ? 15 : 16;\n}\n\nfunction formatNumber(d: string, brand: FieldCardBrand) {\n  const out: string[] = [];\n  let i = 0;\n  for (const g of groupsOf(brand)) {\n    if (i >= d.length) break;\n    out.push(d.slice(i, i + g));\n    i += g;\n  }\n  return out.join(\" \");\n}\n\nfunction luhn(d: string) {\n  let sum = 0;\n  let dbl = false;\n  for (let i = d.length - 1; i >= 0; i--) {\n    let n = d.charCodeAt(i) - 48;\n    if (dbl) {\n      n *= 2;\n      if (n > 9) n -= 9;\n    }\n    sum += n;\n    dbl = !dbl;\n  }\n  return sum % 10 === 0;\n}\n\nfunction formatExpiry(raw: string, typing = false) {\n  let d = raw.replace(/\\D/g, \"\").slice(0, 4);\n  if (typing && d.length === 1 && d > \"1\") d = \"0\" + d;\n  if (d.length < 2) return d;\n  if (d.length === 2) return typing ? d + \" / \" : d;\n  return `${d.slice(0, 2)} / ${d.slice(2)}`;\n}\n\nfunction subscribeNone() {\n  return () => {};\n}\n\nfunction readMonthIndex() {\n  const [y, m] = new Intl.DateTimeFormat(\"en-CA\").format().split(\"-\").map(Number);\n  return (y % 100) * 12 + m;\n}\n\nfunction readNull() {\n  return null;\n}\n\nfunction caretAfterDigits(formatted: string, digits: number) {\n  if (digits <= 0) return 0;\n  let seen = 0;\n  for (let i = 0; i < formatted.length; i++) {\n    if (/\\d/.test(formatted[i])) seen++;\n    if (seen === digits) return i + 1;\n  }\n  return formatted.length;\n}\n\nexport function FieldCardNumber({\n  label = \"Card details\",\n  accent = \"#2f7bff\",\n  defaultNumber = \"\",\n  defaultExpiry = \"\",\n  defaultCvc = \"\",\n  hint = \"Your card is encrypted end to end.\",\n  disabled = false,\n  onChange,\n  onComplete,\n  className = \"\",\n  style,\n  ...rest\n}: FieldCardNumberProps) {\n  const id = React.useId();\n  const numRef = React.useRef<HTMLInputElement>(null);\n  const expRef = React.useRef<HTMLInputElement>(null);\n  const cvcRef = React.useRef<HTMLInputElement>(null);\n  const [digits, setDigits] = React.useState(defaultNumber.replace(/\\D/g, \"\").slice(0, 16));\n  const [expiry, setExpiry] = React.useState(formatExpiry(defaultExpiry));\n  const [cvc, setCvc] = React.useState(defaultCvc.replace(/\\D/g, \"\").slice(0, 4));\n  const [focus, setFocus] = React.useState<\"number\" | \"expiry\" | \"cvc\" | null>(null);\n  const [touched, setTouched] = React.useState(false);\n  const now = React.useSyncExternalStore(subscribeNone, readMonthIndex, readNull);\n  const completed = React.useRef(false);\n\n  const brand = brandOf(digits);\n  const max = lengthOf(brand);\n  const numberFull = digits.length >= max;\n  const numberValid = numberFull && luhn(digits.slice(0, max));\n  const expDigits = expiry.replace(/\\D/g, \"\");\n  const month = Number(expDigits.slice(0, 2));\n  const year = Number(expDigits.slice(2, 4));\n  const expFull = expDigits.length === 4;\n  const expValid = expFull && month >= 1 && month <= 12 && (now === null || year * 12 + month >= now);\n  const cvcLen = brand === \"amex\" ? 4 : 3;\n  const cvcValid = cvc.length === cvcLen;\n  const complete = numberValid && expValid && cvcValid;\n\n  let error = \"\";\n  if (numberFull && !numberValid) error = \"That card number doesn't look right.\";\n  else if (expFull && !expValid) error = month < 1 || month > 12 ? \"Expiry month must be 01 to 12.\" : \"This card has expired.\";\n  else if (touched && focus === null && digits.length > 0 && !numberFull) error = \"Card number is incomplete.\";\n  else if (touched && focus === null && numberValid && cvc.length > 0 && !cvcValid) error = `Security code needs ${cvcLen} digits.`;\n\n  const reveal = numberValid || expiry.length > 0 || cvc.length > 0;\n  const collapsed = numberValid && focus !== \"number\";\n  const state = error ? \"error\" : complete ? \"ok\" : \"idle\";\n\n  const emit = React.useCallback(\n    (d: string, e: string, c: string) => {\n      const b = brandOf(d);\n      const ed = e.replace(/\\D/g, \"\");\n      const m = Number(ed.slice(0, 2));\n      const ok =\n        d.length === lengthOf(b) &&\n        luhn(d) &&\n        ed.length === 4 &&\n        m >= 1 &&\n        m <= 12 &&\n        c.length === (b === \"amex\" ? 4 : 3);\n      const v: FieldCardValue = { number: d, expiry: e.replace(/\\s/g, \"\"), cvc: c, brand: b, complete: ok };\n      onChange?.(v);\n      if (ok && !completed.current) {\n        completed.current = true;\n        onComplete?.(v);\n      }\n      if (!ok) completed.current = false;\n    },\n    [onChange, onComplete],\n  );\n\n  function onNumber(e: React.ChangeEvent<HTMLInputElement>) {\n    const el = e.target;\n    const caret = el.selectionStart ?? el.value.length;\n    const before = el.value.slice(0, caret).replace(/\\D/g, \"\").length;\n    const raw = el.value.replace(/\\D/g, \"\");\n    const b = brandOf(raw);\n    const d = raw.slice(0, lengthOf(b));\n    setDigits(d);\n    emit(d, expiry, cvc);\n    const formatted = formatNumber(d, b);\n    requestAnimationFrame(() => {\n      const pos = caretAfterDigits(formatted, Math.min(before, d.length));\n      numRef.current?.setSelectionRange(pos, pos);\n    });\n    if (d.length === lengthOf(b) && luhn(d) && caret >= el.value.length) {\n      requestAnimationFrame(() => expRef.current?.focus());\n    }\n  }\n\n  function onExpiry(e: React.ChangeEvent<HTMLInputElement>) {\n    const raw = e.target.value;\n    const deleting = raw.length < expiry.length;\n    let d = raw.replace(/\\D/g, \"\").slice(0, 4);\n    if (deleting && /\\/\\s?$/.test(raw)) d = d.slice(0, -1);\n    const v = formatExpiry(d, !deleting);\n    setExpiry(v);\n    emit(digits, v, cvc);\n    if (!deleting && v.replace(/\\D/g, \"\").length === 4) requestAnimationFrame(() => cvcRef.current?.focus());\n  }\n\n  function onCvc(e: React.ChangeEvent<HTMLInputElement>) {\n    const v = e.target.value.replace(/\\D/g, \"\").slice(0, cvcLen);\n    setCvc(v);\n    emit(digits, expiry, v);\n  }\n\n  function back(e: React.KeyboardEvent<HTMLInputElement>, value: string, prev: React.RefObject<HTMLInputElement | null>) {\n    if (e.key === \"Backspace\" && value.length === 0) {\n      e.preventDefault();\n      const el = prev.current;\n      if (!el) return;\n      el.focus();\n      const end = el.value.length;\n      el.setSelectionRange(end, end);\n    }\n  }\n\n  const helpId = `${id}-help`;\n  const vars = { \"--hb-xg1-c\": accent, ...style } as React.CSSProperties;\n\n  return (\n    <div\n      className={`hb-xg1 ${className}`}\n      style={vars}\n      data-state={state}\n      data-brand={brand}\n      data-flip={focus === \"cvc\"}\n      data-reveal={reveal}\n      data-collapsed={collapsed}\n      data-disabled={disabled}\n      {...rest}\n    >\n      <style href=\"hb-field-card-number\" precedence=\"default\">{css}</style>\n      <div className=\"hb-xg1-label\">\n        <label htmlFor={`${id}-n`}>{label}</label>\n        <span className=\"hb-xg1-lock\" aria-hidden=\"true\">\n          <svg viewBox=\"0 0 12 12\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.4\">\n            <rect x=\"2.2\" y=\"5.2\" width=\"7.6\" height=\"5.6\" rx=\"1.4\" />\n            <path d=\"M4 5.2V3.8a2 2 0 0 1 4 0v1.4\" />\n          </svg>\n          Secure\n        </span>\n      </div>\n      <div className=\"hb-xg1-box\" onClick={(e) => e.target === e.currentTarget && numRef.current?.focus()}>\n        <div className=\"hb-xg1-glyph\" aria-hidden=\"true\">\n          <div className=\"hb-xg1-card\">\n            <div className=\"hb-xg1-face hb-xg1-front\">\n              <span className=\"hb-xg1-chip\" />\n              <span className=\"hb-xg1-brand\">{brandLabel[brand]}</span>\n            </div>\n            <div className=\"hb-xg1-face hb-xg1-back\">\n              <span className=\"hb-xg1-stripe\" />\n              <span className=\"hb-xg1-cvcbox\" />\n            </div>\n          </div>\n        </div>\n        <div className=\"hb-xg1-num\">\n          <input\n            ref={numRef}\n            id={`${id}-n`}\n            className=\"hb-xg1-in\"\n            inputMode=\"numeric\"\n            autoComplete=\"cc-number\"\n            placeholder=\"1234 1234 1234 1234\"\n            value={formatNumber(digits, brand)}\n            onChange={onNumber}\n            onFocus={() => setFocus(\"number\")}\n            onBlur={() => {\n              setFocus(null);\n              setTouched(true);\n            }}\n            disabled={disabled}\n            aria-invalid={state === \"error\" && !numberValid}\n            aria-describedby={helpId}\n          />\n          <span\n            className=\"hb-xg1-last\"\n            aria-hidden=\"true\"\n            onMouseDown={(e) => {\n              e.preventDefault();\n              numRef.current?.focus();\n            }}\n          >\n            <span>••••</span>\n            {digits.slice(-4)}\n          </span>\n        </div>\n        <div className=\"hb-xg1-rest\">\n          <span className=\"hb-xg1-sep\" aria-hidden=\"true\" />\n          <input\n            ref={expRef}\n            className=\"hb-xg1-in hb-xg1-exp\"\n            inputMode=\"numeric\"\n            autoComplete=\"cc-exp\"\n            placeholder=\"MM / YY\"\n            aria-label=\"Expiry date\"\n            value={expiry}\n            onChange={onExpiry}\n            onKeyDown={(e) => back(e, expiry, numRef)}\n            onFocus={() => setFocus(\"expiry\")}\n            onBlur={() => {\n              setFocus(null);\n              setTouched(true);\n            }}\n            tabIndex={reveal ? 0 : -1}\n            disabled={disabled}\n            aria-invalid={expFull && !expValid}\n          />\n          <input\n            ref={cvcRef}\n            className=\"hb-xg1-in hb-xg1-cvc\"\n            inputMode=\"numeric\"\n            autoComplete=\"cc-csc\"\n            placeholder={brand === \"amex\" ? \"CVCC\" : \"CVC\"}\n            aria-label=\"Security code\"\n            value={cvc}\n            onChange={onCvc}\n            onKeyDown={(e) => back(e, cvc, expRef)}\n            onFocus={() => setFocus(\"cvc\")}\n            onBlur={() => {\n              setFocus(null);\n              setTouched(true);\n            }}\n            tabIndex={reveal ? 0 : -1}\n            disabled={disabled}\n          />\n        </div>\n      </div>\n      <div className=\"hb-xg1-help\" id={helpId} aria-live=\"polite\">\n        {state === \"error\" ? (\n          <>\n            <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" aria-hidden=\"true\">\n              <circle cx=\"8\" cy=\"8\" r=\"6.3\" />\n              <path d=\"M8 4.8v3.6M8 10.9v.2\" strokeLinecap=\"round\" />\n            </svg>\n            {error}\n          </>\n        ) : state === \"ok\" ? (\n          <>\n            <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" aria-hidden=\"true\">\n              <path className=\"hb-xg1-tick\" d=\"M3.5 8.4l3 3 6-6.4\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n            </svg>\n            {brandName[brand]} ending {digits.slice(-4)}, expires {expiry.replace(/\\s/g, \"\")}\n          </>\n        ) : (\n          hint\n        )}\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}