{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "form-field",
  "title": "Form Field",
  "description": "A form field wrapper with label, description, required or optional marker, info tooltip, character counter and animated helper, error and success messages, with sync or async validation and aria wiring for any control.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/form-field/form-field.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 FormFieldSize = \"sm\" | \"md\" | \"lg\";\nexport type FormFieldStatus = \"idle\" | \"validating\" | \"success\" | \"error\";\n\n/** Props wired onto the control inside the field, for custom children. */\nexport interface FormFieldControlProps {\n  id: string;\n  \"aria-describedby\"?: string;\n  \"aria-invalid\"?: boolean;\n  \"aria-required\"?: boolean;\n}\n\nexport interface FormFieldProps {\n  label: string;\n  /** A line under the label, e.g. what the value is used for. */\n  description?: string;\n  /** Shown in a small tooltip on the info icon next to the label. */\n  info?: string;\n  required?: boolean;\n  /** Marks the label \"(optional)\"; ignored when required. */\n  optional?: boolean;\n  helperText?: string;\n  /** Forces an error message (e.g. from the server). */\n  errorText?: string;\n  /** Shown in the success state, e.g. \"Username is available\". */\n  successText?: string;\n  /** Runs on blur, then on every change once an error has shown. Return a message or null. May be async. */\n  validate?: (value: string) => string | null | undefined | Promise<string | null | undefined>;\n  /** Shows the counter; the built-in input also stops at this length. */\n  maxLength?: number;\n  /** Built-in input only. */\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (value: string) => void;\n  type?: \"text\" | \"email\" | \"password\" | \"url\" | \"search\" | \"tel\";\n  placeholder?: string;\n  multiline?: boolean;\n  name?: string;\n  autoComplete?: string;\n  /** Your own control. A single element gets id and aria props cloned in; a function receives them. */\n  children?: React.ReactElement | ((control: FormFieldControlProps) => React.ReactNode);\n  size?: FormFieldSize;\n  theme?: \"dark\" | \"light\";\n  width?: number | string;\n  disabled?: boolean;\n  className?: string;\n}\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)\", tipBg: \"#161616\", tipBorder: \"rgba(255,255,255,0.12)\", error: \"#FF7A6B\", success: \"#F5F4F1\" },\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)\", tipBg: \"#FFFFFF\", tipBorder: \"rgba(10,10,10,0.12)\", error: \"#E5484D\", success: \"#0A0A0A\" },\n};\n\nconst SIZES: Record<FormFieldSize, { 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 Spinner({ color, size }: { color: string; size: number }) {\n  return (\n    <motion.svg width={size} height={size} viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden=\"true\" animate={{ rotate: 360 }} transition={{ repeat: Infinity, duration: 0.8, ease: \"linear\" }}>\n      <circle cx=\"8\" cy=\"8\" r=\"6\" stroke={color} strokeOpacity=\"0.25\" strokeWidth=\"2\" />\n      <path d=\"M14 8a6 6 0 0 0-6-6\" stroke={color} strokeWidth=\"2\" strokeLinecap=\"round\" />\n    </motion.svg>\n  );\n}\n\nfunction StatusIcon({ status, color }: { status: \"error\" | \"success\"; color: string }) {\n  return (\n    <svg width=\"14\" height=\"14\" viewBox=\"0 0 16 16\" fill=\"none\" className=\"shrink-0\" aria-hidden=\"true\" style={{ marginTop: 1 }}>\n      <circle cx=\"8\" cy=\"8\" r=\"6.75\" stroke={color} strokeWidth=\"1.4\" />\n      {status === \"success\" ? (\n        <motion.path d=\"M5 8.2L7.1 10.3L11 6\" stroke={color} strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" initial={{ pathLength: 0 }} animate={{ pathLength: 1 }} transition={{ duration: 0.25, ease: \"easeOut\" }} />\n      ) : (\n        <path d=\"M8 4.8V8.6M8 11.1v.1\" stroke={color} strokeWidth=\"1.6\" strokeLinecap=\"round\" />\n      )}\n    </svg>\n  );\n}\n\nfunction InfoTip({ text, p, uid }: { text: string; p: (typeof PALETTES)[\"dark\"]; uid: string }) {\n  const [open, setOpen] = React.useState(false);\n  return (\n    <span className=\"relative inline-flex\">\n      <button\n        type=\"button\"\n        aria-label=\"More info\"\n        aria-describedby={open ? `${uid}-tip` : undefined}\n        onMouseEnter={() => setOpen(true)}\n        onMouseLeave={() => setOpen(false)}\n        onFocus={() => setOpen(true)}\n        onBlur={() => setOpen(false)}\n        className=\"flex cursor-help items-center justify-center rounded-full border-none bg-transparent p-0 outline-none focus-visible:ring-2\"\n        style={{ width: 16, height: 16, color: p.muted, [\"--tw-ring-color\" as string]: p.focus }}\n      >\n        <svg width=\"14\" height=\"14\" viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden=\"true\">\n          <circle cx=\"8\" cy=\"8\" r=\"6.75\" stroke=\"currentColor\" strokeWidth=\"1.4\" />\n          <path d=\"M8 7.2v4M8 4.7v.1\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" />\n        </svg>\n      </button>\n      <AnimatePresence>\n        {open && (\n          <motion.span\n            id={`${uid}-tip`}\n            role=\"tooltip\"\n            className=\"absolute left-1/2 z-50 font-normal\"\n            style={{ bottom: \"calc(100% + 8px)\", width: \"max-content\", maxWidth: 240, padding: \"7px 10px\", borderRadius: 9, background: p.tipBg, border: `1px solid ${p.tipBorder}`, color: p.text, fontSize: 12.5, lineHeight: 1.45, letterSpacing: 0, boxShadow: \"0 12px 30px rgba(0,0,0,0.3)\", x: \"-50%\" }}\n            initial={{ opacity: 0, y: 4, scale: 0.97 }}\n            animate={{ opacity: 1, y: 0, scale: 1 }}\n            exit={{ opacity: 0, y: 4, scale: 0.97 }}\n            transition={{ duration: 0.14 }}\n          >\n            {text}\n          </motion.span>\n        )}\n      </AnimatePresence>\n    </span>\n  );\n}\n\nexport function FormField({\n  label,\n  description,\n  info,\n  required = false,\n  optional = false,\n  helperText,\n  errorText,\n  successText,\n  validate,\n  maxLength,\n  value,\n  defaultValue = \"\",\n  onValueChange,\n  type = \"text\",\n  placeholder,\n  multiline = false,\n  name,\n  autoComplete,\n  children,\n  size = \"md\",\n  theme = \"dark\",\n  width = 340,\n  disabled = false,\n  className,\n}: FormFieldProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const controlId = `${uid}-control`;\n  const [internal, setInternal] = React.useState(defaultValue);\n  const text = value !== undefined ? value : internal;\n  const [focused, setFocused] = React.useState(false);\n  const [hovered, setHovered] = React.useState(false);\n  const [message, setMessage] = React.useState<string | null>(null);\n  const [status, setStatus] = React.useState<FormFieldStatus>(\"idle\");\n  const [shaking, setShaking] = React.useState(0);\n  const run = React.useRef(0);\n\n  // \"Reward early, punish late\": check on blur; once an error is showing, re-check on every change\n  // so it clears the moment the value is fixed.\n  const check = async (v: string, fromBlur: boolean) => {\n    if (!validate && !required) return;\n    const id = ++run.current;\n    const requiredMsg = required && v.trim() === \"\" ? `${label} is required` : null;\n    let result: string | null | undefined = requiredMsg;\n    if (!requiredMsg && validate) {\n      const out = validate(v);\n      if (out instanceof Promise) {\n        setStatus(\"validating\");\n        result = await out;\n      } else result = out;\n    }\n    if (id !== run.current) return;\n    setMessage(result ?? null);\n    if (result) {\n      if (fromBlur && status !== \"error\") setShaking((n) => n + 1);\n      setStatus(\"error\");\n    } else setStatus(v.trim() !== \"\" && successText ? \"success\" : \"idle\");\n  };\n\n  const onChange = (v: string) => {\n    const next = maxLength ? v.slice(0, maxLength) : v;\n    if (value === undefined) setInternal(next);\n    onValueChange?.(next);\n    // Once the field has been checked (error, pending or success), keep it in sync while typing.\n    if (status !== \"idle\") void check(next, false);\n  };\n\n  const shownError = errorText ?? (status === \"error\" ? message : null);\n  const shownSuccess = !shownError && status === \"success\" ? successText : null;\n  const msg = shownError ?? shownSuccess ?? helperText ?? null;\n  const msgKind: \"error\" | \"success\" | \"helper\" = shownError ? \"error\" : shownSuccess ? \"success\" : \"helper\";\n\n  const describedBy = [description ? `${uid}-desc` : null, msg ? `${uid}-msg` : null].filter(Boolean).join(\" \") || undefined;\n  const controlProps: FormFieldControlProps = { id: controlId, \"aria-describedby\": describedBy, \"aria-invalid\": shownError ? true : undefined, \"aria-required\": required || undefined };\n\n  const count = text.length;\n  const near = maxLength ? count / maxLength >= 0.9 : false;\n  const borderColor = shownError ? p.error : focused ? p.focus : hovered ? p.borderHover : p.border;\n  const ring = shownError ? p.error : p.focus;\n\n  let control: React.ReactNode;\n  if (typeof children === \"function\") control = children(controlProps);\n  else if (children && React.isValidElement(children)) control = React.cloneElement(children as React.ReactElement<FormFieldControlProps>, controlProps);\n  else {\n    const inputClass = \"w-full border-none bg-transparent outline-none placeholder:text-[color:var(--ff-placeholder)]\";\n    const inputStyle: React.CSSProperties = { color: p.text, fontSize: s.font, fontWeight: 500, fontFamily: \"inherit\", [\"--ff-placeholder\" as string]: p.faint };\n    const onBlur = () => {\n      setFocused(false);\n      void check(text, true);\n    };\n    control = (\n      <motion.div\n        key={shaking}\n        onMouseEnter={() => setHovered(true)}\n        onMouseLeave={() => setHovered(false)}\n        animate={shaking ? { x: [0, -6, 5, -3, 2, 0] } : undefined}\n        transition={{ duration: 0.36 }}\n        className=\"flex items-center\"\n        style={{ minHeight: s.height, padding: multiline ? `${s.padX * 0.7}px ${s.padX}px` : `0 ${s.padX}px`, gap: 8, borderRadius: s.radius, background: p.bg, border: `1px solid ${borderColor}`, boxShadow: focused || shownError ? `0 0 0 3px color-mix(in srgb, ${ring} 14%, transparent)` : \"0 0 0 0 transparent\", transition: \"border-color 0.18s ease, box-shadow 0.18s ease\" }}\n      >\n        {multiline ? (\n          <textarea {...controlProps} name={name} autoComplete={autoComplete} disabled={disabled} placeholder={placeholder} value={text} maxLength={maxLength} rows={4} onChange={(e) => onChange(e.target.value)} onFocus={() => setFocused(true)} onBlur={onBlur} className={cn(inputClass, \"resize-y\")} style={{ ...inputStyle, lineHeight: 1.5 }} />\n        ) : (\n          <input {...controlProps} type={type} name={name} autoComplete={autoComplete} disabled={disabled} placeholder={placeholder} value={text} maxLength={maxLength} onChange={(e) => onChange(e.target.value)} onFocus={() => setFocused(true)} onBlur={onBlur} className={inputClass} style={{ ...inputStyle, height: s.height - 2 }} />\n        )}\n        <AnimatePresence>\n          {status === \"validating\" && (\n            <motion.span key=\"spin\" className=\"flex shrink-0\" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>\n              <Spinner color={p.muted} size={s.font} />\n            </motion.span>\n          )}\n          {status === \"success\" && (\n            <motion.span key=\"ok\" className=\"flex shrink-0\" initial={{ opacity: 0, scale: 0.6 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: 0.6 }}>\n              <svg width={s.font + 1} height={s.font + 1} viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden=\"true\">\n                <motion.path d=\"M3 8.2L6.4 11.6L13 4.4\" stroke={p.success} strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" initial={{ pathLength: 0 }} animate={{ pathLength: 1 }} transition={{ duration: 0.25 }} />\n              </svg>\n            </motion.span>\n          )}\n        </AnimatePresence>\n      </motion.div>\n    );\n  }\n\n  return (\n    <div className={cn(\"inline-flex flex-col\", className)} style={{ width, maxWidth: \"100%\", gap: 8, fontFamily: \"Inter, sans-serif\", opacity: disabled ? 0.5 : 1 }}>\n      <div className=\"flex flex-col\" style={{ gap: 3 }}>\n        <div className=\"flex items-center\" style={{ gap: 6 }}>\n          <label htmlFor={controlId} className=\"font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: s.font }}>\n            {label}\n            {required && (\n              <span aria-hidden=\"true\" style={{ color: p.error, marginLeft: 3 }}>\n                *\n              </span>\n            )}\n            {!required && optional && <span style={{ color: p.muted, fontWeight: 500, marginLeft: 6, fontSize: s.font - 1.5 }}>(optional)</span>}\n          </label>\n          {info && <InfoTip text={info} p={p} uid={uid} />}\n          <span className=\"flex-1\" />\n          {maxLength !== undefined && (\n            <motion.span aria-live=\"polite\" animate={{ color: count >= maxLength ? p.error : near ? p.text : p.faint }} transition={{ duration: 0.2 }} style={{ fontSize: s.font - 2.5, fontVariantNumeric: \"tabular-nums\", fontWeight: 500 }}>\n              <span className=\"sr-only\">{`${count} of ${maxLength} characters`}</span>\n              <span aria-hidden=\"true\">\n                {count}/{maxLength}\n              </span>\n            </motion.span>\n          )}\n        </div>\n        {description && (\n          <span id={`${uid}-desc`} style={{ color: p.muted, fontSize: s.font - 1.5, lineHeight: 1.45 }}>\n            {description}\n          </span>\n        )}\n      </div>\n\n      {control}\n\n      <div style={{ minHeight: helperText || validate || required || errorText ? s.font * 1.4 : 0 }}>\n        <AnimatePresence initial={false} mode=\"wait\">\n          {msg && (\n            <motion.span\n              key={`${msgKind}-${msg}`}\n              id={`${uid}-msg`}\n              role={msgKind === \"error\" ? \"alert\" : undefined}\n              className=\"flex items-start\"\n              style={{ gap: 6, color: msgKind === \"error\" ? p.error : msgKind === \"success\" ? p.success : p.muted, fontSize: s.font - 1.5, lineHeight: 1.4 }}\n              initial={{ opacity: 0, y: -4 }}\n              animate={{ opacity: 1, y: 0 }}\n              exit={{ opacity: 0, y: -4 }}\n              transition={{ duration: 0.16 }}\n            >\n              {msgKind !== \"helper\" && <StatusIcon status={msgKind} color={msgKind === \"error\" ? p.error : p.success} />}\n              {msg}\n            </motion.span>\n          )}\n        </AnimatePresence>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}