{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "masked-input",
  "title": "Masked Input",
  "description": "A formatted text field for phone numbers, dates, times, card numbers, ZIP codes or any pattern you define: separators fill themselves in, the rest of the template waits in faint ghost text, a thin bar tracks how much is filled, and a tick appears when it's complete.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/masked-input/masked-input.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type MaskPreset = \"phone\" | \"date\" | \"time\" | \"card\" | \"zip\";\nexport type MaskedInputSize = \"sm\" | \"md\" | \"lg\";\n\nconst PRESETS: Record<MaskPreset, { mask: string; placeholder: string; inputMode: \"numeric\" | \"text\"; label: string }> = {\n  phone: { mask: \"(999) 999-9999\", placeholder: \"(555) 000-0000\", inputMode: \"numeric\", label: \"Phone number\" },\n  date: { mask: \"99/99/9999\", placeholder: \"DD/MM/YYYY\", inputMode: \"numeric\", label: \"Date of birth\" },\n  time: { mask: \"99:99\", placeholder: \"HH:MM\", inputMode: \"numeric\", label: \"Time\" },\n  card: { mask: \"9999 9999 9999 9999\", placeholder: \"0000 0000 0000 0000\", inputMode: \"numeric\", label: \"Card number\" },\n  zip: { mask: \"99999\", placeholder: \"00000\", inputMode: \"numeric\", label: \"ZIP code\" },\n};\n\nconst TOKENS: Record<string, RegExp> = { \"9\": /\\d/, A: /[a-zA-Z]/, \"*\": /[a-zA-Z0-9]/ };\nconst isToken = (c: string) => c in TOKENS;\n\n/** Pour raw characters into the mask, stopping where the raw input runs out. */\nfunction applyMask(raw: string, mask: string) {\n  let out = \"\";\n  let r = 0;\n  for (let i = 0; i < mask.length && r < raw.length; i++) {\n    const m = mask[i];\n    if (isToken(m)) {\n      out += raw[r++];\n    } else {\n      out += m;\n    }\n  }\n  return out;\n}\n\n/** Keep only characters that fit the mask's slots, in order. */\nfunction extract(text: string, mask: string) {\n  const slots = mask.split(\"\").filter(isToken);\n  let out = \"\";\n  for (const ch of text) {\n    const slot = slots[out.length];\n    if (!slot) break;\n    if (TOKENS[slot].test(ch)) out += slot === \"A\" ? ch.toUpperCase() : ch;\n  }\n  return out;\n}\n\nexport interface MaskedInputProps {\n  preset?: MaskPreset;\n  /** Your own mask: 9 = digit, A = letter, * = either; anything else is inserted as-is. */\n  mask?: string;\n  /** Ghost text for the empty slots, same length as the mask. */\n  placeholder?: string;\n  label?: string;\n  /** Raw characters (without the mask's separators). */\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (raw: string, formatted: string) => void;\n  onComplete?: (raw: string, formatted: string) => void;\n  helperText?: string;\n  error?: string;\n  size?: MaskedInputSize;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", ghost: \"rgba(245,244,241,0.22)\", bg: \"rgba(255,255,255,0.03)\", border: \"rgba(255,255,255,0.12)\", borderHover: \"rgba(255,255,255,0.24)\", focus: \"rgba(245,244,241,0.85)\", ok: \"#3FBF7F\", error: \"#FF7A6B\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", ghost: \"rgba(10,10,10,0.25)\", bg: \"#FFFFFF\", border: \"rgba(10,10,10,0.16)\", borderHover: \"rgba(10,10,10,0.3)\", focus: \"rgba(10,10,10,0.85)\", ok: \"#1F9D5C\", error: \"#E5484D\" },\n};\n\nconst SIZES: Record<MaskedInputSize, { h: number; font: number; radius: number; pad: number }> = {\n  sm: { h: 38, font: 14, radius: 11, pad: 12 },\n  md: { h: 46, font: 15.5, radius: 13, pad: 14 },\n  lg: { h: 54, font: 17, radius: 15, pad: 16 },\n};\n\nexport function MaskedInput({ preset = \"phone\", mask, placeholder, label, value, defaultValue = \"\", onValueChange, onComplete, helperText, error, size = \"md\", theme = \"dark\", className }: MaskedInputProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const reduce = Boolean(useReducedMotion());\n  const id = React.useId();\n  const pre = PRESETS[preset];\n  const m = mask ?? pre.mask;\n  const ghost = placeholder ?? (mask ? m.replace(/[9A*]/g, \"_\") : pre.placeholder);\n  const slots = m.split(\"\").filter(isToken).length;\n  const [internal, setInternal] = React.useState(() => extract(defaultValue, m));\n  const raw = value !== undefined ? extract(value, m) : internal;\n  const formatted = applyMask(raw, m);\n  const complete = raw.length === slots;\n  const [focus, setFocus] = React.useState(false);\n  const [hover, setHover] = React.useState(false);\n  const ref = React.useRef<HTMLInputElement>(null);\n  const caret = React.useRef<number | null>(null);\n\n  // Put the caret back after the same number of real characters it was after before formatting.\n  React.useLayoutEffect(() => {\n    if (caret.current === null || !ref.current) return;\n    ref.current.setSelectionRange(caret.current, caret.current);\n    caret.current = null;\n  });\n\n  const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {\n    const el = e.target;\n    const before = el.value.slice(0, el.selectionStart ?? el.value.length);\n    const nextRaw = extract(el.value, m);\n    const next = applyMask(nextRaw, m);\n    const keep = extract(before, m).length;\n    let pos = 0;\n    for (let seen = 0; pos < next.length && seen < keep; pos++) if (isToken(m[pos])) seen++;\n    // Skip over separators that follow, so typing flows into the next slot.\n    while (pos < next.length && !isToken(m[pos])) pos++;\n    caret.current = pos;\n    if (value === undefined) setInternal(nextRaw);\n    onValueChange?.(nextRaw, next);\n    if (nextRaw.length === slots && raw.length !== slots) onComplete?.(nextRaw, next);\n  };\n\n  const mono = \"ui-monospace, SFMono-Regular, Menlo, monospace\";\n\n  return (\n    <div className={cn(\"flex w-full flex-col\", className)} style={{ gap: 7, fontFamily: \"Inter, sans-serif\" }}>\n      {(label ?? pre.label) && (\n        <label htmlFor={id} className=\"text-[13px] font-medium\" style={{ color: p.text }}>\n          {label ?? pre.label}\n        </label>\n      )}\n      <div\n        className=\"relative flex items-center\"\n        style={{ height: s.h, borderRadius: s.radius, background: p.bg, border: `1px solid ${error ? p.error : focus ? p.focus : hover ? p.borderHover : p.border}`, boxShadow: focus ? `0 0 0 3px color-mix(in srgb, ${error ? p.error : p.focus} 14%, transparent)` : \"none\", transition: \"border-color 0.18s ease, box-shadow 0.18s ease\" }}\n        onMouseEnter={() => setHover(true)}\n        onMouseLeave={() => setHover(false)}\n      >\n        {/* The rest of the template shows through behind what's been typed. */}\n        <span aria-hidden=\"true\" className=\"pointer-events-none absolute whitespace-pre\" style={{ left: s.pad, fontFamily: mono, fontSize: s.font, letterSpacing: \"0.02em\" }}>\n          <span style={{ color: \"transparent\" }}>{formatted}</span>\n          <span style={{ color: p.ghost }}>{ghost.slice(formatted.length)}</span>\n        </span>\n        <input\n          ref={ref}\n          id={id}\n          value={formatted}\n          onChange={onChange}\n          onFocus={() => setFocus(true)}\n          onBlur={() => setFocus(false)}\n          inputMode={mask ? \"text\" : pre.inputMode}\n          autoComplete=\"off\"\n          aria-invalid={error ? true : undefined}\n          aria-describedby={error || helperText ? `${id}-desc` : undefined}\n          placeholder={ghost}\n          className=\"relative h-full w-full border-none bg-transparent outline-none placeholder:text-transparent\"\n          style={{ padding: `0 ${s.pad + 26}px 0 ${s.pad}px`, fontFamily: mono, fontSize: s.font, letterSpacing: \"0.02em\", color: p.text }}\n        />\n        <AnimatePresence>\n          {complete && !error && (\n            <motion.span key=\"ok\" className=\"absolute flex items-center justify-center rounded-full\" style={{ right: s.pad, width: 20, height: 20, background: p.ok }} initial={{ scale: 0 }} animate={{ scale: 1 }} exit={{ scale: 0 }} transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 600, damping: 24 }} role=\"img\" aria-label=\"Complete\">\n              <svg width=\"10\" height=\"10\" viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\">\n                <path d=\"M2.4 6.3 4.9 8.7 9.6 3.4\" stroke=\"#0A0A0A\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n              </svg>\n            </motion.span>\n          )}\n        </AnimatePresence>\n        <motion.span aria-hidden=\"true\" className=\"absolute bottom-0 left-0 block h-[2px] origin-left rounded-full\" style={{ background: complete ? p.ok : p.focus, opacity: focus ? 0.8 : 0 }} initial={false} animate={{ width: `${(raw.length / slots) * 100}%` }} transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 300, damping: 30 }} />\n      </div>\n      {(error || helperText) && (\n        <span id={`${id}-desc`} role={error ? \"alert\" : undefined} className=\"text-[12.5px]\" style={{ color: error ? p.error : p.muted }}>\n          {error ?? helperText}\n        </span>\n      )}\n    </div>\n  );\n}\n\nexport default MaskedInput;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}