{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "exit-intent-popup",
  "title": "Exit-intent Popup",
  "description": "A last offer shown as someone is about to leave: it opens when the pointer heads for the top of the window, or on a fast scroll up on phones, and stays away once closed.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/exit-intent-popup/exit-intent-popup.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface ExitIntentPopupProps extends Omit<\n  React.ComponentPropsWithoutRef<\"div\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  message?: string;\n  /** A coupon to copy. Leave empty to hide it. */\n  code?: string;\n  /** The small line under the code. */\n  fineprint?: string;\n  action?: { label: string; href?: string; onClick?: () => void };\n  dismissLabel?: string;\n  /** Watch this element instead of the whole page. The popup then stays inside it. */\n  boundary?: React.RefObject<HTMLElement | null>;\n  /** Wait this long after load before watching, in milliseconds. */\n  armAfterMs?: number;\n  /** Days to stay away once closed. Use 0 to show it every time. */\n  rememberDays?: number;\n  storageKey?: string;\n  /** Starts open. */\n  defaultOpen?: boolean;\n  onShow?: () => void;\n  onClose?: () => void;\n}\n\nconst FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\nconst GREEN = \"#27A644\";\nconst RING = \"outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2]\";\nconst FOCUSABLE = \"a[href], button:not([disabled]), input:not([disabled])\";\n\nconst PALETTES = {\n  dark: {\n    card: \"#0F1011\",\n    inset: \"#161718\",\n    border: \"rgba(255,255,255,0.08)\",\n    dash: \"rgba(255,255,255,0.18)\",\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.06)\",\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    dash: \"rgba(8,9,10,0.2)\",\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    btn: \"#08090A\",\n    btnText: \"#F7F8F8\",\n    shadow: \"0 24px 64px rgba(8,9,10,0.2)\",\n  },\n};\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\nexport function ExitIntentPopup({\n  theme = \"dark\",\n  title = \"Before you go\",\n  message = \"Take 20% off your first year. It's yours for the next 24 hours.\",\n  code = \"STAY20\",\n  fineprint = \"Works on any annual plan.\",\n  action = { label: \"Claim my discount\" },\n  dismissLabel = \"No thanks\",\n  boundary,\n  armAfterMs = 1500,\n  rememberDays = 7,\n  storageKey = \"exit-intent-popup\",\n  defaultOpen = false,\n  onShow,\n  onClose,\n  className,\n  style,\n  ...rest\n}: ExitIntentPopupProps) {\n  const c = PALETTES[theme];\n  const [open, setOpen] = React.useState(defaultOpen);\n  const [copied, setCopied] = React.useState(false);\n  const dialogRef = React.useRef<HTMLDivElement>(null);\n  const previous = React.useRef<HTMLElement | null>(null);\n  const copyTimer = React.useRef(0);\n  const titleId = React.useId();\n  const contained = Boolean(boundary);\n\n  const show = React.useCallback(() => {\n    if (wasDismissed(storageKey, rememberDays)) return;\n    setOpen(true);\n    onShow?.();\n  }, [onShow, rememberDays, storageKey]);\n\n  // Mouse users: the pointer leaving through the top edge, heading for the tabs or the address bar.\n  // Touch users: a fast flick back up the page, the nearest thing a phone has to the same gesture.\n  React.useEffect(() => {\n    if (open) return;\n    const target = boundary?.current ?? document.documentElement;\n    const onLeave = (e: PointerEvent) => {\n      if (e.pointerType === \"touch\") return;\n      const top = boundary?.current?.getBoundingClientRect().top ?? 0;\n      if (e.clientY <= top + 6) show();\n    };\n    let lastY = window.scrollY;\n    let lastT = 0;\n    const onScroll = () => {\n      const now = performance.now();\n      const speed = (lastY - window.scrollY) / Math.max(now - lastT, 1);\n      if (lastT && speed > 2.4 && window.scrollY > 200) show();\n      lastY = window.scrollY;\n      lastT = now;\n    };\n    const touch = window.matchMedia(\"(pointer: coarse)\").matches;\n    const id = window.setTimeout(() => {\n      target.addEventListener(\"pointerleave\", onLeave as EventListener);\n      if (touch) window.addEventListener(\"scroll\", onScroll, { passive: true });\n    }, armAfterMs);\n    return () => {\n      window.clearTimeout(id);\n      target.removeEventListener(\"pointerleave\", onLeave as EventListener);\n      window.removeEventListener(\"scroll\", onScroll);\n    };\n  }, [armAfterMs, boundary, open, show]);\n\n  React.useEffect(() => {\n    if (!open) return;\n    previous.current = document.activeElement as HTMLElement | null;\n    dialogRef.current?.focus();\n  }, [open]);\n\n  React.useEffect(() => () => window.clearTimeout(copyTimer.current), []);\n\n  const close = () => {\n    setOpen(false);\n    remember(storageKey, rememberDays);\n    previous.current?.focus?.();\n    onClose?.();\n  };\n\n  const copy = async () => {\n    await navigator.clipboard?.writeText(code).catch(() => {});\n    setCopied(true);\n    window.clearTimeout(copyTimer.current);\n    copyTimer.current = window.setTimeout(() => setCopied(false), 1600);\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if (e.key === \"Escape\") {\n      close();\n      return;\n    }\n    if (e.key !== \"Tab\") return;\n    const items = [\n      ...(dialogRef.current?.querySelectorAll<HTMLElement>(FOCUSABLE) ?? []),\n    ];\n    if (!items.length) return;\n    const first = items[0];\n    const last = items[items.length - 1];\n    const active = document.activeElement;\n    if (e.shiftKey && (active === first || active === dialogRef.current)) {\n      e.preventDefault();\n      last.focus();\n    } else if (!e.shiftKey && active === last) {\n      e.preventDefault();\n      first.focus();\n    }\n  };\n\n  const primary = `flex h-10 w-full cursor-pointer items-center justify-center rounded-full border-0 text-[13.5px] leading-[20px] font-[510] tracking-[-0.011em] no-underline ${RING}`;\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={onKeyDown}\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        ref={dialogRef}\n        role=\"dialog\"\n        tabIndex={-1}\n        aria-modal=\"true\"\n        aria-labelledby={titleId}\n        className=\"relative w-full rounded-2xl border p-6 outline-none motion-safe:transition-transform motion-safe:duration-300 motion-safe:ease-[cubic-bezier(0.22,1,0.36,1)]\"\n        style={{\n          maxWidth: 400,\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        <h2\n          id={titleId}\n          className=\"m-0 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\n        {code && (\n          <div\n            className=\"mt-5 flex items-center justify-between gap-3 rounded-xl p-1.5 pl-4\"\n            style={{ background: c.inset, border: `1px dashed ${c.dash}` }}\n          >\n            <span className=\"min-w-0\">\n              <span className=\"block truncate text-[17px] leading-[24px] font-[590] tracking-[0.04em] tabular-nums\">\n                {code}\n              </span>\n              {fineprint && (\n                <span\n                  className=\"block truncate text-[12px] leading-[16px] tracking-[-0.005em]\"\n                  style={{ color: c.faint }}\n                >\n                  {fineprint}\n                </span>\n              )}\n            </span>\n            <button\n              type=\"button\"\n              data-copy=\"\"\n              onClick={copy}\n              aria-label={`Copy code ${code}`}\n              className={`h-8 shrink-0 cursor-pointer rounded-full border-0 text-[13px] leading-[20px] font-[510] tracking-[-0.011em] ${RING}`}\n              style={{\n                width: 70,\n                background: c.chip,\n                color: copied ? GREEN : c.soft,\n              }}\n            >\n              {copied ? \"Copied\" : \"Copy\"}\n            </button>\n          </div>\n        )}\n\n        <div className=\"mt-5\">\n          {action.href ? (\n            <a\n              href={action.href}\n              onClick={action.onClick}\n              className={primary}\n              style={{ background: c.btn, color: c.btnText }}\n            >\n              {action.label}\n            </a>\n          ) : (\n            <button\n              type=\"button\"\n              data-claim=\"\"\n              onClick={action.onClick}\n              className={primary}\n              style={{ background: c.btn, color: c.btnText }}\n            >\n              {action.label}\n            </button>\n          )}\n          <button\n            type=\"button\"\n            data-dismiss=\"\"\n            onClick={close}\n            className={`mt-1 h-9 w-full cursor-pointer rounded-full border-0 bg-transparent text-[13px] leading-[20px] tracking-[-0.011em] ${RING}`}\n            style={{ color: c.sub }}\n          >\n            {dismissLabel}\n          </button>\n        </div>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}