{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"signup-form","type":"registry:ui","title":"Signup Form","description":"A sign-up card with a live, animated password-strength meter and rule checklist, terms consent, inline validation and a celebratory success state.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/signup-form.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useId, useState, type FormEvent, type ReactNode } from \"react\";\nimport { AnimatePresence, motion, useAnimationControls, useReducedMotion } from \"motion/react\";\nimport { Check, CircleAlert, Eye, EyeOff, LoaderCircle, Lock, Mail, MailCheck, User, X } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface SignupValues {\n  name: string;\n  email: string;\n  password: string;\n}\n\nexport interface SignupFormProps {\n  title?: string;\n  description?: string;\n  /** Minimum strength (0-4) the password must reach before the form submits. */\n  minStrength?: number;\n  /** Show the rule checklist under the strength meter. */\n  showRules?: boolean;\n  submitLabel?: string;\n  /** Called with valid values. Throw an Error to show its message as a form error. */\n  onSubmit?: (values: SignupValues) => void | Promise<void>;\n  /** Terms label. Links inside are fine. */\n  terms?: ReactNode;\n  footer?: ReactNode;\n  className?: string;\n}\n\ntype Key = \"name\" | \"email\" | \"password\" | \"terms\";\ntype Errors = Partial<Record<Key, string>>;\n\nconst EMAIL = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]{2,}$/;\n\nexport const passwordRules = [\n  { label: \"8+ characters\", test: (p: string) => p.length >= 8 },\n  { label: \"Upper & lowercase\", test: (p: string) => /[a-z]/.test(p) && /[A-Z]/.test(p) },\n  { label: \"A number\", test: (p: string) => /\\d/.test(p) },\n  { label: \"A symbol\", test: (p: string) => /[^A-Za-z0-9]/.test(p) },\n];\n\n/** 0 (under 8 chars) .. 4 (strong). Each extra rule met, and a length of 12+, adds a point. */\nexport function passwordStrength(p: string) {\n  if (p.length < 8) return 0;\n  const passed = passwordRules.filter((r) => r.test(p)).length + (p.length >= 12 ? 1 : 0);\n  return Math.min(4, Math.max(1, passed - 1));\n}\n\nconst LEVELS = [\n  { label: \"Too short\", color: \"#ef4444\" },\n  { label: \"Weak\", color: \"#f97316\" },\n  { label: \"Fair\", color: \"#eab308\" },\n  { label: \"Good\", color: \"#84cc16\" },\n  { label: \"Strong\", color: \"#10b981\" },\n];\n\nconst inputCls = cn(\n  \"peer h-11 w-full rounded-xl border border-input bg-background/60 pl-10 text-sm text-foreground shadow-xs outline-none\",\n  \"placeholder:text-muted-foreground/70 transition-[border-color,box-shadow,background-color] duration-200\",\n  \"hover:border-foreground/20 focus:border-ring focus:bg-background focus:ring-[3px] focus:ring-ring/25\",\n  \"aria-invalid:border-destructive aria-invalid:focus:ring-destructive/25 disabled:opacity-60\",\n);\nconst iconCls =\n  \"pointer-events-none absolute top-1/2 left-3.5 size-4 -translate-y-1/2 text-muted-foreground transition-colors peer-focus:text-foreground\";\n\nfunction FieldError({ id, children }: { id: string; children?: string }) {\n  return (\n    <AnimatePresence initial={false}>\n      {children && (\n        <motion.p\n          id={id}\n          initial={{ height: 0, opacity: 0 }}\n          animate={{ height: \"auto\", opacity: 1 }}\n          exit={{ height: 0, opacity: 0 }}\n          transition={{ duration: 0.2 }}\n          className=\"overflow-hidden text-xs text-destructive\"\n        >\n          <span className=\"flex items-center gap-1.5 pt-1.5\">\n            <CircleAlert className=\"size-3.5 shrink-0\" aria-hidden />\n            {children}\n          </span>\n        </motion.p>\n      )}\n    </AnimatePresence>\n  );\n}\n\nexport function SignupForm({\n  title = \"Create your account\",\n  description = \"Start your 14-day trial. No credit card needed.\",\n  minStrength = 2,\n  showRules = true,\n  submitLabel = \"Create account\",\n  onSubmit = () => new Promise((r) => setTimeout(r, 1500)),\n  terms = \"I agree to the Terms of Service and Privacy Policy\",\n  footer,\n  className,\n}: SignupFormProps) {\n  const id = useId();\n  const reduce = useReducedMotion();\n  const shake = useAnimationControls();\n  const [v, setV] = useState({ name: \"\", email: \"\", password: \"\", terms: false });\n  const [touched, setTouched] = useState<Partial<Record<Key, boolean>>>({});\n  const [reveal, setReveal] = useState(false);\n  const [status, setStatus] = useState<\"idle\" | \"loading\" | \"success\">(\"idle\");\n  const [formError, setFormError] = useState(\"\");\n\n  const strength = passwordStrength(v.password);\n  const level = LEVELS[strength];\n\n  const errors: Errors = {};\n  if (v.name.trim().length < 2) errors.name = v.name.trim() ? \"Name looks too short.\" : \"Tell us your name.\";\n  if (!v.email.trim()) errors.email = \"Email is required.\";\n  else if (!EMAIL.test(v.email.trim())) errors.email = \"Enter a valid email address.\";\n  if (!v.password) errors.password = \"Choose a password.\";\n  else if (v.password.length < 8) errors.password = \"Use at least 8 characters.\";\n  else if (strength < minStrength) errors.password = `Too weak. Aim for at least \"${LEVELS[minStrength].label}\".`;\n  if (!v.terms) errors.terms = \"Please accept the terms to continue.\";\n  const show = (k: Key) => (touched[k] ? errors[k] : undefined);\n  const blur = (k: Key) => () => setTouched((t) => ({ ...t, [k]: true }));\n  const set = (k: Key) => (e: { target: HTMLInputElement }) =>\n    setV((s) => ({ ...s, [k]: k === \"terms\" ? e.target.checked : e.target.value }));\n\n  const submit = async (e: FormEvent<HTMLFormElement>) => {\n    e.preventDefault();\n    setTouched({ name: true, email: true, password: true, terms: true });\n    setFormError(\"\");\n    if (Object.keys(errors).length) {\n      if (!reduce) shake.start({ x: [0, -10, 9, -6, 4, 0], transition: { duration: 0.4 } });\n      e.currentTarget.querySelector<HTMLInputElement>(\"[aria-invalid=true]\")?.focus();\n      return;\n    }\n    setStatus(\"loading\");\n    try {\n      await onSubmit({ name: v.name.trim(), email: v.email.trim(), password: v.password });\n      setStatus(\"success\");\n    } catch (err) {\n      setStatus(\"idle\");\n      setFormError(err instanceof Error ? err.message : \"Something went wrong. Try again.\");\n    }\n  };\n\n  return (\n    <motion.div\n      animate={shake}\n      className={cn(\n        \"relative w-full max-w-sm overflow-hidden rounded-3xl border border-border bg-card p-7 text-card-foreground shadow-2xl shadow-black/20\",\n        className,\n      )}\n    >\n      <div\n        aria-hidden\n        className=\"pointer-events-none absolute -top-24 -right-16 size-56 rounded-full bg-primary/20 blur-3xl\"\n      />\n      <AnimatePresence mode=\"wait\" initial={false}>\n        {status === \"success\" ? (\n          <motion.div\n            key=\"done\"\n            role=\"status\"\n            initial={{ opacity: 0, y: 10 }}\n            animate={{ opacity: 1, y: 0 }}\n            exit={{ opacity: 0 }}\n            className=\"relative grid place-items-center py-8 text-center\"\n          >\n            <div className=\"relative grid size-16 place-items-center\">\n              {!reduce &&\n                [0, 1].map((i) => (\n                  <motion.span\n                    key={i}\n                    aria-hidden\n                    className=\"absolute inset-0 rounded-full border-2 border-primary\"\n                    initial={{ scale: 0.6, opacity: 0.8 }}\n                    animate={{ scale: 1.9, opacity: 0 }}\n                    transition={{ duration: 1.4, delay: 0.2 + i * 0.35, ease: \"easeOut\" }}\n                  />\n                ))}\n              <motion.span\n                initial={reduce ? false : { scale: 0 }}\n                animate={{ scale: 1 }}\n                transition={{ type: \"spring\", stiffness: 360, damping: 16 }}\n                className=\"grid size-16 place-items-center rounded-full bg-primary text-primary-foreground shadow-lg shadow-primary/30\"\n              >\n                <MailCheck className=\"size-7\" aria-hidden />\n              </motion.span>\n            </div>\n            <h2 className=\"mt-6 text-xl font-semibold tracking-tight\">Check your inbox</h2>\n            <p className=\"mt-1.5 max-w-64 text-sm text-muted-foreground\">\n              Welcome aboard, {v.name.trim().split(\" \")[0]}! We sent a confirmation link to{\" \"}\n              <span className=\"font-medium text-foreground\">{v.email.trim()}</span>.\n            </p>\n            <button\n              type=\"button\"\n              onClick={() => {\n                setStatus(\"idle\");\n                setV({ name: \"\", email: \"\", password: \"\", terms: false });\n                setTouched({});\n              }}\n              className=\"mt-6 rounded-lg px-3 py-1.5 text-sm font-medium text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50\"\n            >\n              Use a different email\n            </button>\n          </motion.div>\n        ) : (\n          <motion.form\n            key=\"form\"\n            noValidate\n            onSubmit={submit}\n            aria-labelledby={`${id}-t`}\n            aria-busy={status === \"loading\"}\n            initial={{ opacity: 0 }}\n            animate={{ opacity: 1 }}\n            exit={{ opacity: 0, scale: 0.98 }}\n            className=\"relative\"\n          >\n            <h2 id={`${id}-t`} className=\"text-2xl font-semibold tracking-tight\">\n              {title}\n            </h2>\n            {description && <p className=\"mt-1 text-sm text-muted-foreground\">{description}</p>}\n\n            <fieldset disabled={status === \"loading\"} className=\"mt-6 grid gap-4\">\n              <AnimatePresence initial={false}>\n                {formError && (\n                  <motion.div\n                    role=\"alert\"\n                    initial={{ opacity: 0, y: -6 }}\n                    animate={{ opacity: 1, y: 0 }}\n                    exit={{ opacity: 0, y: -6 }}\n                    className=\"flex items-start gap-2 rounded-xl border border-destructive/30 bg-destructive/10 px-3 py-2.5 text-sm text-destructive\"\n                  >\n                    <CircleAlert className=\"mt-0.5 size-4 shrink-0\" aria-hidden />\n                    {formError}\n                  </motion.div>\n                )}\n              </AnimatePresence>\n\n              <div>\n                <label htmlFor={`${id}-n`} className=\"mb-1.5 block text-sm font-medium\">\n                  Full name\n                </label>\n                <div className=\"relative\">\n                  <input\n                    id={`${id}-n`}\n                    autoComplete=\"name\"\n                    placeholder=\"Ada Lovelace\"\n                    value={v.name}\n                    onChange={set(\"name\")}\n                    onBlur={blur(\"name\")}\n                    aria-invalid={!!show(\"name\")}\n                    aria-describedby={show(\"name\") ? `${id}-ne` : undefined}\n                    className={cn(inputCls, \"pr-3\")}\n                  />\n                  <User className={iconCls} aria-hidden />\n                </div>\n                <FieldError id={`${id}-ne`}>{show(\"name\")}</FieldError>\n              </div>\n\n              <div>\n                <label htmlFor={`${id}-e`} className=\"mb-1.5 block text-sm font-medium\">\n                  Work email\n                </label>\n                <div className=\"relative\">\n                  <input\n                    id={`${id}-e`}\n                    type=\"email\"\n                    autoComplete=\"email\"\n                    placeholder=\"ada@analytical.co\"\n                    value={v.email}\n                    onChange={set(\"email\")}\n                    onBlur={blur(\"email\")}\n                    aria-invalid={!!show(\"email\")}\n                    aria-describedby={show(\"email\") ? `${id}-ee` : undefined}\n                    className={cn(inputCls, \"pr-3\")}\n                  />\n                  <Mail className={iconCls} aria-hidden />\n                </div>\n                <FieldError id={`${id}-ee`}>{show(\"email\")}</FieldError>\n              </div>\n\n              <div>\n                <label htmlFor={`${id}-p`} className=\"mb-1.5 block text-sm font-medium\">\n                  Password\n                </label>\n                <div className=\"relative\">\n                  <input\n                    id={`${id}-p`}\n                    type={reveal ? \"text\" : \"password\"}\n                    autoComplete=\"new-password\"\n                    placeholder=\"Create a password\"\n                    value={v.password}\n                    onChange={set(\"password\")}\n                    onBlur={blur(\"password\")}\n                    aria-invalid={!!show(\"password\")}\n                    aria-describedby={`${id}-ps${show(\"password\") ? ` ${id}-pe` : \"\"}`}\n                    className={cn(inputCls, \"pr-11\")}\n                  />\n                  <Lock className={iconCls} aria-hidden />\n                  <button\n                    type=\"button\"\n                    onClick={() => setReveal((r) => !r)}\n                    aria-label={reveal ? \"Hide password\" : \"Show password\"}\n                    aria-pressed={reveal}\n                    className=\"absolute top-1/2 right-1.5 grid size-8 -translate-y-1/2 place-items-center rounded-lg text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/50\"\n                  >\n                    {reveal ? <EyeOff className=\"size-4\" aria-hidden /> : <Eye className=\"size-4\" aria-hidden />}\n                  </button>\n                </div>\n\n                {/* strength meter */}\n                <div className=\"mt-2.5 flex items-center gap-3\">\n                  <div className=\"grid flex-1 grid-cols-4 gap-1.5\" aria-hidden>\n                    {[1, 2, 3, 4].map((i) => (\n                      <span key={i} className=\"relative h-1.5 overflow-hidden rounded-full bg-muted\">\n                        <motion.span\n                          className=\"absolute inset-0 origin-left rounded-full\"\n                          initial={false}\n                          animate={{ scaleX: strength >= i ? 1 : 0, backgroundColor: level.color }}\n                          transition={{ type: \"spring\", stiffness: 300, damping: 30, delay: strength >= i ? (i - 1) * 0.04 : 0 }}\n                        />\n                      </span>\n                    ))}\n                  </div>\n                  <span id={`${id}-ps`} aria-live=\"polite\" className=\"w-16 text-right text-xs font-medium tabular-nums\" style={{ color: v.password ? level.color : undefined }}>\n                    <span className=\"sr-only\">Password strength: </span>\n                    {v.password ? level.label : <span className=\"text-muted-foreground\">Strength</span>}\n                  </span>\n                </div>\n                {showRules && (\n                  <ul className=\"mt-2.5 grid grid-cols-2 gap-x-3 gap-y-1.5 text-xs\">\n                    {passwordRules.map((r) => {\n                      const ok = r.test(v.password);\n                      return (\n                        <li key={r.label} className={cn(\"flex items-center gap-1.5 transition-colors duration-300\", ok ? \"text-foreground\" : \"text-muted-foreground\")}>\n                          <motion.span\n                            initial={false}\n                            animate={{ scale: ok ? [1, 1.35, 1] : 1 }}\n                            transition={{ duration: 0.3 }}\n                            className={cn(\n                              \"grid size-3.5 place-items-center rounded-full transition-colors duration-300\",\n                              ok ? \"bg-emerald-500 text-white\" : \"bg-muted text-muted-foreground\",\n                            )}\n                          >\n                            {ok ? <Check className=\"size-2.5\" strokeWidth={4} aria-hidden /> : <X className=\"size-2\" strokeWidth={4} aria-hidden />}\n                          </motion.span>\n                          {r.label}\n                          <span className=\"sr-only\">{ok ? \" (met)\" : \" (not met)\"}</span>\n                        </li>\n                      );\n                    })}\n                  </ul>\n                )}\n                <FieldError id={`${id}-pe`}>{show(\"password\")}</FieldError>\n              </div>\n\n              <div>\n                <label className=\"flex cursor-pointer items-start gap-2.5 text-sm text-muted-foreground select-none\">\n                  <input\n                    type=\"checkbox\"\n                    checked={v.terms}\n                    onChange={set(\"terms\")}\n                    onBlur={blur(\"terms\")}\n                    aria-invalid={!!show(\"terms\")}\n                    aria-describedby={show(\"terms\") ? `${id}-te` : undefined}\n                    className=\"mt-0.5 size-4 shrink-0 cursor-pointer accent-primary outline-none focus-visible:ring-2 focus-visible:ring-ring/50\"\n                  />\n                  <span>{terms}</span>\n                </label>\n                <FieldError id={`${id}-te`}>{show(\"terms\")}</FieldError>\n              </div>\n\n              <button\n                type=\"submit\"\n                className=\"relative mt-1 inline-flex h-11 items-center justify-center gap-2 overflow-hidden rounded-xl bg-primary text-sm font-semibold text-primary-foreground shadow-md shadow-primary/25 outline-none transition-[background-color,scale,box-shadow] hover:bg-primary/90 hover:shadow-lg hover:shadow-primary/30 focus-visible:ring-[3px] focus-visible:ring-ring/50 active:scale-[0.98] disabled:opacity-80\"\n              >\n                {status === \"loading\" ? (\n                  <>\n                    <LoaderCircle className=\"size-4 animate-spin\" aria-hidden />\n                    Creating account…\n                  </>\n                ) : (\n                  submitLabel\n                )}\n              </button>\n            </fieldset>\n            {footer && <div className=\"mt-5 text-center text-sm text-muted-foreground\">{footer}</div>}\n          </motion.form>\n        )}\n      </AnimatePresence>\n    </motion.div>\n  );\n}\n"}]}