{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "currency-input",
  "title": "Currency Input",
  "description": "A money input with a currency picker: live thousands separators for any locale, the right number of decimals per currency, an animated symbol, arrow-key steps and min/max, returning a plain number.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/currency-input/currency-input.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence } 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 CurrencyInputSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface CurrencyInputProps {\n  /** The amount as a number, or null when empty. */\n  value?: number | null;\n  defaultValue?: number | null;\n  onValueChange?: (value: number | null, currency: string) => void;\n  /** ISO 4217 code, controlled. */\n  currency?: string;\n  defaultCurrency?: string;\n  onCurrencyChange?: (currency: string) => void;\n  /** Codes offered in the menu; pass a single code (or showCurrencySelect={false}) for a fixed currency. */\n  currencies?: string[];\n  showCurrencySelect?: boolean;\n  /** Controls the separators and names, e.g. \"en-US\" (1,234.56) or \"de-DE\" (1.234,56). */\n  locale?: string;\n  min?: number;\n  max?: number;\n  /** ArrowUp/ArrowDown step; Shift multiplies it by 10. */\n  step?: number;\n  allowNegative?: boolean;\n  label?: string;\n  placeholder?: string;\n  helperText?: string;\n  size?: CurrencyInputSize;\n  theme?: \"dark\" | \"light\";\n  width?: number | string;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst DEFAULT_CURRENCIES = [\"USD\", \"EUR\", \"GBP\", \"TRY\", \"JPY\", \"CHF\", \"CAD\", \"AUD\", \"CNY\", \"INR\", \"BRL\", \"MXN\", \"SEK\", \"KRW\", \"AED\"];\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.3)\", 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)\", chip: \"rgba(255,255,255,0.07)\", chipHover: \"rgba(255,255,255,0.12)\", menuBg: \"#0E0E0E\", menuBorder: \"rgba(255,255,255,0.08)\", highlight: \"rgba(255,255,255,0.07)\", shadow: \"0 24px 60px rgba(0,0,0,0.55)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.3)\", bg: \"#FFFFFF\", border: \"rgba(10,10,10,0.16)\", borderHover: \"rgba(10,10,10,0.3)\", focus: \"rgba(10,10,10,0.85)\", chip: \"rgba(10,10,10,0.05)\", chipHover: \"rgba(10,10,10,0.09)\", menuBg: \"#FFFFFF\", menuBorder: \"rgba(10,10,10,0.12)\", highlight: \"rgba(10,10,10,0.05)\", shadow: \"0 20px 50px rgba(0,0,0,0.16)\" },\n};\n\nconst SIZES: Record<CurrencyInputSize, { font: number; height: number; padX: number; radius: number }> = {\n  sm: { font: 13, height: 36, padX: 12, radius: 11 },\n  md: { font: 14.5, height: 44, padX: 14, radius: 13 },\n  lg: { font: 16, height: 52, padX: 16, radius: 15 },\n};\n\nfunction currencyMeta(code: string, locale: string) {\n  const fmt = new Intl.NumberFormat(locale, { style: \"currency\", currency: code, currencyDisplay: \"narrowSymbol\" });\n  const symbol = fmt.formatToParts(0).find((part) => part.type === \"currency\")?.value ?? code;\n  const decimals = fmt.resolvedOptions().maximumFractionDigits ?? 2;\n  let name = code;\n  try {\n    name = new Intl.DisplayNames([locale], { type: \"currency\" }).of(code) ?? code;\n  } catch {\n    // Older engines without Intl.DisplayNames just show the code.\n  }\n  return { code, symbol, decimals, name };\n}\n\nfunction separators(locale: string) {\n  const parts = new Intl.NumberFormat(locale).formatToParts(12345.6);\n  return { group: parts.find((p) => p.type === \"group\")?.value ?? \",\", decimal: parts.find((p) => p.type === \"decimal\")?.value ?? \".\" };\n}\n\n/**\n * Turns raw text into a clean draft (\"-1234.5\", \".\" as the decimal) while the person types: group\n * separators and other characters drop out, the locale's decimal separator starts the fraction, and\n * the fraction is capped at the currency's decimals.\n */\nfunction toDraft(raw: string, decimalSep: string, decimals: number, allowNegative: boolean) {\n  const negative = allowNegative && raw.trim().startsWith(\"-\");\n  const [intRaw, ...rest] = raw.split(decimalSep);\n  const int = intRaw.replace(/\\D/g, \"\").replace(/^0+(?=\\d)/, \"\");\n  const hasDecimal = decimals > 0 && rest.length > 0;\n  const frac = hasDecimal ? rest.join(\"\").replace(/\\D/g, \"\").slice(0, decimals) : \"\";\n  return `${negative ? \"-\" : \"\"}${int}${hasDecimal ? `.${frac}` : \"\"}`;\n}\n\n/** Shows a draft with the locale's separators, keeping a trailing decimal or zeros the person typed. */\nfunction displayDraft(draft: string, locale: string, decimalSep: string) {\n  if (draft === \"\" || draft === \"-\") return draft;\n  const negative = draft.startsWith(\"-\");\n  const [int, frac] = draft.replace(\"-\", \"\").split(\".\");\n  const grouped = int === \"\" ? \"\" : new Intl.NumberFormat(locale, { maximumFractionDigits: 0, useGrouping: true }).format(BigInt(int));\n  return `${negative ? \"-\" : \"\"}${grouped || (frac !== undefined ? \"0\" : \"\")}${frac !== undefined ? `${decimalSep}${frac}` : \"\"}`;\n}\n\n// The caret is restored by counting these \"meaningful\" characters (digits, the decimal separator and\n// a minus); group separators come and go as the number is reformatted, so they don't count.\nconst isMark = (ch: string, decimalSep: string) => /\\d/.test(ch) || ch === decimalSep || ch === \"-\";\nconst countMarks = (text: string, decimalSep: string) => [...text].filter((ch) => isMark(ch, decimalSep)).length;\n\nconst draftToNumber = (draft: string) => (draft === \"\" || draft === \"-\" || draft === \".\" ? null : Number(draft));\n\nfunction numberToDraft(n: number | null | undefined, decimals: number) {\n  if (n === null || n === undefined || Number.isNaN(n)) return \"\";\n  return decimals > 0 ? n.toFixed(decimals) : String(Math.round(n));\n}\n\nfunction Chevron({ open, color }: { open: boolean; color: string }) {\n  return (\n    <motion.span aria-hidden=\"true\" className=\"flex\" animate={{ rotate: open ? 180 : 0 }} transition={{ type: \"spring\", stiffness: 420, damping: 28 }} style={{ color }}>\n      <svg width=\"12\" height=\"12\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n        <path d=\"M4 6.2L8 10L12 6.2\" />\n      </svg>\n    </motion.span>\n  );\n}\n\nexport function CurrencyInput({\n  value,\n  defaultValue = null,\n  onValueChange,\n  currency: currencyProp,\n  defaultCurrency = \"USD\",\n  onCurrencyChange,\n  currencies = DEFAULT_CURRENCIES,\n  showCurrencySelect = true,\n  locale = \"en-US\",\n  min,\n  max,\n  step = 1,\n  allowNegative = false,\n  label,\n  placeholder,\n  helperText,\n  size = \"md\",\n  theme = \"dark\",\n  width = 300,\n  disabled = false,\n  className,\n}: CurrencyInputProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const inputRef = React.useRef<HTMLInputElement>(null);\n  const caretDigits = React.useRef<number | null>(null);\n\n  const [currencyState, setCurrencyState] = React.useState(defaultCurrency);\n  const code = (currencyProp ?? currencyState).toUpperCase();\n  const meta = React.useMemo(() => currencyMeta(code, locale), [code, locale]);\n  const menu = React.useMemo(() => currencies.map((c) => currencyMeta(c, locale)), [currencies, locale]);\n  const { decimal: decimalSep } = React.useMemo(() => separators(locale), [locale]);\n\n  const isControlled = value !== undefined;\n  const [draft, setDraft] = React.useState(() => numberToDraft(defaultValue, meta.decimals));\n  const [focused, setFocused] = React.useState(false);\n  const [hovered, setHovered] = React.useState(false);\n  const [open, setOpen] = React.useState(false);\n  const [activeIndex, setActiveIndex] = React.useState(0);\n\n  // A controlled value that changes from outside replaces the draft, unless it's what the draft already says.\n  const shownDraft = isControlled && draftToNumber(draft) !== (value ?? null) ? numberToDraft(value, meta.decimals) : draft;\n\n  const clamp = (n: number) => Math.min(max ?? Infinity, Math.max(min ?? -Infinity, n));\n\n  const commitDraft = (next: string) => {\n    setDraft(next);\n    onValueChange?.(draftToNumber(next), code);\n  };\n\n  const onInput = (raw: string, caret: number | null) => {\n    const next = toDraft(raw, decimalSep, meta.decimals, allowNegative);\n    caretDigits.current = caret === null ? null : countMarks(raw.slice(0, caret), decimalSep);\n    commitDraft(next);\n  };\n\n  const shown = focused ? displayDraft(shownDraft, locale, decimalSep) : shownDraft === \"\" ? \"\" : new Intl.NumberFormat(locale, { minimumFractionDigits: meta.decimals, maximumFractionDigits: meta.decimals }).format(Number(shownDraft));\n\n  React.useLayoutEffect(() => {\n    const el = inputRef.current;\n    const n = caretDigits.current;\n    if (!el || n === null || document.activeElement !== el) return;\n    let pos = 0;\n    let seen = 0;\n    while (pos < shown.length && seen < n) {\n      if (isMark(shown[pos], decimalSep)) seen++;\n      pos++;\n    }\n    el.setSelectionRange(pos, pos);\n    caretDigits.current = null;\n  }, [shown, decimalSep]);\n\n  const nudge = (dir: 1 | -1, big: boolean) => {\n    const current = draftToNumber(shownDraft) ?? 0;\n    const next = clamp(Math.round((current + dir * step * (big ? 10 : 1)) * 10 ** meta.decimals) / 10 ** meta.decimals);\n    if (!allowNegative && next < 0) return;\n    commitDraft(numberToDraft(next, meta.decimals));\n  };\n\n  const onBlur = () => {\n    setFocused(false);\n    const n = draftToNumber(shownDraft);\n    if (n === null) return commitDraft(\"\");\n    commitDraft(numberToDraft(clamp(n), meta.decimals));\n  };\n\n  const pickCurrency = (c: string) => {\n    if (!currencyProp) setCurrencyState(c);\n    onCurrencyChange?.(c);\n    setOpen(false);\n    const d = currencyMeta(c, locale).decimals;\n    const n = draftToNumber(shownDraft);\n    if (n !== null) {\n      const next = numberToDraft(n, d);\n      setDraft(next);\n      onValueChange?.(draftToNumber(next), c);\n    }\n    inputRef.current?.focus();\n  };\n\n  const onMenuKey = (e: React.KeyboardEvent) => {\n    if (e.key === \"ArrowDown\" || e.key === \"ArrowUp\") {\n      e.preventDefault();\n      if (!open) {\n        setActiveIndex(Math.max(0, menu.findIndex((m) => m.code === code)));\n        return setOpen(true);\n      }\n      setActiveIndex((i) => (i + (e.key === \"ArrowDown\" ? 1 : -1) + menu.length) % menu.length);\n    } else if ((e.key === \"Enter\" || e.key === \" \") && open) {\n      e.preventDefault();\n      pickCurrency(menu[activeIndex].code);\n    } else if (e.key === \"Escape\" && open) {\n      e.preventDefault();\n      setOpen(false);\n    } else if (e.key === \"Tab\") {\n      setOpen(false);\n    }\n  };\n\n  React.useEffect(() => {\n    if (!open) return;\n    const onDown = (e: MouseEvent) => {\n      if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);\n    };\n    document.addEventListener(\"mousedown\", onDown);\n    return () => document.removeEventListener(\"mousedown\", onDown);\n  }, [open]);\n\n  const active = focused || open;\n  const borderColor = active ? p.focus : hovered ? p.borderHover : p.border;\n  const selectable = showCurrencySelect && currencies.length > 1;\n  const shownPlaceholder = placeholder ?? new Intl.NumberFormat(locale, { minimumFractionDigits: meta.decimals, maximumFractionDigits: meta.decimals }).format(0);\n\n  return (\n    <div ref={rootRef} className={cn(\"relative inline-flex flex-col gap-2\", className)} style={{ width, maxWidth: \"100%\", fontFamily: \"Inter, sans-serif\", opacity: disabled ? 0.5 : 1 }}>\n      {label && (\n        <label htmlFor={`${uid}-input`} className=\"font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: s.font }}>\n          {label}\n        </label>\n      )}\n\n      <div className=\"relative\">\n        <div\n          onMouseEnter={() => setHovered(true)}\n          onMouseLeave={() => setHovered(false)}\n          onClick={() => inputRef.current?.focus()}\n          className=\"flex items-center\"\n          style={{ height: s.height, gap: 8, padding: `0 ${selectable ? 6 : s.padX}px 0 ${s.padX}px`, borderRadius: s.radius, background: p.bg, border: `1px solid ${borderColor}`, boxShadow: active ? `0 0 0 3px color-mix(in srgb, ${p.focus} 14%, transparent)` : \"0 0 0 0 transparent\", cursor: disabled ? \"default\" : \"text\", transition: \"border-color 0.18s ease, box-shadow 0.18s ease\" }}\n        >\n          <span className=\"relative flex shrink-0 justify-center overflow-hidden\" style={{ minWidth: s.font * 0.8, height: s.font * 1.5, color: shownDraft ? p.text : p.muted, fontSize: s.font, fontWeight: 600 }} aria-hidden=\"true\">\n            <AnimatePresence mode=\"popLayout\" initial={false}>\n              <motion.span key={meta.symbol} initial={{ y: \"100%\", opacity: 0 }} animate={{ y: 0, opacity: 1 }} exit={{ y: \"-100%\", opacity: 0 }} transition={{ duration: 0.2, ease: [0.16, 1, 0.3, 1] }} style={{ lineHeight: `${s.font * 1.5}px` }}>\n                {meta.symbol}\n              </motion.span>\n            </AnimatePresence>\n          </span>\n          <input\n            ref={inputRef}\n            id={`${uid}-input`}\n            type=\"text\"\n            inputMode=\"decimal\"\n            autoComplete=\"off\"\n            aria-describedby={helperText ? `${uid}-help` : undefined}\n            aria-label={label ? undefined : `Amount in ${meta.name}`}\n            disabled={disabled}\n            value={shown}\n            placeholder={shownPlaceholder}\n            onChange={(e) => onInput(e.target.value, e.target.selectionStart)}\n            onKeyDown={(e) => {\n              if (e.key === \"ArrowUp\" || e.key === \"ArrowDown\") {\n                e.preventDefault();\n                nudge(e.key === \"ArrowUp\" ? 1 : -1, e.shiftKey);\n              }\n            }}\n            onFocus={() => setFocused(true)}\n            onBlur={onBlur}\n            className=\"h-full min-w-0 flex-1 border-none bg-transparent p-0 outline-none placeholder:text-[color:var(--ci-placeholder)]\"\n            style={{ color: p.text, fontSize: s.font, fontWeight: 500, fontVariantNumeric: \"tabular-nums\", [\"--ci-placeholder\" as string]: p.faint }}\n          />\n          {selectable ? (\n            <button\n              type=\"button\"\n              role=\"combobox\"\n              aria-haspopup=\"listbox\"\n              aria-expanded={open}\n              aria-controls={`${uid}-list`}\n              aria-activedescendant={open ? `${uid}-c-${menu[activeIndex]?.code}` : undefined}\n              aria-label={`Currency: ${meta.name}`}\n              disabled={disabled}\n              onClick={(e) => {\n                e.stopPropagation();\n                setActiveIndex(Math.max(0, menu.findIndex((m) => m.code === code)));\n                setOpen((o) => !o);\n              }}\n              onKeyDown={onMenuKey}\n              className=\"flex shrink-0 cursor-pointer items-center border-none outline-none focus-visible:ring-2\"\n              style={{ gap: 5, height: s.height - 12, padding: \"0 9px\", borderRadius: s.radius - 5, background: open ? p.chipHover : p.chip, color: p.text, fontSize: s.font - 1.5, fontWeight: 600, letterSpacing: \"0.02em\", transition: \"background 0.15s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n              onMouseEnter={(e) => (e.currentTarget.style.background = p.chipHover)}\n              onMouseLeave={(e) => (e.currentTarget.style.background = open ? p.chipHover : p.chip)}\n            >\n              {code}\n              <Chevron open={open} color={p.muted} />\n            </button>\n          ) : (\n            <span className=\"shrink-0\" style={{ color: p.muted, fontSize: s.font - 1.5, fontWeight: 600, letterSpacing: \"0.02em\" }}>\n              {code}\n            </span>\n          )}\n        </div>\n\n        <AnimatePresence>\n          {open && (\n            <motion.ul\n              id={`${uid}-list`}\n              role=\"listbox\"\n              aria-label=\"Currency\"\n              className=\"absolute right-0 z-50 m-0 list-none overflow-auto p-1.5\"\n              style={{ top: \"calc(100% + 8px)\", width: \"min(100%, 280px)\", maxHeight: 272, borderRadius: s.radius + 2, border: `1px solid ${p.menuBorder}`, background: p.menuBg, boxShadow: p.shadow, transformOrigin: \"top right\" }}\n              initial={{ opacity: 0, y: -6, scale: 0.98 }}\n              animate={{ opacity: 1, y: 0, scale: 1 }}\n              exit={{ opacity: 0, y: -6, scale: 0.98 }}\n              transition={{ duration: 0.16, ease: [0.16, 1, 0.3, 1] }}\n            >\n              {menu.map((m, i) => {\n                const isSelected = m.code === code;\n                return (\n                  <li\n                    key={m.code}\n                    id={`${uid}-c-${m.code}`}\n                    role=\"option\"\n                    aria-selected={isSelected}\n                    onMouseEnter={() => setActiveIndex(i)}\n                    onMouseDown={(e) => e.preventDefault()}\n                    onClick={() => pickCurrency(m.code)}\n                    className=\"flex cursor-pointer items-center\"\n                    style={{ gap: 10, padding: `${s.height * 0.16}px ${s.padX - 6}px`, borderRadius: s.radius - 4, background: i === activeIndex ? p.highlight : \"transparent\", color: p.text, transition: \"background 0.12s ease\" }}\n                  >\n                    <span className=\"flex shrink-0 items-center justify-center\" style={{ width: 30, height: 24, borderRadius: 7, background: p.chip, fontSize: m.symbol.length > 2 ? s.font - 5 : s.font - 2, fontWeight: 600 }}>\n                      {m.symbol}\n                    </span>\n                    <span style={{ fontSize: s.font - 1, fontWeight: isSelected ? 700 : 600, letterSpacing: \"0.02em\" }}>{m.code}</span>\n                    <span className=\"min-w-0 flex-1 truncate\" style={{ fontSize: s.font - 2, color: p.muted }}>\n                      {m.name}\n                    </span>\n                    <span className=\"flex shrink-0 justify-end\" style={{ width: 14 }}>\n                      {isSelected && (\n                        <svg width=\"14\" height=\"14\" viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden=\"true\">\n                          <motion.path d=\"M3 8.2L6.4 11.6L13 4.4\" stroke={p.text} strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" initial={{ pathLength: 0 }} animate={{ pathLength: 1 }} transition={{ duration: 0.25, ease: \"easeOut\" }} />\n                        </svg>\n                      )}\n                    </span>\n                  </li>\n                );\n              })}\n            </motion.ul>\n          )}\n        </AnimatePresence>\n      </div>\n\n      {helperText && (\n        <span id={`${uid}-help`} style={{ color: p.muted, fontSize: s.font - 1.5, lineHeight: 1.4, visibility: open ? \"hidden\" : \"visible\" }}>\n          {helperText}\n        </span>\n      )}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}