|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, type FormEvent } from "react"; |
| 4 | + |
| 5 | +type Status = "idle" | "loading" | "success" | "error"; |
| 6 | + |
| 7 | +export function NewsletterSignup({ |
| 8 | + variant = "card", |
| 9 | + heading, |
| 10 | + description, |
| 11 | +}: { |
| 12 | + variant?: "inline" | "card"; |
| 13 | + heading?: string; |
| 14 | + description?: string; |
| 15 | +}) { |
| 16 | + const [email, setEmail] = useState(""); |
| 17 | + const [status, setStatus] = useState<Status>("idle"); |
| 18 | + const [message, setMessage] = useState(""); |
| 19 | + |
| 20 | + async function handleSubmit(e: FormEvent) { |
| 21 | + e.preventDefault(); |
| 22 | + if (!email) return; |
| 23 | + setStatus("loading"); |
| 24 | + |
| 25 | + try { |
| 26 | + const res = await fetch("/api/newsletter/subscribe", { |
| 27 | + method: "POST", |
| 28 | + headers: { "Content-Type": "application/json" }, |
| 29 | + body: JSON.stringify({ email }), |
| 30 | + }); |
| 31 | + const data = (await res.json()) as { |
| 32 | + ok: boolean; |
| 33 | + error?: string; |
| 34 | + message?: string; |
| 35 | + }; |
| 36 | + |
| 37 | + if (data.ok) { |
| 38 | + setStatus("success"); |
| 39 | + setMessage(data.message ?? "Check your inbox to confirm."); |
| 40 | + setEmail(""); |
| 41 | + } else { |
| 42 | + setStatus("error"); |
| 43 | + setMessage(data.error ?? "Something went wrong."); |
| 44 | + } |
| 45 | + } catch { |
| 46 | + setStatus("error"); |
| 47 | + setMessage("Something went wrong. Try again."); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (variant === "inline") return <InlineForm email={email} setEmail={setEmail} status={status} setStatus={setStatus} message={message} onSubmit={handleSubmit} />; |
| 52 | + return <CardForm email={email} setEmail={setEmail} status={status} setStatus={setStatus} message={message} heading={heading} description={description} onSubmit={handleSubmit} />; |
| 53 | +} |
| 54 | + |
| 55 | +function InlineForm({ |
| 56 | + email, |
| 57 | + setEmail, |
| 58 | + status, |
| 59 | + setStatus, |
| 60 | + message, |
| 61 | + onSubmit, |
| 62 | +}: { |
| 63 | + email: string; |
| 64 | + setEmail: (v: string) => void; |
| 65 | + status: Status; |
| 66 | + setStatus: (v: Status) => void; |
| 67 | + message: string; |
| 68 | + onSubmit: (e: FormEvent) => void; |
| 69 | +}) { |
| 70 | + if (status === "success") { |
| 71 | + return ( |
| 72 | + <p className="mt-4 font-serif text-[0.85rem] leading-relaxed text-moss"> |
| 73 | + {message} |
| 74 | + </p> |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + return ( |
| 79 | + <form onSubmit={onSubmit} className="mt-4 flex gap-2"> |
| 80 | + <label htmlFor="footer-email" className="sr-only">Email address</label> |
| 81 | + <input |
| 82 | + id="footer-email" |
| 83 | + type="email" |
| 84 | + required |
| 85 | + placeholder="you@example.com" |
| 86 | + value={email} |
| 87 | + onChange={(e) => { |
| 88 | + setEmail(e.target.value); |
| 89 | + if (status === "error") setStatus("idle"); |
| 90 | + }} |
| 91 | + className="w-full max-w-56 rounded-full border border-rule bg-paper px-4 py-2 font-mono text-xs text-ink placeholder:text-ink-faint/50 transition-colors focus:border-terracotta/50 focus:outline-none" |
| 92 | + /> |
| 93 | + <input type="text" name="hp" aria-hidden="true" tabIndex={-1} className="absolute -left-[9999px]" autoComplete="off" /> |
| 94 | + <button |
| 95 | + type="submit" |
| 96 | + disabled={status === "loading"} |
| 97 | + className="shrink-0 rounded-full bg-ink px-4 py-2 text-xs font-medium tracking-wide text-paper transition-all hover:-translate-y-0.5 hover:bg-ink/85 disabled:opacity-50" |
| 98 | + > |
| 99 | + {status === "loading" ? "…" : "Subscribe"} |
| 100 | + </button> |
| 101 | + {status === "error" && ( |
| 102 | + <p className="self-center font-serif text-xs text-terracotta">{message}</p> |
| 103 | + )} |
| 104 | + </form> |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +function CardForm({ |
| 109 | + email, |
| 110 | + setEmail, |
| 111 | + status, |
| 112 | + setStatus, |
| 113 | + message, |
| 114 | + heading, |
| 115 | + description, |
| 116 | + onSubmit, |
| 117 | +}: { |
| 118 | + email: string; |
| 119 | + setEmail: (v: string) => void; |
| 120 | + status: Status; |
| 121 | + setStatus: (v: Status) => void; |
| 122 | + message: string; |
| 123 | + heading?: string; |
| 124 | + description?: string; |
| 125 | + onSubmit: (e: FormEvent) => void; |
| 126 | +}) { |
| 127 | + return ( |
| 128 | + <div className="relative overflow-hidden rounded-[1.5rem] border border-rule bg-paper-deep/50 px-6 py-10 sm:rounded-[2rem] sm:px-14 sm:py-14"> |
| 129 | + <div className="absolute -left-20 -top-20 h-64 w-64 rounded-full bg-peach/50 blur-3xl" /> |
| 130 | + <div className="absolute -bottom-24 -right-16 h-72 w-72 rounded-full bg-lavender/40 blur-3xl" /> |
| 131 | + |
| 132 | + <div className="relative"> |
| 133 | + <p className="font-display text-sm uppercase tracking-[0.28em] text-terracotta"> |
| 134 | + ✦ Newsletter |
| 135 | + </p> |
| 136 | + <h3 className="mt-3 font-display text-[clamp(1.6rem,3.2vw,2.2rem)] leading-[1.1] tracking-tight text-ink"> |
| 137 | + {heading ?? "New essays, straight to your inbox."} |
| 138 | + </h3> |
| 139 | + <p className="mt-4 max-w-lg font-serif text-[1.02rem] leading-relaxed text-ink-soft"> |
| 140 | + {description ?? |
| 141 | + "One email when we publish. No spam, no sales pitches, just the next piece on proactive agents."} |
| 142 | + </p> |
| 143 | + |
| 144 | + {status === "success" ? ( |
| 145 | + <div className="mt-8 flex items-center gap-3 rounded-xl border border-sage/60 bg-sage/20 px-5 py-4"> |
| 146 | + <span className="text-lg" aria-hidden>✦</span> |
| 147 | + <p className="font-serif text-[0.95rem] text-ink">{message}</p> |
| 148 | + </div> |
| 149 | + ) : ( |
| 150 | + <form |
| 151 | + onSubmit={onSubmit} |
| 152 | + className="mt-8 flex flex-col gap-3 sm:flex-row sm:items-center" |
| 153 | + > |
| 154 | + <label htmlFor="card-email" className="sr-only">Email address</label> |
| 155 | + <input |
| 156 | + id="card-email" |
| 157 | + type="email" |
| 158 | + required |
| 159 | + placeholder="you@example.com" |
| 160 | + value={email} |
| 161 | + onChange={(e) => { |
| 162 | + setEmail(e.target.value); |
| 163 | + if (status === "error") setStatus("idle"); |
| 164 | + }} |
| 165 | + className="w-full rounded-full border border-rule bg-paper px-5 py-3 font-mono text-sm text-ink placeholder:text-ink-faint/50 transition-colors focus:border-terracotta/50 focus:outline-none sm:max-w-72" |
| 166 | + /> |
| 167 | + <input type="text" name="hp" aria-hidden="true" tabIndex={-1} className="absolute -left-[9999px]" autoComplete="off" /> |
| 168 | + <button |
| 169 | + type="submit" |
| 170 | + disabled={status === "loading"} |
| 171 | + className="group inline-flex shrink-0 items-center justify-center gap-3 rounded-full bg-ink px-6 py-3 text-sm font-medium tracking-wide text-paper transition-all hover:-translate-y-0.5 hover:bg-ink/85 disabled:opacity-50" |
| 172 | + > |
| 173 | + {status === "loading" ? "Subscribing…" : "Subscribe"} |
| 174 | + <span |
| 175 | + aria-hidden |
| 176 | + className="transition-transform group-hover:translate-x-1" |
| 177 | + > |
| 178 | + → |
| 179 | + </span> |
| 180 | + </button> |
| 181 | + </form> |
| 182 | + )} |
| 183 | + {status === "error" && ( |
| 184 | + <p className="mt-3 font-serif text-sm text-terracotta">{message}</p> |
| 185 | + )} |
| 186 | + </div> |
| 187 | + </div> |
| 188 | + ); |
| 189 | +} |
0 commit comments