{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"nudge-field","type":"registry:ui","title":"Nudge Field","description":"A text field that answers a bad value with a quick damped shake, a destructive border and an error message that slides in below. Shakes again on every failed submit.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/nudge-field.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { AnimatePresence, MotionConfig, motion, useAnimate } from \"motion/react\";\nimport { useEffect, useId, useRef, type InputHTMLAttributes } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface NudgeFieldProps extends Omit<InputHTMLAttributes<HTMLInputElement>, \"size\"> {\n  label?: string;\n  /** Error message. The field shakes when it appears or changes. */\n  error?: string;\n  /** Bump this on every failed submit to shake again with the same error. */\n  attempt?: number;\n  /** Shake distance in px. */\n  intensity?: number;\n  /** Shake length in seconds. */\n  duration?: number;\n  className?: string;\n}\n\n/**\n * A text field that answers a bad value with a quick, damped head-shake,\n * turns its border to the destructive color and slides the message in below.\n */\nexport function NudgeField({\n  label,\n  error,\n  attempt = 0,\n  intensity = 9,\n  duration = 0.45,\n  className,\n  id,\n  ...input\n}: NudgeFieldProps) {\n  const autoId = useId();\n  const fieldId = id ?? autoId;\n  const msgId = `${fieldId}-error`;\n  const [scope, animate] = useAnimate<HTMLDivElement>();\n  const first = useRef(true);\n\n  useEffect(() => {\n    if (first.current) {\n      first.current = false;\n      return;\n    }\n    if (!error || !scope.current) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    // Damped: each swing is smaller than the last, then it settles at rest.\n    const i = intensity;\n    animate(scope.current, { x: [0, -i, i * 0.8, -i * 0.55, i * 0.35, -i * 0.15, 0] }, { duration, ease: \"easeOut\" });\n  }, [error, attempt, intensity, duration, animate, scope]);\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div className={cn(\"grid gap-1.5\", className)}>\n        {label && (\n          <label htmlFor={fieldId} className=\"text-sm font-medium text-foreground\">\n            {label}\n          </label>\n        )}\n        <div ref={scope}>\n          <input\n            id={fieldId}\n            aria-invalid={!!error}\n            aria-describedby={error ? msgId : undefined}\n            className={cn(\n              \"h-10 w-full rounded-lg border bg-background px-3 text-sm text-foreground outline-none transition-[border-color,box-shadow] placeholder:text-muted-foreground\",\n              error\n                ? \"border-destructive focus:ring-[3px] focus:ring-destructive/25\"\n                : \"border-border focus:border-ring focus:ring-[3px] focus:ring-ring/30\",\n            )}\n            {...input}\n          />\n        </div>\n        <AnimatePresence initial={false}>\n          {error && (\n            <motion.p\n              key=\"error\"\n              id={msgId}\n              role=\"alert\"\n              className=\"overflow-hidden text-xs font-medium text-destructive\"\n              initial={{ height: 0, opacity: 0, y: -4 }}\n              animate={{ height: \"auto\", opacity: 1, y: 0 }}\n              exit={{ height: 0, opacity: 0, y: -4 }}\n              transition={{ duration: 0.22, ease: \"easeOut\" }}\n            >\n              {error}\n            </motion.p>\n          )}\n        </AnimatePresence>\n      </div>\n    </MotionConfig>\n  );\n}\n"}]}