@atelic-action/ui 8.4.0 → 8.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -5
- package/dist/lib/contactLinks.d.ts +31 -0
- package/dist/lib/contactLinks.js +55 -0
- package/dist/scripts/check-contacts.d.ts +17 -0
- package/dist/scripts/check-contacts.js +162 -0
- package/package.json +10 -13
- package/src/chrome/Footer.tsx +4 -3
- package/src/chrome/SiteHeader.tsx +7 -6
- package/src/chrome/SiteMenu.tsx +11 -9
- package/src/chrome/StickyCTABar.tsx +3 -2
- package/src/components/GateLogin.tsx +2 -1
- package/src/components/MultiSelect.tsx +225 -0
- package/src/components/index.ts +1 -0
- package/src/lib/contactForm.ts +184 -0
- package/src/lib/contactLinks.ts +57 -0
- package/src/lib/googlePlaces.ts +392 -0
- package/src/lib/latestOnly.ts +47 -0
- package/src/primitives/AddressSearch.tsx +341 -0
- package/src/primitives/EmailLink.tsx +55 -0
- package/src/primitives/ExpandableText.tsx +137 -0
- package/src/primitives/GoogleMapsAttribution.tsx +14 -0
- package/src/primitives/PhoneLink.tsx +61 -0
- package/src/primitives/index.ts +18 -0
- package/src/scripts/check-base-path.ts +15 -0
- package/src/scripts/check-contacts.ts +177 -0
- package/src/sections/ContactForm.tsx +683 -87
- package/src/sections/ContactInfoCard.tsx +20 -5
- package/src/sections/InfoRows.tsx +103 -0
- package/src/sections/LocationSection.tsx +2 -1
- package/src/sections/Locations.tsx +3 -2
- package/src/sections/PageHero.tsx +22 -5
- package/src/sections/index.ts +23 -1
- package/src/styles/components.css +123 -0
- package/src/styles/defaults.css +31 -1
- package/src/styles/fields.css +33 -0
- package/src/styles/primitives.css +165 -0
- package/src/styles/sections.css +306 -19
|
@@ -1,21 +1,156 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Check } from "lucide-react";
|
|
2
|
+
import {
|
|
3
|
+
type FormEvent,
|
|
4
|
+
type ReactNode,
|
|
5
|
+
type RefObject,
|
|
6
|
+
useEffect,
|
|
7
|
+
useId,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
} from "react";
|
|
12
|
+
import { MultiSelect } from "../components/MultiSelect";
|
|
13
|
+
import {
|
|
14
|
+
addressLine,
|
|
15
|
+
type ContactFormField,
|
|
16
|
+
type ContactFormRefusals,
|
|
17
|
+
type ContactFormShape,
|
|
18
|
+
type ContactFormValues,
|
|
19
|
+
checkContactForm,
|
|
20
|
+
contactFormBody,
|
|
21
|
+
firstRefused,
|
|
22
|
+
isZIP,
|
|
23
|
+
readContactForm,
|
|
24
|
+
} from "../lib/contactForm";
|
|
25
|
+
import { type GooglePlacesOptions, googlePlacesProvider } from "../lib/googlePlaces";
|
|
2
26
|
import { isHoneypotFilled } from "../lib/honeypot";
|
|
27
|
+
import { newTabProps } from "../lib/newTabProps";
|
|
3
28
|
import { trackEvent } from "../lib/trackEvent";
|
|
29
|
+
import {
|
|
30
|
+
type AddressParts,
|
|
31
|
+
type AddressProvider,
|
|
32
|
+
AddressSearch,
|
|
33
|
+
} from "../primitives/AddressSearch";
|
|
4
34
|
import { Button } from "../primitives/Button";
|
|
35
|
+
import { EmailLink } from "../primitives/EmailLink";
|
|
36
|
+
import { ExpandableText } from "../primitives/ExpandableText";
|
|
37
|
+
import { GoogleMapsAttribution } from "../primitives/GoogleMapsAttribution";
|
|
38
|
+
import { PhoneLink } from "../primitives/PhoneLink";
|
|
5
39
|
import { useWithBase } from "../routing/BasePathProvider";
|
|
6
40
|
|
|
41
|
+
/** Every word the form draws that is not a visitor's, each with a neutral default. */
|
|
42
|
+
export interface ContactFormLabels {
|
|
43
|
+
name: string;
|
|
44
|
+
firstName: string;
|
|
45
|
+
lastName: string;
|
|
46
|
+
email: string;
|
|
47
|
+
phone: string;
|
|
48
|
+
topic: string;
|
|
49
|
+
message: string;
|
|
50
|
+
/** The address search's label, when the form has a provider. */
|
|
51
|
+
addressSearch: string;
|
|
52
|
+
street: string;
|
|
53
|
+
city: string;
|
|
54
|
+
state: string;
|
|
55
|
+
zip: string;
|
|
56
|
+
services: string;
|
|
57
|
+
/** Beside the services label, such as "(check all that apply)". None by default. */
|
|
58
|
+
servicesHint?: string;
|
|
59
|
+
/** Beside the label of an optional phone and an optional address. */
|
|
60
|
+
optional: string;
|
|
61
|
+
/** The services button's words for more chosen than it lists. */
|
|
62
|
+
servicesCount: (count: number) => string;
|
|
63
|
+
/** The address suggestions' accessible name. */
|
|
64
|
+
suggestions: string;
|
|
65
|
+
showMore: string;
|
|
66
|
+
showLess: string;
|
|
67
|
+
submit: string;
|
|
68
|
+
sending: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A placeholder for each text field that takes one; none by default but the two pickers'. */
|
|
72
|
+
export type ContactFormPlaceholders = Partial<
|
|
73
|
+
Record<Exclude<ContactFormField, "state"> | "addressSearch" | "services", string>
|
|
74
|
+
>;
|
|
75
|
+
|
|
76
|
+
/** The property address block: an optional search over street, city, state, and ZIP. */
|
|
77
|
+
export interface ContactFormAddress {
|
|
78
|
+
/** Refuse a send without every part and a ZIP code. Optional by default. */
|
|
79
|
+
required?: boolean;
|
|
80
|
+
/** Looks addresses up as they are typed and fills the four fields; without it, the fields stand alone. */
|
|
81
|
+
provider?: AddressProvider;
|
|
82
|
+
/**
|
|
83
|
+
* A Maps JavaScript API key: the search then offers Google's addresses
|
|
84
|
+
* (`googlePlacesProvider`) under the "Google Maps" attribution Google's
|
|
85
|
+
* terms ask for. `provider` wins when both are given; with neither, the
|
|
86
|
+
* fields stand alone.
|
|
87
|
+
*/
|
|
88
|
+
googlePlacesKey?: string;
|
|
89
|
+
/** Google's options beside the key: the countries, a point to prefer, the types, the language. */
|
|
90
|
+
googlePlacesOptions?: Omit<GooglePlacesOptions, "apiKey">;
|
|
91
|
+
/** Under the suggestions, for a provider whose terms ask for it; with `googlePlacesKey`, Google's by default. */
|
|
92
|
+
attribution?: ReactNode;
|
|
93
|
+
/** Draws the state as a select of these; a text field when left out. */
|
|
94
|
+
states?: string[];
|
|
95
|
+
/** The state chosen before anyone picks; the first of `states` when left out. */
|
|
96
|
+
defaultState?: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** A consent checkbox under the message, its words cut short behind Show more. */
|
|
100
|
+
export interface ContactFormConsent {
|
|
101
|
+
/** The consent wording, verbatim, as prose: it is the checkbox's whole label. */
|
|
102
|
+
text: ReactNode;
|
|
103
|
+
/** Links shown once the wording is opened (terms, privacy policy). */
|
|
104
|
+
links?: { label: string; href: string; newTab?: boolean }[];
|
|
105
|
+
/** The lines shown while closed. Defaults to 2. */
|
|
106
|
+
lines?: number;
|
|
107
|
+
/** Show more at the end of the last line rather than under the wording (`ExpandableText`'s `inline`). */
|
|
108
|
+
inline?: boolean;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The thanks that replaces the form once a message is sent. */
|
|
112
|
+
export interface ContactFormThanks {
|
|
113
|
+
/** The heading; a function of what was sent, to greet by name. */
|
|
114
|
+
title: ReactNode | ((values: ContactFormValues) => ReactNode);
|
|
115
|
+
/**
|
|
116
|
+
* The line under it. Defaults to the form's `successMessage`; `null` or
|
|
117
|
+
* `false` draws none, no element at all.
|
|
118
|
+
*/
|
|
119
|
+
lead?: ReactNode | ((values: ContactFormValues) => ReactNode);
|
|
120
|
+
/**
|
|
121
|
+
* What was sent, read back: the labels of its rows (services, topic,
|
|
122
|
+
* address, and how to reach the visitor), or false for none. Shown by
|
|
123
|
+
* default, each row only when the form drew its field and it was filled.
|
|
124
|
+
*/
|
|
125
|
+
summary?: false | Partial<Record<"services" | "topic" | "address" | "contact", string>>;
|
|
126
|
+
/** The steps that follow, as a numbered list. */
|
|
127
|
+
steps?: string[];
|
|
128
|
+
/** The heading over the steps. */
|
|
129
|
+
stepsHeading?: string;
|
|
130
|
+
/** The words of the control that brings the form back. */
|
|
131
|
+
again?: string;
|
|
132
|
+
/** The level the heading is read at. Defaults to 3. */
|
|
133
|
+
headingLevel?: 2 | 3 | 4 | 5;
|
|
134
|
+
}
|
|
135
|
+
|
|
7
136
|
export interface ContactFormProps {
|
|
8
137
|
/**
|
|
9
138
|
* POST target for a real form backend (e.g. "/api/contact"). The body is
|
|
10
139
|
* JSON: { name, email, topic, message, company } where company is the
|
|
11
|
-
* honeypot and must be relayed verbatim.
|
|
12
|
-
*
|
|
140
|
+
* honeypot and must be relayed verbatim. Those five keys are always
|
|
141
|
+
* there, so a relay that reads only them keeps working whatever else the
|
|
142
|
+
* form draws: with `splitName`, `name` is the first and last joined, and
|
|
143
|
+
* with `services`, `topic` is the chosen services joined by commas. Each
|
|
144
|
+
* opt in field adds its own keys only when it is drawn: `firstName` and
|
|
145
|
+
* `lastName`; `phone`; `address`, { street, city, state, zip }; `services`,
|
|
146
|
+
* an array of the chosen values; `consent`, a boolean. When omitted, the
|
|
147
|
+
* form falls back to a mailto handoff via toEmail.
|
|
13
148
|
*/
|
|
14
149
|
endpoint?: string;
|
|
15
150
|
/** Destination inbox for the mailto fallback (required without endpoint). */
|
|
16
151
|
toEmail?: string;
|
|
17
|
-
/** Options for the topic select. */
|
|
18
|
-
topics
|
|
152
|
+
/** Options for the topic select; no select when left out, or when `services` is set. */
|
|
153
|
+
topics?: string[];
|
|
19
154
|
/** Message shown after a successful submit (or after the mail draft opens). */
|
|
20
155
|
successMessage: string;
|
|
21
156
|
/** Message shown when the endpoint submission fails. */
|
|
@@ -30,25 +165,93 @@ export interface ContactFormProps {
|
|
|
30
165
|
* It is passed nothing, so no personal data can ride along.
|
|
31
166
|
*/
|
|
32
167
|
onLead?: () => void;
|
|
168
|
+
/** First and last name as two fields, side by side, in place of the one name. */
|
|
169
|
+
splitName?: boolean;
|
|
170
|
+
/** A phone field beside the email, optional or required. None when left out. */
|
|
171
|
+
phone?: "optional" | "required";
|
|
172
|
+
/** The property address: street, city, state, and ZIP, under an address search when given a provider. */
|
|
173
|
+
address?: ContactFormAddress;
|
|
174
|
+
/** A multi select of these services in place of the topic select. */
|
|
175
|
+
services?: string[];
|
|
176
|
+
/** A consent checkbox under the message. */
|
|
177
|
+
consent?: ContactFormConsent;
|
|
178
|
+
/** The form's words, each replacing its neutral default. */
|
|
179
|
+
labels?: Partial<ContactFormLabels>;
|
|
180
|
+
placeholders?: ContactFormPlaceholders;
|
|
181
|
+
/** The message under each refused field, each replacing its neutral default. */
|
|
182
|
+
errorText?: Partial<Record<ContactFormField, string>>;
|
|
183
|
+
/**
|
|
184
|
+
* A count of the fields to fix, over the form, after a refused send: true
|
|
185
|
+
* for the default words, or a function of the count for the site's own.
|
|
186
|
+
*/
|
|
187
|
+
errorSummary?: boolean | ((count: number) => string);
|
|
188
|
+
/** Replaces the form with a thanks once a message is sent; by default the thanks shows over the cleared form. */
|
|
189
|
+
thanks?: ContactFormThanks;
|
|
190
|
+
/**
|
|
191
|
+
* Whether a phone number is one to accept. Defaults to `isPhoneNumber`: a
|
|
192
|
+
* ten digit North American number typed any common way (a leading 1
|
|
193
|
+
* aside), or a full international one starting with + and its country code.
|
|
194
|
+
*/
|
|
195
|
+
validatePhone?: (value: string) => boolean;
|
|
33
196
|
}
|
|
34
197
|
|
|
35
198
|
function sendLead() {
|
|
36
199
|
trackEvent("generate_lead", { form_name: "contact" });
|
|
37
200
|
}
|
|
38
201
|
|
|
39
|
-
interface FieldErrors {
|
|
40
|
-
name?: boolean;
|
|
41
|
-
email?: boolean;
|
|
42
|
-
message?: boolean;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
202
|
type Status = "idle" | "sending" | "sent" | "failed";
|
|
46
203
|
|
|
47
|
-
const
|
|
204
|
+
const LABELS: ContactFormLabels = {
|
|
205
|
+
name: "Your name",
|
|
206
|
+
firstName: "First name",
|
|
207
|
+
lastName: "Last name",
|
|
208
|
+
email: "Email",
|
|
209
|
+
phone: "Phone",
|
|
210
|
+
topic: "What can we help with?",
|
|
211
|
+
message: "Message",
|
|
212
|
+
addressSearch: "Address",
|
|
213
|
+
street: "Street address",
|
|
214
|
+
city: "City",
|
|
215
|
+
state: "State",
|
|
216
|
+
zip: "ZIP",
|
|
217
|
+
services: "Services",
|
|
218
|
+
optional: "(optional)",
|
|
219
|
+
servicesCount: (count) => `${count} chosen`,
|
|
220
|
+
suggestions: "Address suggestions",
|
|
221
|
+
showMore: "Show more",
|
|
222
|
+
showLess: "Show less",
|
|
223
|
+
submit: "Send Message",
|
|
224
|
+
sending: "Sending…",
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const ERRORS: Record<ContactFormField, string> = {
|
|
228
|
+
name: "Please enter your name.",
|
|
229
|
+
firstName: "Please enter your first name.",
|
|
230
|
+
lastName: "Please enter your last name.",
|
|
231
|
+
phone: "Please enter a phone number with its area code.",
|
|
232
|
+
email: "Please enter a valid email.",
|
|
233
|
+
street: "Please enter the street address.",
|
|
234
|
+
city: "Please enter the city.",
|
|
235
|
+
state: "Please enter the state.",
|
|
236
|
+
zip: "Please enter a 5 digit ZIP code.",
|
|
237
|
+
message: "Please add a short message.",
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const countWords = (count: number) =>
|
|
241
|
+
count === 1
|
|
242
|
+
? "One field needs fixing before this can be sent."
|
|
243
|
+
: `${count} fields need fixing before this can be sent.`;
|
|
48
244
|
|
|
49
245
|
/**
|
|
50
246
|
* Contact form with client validation and a honeypot. POSTs to `endpoint`
|
|
51
247
|
* when one is configured; otherwise hands off to the visitor's mail app.
|
|
248
|
+
*
|
|
249
|
+
* With no opt in prop it is the form it always was: a name, an email, the
|
|
250
|
+
* topic select, and a message. The opt in props add a service business's
|
|
251
|
+
* fields: a split name, a phone, the property address (with an address
|
|
252
|
+
* search when the site gives it a provider), a multi select of services in
|
|
253
|
+
* place of the topic, a consent line cut short behind Show more, a count of
|
|
254
|
+
* the fields to fix, and a thanks that replaces the form once it is sent.
|
|
52
255
|
*/
|
|
53
256
|
export function ContactForm({
|
|
54
257
|
endpoint,
|
|
@@ -58,51 +261,146 @@ export function ContactForm({
|
|
|
58
261
|
errorMessage = "Something went wrong sending your message. Please try again.",
|
|
59
262
|
footnote,
|
|
60
263
|
onLead = sendLead,
|
|
264
|
+
splitName = false,
|
|
265
|
+
phone,
|
|
266
|
+
address,
|
|
267
|
+
services,
|
|
268
|
+
consent,
|
|
269
|
+
labels: labelProps,
|
|
270
|
+
placeholders = {},
|
|
271
|
+
errorText,
|
|
272
|
+
errorSummary,
|
|
273
|
+
thanks,
|
|
274
|
+
validatePhone,
|
|
61
275
|
}: ContactFormProps) {
|
|
62
276
|
const withBase = useWithBase();
|
|
63
|
-
const
|
|
277
|
+
const labels = { ...LABELS, ...labelProps };
|
|
278
|
+
const errorWords = { ...ERRORS, ...errorText };
|
|
279
|
+
const shape: ContactFormShape = {
|
|
280
|
+
splitName,
|
|
281
|
+
phone: phone ?? "off",
|
|
282
|
+
address: address ? (address.required ? "required" : "optional") : "off",
|
|
283
|
+
services: Boolean(services),
|
|
284
|
+
consent: Boolean(consent),
|
|
285
|
+
};
|
|
286
|
+
// An empty list of topics is no topic select, as no list is.
|
|
287
|
+
const hasTopic = Boolean(topics?.length) && !services;
|
|
288
|
+
const startAddress = (): AddressParts => ({
|
|
289
|
+
street: "",
|
|
290
|
+
city: "",
|
|
291
|
+
state: address?.defaultState ?? address?.states?.[0] ?? "",
|
|
292
|
+
zip: "",
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
// Google's provider, made once per key and options, so the search keeps
|
|
296
|
+
// one session across renders.
|
|
297
|
+
const googleKey = address?.provider ? undefined : address?.googlePlacesKey;
|
|
298
|
+
const googleOptions = JSON.stringify(address?.googlePlacesOptions ?? {});
|
|
299
|
+
const google = useMemo(
|
|
300
|
+
() =>
|
|
301
|
+
googleKey
|
|
302
|
+
? googlePlacesProvider({ apiKey: googleKey, ...JSON.parse(googleOptions) })
|
|
303
|
+
: undefined,
|
|
304
|
+
[googleKey, googleOptions],
|
|
305
|
+
);
|
|
306
|
+
const addressProvider = address?.provider ?? google;
|
|
307
|
+
const addressAttribution =
|
|
308
|
+
address?.attribution ?? (google ? <GoogleMapsAttribution /> : undefined);
|
|
309
|
+
const [errors, setErrors] = useState<ContactFormRefusals>({});
|
|
64
310
|
const [status, setStatus] = useState<Status>("idle");
|
|
311
|
+
const [place, setPlace] = useState<AddressParts>(startAddress);
|
|
312
|
+
// Whether a chosen suggestion's parts are still on their way.
|
|
313
|
+
const choosing = useRef(false);
|
|
314
|
+
const [sentValues, setSentValues] = useState<ContactFormValues | null>(null);
|
|
315
|
+
const formRef = useRef<HTMLFormElement>(null);
|
|
316
|
+
const thanksRef = useRef<HTMLDivElement>(null);
|
|
317
|
+
// Where focus goes once the form or the thanks is drawn: the thanks's
|
|
318
|
+
// heading when it replaces the form, the first field when the form comes back.
|
|
319
|
+
const [focusOn, setFocusOn] = useState<"thanks" | "form" | null>(null);
|
|
65
320
|
// Ids from useId, so two forms on one page never share a label's target.
|
|
66
321
|
const uid = useId();
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
322
|
+
const idOf = (key: string) => `${uid}-${key}`;
|
|
323
|
+
|
|
324
|
+
useEffect(() => {
|
|
325
|
+
if (focusOn === "thanks") thanksRef.current?.focus();
|
|
326
|
+
if (focusOn === "form") {
|
|
327
|
+
const first = formRef.current?.elements.namedItem(splitName ? "firstName" : "name");
|
|
328
|
+
if (first instanceof HTMLElement) first.focus();
|
|
329
|
+
}
|
|
330
|
+
if (focusOn) setFocusOn(null);
|
|
331
|
+
}, [focusOn, splitName]);
|
|
332
|
+
|
|
74
333
|
// A refused field says so and why: aria-invalid, and its message as its
|
|
75
334
|
// description, only while it is refused.
|
|
76
|
-
const refused = (field:
|
|
335
|
+
const refused = (field: ContactFormField) =>
|
|
77
336
|
errors[field]
|
|
78
|
-
? { "aria-invalid": true as const, "aria-describedby": `${
|
|
337
|
+
? { "aria-invalid": true as const, "aria-describedby": `${idOf(field)}-error` }
|
|
79
338
|
: {};
|
|
339
|
+
const fieldClass = (field: ContactFormField) =>
|
|
340
|
+
["field", errors[field] && "invalid"].filter(Boolean).join(" ");
|
|
341
|
+
const optionalHint = (on: boolean) =>
|
|
342
|
+
on && (
|
|
343
|
+
<>
|
|
344
|
+
{" "}
|
|
345
|
+
<small className="field-hint">{labels.optional}</small>
|
|
346
|
+
</>
|
|
347
|
+
);
|
|
348
|
+
const errorLine = (field: ContactFormField) => (
|
|
349
|
+
<span className="err" id={`${idOf(field)}-error`}>
|
|
350
|
+
{errorWords[field]}
|
|
351
|
+
</span>
|
|
352
|
+
);
|
|
353
|
+
const textField = (
|
|
354
|
+
field: ContactFormField,
|
|
355
|
+
input: {
|
|
356
|
+
type?: string;
|
|
357
|
+
autoComplete?: string;
|
|
358
|
+
inputMode?: "tel" | "numeric";
|
|
359
|
+
required?: boolean;
|
|
360
|
+
hint?: boolean;
|
|
361
|
+
value?: string;
|
|
362
|
+
onChange?: (value: string) => void;
|
|
363
|
+
},
|
|
364
|
+
) => (
|
|
365
|
+
<div className={fieldClass(field)}>
|
|
366
|
+
<label htmlFor={idOf(field)}>
|
|
367
|
+
{labels[field]}
|
|
368
|
+
{optionalHint(Boolean(input.hint))}
|
|
369
|
+
</label>
|
|
370
|
+
<input
|
|
371
|
+
id={idOf(field)}
|
|
372
|
+
name={field}
|
|
373
|
+
type={input.type ?? "text"}
|
|
374
|
+
autoComplete={input.autoComplete}
|
|
375
|
+
inputMode={input.inputMode}
|
|
376
|
+
placeholder={placeholders[field as keyof ContactFormPlaceholders]}
|
|
377
|
+
required={input.required}
|
|
378
|
+
{...(input.onChange && {
|
|
379
|
+
value: input.value,
|
|
380
|
+
onChange: (event) => input.onChange?.(event.target.value),
|
|
381
|
+
})}
|
|
382
|
+
{...refused(field)}
|
|
383
|
+
/>
|
|
384
|
+
{errorLine(field)}
|
|
385
|
+
</div>
|
|
386
|
+
);
|
|
80
387
|
|
|
81
388
|
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
|
|
82
389
|
event.preventDefault();
|
|
83
390
|
if (status === "sending") return;
|
|
84
391
|
const form = event.currentTarget;
|
|
85
|
-
const
|
|
86
|
-
const
|
|
87
|
-
const email = String(data.get("email") ?? "").trim();
|
|
88
|
-
const topic = String(data.get("topic") ?? "");
|
|
89
|
-
const message = String(data.get("message") ?? "").trim();
|
|
90
|
-
const company = String(data.get("company") ?? "");
|
|
91
|
-
|
|
92
|
-
const nextErrors: FieldErrors = {
|
|
93
|
-
name: !name,
|
|
94
|
-
email: !EMAIL_PATTERN.test(email),
|
|
95
|
-
message: !message,
|
|
96
|
-
};
|
|
392
|
+
const values = readContactForm(new FormData(form), shape);
|
|
393
|
+
const nextErrors = checkContactForm(values, shape, validatePhone);
|
|
97
394
|
setErrors(nextErrors);
|
|
98
|
-
const
|
|
99
|
-
if (
|
|
395
|
+
const first = firstRefused(nextErrors);
|
|
396
|
+
if (first) {
|
|
100
397
|
setStatus("idle");
|
|
101
398
|
// Focus goes to the first field to fix, where its message is read.
|
|
102
|
-
const field = form.elements.namedItem(
|
|
399
|
+
const field = form.elements.namedItem(first);
|
|
103
400
|
if (field instanceof HTMLElement) field.focus();
|
|
104
401
|
return;
|
|
105
402
|
}
|
|
403
|
+
const { name, email, topic, message, company } = values;
|
|
106
404
|
|
|
107
405
|
if (!endpoint) {
|
|
108
406
|
if (!toEmail) {
|
|
@@ -110,8 +408,16 @@ export function ContactForm({
|
|
|
110
408
|
return;
|
|
111
409
|
}
|
|
112
410
|
const subject = encodeURIComponent(`Website inquiry: ${topic}`);
|
|
113
|
-
const
|
|
411
|
+
const extra = [
|
|
412
|
+
shape.phone !== "off" && values.phone && `Phone: ${values.phone}`,
|
|
413
|
+
shape.address !== "off" && addressLine(values.address),
|
|
414
|
+
].filter(Boolean);
|
|
415
|
+
const body = encodeURIComponent(
|
|
416
|
+
`${name} (${email}) wrote:\n\n${message}${extra.length ? `\n\n${extra.join("\n")}` : ""}`,
|
|
417
|
+
);
|
|
418
|
+
setSentValues(values);
|
|
114
419
|
setStatus("sent");
|
|
420
|
+
if (thanks) setFocusOn("thanks");
|
|
115
421
|
window.location.href = `mailto:${toEmail}?subject=${subject}&body=${body}`;
|
|
116
422
|
return;
|
|
117
423
|
}
|
|
@@ -121,11 +427,14 @@ export function ContactForm({
|
|
|
121
427
|
const response = await fetch(withBase(endpoint), {
|
|
122
428
|
method: "POST",
|
|
123
429
|
headers: { "Content-Type": "application/json" },
|
|
124
|
-
body: JSON.stringify(
|
|
430
|
+
body: JSON.stringify(contactFormBody(values, shape)),
|
|
125
431
|
});
|
|
126
432
|
if (!response.ok) throw new Error(`status ${response.status}`);
|
|
433
|
+
setSentValues(values);
|
|
127
434
|
setStatus("sent");
|
|
128
435
|
form.reset();
|
|
436
|
+
setPlace(startAddress());
|
|
437
|
+
if (thanks) setFocusOn("thanks");
|
|
129
438
|
// The conversion: only a relay the endpoint accepted counts, never a
|
|
130
439
|
// press of the button, a failed send, or a honeypot catch (which the
|
|
131
440
|
// relay answers with the same success so the bot never learns). No
|
|
@@ -136,8 +445,45 @@ export function ContactForm({
|
|
|
136
445
|
}
|
|
137
446
|
}
|
|
138
447
|
|
|
448
|
+
if (thanks && status === "sent" && sentValues) {
|
|
449
|
+
return (
|
|
450
|
+
<ContactThanks
|
|
451
|
+
thanks={thanks}
|
|
452
|
+
values={sentValues}
|
|
453
|
+
successMessage={successMessage}
|
|
454
|
+
shape={shape}
|
|
455
|
+
hasTopic={hasTopic}
|
|
456
|
+
titleRef={thanksRef}
|
|
457
|
+
onAgain={() => {
|
|
458
|
+
// Back to a fresh form, whichever way it was sent: the address the
|
|
459
|
+
// form holds goes with the rest of the last visitor's words.
|
|
460
|
+
setStatus("idle");
|
|
461
|
+
setErrors({});
|
|
462
|
+
setSentValues(null);
|
|
463
|
+
setPlace(startAddress());
|
|
464
|
+
setFocusOn("form");
|
|
465
|
+
}}
|
|
466
|
+
/>
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const refusedCount = Object.values(errors).filter(Boolean).length;
|
|
471
|
+
const summaryWords = typeof errorSummary === "function" ? errorSummary : countWords;
|
|
472
|
+
const states = address?.states;
|
|
473
|
+
// A state a provider hands back that the list lacks is still offered, so a pick is never lost.
|
|
474
|
+
const stateOptions =
|
|
475
|
+
states && place.state && !states.includes(place.state) ? [...states, place.state] : states;
|
|
476
|
+
// A hand edit to an address field drops a pick still being read, so the
|
|
477
|
+
// late pick never writes over what the visitor typed.
|
|
478
|
+
const setPart = (part: keyof AddressParts) => (value: string) => {
|
|
479
|
+
choosing.current = false;
|
|
480
|
+
setPlace((was) => ({ ...was, [part]: value }));
|
|
481
|
+
};
|
|
482
|
+
const addressOptional = shape.address === "optional";
|
|
483
|
+
const searchShown = Boolean(addressProvider);
|
|
484
|
+
|
|
139
485
|
return (
|
|
140
|
-
<form onSubmit={handleSubmit} noValidate>
|
|
486
|
+
<form ref={formRef} onSubmit={handleSubmit} noValidate>
|
|
141
487
|
{/* One shot status: the thanks or the failure is announced once when it
|
|
142
488
|
appears (Component Rules 14). The region is always drawn, so a screen
|
|
143
489
|
reader is already watching it when the words arrive. */}
|
|
@@ -145,63 +491,313 @@ export function ContactForm({
|
|
|
145
491
|
{status === "sent" && <span className="form-success">{successMessage}</span>}
|
|
146
492
|
{status === "failed" && <span className="form-error">{errorMessage}</span>}
|
|
147
493
|
</output>
|
|
148
|
-
|
|
149
|
-
<
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
required
|
|
156
|
-
{
|
|
494
|
+
{errorSummary && refusedCount > 0 && (
|
|
495
|
+
<div className="form-alert" role="alert">
|
|
496
|
+
{summaryWords(refusedCount)}
|
|
497
|
+
</div>
|
|
498
|
+
)}
|
|
499
|
+
{splitName ? (
|
|
500
|
+
<div className="field-row">
|
|
501
|
+
{textField("firstName", { autoComplete: "given-name", required: true })}
|
|
502
|
+
{textField("lastName", { autoComplete: "family-name", required: true })}
|
|
503
|
+
</div>
|
|
504
|
+
) : (
|
|
505
|
+
textField("name", { autoComplete: "name", required: true })
|
|
506
|
+
)}
|
|
507
|
+
{(() => {
|
|
508
|
+
const emailField = textField("email", {
|
|
509
|
+
type: "email",
|
|
510
|
+
autoComplete: "email",
|
|
511
|
+
required: true,
|
|
512
|
+
});
|
|
513
|
+
if (shape.phone === "off") return emailField;
|
|
514
|
+
return (
|
|
515
|
+
<div className="field-row">
|
|
516
|
+
{textField("phone", {
|
|
517
|
+
type: "tel",
|
|
518
|
+
autoComplete: "tel",
|
|
519
|
+
inputMode: "tel",
|
|
520
|
+
required: shape.phone === "required",
|
|
521
|
+
hint: shape.phone === "optional",
|
|
522
|
+
})}
|
|
523
|
+
{emailField}
|
|
524
|
+
</div>
|
|
525
|
+
);
|
|
526
|
+
})()}
|
|
527
|
+
{address && (
|
|
528
|
+
<>
|
|
529
|
+
{searchShown && (
|
|
530
|
+
<AddressSearch
|
|
531
|
+
className="field"
|
|
532
|
+
provider={addressProvider}
|
|
533
|
+
label={labels.addressSearch}
|
|
534
|
+
hint={addressOptional ? labels.optional : undefined}
|
|
535
|
+
placeholder={placeholders.addressSearch ?? "Start typing an address"}
|
|
536
|
+
attribution={addressAttribution}
|
|
537
|
+
listLabel={labels.suggestions}
|
|
538
|
+
onChoose={() => {
|
|
539
|
+
choosing.current = true;
|
|
540
|
+
}}
|
|
541
|
+
onPick={(picked) => {
|
|
542
|
+
if (!choosing.current) return;
|
|
543
|
+
choosing.current = false;
|
|
544
|
+
// A pick with no state keeps the one the field shows, so a state
|
|
545
|
+
// select and what it submits never part ways.
|
|
546
|
+
setPlace((was) => ({ ...picked, state: picked.state || was.state }));
|
|
547
|
+
// Only a field the pick filled with a valid value stops being refused.
|
|
548
|
+
const filled: Record<keyof AddressParts, boolean> = {
|
|
549
|
+
street: Boolean(picked.street),
|
|
550
|
+
city: Boolean(picked.city),
|
|
551
|
+
state: Boolean(picked.state),
|
|
552
|
+
zip: isZIP(picked.zip),
|
|
553
|
+
};
|
|
554
|
+
setErrors((was) => ({
|
|
555
|
+
...was,
|
|
556
|
+
...Object.fromEntries(
|
|
557
|
+
(Object.keys(filled) as (keyof AddressParts)[])
|
|
558
|
+
.filter((part) => filled[part])
|
|
559
|
+
.map((part) => [part, undefined]),
|
|
560
|
+
),
|
|
561
|
+
}));
|
|
562
|
+
}}
|
|
563
|
+
/>
|
|
564
|
+
)}
|
|
565
|
+
{textField("street", {
|
|
566
|
+
autoComplete: "address-line1",
|
|
567
|
+
required: !addressOptional,
|
|
568
|
+
hint: addressOptional && !searchShown,
|
|
569
|
+
value: place.street,
|
|
570
|
+
onChange: setPart("street"),
|
|
571
|
+
})}
|
|
572
|
+
<div className="field-row field-row-3">
|
|
573
|
+
{textField("city", {
|
|
574
|
+
autoComplete: "address-level2",
|
|
575
|
+
required: !addressOptional,
|
|
576
|
+
value: place.city,
|
|
577
|
+
onChange: setPart("city"),
|
|
578
|
+
})}
|
|
579
|
+
{stateOptions ? (
|
|
580
|
+
<div className={fieldClass("state")}>
|
|
581
|
+
<label htmlFor={idOf("state")}>{labels.state}</label>
|
|
582
|
+
<select
|
|
583
|
+
id={idOf("state")}
|
|
584
|
+
name="state"
|
|
585
|
+
autoComplete="address-level1"
|
|
586
|
+
required={!addressOptional}
|
|
587
|
+
value={place.state}
|
|
588
|
+
onChange={(event) => setPart("state")(event.target.value)}
|
|
589
|
+
{...refused("state")}
|
|
590
|
+
>
|
|
591
|
+
{stateOptions.map((state) => (
|
|
592
|
+
<option key={state}>{state}</option>
|
|
593
|
+
))}
|
|
594
|
+
</select>
|
|
595
|
+
{errorLine("state")}
|
|
596
|
+
</div>
|
|
597
|
+
) : (
|
|
598
|
+
textField("state", {
|
|
599
|
+
autoComplete: "address-level1",
|
|
600
|
+
required: !addressOptional,
|
|
601
|
+
value: place.state,
|
|
602
|
+
onChange: setPart("state"),
|
|
603
|
+
})
|
|
604
|
+
)}
|
|
605
|
+
{textField("zip", {
|
|
606
|
+
autoComplete: "postal-code",
|
|
607
|
+
inputMode: "numeric",
|
|
608
|
+
required: !addressOptional,
|
|
609
|
+
value: place.zip,
|
|
610
|
+
onChange: setPart("zip"),
|
|
611
|
+
})}
|
|
612
|
+
</div>
|
|
613
|
+
</>
|
|
614
|
+
)}
|
|
615
|
+
{services ? (
|
|
616
|
+
<MultiSelect
|
|
617
|
+
className="field"
|
|
618
|
+
label={labels.services}
|
|
619
|
+
hint={labels.servicesHint}
|
|
620
|
+
name="services"
|
|
621
|
+
options={services}
|
|
622
|
+
placeholder={placeholders.services}
|
|
623
|
+
countLabel={labels.servicesCount}
|
|
157
624
|
/>
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
625
|
+
) : (
|
|
626
|
+
hasTopic && (
|
|
627
|
+
<div className="field">
|
|
628
|
+
<label htmlFor={idOf("topic")}>{labels.topic}</label>
|
|
629
|
+
<select id={idOf("topic")} name="topic">
|
|
630
|
+
{topics?.map((topic) => (
|
|
631
|
+
<option key={topic}>{topic}</option>
|
|
632
|
+
))}
|
|
633
|
+
</select>
|
|
634
|
+
</div>
|
|
635
|
+
)
|
|
636
|
+
)}
|
|
637
|
+
<div className={fieldClass("message")}>
|
|
638
|
+
<label htmlFor={idOf("message")}>{labels.message}</label>
|
|
639
|
+
<textarea
|
|
640
|
+
id={idOf("message")}
|
|
641
|
+
name="message"
|
|
642
|
+
placeholder={placeholders.message}
|
|
169
643
|
required
|
|
170
|
-
{...refused("
|
|
644
|
+
{...refused("message")}
|
|
171
645
|
/>
|
|
172
|
-
|
|
173
|
-
Please enter a valid email.
|
|
174
|
-
</span>
|
|
175
|
-
</div>
|
|
176
|
-
<div className="field">
|
|
177
|
-
<label htmlFor={ids.topic}>What can we help with?</label>
|
|
178
|
-
<select id={ids.topic} name="topic">
|
|
179
|
-
{topics.map((topic) => (
|
|
180
|
-
<option key={topic}>{topic}</option>
|
|
181
|
-
))}
|
|
182
|
-
</select>
|
|
183
|
-
</div>
|
|
184
|
-
<div className={["field", errors.message && "invalid"].filter(Boolean).join(" ")}>
|
|
185
|
-
<label htmlFor={ids.message}>Message</label>
|
|
186
|
-
<textarea id={ids.message} name="message" required {...refused("message")} />
|
|
187
|
-
<span className="err" id={`${ids.message}-error`}>
|
|
188
|
-
Please add a short message.
|
|
189
|
-
</span>
|
|
646
|
+
{errorLine("message")}
|
|
190
647
|
</div>
|
|
648
|
+
{consent && (
|
|
649
|
+
<div className="contact-consent">
|
|
650
|
+
<input
|
|
651
|
+
id={idOf("consent")}
|
|
652
|
+
className="contact-consent-box"
|
|
653
|
+
type="checkbox"
|
|
654
|
+
name="consent"
|
|
655
|
+
value="yes"
|
|
656
|
+
/>
|
|
657
|
+
<ExpandableText
|
|
658
|
+
labelFor={idOf("consent")}
|
|
659
|
+
lines={consent.lines ?? 2}
|
|
660
|
+
inline={consent.inline}
|
|
661
|
+
moreLabel={labels.showMore}
|
|
662
|
+
lessLabel={labels.showLess}
|
|
663
|
+
more={
|
|
664
|
+
consent.links?.length ? (
|
|
665
|
+
<span className="contact-consent-links">
|
|
666
|
+
{consent.links.map((link) => (
|
|
667
|
+
<a
|
|
668
|
+
key={link.href + link.label}
|
|
669
|
+
href={withBase(link.href)}
|
|
670
|
+
{...newTabProps(link.href, link.newTab)}
|
|
671
|
+
>
|
|
672
|
+
{link.label}
|
|
673
|
+
</a>
|
|
674
|
+
))}
|
|
675
|
+
</span>
|
|
676
|
+
) : undefined
|
|
677
|
+
}
|
|
678
|
+
>
|
|
679
|
+
{consent.text}
|
|
680
|
+
</ExpandableText>
|
|
681
|
+
</div>
|
|
682
|
+
)}
|
|
191
683
|
{/* Honeypot: visually hidden and skipped by keyboard/screen readers;
|
|
192
684
|
bots that fill it are quietly dropped by the backend. */}
|
|
193
685
|
<div className="hp-field" aria-hidden="true">
|
|
194
|
-
<label htmlFor={
|
|
195
|
-
<input id={
|
|
686
|
+
<label htmlFor={idOf("company")}>Company</label>
|
|
687
|
+
<input id={idOf("company")} name="company" type="text" tabIndex={-1} autoComplete="off" />
|
|
196
688
|
</div>
|
|
197
689
|
<Button type="submit" size="lg" arrow style={{ width: "100%" }}>
|
|
198
|
-
{status === "sending" ?
|
|
690
|
+
{status === "sending" ? labels.sending : labels.submit}
|
|
199
691
|
</Button>
|
|
200
|
-
{footnote &&
|
|
201
|
-
<p className="muted form-footnote" style={{ fontSize: "0.82rem", marginTop: 14 }}>
|
|
202
|
-
{footnote}
|
|
203
|
-
</p>
|
|
204
|
-
)}
|
|
692
|
+
{footnote && <p className="muted form-footnote">{footnote}</p>}
|
|
205
693
|
</form>
|
|
206
694
|
);
|
|
207
695
|
}
|
|
696
|
+
|
|
697
|
+
interface ContactThanksProps {
|
|
698
|
+
thanks: ContactFormThanks;
|
|
699
|
+
values: ContactFormValues;
|
|
700
|
+
successMessage: string;
|
|
701
|
+
shape: ContactFormShape;
|
|
702
|
+
hasTopic: boolean;
|
|
703
|
+
titleRef: RefObject<HTMLDivElement | null>;
|
|
704
|
+
onAgain: () => void;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/** The thanks that replaces the form: the heading, the lead, what was sent, and what comes next. */
|
|
708
|
+
function ContactThanks({
|
|
709
|
+
thanks,
|
|
710
|
+
values,
|
|
711
|
+
successMessage,
|
|
712
|
+
shape,
|
|
713
|
+
hasTopic,
|
|
714
|
+
titleRef,
|
|
715
|
+
onAgain,
|
|
716
|
+
}: ContactThanksProps) {
|
|
717
|
+
const level = thanks.headingLevel ?? 3;
|
|
718
|
+
const read = (words: ReactNode | ((values: ContactFormValues) => ReactNode)) =>
|
|
719
|
+
typeof words === "function" ? words(values) : words;
|
|
720
|
+
const labels =
|
|
721
|
+
thanks.summary === false
|
|
722
|
+
? null
|
|
723
|
+
: {
|
|
724
|
+
services: "Services",
|
|
725
|
+
topic: "Topic",
|
|
726
|
+
address: "Address",
|
|
727
|
+
contact: "Contact",
|
|
728
|
+
...thanks.summary,
|
|
729
|
+
};
|
|
730
|
+
const rows: { label: string; value: ReactNode }[] = [];
|
|
731
|
+
if (labels) {
|
|
732
|
+
const { address } = values;
|
|
733
|
+
if (shape.services && values.services.length > 0)
|
|
734
|
+
rows.push({ label: labels.services, value: values.services.join(", ") });
|
|
735
|
+
if (hasTopic && values.topic) rows.push({ label: labels.topic, value: values.topic });
|
|
736
|
+
// A state alone, left at its default, is not an address.
|
|
737
|
+
if (shape.address !== "off" && (address.street || address.city || address.zip))
|
|
738
|
+
rows.push({ label: labels.address, value: addressLine(address) });
|
|
739
|
+
// The visitor's own number and address, read back as links (Component Rules 17).
|
|
740
|
+
const reach = [
|
|
741
|
+
values.phone && <PhoneLink key="phone" phone={values.phone} />,
|
|
742
|
+
values.email && (
|
|
743
|
+
<EmailLink key="email" email={values.email}>
|
|
744
|
+
{values.email}
|
|
745
|
+
</EmailLink>
|
|
746
|
+
),
|
|
747
|
+
].filter(Boolean);
|
|
748
|
+
if (reach.length)
|
|
749
|
+
rows.push({
|
|
750
|
+
label: labels.contact,
|
|
751
|
+
value: reach.flatMap((link, at) => (at ? [", ", link] : [link])),
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
return (
|
|
756
|
+
<div className="contact-thanks">
|
|
757
|
+
<div className="contact-thanks-mark" aria-hidden="true">
|
|
758
|
+
<Check size={26} strokeWidth={2.5} />
|
|
759
|
+
</div>
|
|
760
|
+
{/* biome-ignore lint/a11y/useSemanticElements: a heading element would take a site's bare heading rules (Component Rules 21) */}
|
|
761
|
+
<div
|
|
762
|
+
ref={titleRef}
|
|
763
|
+
className="contact-thanks-title"
|
|
764
|
+
role="heading"
|
|
765
|
+
aria-level={level}
|
|
766
|
+
tabIndex={-1}
|
|
767
|
+
>
|
|
768
|
+
{read(thanks.title)}
|
|
769
|
+
</div>
|
|
770
|
+
{thanks.lead !== null && thanks.lead !== false && (
|
|
771
|
+
<p className="contact-thanks-lead">{read(thanks.lead ?? successMessage)}</p>
|
|
772
|
+
)}
|
|
773
|
+
{rows.length > 0 && (
|
|
774
|
+
<dl className="contact-thanks-summary">
|
|
775
|
+
{rows.map((row) => (
|
|
776
|
+
<div key={row.label}>
|
|
777
|
+
<dt>{row.label}</dt>
|
|
778
|
+
<dd>{row.value}</dd>
|
|
779
|
+
</div>
|
|
780
|
+
))}
|
|
781
|
+
</dl>
|
|
782
|
+
)}
|
|
783
|
+
{thanks.steps && thanks.steps.length > 0 && (
|
|
784
|
+
<>
|
|
785
|
+
{thanks.stepsHeading && (
|
|
786
|
+
// biome-ignore lint/a11y/useSemanticElements: a heading element would take a site's bare heading rules (Component Rules 21)
|
|
787
|
+
<div className="contact-thanks-steps-head" role="heading" aria-level={level + 1}>
|
|
788
|
+
{thanks.stepsHeading}
|
|
789
|
+
</div>
|
|
790
|
+
)}
|
|
791
|
+
<ol className="contact-thanks-steps">
|
|
792
|
+
{thanks.steps.map((step) => (
|
|
793
|
+
<li key={step}>{step}</li>
|
|
794
|
+
))}
|
|
795
|
+
</ol>
|
|
796
|
+
</>
|
|
797
|
+
)}
|
|
798
|
+
<button type="button" className="contact-thanks-again" onClick={onAgain}>
|
|
799
|
+
{thanks.again ?? "Send another message"}
|
|
800
|
+
</button>
|
|
801
|
+
</div>
|
|
802
|
+
);
|
|
803
|
+
}
|