{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "newsletter-cta",
  "type": "registry:block",
  "title": "Newsletter CTA",
  "description": "An email capture that checks as you go, catches gmial-style typos, shakes once on a bad address, then morphs into a success note while your avatar joins the stack and the reader count rolls up by one.",
  "categories": [
    "block",
    "Marketing"
  ],
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils",
    "https://coss.com/ui/r/button.json"
  ],
  "files": [
    {
      "path": "registry/devl/newsletter-cta.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport { AlertCircleIcon, ArrowRightIcon, LockIcon } from \"lucide-react\";\nimport { AnimatePresence, motion, MotionConfig, useAnimationControls, useReducedMotion } from \"motion/react\";\nimport { type FormEvent, type ReactNode, useEffect, useId, useRef, useState } from \"react\";\nimport { Button } from \"@/components/ui/button\";\nimport { cn } from \"@/lib/utils\";\n\nfunction useReducedMotionSafe() {\n  const prefers = useReducedMotion();\n  const [mounted, setMounted] = useState(false);\n  useEffect(() => setMounted(true), []);\n  return mounted && Boolean(prefers);\n}\n\nexport interface NewsletterIssue {\n  number: number;\n  title: string;\n  date: string;\n  minutes: number;\n}\n\nexport interface NewsletterCtaProps {\n  eyebrow?: string;\n  title: ReactNode;\n  description?: ReactNode;\n  subscribers: number;\n  readers?: string[];\n  issues?: NewsletterIssue[];\n  note?: string;\n  onSubscribe?: (email: string) => Promise<void> | void;\n  className?: string;\n}\n\nconst ease = [0.23, 1, 0.32, 1] as const;\nconst EMAIL = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]{2,}$/;\n\nconst tones = [\n  \"bg-rose-100 text-rose-700 dark:bg-rose-950 dark:text-rose-300\",\n  \"bg-sky-100 text-sky-700 dark:bg-sky-950 dark:text-sky-300\",\n  \"bg-amber-100 text-amber-800 dark:bg-amber-950 dark:text-amber-300\",\n  \"bg-emerald-100 text-emerald-700 dark:bg-emerald-950 dark:text-emerald-300\",\n  \"bg-violet-100 text-violet-700 dark:bg-violet-950 dark:text-violet-300\",\n];\n\nconst typos: Record<string, string> = {\n  \"gmial.com\": \"gmail.com\",\n  \"gmal.com\": \"gmail.com\",\n  \"gmail.co\": \"gmail.com\",\n  \"gnail.com\": \"gmail.com\",\n  \"hotmial.com\": \"hotmail.com\",\n  \"hotmail.co\": \"hotmail.com\",\n  \"yaho.com\": \"yahoo.com\",\n  \"outlok.com\": \"outlook.com\",\n  \"icloud.co\": \"icloud.com\",\n  \"iclod.com\": \"icloud.com\",\n};\n\nfunction suggest(email: string) {\n  const [user, domain] = email.toLowerCase().split(\"@\");\n  if (!user || !domain) return null;\n  const fixed = typos[domain];\n  return fixed ? `${user}@${fixed}` : null;\n}\n\nfunction RollingDigit({ digit }: { digit: number }) {\n  const reduce = useReducedMotionSafe();\n  return (\n    <span aria-hidden className=\"relative inline-block h-[1lh] w-[1ch] overflow-hidden align-top\">\n      <motion.span\n        animate={{ y: `${-digit * 10}%` }}\n        className=\"absolute inset-x-0 top-0 flex flex-col\"\n        initial={false}\n        transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 260, damping: 28 }}\n      >\n        {Array.from({ length: 10 }, (_, n) => (\n          <span className=\"block h-[1lh]\" key={n}>\n            {n}\n          </span>\n        ))}\n      </motion.span>\n    </span>\n  );\n}\n\nfunction Count({ value }: { value: number }) {\n  const text = value.toLocaleString(\"en-US\");\n  const chars = text.split(\"\");\n  return (\n    <span className=\"inline-flex tabular-nums\">\n      <span className=\"sr-only\">{text}</span>\n      {chars.map((char, i) => {\n        const key = chars.length - i;\n        return /\\d/.test(char) ? (\n          <RollingDigit digit={Number(char)} key={`d${key}`} />\n        ) : (\n          <span aria-hidden key={`s${key}`}>\n            {char}\n          </span>\n        );\n      })}\n    </span>\n  );\n}\n\nfunction initialsFrom(email: string) {\n  const name = email.split(\"@\")[0].replace(/[^a-z]/gi, \" \").trim();\n  const parts = name.split(/\\s+/).filter(Boolean);\n  return ((parts[0]?.[0] ?? \"Y\") + (parts[1]?.[0] ?? parts[0]?.[1] ?? \"\")).toUpperCase();\n}\n\nfunction IssueStack({ issues }: { issues: NewsletterIssue[] }) {\n  const reduce = useReducedMotionSafe();\n  const [open, setOpen] = useState(false);\n  const shown = issues.slice(0, 3);\n  return (\n    <div\n      className=\"relative mx-auto h-64 w-full max-w-[19rem]\"\n      onPointerEnter={(e) => e.pointerType === \"mouse\" && setOpen(true)}\n      onPointerLeave={() => setOpen(false)}\n    >\n      {shown.map((issue, i) => {\n        const depth = shown.length - 1 - i;\n        const rest = { y: depth * -18, scale: 1 - depth * 0.05, rotate: depth === 0 ? -2 : depth === 1 ? 2 : -1 };\n        const fanned = { y: depth * -46, scale: 1 - depth * 0.03, rotate: depth === 0 ? -2 : depth === 1 ? 1.5 : -1 };\n        return (\n          <motion.article\n            animate={reduce ? rest : open ? fanned : rest}\n            className={cn(\n              \"absolute inset-x-0 bottom-0 rounded-2xl border bg-popover p-5 shadow-lg/5 dark:shadow-black/30\",\n              depth > 0 && \"pointer-events-none\",\n            )}\n            initial={false}\n            key={issue.number}\n            style={{ zIndex: i }}\n            transition={{ type: \"spring\", stiffness: 300, damping: 30 }}\n          >\n            <div\n              className={cn(\n                \"flex items-center justify-between font-mono text-[0.6875rem] text-muted-foreground tabular-nums transition-opacity duration-200 ease-out\",\n                depth > 0 && !open && \"opacity-0\",\n              )}\n            >\n              <span>No. {issue.number}</span>\n              <span>{issue.date}</span>\n            </div>\n            <h3 className={cn(\"mt-6 line-clamp-2 min-h-[2lh] text-balance font-heading font-semibold text-lg leading-snug tracking-tight\", depth > 0 && \"opacity-0\")}>\n              {issue.title}\n            </h3>\n            <div className={cn(\"mt-5 space-y-1.5\", depth > 0 && \"opacity-0\")} aria-hidden>\n              <div className=\"h-1.5 w-full rounded-full bg-foreground/8\" />\n              <div className=\"h-1.5 w-11/12 rounded-full bg-foreground/8\" />\n              <div className=\"h-1.5 w-3/5 rounded-full bg-foreground/8\" />\n            </div>\n            <p className={cn(\"mt-5 text-muted-foreground text-xs\", depth > 0 && \"opacity-0\")}>\n              {issue.minutes} min read\n            </p>\n          </motion.article>\n        );\n      })}\n    </div>\n  );\n}\n\nexport function NewsletterCta({\n  eyebrow,\n  title,\n  description,\n  subscribers,\n  readers = [],\n  issues = [],\n  note = \"No spam. One click to unsubscribe.\",\n  onSubscribe,\n  className,\n}: NewsletterCtaProps) {\n  const reduce = useReducedMotionSafe();\n  const fieldId = useId();\n  const errorId = useId();\n  const inputRef = useRef<HTMLInputElement>(null);\n  const shake = useAnimationControls();\n  const [email, setEmail] = useState(\"\");\n  const [error, setError] = useState<string | null>(null);\n  const [status, setStatus] = useState<\"idle\" | \"sending\" | \"done\">(\"idle\");\n  const [count, setCount] = useState(subscribers);\n  const [joined, setJoined] = useState<string | null>(null);\n  const hint = error ? null : suggest(email);\n\n  useEffect(() => setCount(subscribers), [subscribers]);\n\n  const fail = (message: string) => {\n    setError(message);\n    if (!reduce) shake.start({ x: [0, -6, 5, -3, 2, 0], transition: { duration: 0.32, ease: \"easeOut\" } });\n    inputRef.current?.focus();\n  };\n\n  const submit = async (event: FormEvent) => {\n    event.preventDefault();\n    const value = email.trim();\n    if (!value) return fail(\"Add your email so we know where to send it.\");\n    if (!EMAIL.test(value)) return fail(\"That doesn't look like an email address yet.\");\n    setError(null);\n    setStatus(\"sending\");\n    try {\n      await Promise.all([onSubscribe?.(value), new Promise((r) => setTimeout(r, 900))]);\n      setJoined(initialsFrom(value));\n      setCount((c) => c + 1);\n      setStatus(\"done\");\n    } catch (err) {\n      setStatus(\"idle\");\n      fail(err instanceof Error ? err.message : \"Something went wrong. Try again in a moment.\");\n    }\n  };\n\n  const undo = () => {\n    setStatus(\"idle\");\n    setJoined(null);\n    setCount((c) => c - 1);\n    setEmail(\"\");\n    requestAnimationFrame(() => inputRef.current?.focus());\n  };\n\n  const stack = joined ? [...readers.slice(0, 4), joined] : readers.slice(0, 4);\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <section\n        className={cn(\n          \"relative isolate mx-auto w-full max-w-[1100px] overflow-hidden rounded-3xl border bg-card shadow-xs/5\",\n          className,\n        )}\n      >\n        <div\n          aria-hidden\n          className=\"absolute inset-0 -z-10 bg-[radial-gradient(var(--color-border)_1px,transparent_1px)] [background-size:16px_16px] [mask-image:radial-gradient(70%_80%_at_85%_50%,black,transparent)]\"\n        />\n        <div className=\"grid items-center gap-12 px-5 py-10 sm:px-12 sm:py-14 md:grid-cols-[minmax(0,1.15fr)_minmax(0,0.85fr)] md:gap-10\">\n          <div className=\"min-w-0\">\n            <div className=\"flex items-center gap-3\">\n              <div className=\"flex -space-x-1.5\">\n                <AnimatePresence initial={false}>\n                  {stack.map((initials, i) => (\n                    <motion.span\n                      animate={{ opacity: 1, scale: 1, x: 0 }}\n                      className={cn(\n                        \"flex size-8 items-center justify-center rounded-full font-semibold text-[0.625rem] tracking-tight ring-2 ring-card\",\n                        joined && i === stack.length - 1\n                          ? \"bg-primary text-primary-foreground\"\n                          : tones[i % tones.length],\n                      )}\n                      exit={{ opacity: 0, scale: 0.9, x: -6 }}\n                      initial={{ opacity: 0, scale: 0.9, x: -6 }}\n                      key={`${initials}-${i}`}\n                      layout\n                      transition={{ type: \"spring\", stiffness: 420, damping: 30 }}\n                    >\n                      {initials}\n                    </motion.span>\n                  ))}\n                </AnimatePresence>\n              </div>\n              <p className=\"flex items-center gap-1 text-muted-foreground text-xs\">\n                {joined ? \"You're reader no.\" : \"Join\"}\n                <span className=\"flex font-medium text-foreground\">\n                  <Count value={count} />\n                </span>\n                {!joined && \"readers\"}\n              </p>\n            </div>\n\n            {eyebrow && (\n              <p className=\"mt-8 font-medium text-muted-foreground text-xs uppercase tracking-widest\">{eyebrow}</p>\n            )}\n            <h2\n              className={cn(\n                \"text-balance font-heading font-semibold text-3xl leading-[1.1] tracking-tight sm:text-4xl\",\n                eyebrow ? \"mt-2\" : \"mt-8\",\n              )}\n            >\n              {title}\n            </h2>\n            {description && (\n              <p className=\"mt-4 max-w-md text-pretty text-muted-foreground text-sm leading-relaxed sm:text-base\">\n                {description}\n              </p>\n            )}\n\n            <div className=\"mt-8 max-w-md\">\n              <AnimatePresence initial={false} mode=\"popLayout\">\n                {status !== \"done\" ? (\n                  <motion.form\n                    animate={{ opacity: 1, scale: 1, filter: \"blur(0px)\" }}\n                    exit={{ opacity: 0, scale: 0.98, filter: \"blur(4px)\" }}\n                    initial={{ opacity: 0, scale: 0.98, filter: \"blur(4px)\" }}\n                    key=\"form\"\n                    noValidate\n                    onSubmit={submit}\n                    transition={{ duration: 0.25, ease }}\n                  >\n                    <label className=\"sr-only\" htmlFor={fieldId}>\n                      Email address\n                    </label>\n                    <motion.div\n                      animate={shake}\n                      className={cn(\n                        \"flex flex-col gap-2 rounded-xl sm:flex-row sm:gap-0 sm:border sm:bg-background sm:p-1 sm:shadow-xs/5 sm:transition-[border-color,box-shadow] sm:duration-150 dark:sm:bg-input/32\",\n                        \"sm:has-focus-visible:border-ring sm:has-focus-visible:ring-[3px] sm:has-focus-visible:ring-ring/24\",\n                        error && \"sm:border-destructive/40 sm:has-focus-visible:border-destructive/60 sm:has-focus-visible:ring-destructive/16\",\n                      )}\n                    >\n                      <input\n                        aria-describedby={error ? errorId : undefined}\n                        aria-invalid={Boolean(error)}\n                        autoComplete=\"email\"\n                        className={cn(\n                          \"h-11 w-full min-w-0 rounded-lg border sm:flex-1 bg-background px-3.5 text-base outline-none placeholder:text-muted-foreground/70 focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/24 sm:h-9 sm:border-0 sm:bg-transparent sm:px-2.5 sm:text-sm sm:focus-visible:ring-0 dark:bg-input/32 dark:sm:bg-transparent\",\n                          error && \"max-sm:border-destructive/40\",\n                        )}\n                        disabled={status === \"sending\"}\n                        id={fieldId}\n                        inputMode=\"email\"\n                        onBlur={() => {\n                          const v = email.trim();\n                          if (v && !EMAIL.test(v)) setError(\"That doesn't look like an email address yet.\");\n                        }}\n                        onChange={(e) => {\n                          setEmail(e.target.value);\n                          if (error && EMAIL.test(e.target.value.trim())) setError(null);\n                        }}\n                        placeholder=\"you@company.com\"\n                        ref={inputRef}\n                        type=\"email\"\n                        value={email}\n                      />\n                      <Button\n                        className=\"h-11 rounded-lg transition-[box-shadow,translate,scale,rotate] duration-150 ease-out active:scale-[0.97] sm:h-9\"\n                        loading={status === \"sending\"}\n                        type=\"submit\"\n                      >\n                        Subscribe\n                      </Button>\n                    </motion.div>\n                    <div className=\"mt-2 min-h-5 text-xs\" id={errorId}>\n                      <AnimatePresence initial={false} mode=\"wait\">\n                        {error ? (\n                          <motion.p\n                            animate={{ opacity: 1, y: 0 }}\n                            className=\"flex items-center gap-1.5 text-destructive-foreground\"\n                            exit={{ opacity: 0, y: -2 }}\n                            initial={{ opacity: 0, y: -2 }}\n                            key=\"error\"\n                            role=\"alert\"\n                            transition={{ duration: 0.15, ease }}\n                          >\n                            <AlertCircleIcon className=\"size-3.5 shrink-0\" />\n                            {error}\n                          </motion.p>\n                        ) : hint ? (\n                          <motion.p\n                            animate={{ opacity: 1, y: 0 }}\n                            className=\"text-muted-foreground\"\n                            exit={{ opacity: 0, y: -2 }}\n                            initial={{ opacity: 0, y: -2 }}\n                            key=\"hint\"\n                            transition={{ duration: 0.15, ease }}\n                          >\n                            Did you mean{\" \"}\n                            <button\n                              className=\"cursor-pointer rounded-sm font-medium text-foreground underline decoration-foreground/30 underline-offset-2 outline-none hover:decoration-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n                              onClick={() => {\n                                setEmail(hint);\n                                inputRef.current?.focus();\n                              }}\n                              type=\"button\"\n                            >\n                              {hint}\n                            </button>\n                            ?\n                          </motion.p>\n                        ) : (\n                          <motion.p\n                            animate={{ opacity: 1 }}\n                            className=\"flex items-center gap-1.5 text-muted-foreground\"\n                            exit={{ opacity: 0 }}\n                            initial={{ opacity: 0 }}\n                            key=\"note\"\n                            transition={{ duration: 0.15 }}\n                          >\n                            <LockIcon className=\"size-3 shrink-0\" />\n                            {note}\n                          </motion.p>\n                        )}\n                      </AnimatePresence>\n                    </div>\n                  </motion.form>\n                ) : (\n                  <motion.div\n                    animate={{ opacity: 1, scale: 1, filter: \"blur(0px)\" }}\n                    className=\"flex items-start gap-3 rounded-xl border border-success/25 bg-success/8 p-3.5\"\n                    exit={{ opacity: 0, scale: 0.98, filter: \"blur(4px)\" }}\n                    initial={{ opacity: 0, scale: 0.98, filter: \"blur(4px)\" }}\n                    key=\"done\"\n                    role=\"status\"\n                    transition={{ duration: 0.3, ease }}\n                  >\n                    <span className=\"mt-px flex size-6 shrink-0 items-center justify-center rounded-full bg-success text-white\">\n                      <svg aria-hidden className=\"size-3.5\" fill=\"none\" viewBox=\"0 0 16 16\">\n                        <motion.path\n                          animate={{ pathLength: 1 }}\n                          d=\"M3.5 8.5 6.5 11.5 12.5 4.5\"\n                          initial={{ pathLength: reduce ? 1 : 0 }}\n                          stroke=\"currentColor\"\n                          strokeLinecap=\"round\"\n                          strokeLinejoin=\"round\"\n                          strokeWidth=\"2.2\"\n                          transition={{ duration: 0.35, ease, delay: 0.15 }}\n                        />\n                      </svg>\n                    </span>\n                    <div className=\"min-w-0 flex-1 text-sm\">\n                      <p className=\"font-medium\">You're on the list.</p>\n                      <p className=\"mt-0.5 text-muted-foreground text-xs\">\n                        We sent a confirmation to <span className=\"break-all text-foreground\">{email.trim()}</span>. The next\n                        issue lands Thursday.\n                      </p>\n                    </div>\n                    <button\n                      className=\"-my-1.5 -me-1.5 h-8 shrink-0 cursor-pointer rounded-md px-2.5 text-muted-foreground text-xs outline-none transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n                      onClick={undo}\n                      type=\"button\"\n                    >\n                      Undo\n                    </button>\n                  </motion.div>\n                )}\n              </AnimatePresence>\n            </div>\n          </div>\n\n          {issues.length > 0 && (\n            <div className=\"hidden min-w-0 pt-6 md:block\">\n              <IssueStack issues={issues} />\n              <a\n                className=\"group mx-auto mt-5 flex w-fit items-center gap-1 rounded-md text-muted-foreground text-xs outline-none transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n                href=\"#archive\"\n              >\n                Read past issues\n                <ArrowRightIcon className=\"size-3 transition-transform duration-200 ease-out group-hover:translate-x-0.5\" />\n              </a>\n            </div>\n          )}\n        </div>\n      </section>\n    </MotionConfig>\n  );\n}\n"
    }
  ]
}