Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | 4x 2x 2x 4x 4x 3x 2x 2x 6x 36x 6x 4x 4x 17x 17x 17x 17x 17x 16x 7x 7x 6x 6x 6x 6x 21x 6x 1x 1x 5x 5x 5x 2x 2x 2x 1x 1x 2x 60x 17x 17x 60x 17x 17x 1x 15x 15x 45x | "use client";
// Client component: the form validates as you submit and sends from the browser through
// EmailJS. The SDK is only downloaded when someone actually sends a message.
import { useEffect, useId, useRef, useState, type FormEvent } from "react";
import {
LIMITS,
isSpam,
readEmailJsConfig,
toTemplateParams,
topics,
validateContact,
type ContactInput,
type FieldErrors,
} from "@/lib/contact";
export type SendMessage = (params: Record<string, string>) => Promise<void>;
export class NotConfiguredError extends Error {
constructor() {
super("The contact form isn't connected to EmailJS yet.");
this.name = "NotConfiguredError";
}
}
/** Next.js only inlines NEXT_PUBLIC_ values that are referenced literally. */
const config = readEmailJsConfig({
NEXT_PUBLIC_EMAILJS_SERVICE_ID: process.env.NEXT_PUBLIC_EMAILJS_SERVICE_ID,
NEXT_PUBLIC_EMAILJS_TEMPLATE_ID: process.env.NEXT_PUBLIC_EMAILJS_TEMPLATE_ID,
NEXT_PUBLIC_EMAILJS_PUBLIC_KEY: process.env.NEXT_PUBLIC_EMAILJS_PUBLIC_KEY,
});
export const sendWithEmailJs: SendMessage = async (params) => {
if (!config) throw new NotConfiguredError();
const { default: emailjs } = await import("@emailjs/browser");
await emailjs.send(config.serviceId, config.templateId, params, {
publicKey: config.publicKey,
blockHeadless: true,
// One message per browser per 30 seconds.
limitRate: { id: "contact", throttle: 30_000 },
});
};
type Status =
| { kind: "idle" }
| { kind: "sending" }
| { kind: "sent"; email: string }
| { kind: "error"; notConfigured: boolean };
function readForm(form: HTMLFormElement): ContactInput {
const data = new FormData(form);
const get = (key: string) => String(data.get(key) ?? "");
return {
name: get("name"),
email: get("email"),
company: get("company"),
topic: get("topic"),
message: get("message"),
website: get("website"),
};
}
const fieldOrder = ["name", "email", "topic", "message"] as const;
const inputClass =
"w-full rounded-md border-2 border-line bg-paper px-3 py-2.5 text-ink transition-colors hover:border-muted focus:border-spruce focus:outline-none aria-[invalid=true]:border-danger";
export interface ContactFormProps {
/** Address shown as a fallback if sending fails. */
email: string;
/** Injected in stories and tests; defaults to EmailJS. */
send?: SendMessage;
}
export function ContactForm({ email, send = sendWithEmailJs }: ContactFormProps) {
const id = useId();
const statusRef = useRef<HTMLDivElement>(null);
const [errors, setErrors] = useState<FieldErrors>({});
const [status, setStatus] = useState<Status>({ kind: "idle" });
// Once the result has rendered, move focus to it so keyboard and screen reader users hear it.
useEffect(() => {
if (status.kind === "sent" || status.kind === "error") statusRef.current?.focus();
}, [status]);
async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
if (status.kind === "sending") return;
const form = event.currentTarget;
const input = readForm(form);
const found = validateContact(input);
setErrors(found);
const firstInvalid = fieldOrder.find((key) => found[key]);
if (firstInvalid) {
form.querySelector<HTMLElement>(`[name="${firstInvalid}"]`)?.focus();
return;
}
setStatus({ kind: "sending" });
try {
// Bots that fill the hidden field get the same confirmation, and nothing is sent.
if (!isSpam(input)) await send(toTemplateParams(input));
form.reset();
setStatus({ kind: "sent", email: input.email.trim() });
} catch (error) {
if (!(error instanceof NotConfiguredError)) {
// EmailJS explains refusals (e.g. a disconnected email service) in `text`.
const { status, text } = (error ?? {}) as { status?: number; text?: string };
console.error("Contact form: EmailJS didn't send the message.", status, text ?? error);
}
setStatus({ kind: "error", notConfigured: error instanceof NotConfiguredError });
}
}
const describedBy = (key: keyof FieldErrors, hint?: string) =>
[hint, errors[key] ? `${id}-${key}-error` : null].filter(Boolean).join(" ") || undefined;
const fieldError = (key: keyof FieldErrors) =>
errors[key] ? (
<p id={`${id}-${key}-error`} className="mt-1.5 text-sm font-semibold text-danger">
{errors[key]}
</p>
) : null;
if (status.kind === "sent") {
return (
<div
ref={statusRef}
tabIndex={-1}
role="status"
className="border-l-4 border-spruce bg-paper-sunk p-6 focus:outline-none"
>
<h2 className="display text-4xl">Message sent</h2>
<p className="mt-3">
Thanks. I’ll reply to <strong>{status.email}</strong>.
</p>
<button
type="button"
onClick={() => setStatus({ kind: "idle" })}
className="link mt-4 font-semibold"
>
Send another message
</button>
</div>
);
}
const sending = status.kind === "sending";
return (
<form
onSubmit={onSubmit}
noValidate
aria-labelledby={`${id}-title`}
className="flex flex-col gap-6"
>
<h2 id={`${id}-title`} className="sr-only">
Send a message
</h2>
{status.kind === "error" && (
<div
ref={statusRef}
tabIndex={-1}
role="alert"
className="border-l-4 border-danger bg-paper-sunk p-4 focus:outline-none"
>
<p className="font-semibold">Your message wasn’t sent.</p>
<p className="mt-1">
{status.notConfigured
? "The form isn't connected yet. "
: "Something went wrong on the way. Try again in a minute, or "}
email me directly at{" "}
<a className="link" href={`mailto:${email}`}>
{email}
</a>
.
</p>
</div>
)}
<div className="grid gap-6 sm:grid-cols-2">
<div>
<label htmlFor={`${id}-name`} className="mb-1.5 block font-semibold">
Name
</label>
<input
id={`${id}-name`}
name="name"
type="text"
autoComplete="name"
maxLength={LIMITS.name}
required
aria-invalid={errors.name ? true : undefined}
aria-describedby={describedBy("name")}
className={inputClass}
/>
{fieldError("name")}
</div>
<div>
<label htmlFor={`${id}-email`} className="mb-1.5 block font-semibold">
Email
</label>
<input
id={`${id}-email`}
name="email"
type="email"
autoComplete="email"
inputMode="email"
maxLength={LIMITS.email}
required
aria-invalid={errors.email ? true : undefined}
aria-describedby={describedBy("email")}
className={inputClass}
/>
{fieldError("email")}
</div>
</div>
<div className="grid gap-6 sm:grid-cols-2">
<div>
<label htmlFor={`${id}-company`} className="mb-1.5 block font-semibold">
Company <span className="font-normal text-muted">(optional)</span>
</label>
<input
id={`${id}-company`}
name="company"
type="text"
autoComplete="organization"
maxLength={LIMITS.company}
className={inputClass}
/>
</div>
<div>
<label htmlFor={`${id}-topic`} className="mb-1.5 block font-semibold">
What it’s about
</label>
<select
id={`${id}-topic`}
name="topic"
required
defaultValue=""
aria-invalid={errors.topic ? true : undefined}
aria-describedby={describedBy("topic")}
className={inputClass}
>
<option value="" disabled>
Choose one
</option>
{topics.map((t) => (
<option key={t.value} value={t.value}>
{t.label}
</option>
))}
</select>
{fieldError("topic")}
</div>
</div>
<div>
<label htmlFor={`${id}-message`} className="mb-1.5 block font-semibold">
Message
</label>
<textarea
id={`${id}-message`}
name="message"
rows={7}
maxLength={LIMITS.message}
required
aria-invalid={errors.message ? true : undefined}
aria-describedby={describedBy("message")}
className={`${inputClass} resize-y`}
/>
{fieldError("message")}
</div>
{/* Honeypot. Off-screen rather than display:none, which some bots skip. */}
<div aria-hidden="true" className="absolute -left-[10000px] h-px w-px overflow-hidden">
<label htmlFor={`${id}-website`}>Leave this field empty</label>
<input id={`${id}-website`} name="website" type="text" tabIndex={-1} autoComplete="off" />
</div>
<div className="flex flex-wrap items-center gap-4">
<button
type="submit"
aria-disabled={sending || undefined}
className="inline-flex min-h-11 items-center rounded-full border-2 border-ink bg-ink px-6 py-2 font-semibold text-paper transition-colors hover:border-spruce hover:bg-spruce aria-disabled:cursor-wait aria-disabled:opacity-70"
>
{sending ? "Sending…" : "Send message"}
</button>
<p className="text-sm text-muted">Your message goes straight to my inbox.</p>
</div>
</form>
);
}
|