{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "referral-widget",
  "title": "Referral Widget",
  "description": "A refer-a-friend card: give-and-get explainer, your link with a Copy button that flips to Copied, quick share links, email invites, how much you have earned, a milestone track that unlocks bonuses with confetti, and your friends' progress from invited to subscribed with reminders.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/referral-widget/referral-widget.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type ReferralStatus = \"invited\" | \"signed-up\" | \"subscribed\";\n\nexport interface Referral {\n  id: string;\n  email: string;\n  name?: string;\n  status: ReferralStatus;\n  /** Free text, e.g. \"2d ago\". */\n  when: string;\n}\n\nexport interface ReferralWidgetProps {\n  link?: string;\n  /** Credit each side gets when a friend subscribes. */\n  reward?: number;\n  currency?: string;\n  referrals?: Referral[];\n  /** Subscribed-friend counts that unlock bonuses. */\n  milestones?: { at: number; label: string }[];\n  onInvite?: (emails: string[]) => void;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { bg: \"#0A0A0A\", text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.62)\", faint: \"rgba(245,244,241,0.36)\", card: \"#121212\", inner: \"#171717\", border: \"rgba(255,255,255,0.09)\", chip: \"rgba(255,255,255,0.06)\", input: \"#0D0D0D\", on: \"#F5F4F1\", onText: \"#0A0A0A\", green: \"#4ADE80\", blue: \"#60A5FA\", accent: \"#FB923C\", track: \"rgba(255,255,255,0.08)\", ring: \"rgba(245,244,241,0.6)\" },\n  light: { bg: \"#FAFAF8\", text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.62)\", faint: \"rgba(10,10,10,0.4)\", card: \"#FFFFFF\", inner: \"#FAFAF8\", border: \"rgba(10,10,10,0.09)\", chip: \"rgba(10,10,10,0.05)\", input: \"#FAFAF8\", on: \"#0A0A0A\", onText: \"#FAFAF8\", green: \"#15803D\", blue: \"#2563EB\", accent: \"#EA580C\", track: \"rgba(10,10,10,0.07)\", ring: \"rgba(10,10,10,0.5)\" },\n};\nconst CONFETTI = [\"#FB923C\", \"#F472B6\", \"#60A5FA\", \"#4ADE80\", \"#FBBF24\"];\n\nconst DEFAULT_REFERRALS: Referral[] = [\n  { id: \"r1\", email: \"lena@fieldnote.co\", name: \"Lena Brandt\", status: \"subscribed\", when: \"Oct 12\" },\n  { id: \"r2\", email: \"tomas@kestrel.dev\", name: \"Tomás Ferreira\", status: \"subscribed\", when: \"Oct 20\" },\n  { id: \"r3\", email: \"priya@halcyon.app\", name: \"Priya Nair\", status: \"signed-up\", when: \"Oct 28\" },\n  { id: \"r4\", email: \"jonah@oakline.io\", status: \"invited\", when: \"Oct 30\" },\n];\nconst DEFAULT_MILESTONES = [\n  { at: 1, label: \"$20 credit\" },\n  { at: 3, label: \"Free month\" },\n  { at: 5, label: \"Pro for a year\" },\n];\nconst STATUS: Record<ReferralStatus, { label: string; step: number }> = {\n  invited: { label: \"Invited\", step: 1 },\n  \"signed-up\": { label: \"Signed up\", step: 2 },\n  subscribed: { label: \"Subscribed\", step: 3 },\n};\nconst EMAIL = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;\n\nexport function ReferralWidget({ link = \"https://nexora.app/r/mara-7Q2X\", reward = 20, currency = \"$\", referrals, milestones = DEFAULT_MILESTONES, onInvite, theme = \"dark\", className }: ReferralWidgetProps) {\n  const p = PALETTES[theme];\n  const reduce = !!useReducedMotion();\n  const uid = React.useId();\n  const [list, setList] = React.useState<Referral[]>(() => referrals ?? DEFAULT_REFERRALS);\n  const [copied, setCopied] = React.useState(false);\n  const [emails, setEmails] = React.useState(\"\");\n  const [error, setError] = React.useState(\"\");\n  const [sentNote, setSentNote] = React.useState<string | null>(null);\n  const [burst, setBurst] = React.useState(0);\n  const [fresh, setFresh] = React.useState<string[]>([]);\n  const seq = React.useRef(0);\n  const timers = React.useRef<ReturnType<typeof setTimeout>[]>([]);\n  React.useEffect(() => () => timers.current.forEach(clearTimeout), []);\n\n  const subscribed = list.filter((r) => r.status === \"subscribed\").length;\n  const earned = subscribed * reward;\n  const maxAt = milestones[milestones.length - 1]?.at ?? 1;\n  const next = milestones.find((m) => m.at > subscribed);\n  const pct = Math.min(1, subscribed / maxAt);\n\n  const copy = () => {\n    navigator.clipboard?.writeText(link).catch(() => {});\n    setCopied(true);\n    timers.current.push(setTimeout(() => setCopied(false), 1800));\n  };\n  const invite = () => {\n    const list2 = emails\n      .split(/[\\s,;]+/)\n      .map((s) => s.trim().toLowerCase())\n      .filter(Boolean);\n    const bad = list2.filter((e) => !EMAIL.test(e));\n    if (!list2.length) return setError(\"Add at least one email.\");\n    if (bad.length) return setError(`Check ${bad.join(\", \")}`);\n    const existing = list2.filter((e) => list.some((r) => r.email === e));\n    const ok = list2.filter((e) => !existing.includes(e));\n    const added = ok.map((email) => {\n      seq.current += 1;\n      return { id: `n${seq.current}`, email, status: \"invited\" as const, when: \"Just now\" };\n    });\n    setList((l) => [...added, ...l]);\n    setFresh(added.map((a) => a.id));\n    setEmails(\"\");\n    setError(\"\");\n    onInvite?.(ok);\n    setSentNote(ok.length ? `Sent ${ok.length} ${ok.length === 1 ? \"invite\" : \"invites\"}${existing.length ? `, ${existing.length} already invited` : \"\"}` : \"Already invited\");\n    timers.current.push(setTimeout(() => setSentNote(null), 2600));\n  };\n  // Demo: a reminder is \"sent\", then the friend moves one step on, so statuses and rewards can be seen updating.\n  const [reminded, setReminded] = React.useState<string[]>([]);\n  const remind = (r: Referral) => {\n    setReminded((x) => [...x, r.id]);\n    timers.current.push(\n      setTimeout(() => {\n        setReminded((x) => x.filter((id) => id !== r.id));\n        advance(r);\n      }, reduce ? 200 : 1100),\n    );\n  };\n  const advance = (r: Referral) => {\n    const nextStatus: ReferralStatus = r.status === \"invited\" ? \"signed-up\" : \"subscribed\";\n    setList((l) => l.map((x) => (x.id === r.id ? { ...x, status: nextStatus, name: x.name ?? x.email.split(\"@\")[0].replace(/^\\w/, (c) => c.toUpperCase()) } : x)));\n    if (nextStatus === \"subscribed\" && milestones.some((m) => m.at === subscribed + 1)) setBurst((b) => b + 1);\n  };\n\n  const ring = { [\"--tw-ring-color\" as string]: p.ring, [\"--tw-ring-offset-color\" as string]: p.card } as React.CSSProperties;\n  const focus = \"outline-none focus-visible:ring-2 focus-visible:ring-offset-2\";\n  const shareTargets = [\n    { label: \"Email\", href: `mailto:?subject=${encodeURIComponent(`${currency}${reward} off Nexora`)}&body=${encodeURIComponent(`I use Nexora and think you'd like it. Here's ${currency}${reward} off: ${link}`)}`, d: \"M3 7l9 6 9-6M4 5h16v14H4z\" },\n    { label: \"Message\", href: `sms:?&body=${encodeURIComponent(link)}`, d: \"M4 5h16v11H8l-4 4V5Z\" },\n    { label: \"Post\", href: `https://x.com/intent/post?text=${encodeURIComponent(link)}`, d: \"M4 12h16M14 6l6 6-6 6\" },\n  ];\n\n  return (\n    <section className={cn(\"@container w-full\", className)} style={{ background: p.bg, color: p.text }}>\n      <div className=\"mx-auto max-w-4xl px-4 py-10 @[640px]:px-6 @[640px]:py-14\">\n        <div className=\"relative overflow-hidden rounded-2xl border\" style={{ background: p.card, borderColor: p.border }}>\n          {burst > 0 && !reduce && (\n            <div key={burst} className=\"pointer-events-none absolute inset-x-0 top-24 z-20 flex justify-center\" aria-hidden=\"true\">\n              {Array.from({ length: 30 }, (_, i) => {\n                const a = (i / 30) * Math.PI * 2;\n                const d = 110 + (i % 4) * 30;\n                return <motion.span key={i} className=\"absolute h-2.5 w-1.5 rounded-sm\" style={{ background: CONFETTI[i % CONFETTI.length] }} initial={{ x: 0, y: 0, opacity: 1 }} animate={{ x: Math.cos(a) * d, y: Math.sin(a) * d + 80, opacity: 0, rotate: 420 }} transition={{ duration: 1.4, ease: \"easeOut\" }} />;\n              })}\n            </div>\n          )}\n          <div className=\"grid @[720px]:grid-cols-[1.1fr_1fr]\">\n            {/* Share */}\n            <div className=\"p-6 @[640px]:p-7\">\n              <span className=\"inline-flex items-center gap-1.5 rounded-full px-2.5 py-1 text-[12px] font-medium\" style={{ background: p.chip, color: p.accent }}>\n                <span aria-hidden=\"true\">🎁</span> Refer a friend\n              </span>\n              <h2 className=\"m-0 mt-3 text-[24px] leading-tight font-semibold tracking-[-0.02em]\">\n                Give {currency}\n                {reward}, get {currency}\n                {reward}\n              </h2>\n              <p className=\"m-0 mt-2 text-[14px] leading-[1.6]\" style={{ color: p.muted }}>\n                Friends get {currency}\n                {reward} off their first month. You get {currency}\n                {reward} in credit when they subscribe, plus bonuses as more friends join.\n              </p>\n\n              <label htmlFor={`${uid}-link`} className=\"mt-5 mb-1.5 block text-[12.5px] font-medium\">\n                Your link\n              </label>\n              <div className=\"flex gap-2\">\n                <input id={`${uid}-link`} readOnly value={link} onFocus={(e) => e.currentTarget.select()} className={cn(\"h-10 min-w-0 flex-1 rounded-lg border px-3 font-mono text-[12.5px]\", focus)} style={{ borderColor: p.border, background: p.input, color: p.text, ...ring }} />\n                <button type=\"button\" onClick={copy} className={cn(\"relative flex h-10 w-24 shrink-0 cursor-pointer items-center justify-center gap-1.5 overflow-hidden rounded-lg border-0 text-[13px] font-medium\", focus)} style={{ background: copied ? p.green : p.on, color: p.onText, ...ring }} aria-live=\"polite\">\n                  <AnimatePresence mode=\"wait\" initial={false}>\n                    <motion.span key={String(copied)} className=\"flex items-center gap-1.5\" initial={reduce ? false : { y: 12, opacity: 0 }} animate={{ y: 0, opacity: 1 }} exit={reduce ? undefined : { y: -12, opacity: 0 }} transition={{ duration: 0.16 }}>\n                      {copied ? (\n                        <>\n                          <svg width=\"13\" height=\"13\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.4\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                            <path d=\"m3 8.5 3 3 7-7\" />\n                          </svg>\n                          Copied\n                        </>\n                      ) : (\n                        \"Copy link\"\n                      )}\n                    </motion.span>\n                  </AnimatePresence>\n                </button>\n              </div>\n              <div className=\"mt-2.5 flex flex-wrap gap-2\">\n                {shareTargets.map((s) => (\n                  <a key={s.label} href={s.href} target={s.href.startsWith(\"http\") ? \"_blank\" : undefined} rel=\"noreferrer\" className={cn(\"flex h-8 items-center gap-1.5 rounded-full border px-3 text-[12.5px] no-underline\", focus)} style={{ borderColor: p.border, color: p.muted, ...ring }}>\n                    <svg width=\"13\" height=\"13\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                      <path d={s.d} />\n                    </svg>\n                    {s.label}\n                  </a>\n                ))}\n              </div>\n\n              <form\n                className=\"mt-5\"\n                onSubmit={(e) => {\n                  e.preventDefault();\n                  invite();\n                }}\n                noValidate\n              >\n                <label htmlFor={`${uid}-emails`} className=\"mb-1.5 block text-[12.5px] font-medium\">\n                  Or invite by email\n                </label>\n                <div className=\"flex gap-2\">\n                  <input id={`${uid}-emails`} value={emails} onChange={(e) => { setEmails(e.target.value); setError(\"\"); }} placeholder=\"friend@company.com, another@company.com\" aria-invalid={!!error} aria-describedby={`${uid}-msg`} className={cn(\"h-10 min-w-0 flex-1 rounded-lg border px-3 text-[13px]\", focus)} style={{ borderColor: error ? \"#F87171\" : p.border, background: p.input, color: p.text, ...ring }} />\n                  <button type=\"submit\" className={cn(\"h-10 shrink-0 cursor-pointer rounded-lg border bg-transparent px-4 text-[13px] font-medium\", focus)} style={{ borderColor: p.border, color: p.text, ...ring }}>\n                    Send\n                  </button>\n                </div>\n                <p id={`${uid}-msg`} className=\"m-0 mt-1.5 h-4 text-[12px]\" style={{ color: error ? \"#F87171\" : p.green }} role={error ? \"alert\" : \"status\"}>\n                  {error || sentNote}\n                </p>\n              </form>\n            </div>\n\n            {/* Progress */}\n            <div className=\"border-t p-6 @[640px]:p-7 @[720px]:border-t-0 @[720px]:border-l\" style={{ borderColor: p.border, background: p.inner }}>\n              <div className=\"flex items-end justify-between gap-3\">\n                <div>\n                  <p className=\"m-0 text-[12.5px]\" style={{ color: p.faint }}>\n                    You have earned\n                  </p>\n                  <motion.p key={earned} className=\"m-0 mt-0.5 text-[30px] leading-none font-semibold tracking-[-0.03em] tabular-nums\" initial={reduce ? false : { scale: 1.15, color: p.green }} animate={{ scale: 1, color: p.text }} transition={{ duration: 0.5 }} aria-live=\"polite\">\n                    {currency}\n                    {earned}\n                  </motion.p>\n                </div>\n                <p className=\"m-0 text-right text-[12.5px]\" style={{ color: p.muted }}>\n                  {next ? (\n                    <>\n                      {next.at - subscribed} more for <span style={{ color: p.text }}>{next.label}</span>\n                    </>\n                  ) : (\n                    \"Every bonus unlocked 🎉\"\n                  )}\n                </p>\n              </div>\n\n              {/* Milestone track */}\n              <div className=\"relative mx-8 mt-6 mb-12 px-2\" role=\"progressbar\" aria-label=\"Subscribed friends\" aria-valuenow={subscribed} aria-valuemin={0} aria-valuemax={maxAt}>\n                <div className=\"h-1.5 rounded-full\" style={{ background: p.track }} />\n                <motion.div className=\"absolute top-0 left-2 h-1.5 rounded-full\" style={{ background: p.green }} initial={false} animate={{ width: `calc(${pct * 100}% - ${pct * 16}px)` }} transition={{ duration: reduce ? 0 : 0.6, ease: [0.22, 1, 0.36, 1] }} />\n                {milestones.map((m) => {\n                  const hit = subscribed >= m.at;\n                  const left = `calc(${(m.at / maxAt) * 100}% - ${(m.at / maxAt) * 16}px + 8px)`;\n                  return (\n                    <div key={m.at} className=\"absolute top-[3px] flex -translate-x-1/2 flex-col items-center\" style={{ left }}>\n                      <motion.span className=\"flex h-6 w-6 -translate-y-1/2 items-center justify-center rounded-full border-2 text-[10px] font-bold\" initial={false} animate={{ background: hit ? p.green : p.card, borderColor: hit ? p.green : p.track, color: hit ? p.onText : p.faint, scale: hit ? 1 : 0.9 }}>\n                        {hit ? \"✓\" : m.at}\n                      </motion.span>\n                      <span className=\"mt-0 text-[11px] whitespace-nowrap\" style={{ color: hit ? p.text : p.faint }}>\n                        {m.label}\n                      </span>\n                    </div>\n                  );\n                })}\n              </div>\n\n              <h3 className=\"m-0 mb-2 text-[12.5px] font-medium\" style={{ color: p.muted }}>\n                Your friends · {list.length}\n              </h3>\n              <ul className=\"m-0 flex list-none flex-col gap-1.5 p-0\">\n                <AnimatePresence initial={false}>\n                  {list.map((r) => {\n                    const st = STATUS[r.status];\n                    return (\n                      <motion.li key={r.id} layout={!reduce} initial={reduce ? false : { opacity: 0, height: 0 }} animate={{ opacity: 1, height: \"auto\" }} exit={reduce ? undefined : { opacity: 0, height: 0 }} className=\"overflow-hidden\">\n                        <motion.div className=\"flex items-center gap-3 rounded-xl border px-3 py-2.5\" style={{ borderColor: p.border }} initial={fresh.includes(r.id) && !reduce ? { backgroundColor: p.chip } : false} animate={{ backgroundColor: \"rgba(0,0,0,0)\" }} transition={{ duration: 2 }}>\n                          <span className=\"flex h-8 w-8 shrink-0 items-center justify-center rounded-full text-[11.5px] font-semibold uppercase\" style={{ background: p.chip, color: p.muted }} aria-hidden=\"true\">\n                            {(r.name ?? r.email)[0]}\n                          </span>\n                          <span className=\"min-w-0 flex-1\">\n                            <span className=\"block truncate text-[13px] font-medium\">{r.name ?? r.email}</span>\n                            <span className=\"mt-1 flex items-center gap-1\" aria-label={`${st.label}, step ${st.step} of 3`}>\n                              {[1, 2, 3].map((s) => (\n                                <motion.span key={s} className=\"h-1 w-6 rounded-full\" initial={false} animate={{ background: s <= st.step ? (st.step === 3 ? p.green : p.blue) : p.track }} />\n                              ))}\n                              <span className=\"ml-1.5 text-[11.5px]\" style={{ color: st.step === 3 ? p.green : p.faint }}>\n                                {st.label}\n                              </span>\n                            </span>\n                          </span>\n                          {r.status === \"subscribed\" ? (\n                            <span className=\"text-[12.5px] font-semibold tabular-nums\" style={{ color: p.green }}>\n                              +{currency}\n                              {reward}\n                            </span>\n                          ) : (\n                            <button type=\"button\" disabled={reminded.includes(r.id)} onClick={() => remind(r)} className={cn(\"h-7 cursor-pointer rounded-md border bg-transparent px-2 text-[11.5px] disabled:cursor-default\", focus)} style={{ borderColor: p.border, color: reminded.includes(r.id) ? p.green : p.muted, ...ring }} aria-label={`Send ${r.name ?? r.email} a reminder`}>\n                              {reminded.includes(r.id) ? \"Sent ✓\" : \"Remind\"}\n                            </button>\n                          )}\n                        </motion.div>\n                      </motion.li>\n                    );\n                  })}\n                </AnimatePresence>\n              </ul>\n            </div>\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n\nexport default ReferralWidget;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}