{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "newsletter-popup",
  "title": "Newsletter Popup",
  "description": "A newsletter signup that appears after a delay or part way down the page, with what readers get, how often, a checked email field and a clear thank-you.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/newsletter-popup/newsletter-popup.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface NewsletterPopupProps extends Omit<\n  React.ComponentPropsWithoutRef<\"div\">,\n  \"title\" | \"onSubmit\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  message?: string;\n  /** What readers get, as short lines. */\n  perks?: string[];\n  /** Social proof, e.g. \"12,400 readers\". */\n  proof?: string;\n  frequencies?: string[];\n  buttonLabel?: string;\n  /** Open after this many milliseconds. Use 0 to turn it off. */\n  delayMs?: number;\n  /** Open past this share of the page, from 0 to 100. Use 0 to turn it off. */\n  scrollPercent?: number;\n  /** Days to stay away once closed. Use 0 to show it every time. */\n  rememberDays?: number;\n  storageKey?: string;\n  /** Keep it inside the nearest positioned parent instead of the whole screen. */\n  contained?: boolean;\n  /** Return a promise to show a loading state. Throw to show an error. */\n  onSubmit?: (email: string, frequency: string) => void | Promise<void>;\n  onClose?: () => void;\n}\n\ntype Stage = \"form\" | \"sending\" | \"done\";\n\nconst FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\nconst ACCENT = \"#5E6AD2\";\nconst GREEN = \"#27A644\";\nconst RED = \"#EB5757\";\nconst RING = \"outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2]\";\nconst EMAIL = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]{2,}$/;\n\nconst PALETTES = {\n  dark: {\n    card: \"#0F1011\",\n    inset: \"#161718\",\n    border: \"rgba(255,255,255,0.08)\",\n    scrim: \"rgba(8,9,10,0.62)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    chip: \"rgba(255,255,255,0.05)\",\n    tab: \"rgba(255,255,255,0.08)\",\n    btn: \"#E5E5E6\",\n    btnText: \"#08090A\",\n    shadow: \"0 24px 64px rgba(0,0,0,0.55)\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    inset: \"#F7F8F8\",\n    border: \"rgba(8,9,10,0.1)\",\n    scrim: \"rgba(8,9,10,0.32)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    chip: \"rgba(8,9,10,0.05)\",\n    tab: \"#FFFFFF\",\n    btn: \"#08090A\",\n    btnText: \"#F7F8F8\",\n    shadow: \"0 24px 64px rgba(8,9,10,0.2)\",\n  },\n};\n\nconst DEFAULT_PERKS = [\n  \"One useful idea each week\",\n  \"New components before anyone else\",\n  \"No sales emails, ever\",\n];\n\nconst DEFAULT_FREQUENCIES = [\"Weekly\", \"Monthly\"];\n\nfunction wasDismissed(key: string, days: number) {\n  if (days <= 0) return false;\n  try {\n    const at = Number(window.localStorage.getItem(key));\n    return Boolean(at) && Date.now() - at < days * 86400000;\n  } catch {\n    return false;\n  }\n}\n\nfunction remember(key: string, days: number) {\n  if (days <= 0) return;\n  try {\n    window.localStorage.setItem(key, String(Date.now()));\n  } catch {\n    // Storage can be blocked. The popup simply comes back next visit.\n  }\n}\n\nfunction Check({ color }: { color: string }) {\n  return (\n    <svg\n      aria-hidden=\"true\"\n      viewBox=\"0 0 16 16\"\n      width=\"16\"\n      height=\"16\"\n      fill=\"none\"\n      className=\"block shrink-0\"\n    >\n      <path\n        d=\"m3.5 8.5 3 3 6-7\"\n        stroke={color}\n        strokeWidth=\"1.5\"\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n      />\n    </svg>\n  );\n}\n\nexport function NewsletterPopup({\n  theme = \"dark\",\n  title = \"The Friday Note\",\n  message = \"A short email with what we learned building interfaces this week.\",\n  perks = DEFAULT_PERKS,\n  proof = \"Read by 12,400 designers and engineers\",\n  frequencies = DEFAULT_FREQUENCIES,\n  buttonLabel = \"Subscribe\",\n  delayMs = 8000,\n  scrollPercent = 0,\n  rememberDays = 14,\n  storageKey = \"newsletter-popup\",\n  contained = false,\n  onSubmit,\n  onClose,\n  className,\n  style,\n  ...rest\n}: NewsletterPopupProps) {\n  const c = PALETTES[theme];\n  const [open, setOpen] = React.useState(false);\n  const [stage, setStage] = React.useState<Stage>(\"form\");\n  const [email, setEmail] = React.useState(\"\");\n  const [frequency, setFrequency] = React.useState(frequencies[0] ?? \"\");\n  const [error, setError] = React.useState(\"\");\n  const inputRef = React.useRef<HTMLInputElement>(null);\n  const closeRef = React.useRef<HTMLButtonElement>(null);\n  const titleId = React.useId();\n  const errorId = React.useId();\n  const index = Math.max(0, frequencies.indexOf(frequency));\n\n  React.useEffect(() => {\n    if (open || wasDismissed(storageKey, rememberDays)) return;\n    const reveal = () => setOpen(true);\n    const onScroll = () => {\n      const room = document.documentElement.scrollHeight - window.innerHeight;\n      if (room > 0 && (window.scrollY / room) * 100 >= scrollPercent) reveal();\n    };\n    const id = delayMs > 0 ? window.setTimeout(reveal, delayMs) : 0;\n    if (scrollPercent > 0) {\n      window.addEventListener(\"scroll\", onScroll, { passive: true });\n    }\n    return () => {\n      window.clearTimeout(id);\n      window.removeEventListener(\"scroll\", onScroll);\n    };\n    // Reopens only through a fresh mount, so closing it stays closed.\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [delayMs, rememberDays, scrollPercent, storageKey]);\n\n  React.useEffect(() => {\n    if (!open) return;\n    // Focusing the field on a phone would throw up the keyboard before anyone asked.\n    const touch = window.matchMedia(\"(pointer: coarse)\").matches;\n    (touch ? closeRef : inputRef).current?.focus({ preventScroll: true });\n  }, [open]);\n\n  const close = () => {\n    setOpen(false);\n    remember(storageKey, rememberDays);\n    onClose?.();\n  };\n\n  const submit = async (e: React.FormEvent) => {\n    e.preventDefault();\n    if (stage === \"sending\") return;\n    const value = email.trim();\n    if (!EMAIL.test(value)) {\n      setError(\"Enter a valid email address.\");\n      inputRef.current?.focus();\n      return;\n    }\n    setError(\"\");\n    setStage(\"sending\");\n    try {\n      await onSubmit?.(value, frequency);\n      setStage(\"done\");\n      remember(storageKey, Math.max(rememberDays, 365));\n      closeRef.current?.focus();\n    } catch {\n      setStage(\"form\");\n      setError(\"That didn't work. Please try again.\");\n    }\n  };\n\n  const layer = (on: boolean) =>\n    `flex min-w-0 flex-col [grid-area:1/1] motion-safe:transition-opacity motion-safe:duration-200 ${on ? \"visible opacity-100\" : \"invisible opacity-0\"}`;\n\n  return (\n    <div\n      aria-hidden={!open}\n      inert={!open}\n      onClick={(e) => {\n        if (e.target === e.currentTarget) close();\n      }}\n      onKeyDown={(e) => {\n        if (e.key === \"Escape\") close();\n      }}\n      className={`z-50 flex items-center justify-center p-4 motion-safe:transition-opacity motion-safe:duration-200 ${className ?? \"\"}`}\n      style={{\n        position: contained ? \"absolute\" : \"fixed\",\n        inset: 0,\n        background: c.scrim,\n        opacity: open ? 1 : 0,\n        visibility: open ? \"visible\" : \"hidden\",\n        fontFamily: FONT,\n        ...style,\n      }}\n      {...rest}\n    >\n      <div\n        role=\"dialog\"\n        aria-modal=\"true\"\n        aria-labelledby={titleId}\n        className=\"relative grid w-full rounded-2xl border p-6 motion-safe:transition-transform motion-safe:duration-300 motion-safe:ease-[cubic-bezier(0.22,1,0.36,1)]\"\n        style={{\n          maxWidth: 420,\n          gridTemplateColumns: \"minmax(0, 1fr)\",\n          background: c.card,\n          borderColor: c.border,\n          color: c.text,\n          boxShadow: c.shadow,\n          transform: open ? \"none\" : \"translateY(14px) scale(0.98)\",\n        }}\n      >\n        <button\n          ref={closeRef}\n          type=\"button\"\n          data-close=\"\"\n          aria-label=\"Close\"\n          onClick={close}\n          className={`absolute z-[1] flex cursor-pointer items-center justify-center rounded-full border-0 ${RING}`}\n          style={{\n            top: 14,\n            right: 14,\n            width: 30,\n            height: 30,\n            background: c.chip,\n            color: c.sub,\n          }}\n        >\n          <svg\n            aria-hidden=\"true\"\n            viewBox=\"0 0 16 16\"\n            width=\"14\"\n            height=\"14\"\n            fill=\"none\"\n            className=\"block\"\n          >\n            <path\n              d=\"m4.5 4.5 7 7m0-7-7 7\"\n              stroke=\"currentColor\"\n              strokeWidth=\"1.5\"\n              strokeLinecap=\"round\"\n            />\n          </svg>\n        </button>\n\n        {/* The form and the thank-you share one cell, so the card keeps its size. */}\n        <form noValidate onSubmit={submit} className={layer(stage !== \"done\")}>\n          <h2\n            id={titleId}\n            className=\"m-0 pr-8 text-[24px] leading-[30px] font-[590] tracking-[-0.018em]\"\n          >\n            {title}\n          </h2>\n          <p\n            className=\"m-0 mt-2 text-[14px] leading-[22px] tracking-[-0.011em]\"\n            style={{ color: c.sub }}\n          >\n            {message}\n          </p>\n          <ul className=\"m-0 mt-4 list-none p-0\">\n            {perks.map((perk) => (\n              <li\n                key={perk}\n                className=\"flex items-center gap-2 py-[3px] text-[13px] leading-[20px] tracking-[-0.011em]\"\n                style={{ color: c.soft }}\n              >\n                <Check color={ACCENT} />\n                <span className=\"truncate\">{perk}</span>\n              </li>\n            ))}\n          </ul>\n\n          {frequencies.length > 1 && (\n            <div\n              role=\"group\"\n              aria-label=\"How often\"\n              className=\"relative mt-4 grid h-8 rounded-full\"\n              style={{\n                padding: 2,\n                background: c.chip,\n                gridTemplateColumns: `repeat(${frequencies.length}, minmax(0, 1fr))`,\n              }}\n            >\n              <span\n                aria-hidden=\"true\"\n                className=\"absolute rounded-full motion-safe:transition-transform motion-safe:duration-300\"\n                style={{\n                  top: 2,\n                  bottom: 2,\n                  left: 2,\n                  width: `calc((100% - 4px) / ${frequencies.length})`,\n                  transform: `translateX(${index * 100}%)`,\n                  background: c.tab,\n                }}\n              />\n              {frequencies.map((f) => (\n                <button\n                  key={f}\n                  type=\"button\"\n                  data-frequency={f}\n                  aria-pressed={f === frequency}\n                  onClick={() => setFrequency(f)}\n                  className={`relative cursor-pointer rounded-full border-0 bg-transparent text-[12.5px] leading-[18px] font-[510] tracking-[-0.011em] motion-safe:transition-colors motion-safe:duration-200 ${RING}`}\n                  style={{ color: f === frequency ? c.text : c.sub }}\n                >\n                  {f}\n                </button>\n              ))}\n            </div>\n          )}\n\n          <div className=\"mt-3 flex gap-2\">\n            <input\n              ref={inputRef}\n              type=\"email\"\n              value={email}\n              autoComplete=\"email\"\n              inputMode=\"email\"\n              onChange={(e) => {\n                setEmail(e.target.value);\n                if (error) setError(\"\");\n              }}\n              aria-label=\"Email address\"\n              aria-invalid={Boolean(error)}\n              aria-describedby={errorId}\n              placeholder=\"you@company.com\"\n              className=\"h-10 min-w-0 flex-1 rounded-full border px-4 text-[16px] tracking-[-0.011em] outline-none focus:border-[#5E6AD2] sm:text-[13.5px]\"\n              style={{\n                background: c.inset,\n                borderColor: error ? RED : c.border,\n                color: c.text,\n              }}\n            />\n            <button\n              type=\"submit\"\n              data-submit=\"\"\n              disabled={stage === \"sending\"}\n              className={`h-10 shrink-0 cursor-pointer rounded-full border-0 text-[13.5px] leading-[20px] font-[510] tracking-[-0.011em] disabled:cursor-default ${RING}`}\n              style={{ width: 104, background: c.btn, color: c.btnText }}\n            >\n              {stage === \"sending\" ? \"Sending\" : buttonLabel}\n            </button>\n          </div>\n          <p\n            id={errorId}\n            role=\"alert\"\n            className=\"m-0 mt-2 text-[12.5px] leading-[18px] tracking-[-0.008em]\"\n            style={{ minHeight: 18, color: error ? RED : c.faint }}\n          >\n            {error || proof}\n          </p>\n        </form>\n\n        <div\n          aria-hidden={stage !== \"done\"}\n          className={`${layer(stage === \"done\")} items-center justify-center text-center`}\n        >\n          <span\n            className=\"flex items-center justify-center rounded-full\"\n            style={{ width: 44, height: 44, background: c.chip }}\n          >\n            <Check color={GREEN} />\n          </span>\n          <p className=\"m-0 mt-4 text-[20px] leading-[26px] font-[590] tracking-[-0.018em]\">\n            You&apos;re on the list\n          </p>\n          <p\n            aria-live=\"polite\"\n            className=\"m-0 mt-1 max-w-[300px] text-[14px] leading-[22px] tracking-[-0.011em]\"\n            style={{ color: c.sub }}\n          >\n            {stage === \"done\"\n              ? `Your first ${frequency.toLowerCase()} note goes to ${email.trim()}.`\n              : \"\"}\n          </p>\n          <button\n            type=\"button\"\n            data-finish=\"\"\n            tabIndex={stage === \"done\" ? 0 : -1}\n            onClick={close}\n            className={`mt-5 h-9 cursor-pointer rounded-full border-0 px-5 text-[13px] leading-[20px] font-[510] tracking-[-0.011em] ${RING}`}\n            style={{ background: c.chip, color: c.soft }}\n          >\n            Close\n          </button>\n        </div>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}