Contact form
A contact form under a dithered band of your accent: name, email, subject and message. Each field checks itself when you leave it, and the card shows a confirmation in place once the message is sent.
Options
Code
import { useId, useRef, useState } from "react";
import "./contact-form.css";
/* A contact form on a raised card. Each field checks itself when you leave it,
and the card shows a confirmation in place once the message is sent.
<ContactForm onSend={(values) => fetch("/api/contact", { method: "POST", body: JSON.stringify(values) })} /> */
const rules = {
name: (value) => (value ? "" : "Enter your name."),
email: (value) => {
if (!value) return "Enter your email.";
return /^[^ @]+@[^ @]+[.][^ @]+$/.test(value) ? "" : "That email looks incomplete.";
},
subject: (value) => (value ? "" : "Add a subject."),
message: (value) => {
if (!value) return "Write a short message.";
return value.length < 10 ? "Add a little more detail." : "";
},
};
const empty = { name: "", email: "", subject: "", message: "" };
/* With no onSend, the send is faked with a short wait. */
const pretend = () => new Promise((resolve) => setTimeout(resolve, 900));
export function ContactForm({
title = "Get in touch",
lead = "Tell us what you need and we will reply within one working day.",
note = "We only use this to reply to you.",
limit = 500,
onSend = pretend,
}) {
const id = useId();
const form = useRef(null);
const heading = useRef(null);
const [values, setValues] = useState(empty);
const [problems, setProblems] = useState({});
const [state, setState] = useState("");
const check = (name) => {
const problem = rules[name](values[name].trim());
setProblems((now) => ({ ...now, [name]: problem }));
return !problem;
};
const change = (event) => {
const { name, value } = event.target;
setValues((now) => ({ ...now, [name]: value }));
/* A field already marked wrong clears as soon as it is put right. */
if (problems[name]) setProblems((now) => ({ ...now, [name]: rules[name](value.trim()) }));
};
const submit = async (event) => {
event.preventDefault();
if (state) return;
const wrong = Object.keys(rules).filter((name) => !check(name));
if (wrong.length) {
form.current.elements[wrong[0]].focus();
return;
}
setState("busy");
await onSend(values);
setState("done");
requestAnimationFrame(() => heading.current && heading.current.focus());
};
const again = () => {
setValues(empty);
setProblems({});
setState("");
requestAnimationFrame(() => form.current.elements.name.focus());
};
const field = (name, label, props) => ({
label: { htmlFor: id + name, children: label },
input: {
id: id + name,
name,
value: values[name],
onChange: change,
onBlur: () => check(name),
"aria-invalid": name in problems ? Boolean(problems[name]) : undefined,
"aria-describedby": id + name + "note",
...props,
},
note: { id: id + name + "note", "aria-live": "polite", children: problems[name] },
});
const name = field("name", "Name", { type: "text", autoComplete: "name", placeholder: "Maya Okafor" });
const email = field("email", "Email", { type: "email", autoComplete: "email", placeholder: "[email protected]" });
const subject = field("subject", "Subject", { type: "text", placeholder: "A new website" });
const message = field("message", "Message", { rows: 4, maxLength: limit, placeholder: "What are you working on?" });
return (
<section className="dh-contact">
<form className="dh-contact-card" noValidate ref={form} data-state={state || undefined} onSubmit={submit}>
<div className="dh-contact-top">
<svg viewBox="0 0 20 20" aria-hidden="true">
<path d="M3.500 4A2.500 2.500 0 0 0 1 6.500v.4l8.300 4.400a1.500 1.500 0 0 0 1.400 0L19 6.900v-.4A2.500 2.500 0 0 0 16.500 4h-13ZM19 8.600l-7.600 4a3 3 0 0 1-2.800 0L1 8.600v4.900A2.500 2.500 0 0 0 3.500 16h13a2.500 2.500 0 0 0 2.500-2.500V8.600Z" />
</svg>
</div>
<div className="dh-contact-body" inert={state === "done" ? "" : undefined}>
<h2>{title}</h2>
<p className="dh-contact-lead">{lead}</p>
<div className="dh-contact-grid">
<div className="dh-contact-field">
<label {...name.label} />
<input {...name.input} />
<small {...name.note} />
</div>
<div className="dh-contact-field">
<label {...email.label} />
<input {...email.input} />
<small {...email.note} />
</div>
<div className="dh-contact-field dh-contact-wide">
<label {...subject.label} />
<input {...subject.input} />
<small {...subject.note} />
</div>
<div className="dh-contact-field dh-contact-wide">
<label htmlFor={message.label.htmlFor}>
Message{" "}
<span className="dh-contact-count">
{values.message.length} / {limit}
</span>
</label>
<textarea {...message.input} />
<small {...message.note} />
</div>
</div>
<div className="dh-contact-foot">
<p>{note}</p>
<button className="dh-contact-send" type="submit">
<i aria-hidden="true" />
<span>{state === "busy" ? "Sending…" : "Send message"}</span>
</button>
</div>
</div>
{state === "done" && (
<div className="dh-contact-done">
<svg viewBox="0 0 20 20" aria-hidden="true">
<path
d="M10 2a8 8 0 1 0 0 16 8 8 0 0 0 0-16Zm3.7 6.3a.9.9 0 0 0-1.3-1.2L9 10.700 7.600 9.300a.9.9 0 0 0-1.300 1.300l2.100 2a.9.9 0 0 0 1.300 0l4-4.300Z"
fillRule="evenodd"
/>
</svg>
<h2 tabIndex={-1} ref={heading}>
Message sent
</h2>
<p>
Thanks, <span>{values.name.trim().split(" ")[0]}</span>. We will reply to <span>{values.email.trim()}</span> within one
working day.
</p>
<button className="dh-contact-again" type="button" onClick={again}>
Send another message
</button>
</div>
)}
</form>
</section>
);
}
export function Example() {
return <ContactForm />;
}/* A contact form on a raised card under a dithered band of the accent. Each
field checks itself when you leave it, and the card shows a confirmation in
place once the message is sent. */
.dh-contact {
container: dh-contact / inline-size;
display: grid;
grid-template-columns: minmax(0, 440px);
justify-content: center;
padding: 56px 20px;
background: var(--dh-bg, #f7f7f6);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-contact *,
.dh-contact *::before,
.dh-contact *::after {
box-sizing: border-box;
}
/* The form and the confirmation share one grid cell, so the card keeps its
size when one takes the place of the other. */
.dh-contact-card {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
display: grid;
grid-template-rows: auto 1fr;
overflow: hidden;
border-radius: 14px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
}
.dh-contact-top {
height: 76px;
padding: 18px 20px;
background: linear-gradient(color-mix(in srgb, var(--accent) 82%, #000), var(--accent));
color: var(--dh-on-accent, #fff);
-webkit-mask-image: linear-gradient(#000 46%, transparent), radial-gradient(circle, #000 1.3px, transparent 1.6px);
mask-image: linear-gradient(#000 46%, transparent), radial-gradient(circle, #000 1.3px, transparent 1.6px);
-webkit-mask-size: auto, 5px 5px;
mask-size: auto, 5px 5px;
}
.dh-contact-top svg {
display: block;
width: 20px;
height: 20px;
fill: currentColor;
}
.dh-contact-body,
.dh-contact-done {
grid-area: 2 / 1;
min-width: 0;
}
.dh-contact :is(h2, p) {
margin: 0;
}
.dh-contact h2 {
padding: 0 20px;
font-size: 16px;
font-weight: 500;
line-height: 24px;
letter-spacing: -0.02em;
}
.dh-contact-lead {
margin-top: 2px !important;
padding: 0 20px;
color: var(--dh-muted, #5c5c5c);
}
.dh-contact-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0 8px;
padding: 16px 20px 4px;
}
.dh-contact-field {
display: grid;
min-width: 0;
}
.dh-contact-wide {
grid-column: 1 / -1;
}
.dh-contact-field label {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-contact-count {
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.dh-contact-field :is(input, textarea) {
width: 100%;
height: 32px;
padding: 0 10px;
border: 0;
border-radius: 7px;
background: var(--dh-well, #ededeb);
color: inherit;
font: inherit;
outline: none;
transition: box-shadow 150ms var(--ease);
}
.dh-contact-field textarea {
display: block;
height: auto;
min-height: 84px;
padding: 6px 10px;
resize: vertical;
}
.dh-contact-field ::placeholder {
color: var(--dh-faint, #8a8a8a);
}
.dh-contact-field :is(input, textarea):focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-contact-field [aria-invalid="true"] {
box-shadow: 0 0 0 1px #d92d20;
}
.dh-contact-field [aria-invalid="true"]:focus-visible {
box-shadow:
0 0 0 1px #d92d20,
0 0 0 3px color-mix(in srgb, #d92d20 30%, transparent);
}
/* The message line keeps its height when empty, so nothing jumps. */
.dh-contact-field small {
min-height: 16px;
margin: 3px 0 4px;
color: color-mix(in srgb, #d92d20 82%, var(--dh-fg, #171717));
font-size: 11px;
line-height: 16px;
}
/* The foot is a quiet strip: a note on the left, the one action on the right. */
.dh-contact-foot {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 10px 12px 10px 20px;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
}
.dh-contact-foot p {
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
}
.dh-contact :is(.dh-contact-send, .dh-contact-again) {
display: inline-flex;
flex: none;
align-items: center;
justify-content: center;
gap: 7px;
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 8px;
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
transition:
transform 150ms var(--ease),
opacity 150ms var(--ease);
}
.dh-contact :is(.dh-contact-send, .dh-contact-again):active {
transform: scale(0.97);
}
.dh-contact :is(.dh-contact-send, .dh-contact-again):focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
.dh-contact-send {
min-width: 112px;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
color: var(--dh-on-accent, #fff);
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
}
/* While sending, the label gives way to a spinner and "Sending…". */
.dh-contact-send i {
display: none;
width: 12px;
height: 12px;
border: 1.5px solid color-mix(in srgb, currentColor 30%, transparent);
border-top-color: currentColor;
border-radius: 50%;
animation: dh-contact-spin 0.7s linear infinite;
}
.dh-contact-card[data-state="busy"] .dh-contact-send {
opacity: 0.7;
pointer-events: none;
}
.dh-contact-card[data-state="busy"] .dh-contact-send i {
display: block;
}
.dh-contact-card[data-state="done"] .dh-contact-body {
visibility: hidden;
}
.dh-contact-done {
display: grid;
place-content: center;
justify-items: center;
gap: 2px;
padding: 0 20px 20px;
text-align: center;
animation: dh-contact-in 280ms var(--ease) both;
}
.dh-contact-done[hidden] {
display: none;
}
/* The tick sits in a small tinted tile, like an app icon. */
.dh-contact-done svg {
width: 36px;
height: 36px;
margin-bottom: 10px;
padding: 8px;
border-radius: 10px;
background: color-mix(in srgb, #1fa750 12%, transparent);
box-shadow: inset 0 0 0 1px color-mix(in srgb, #1fa750 18%, transparent);
fill: #1fa750;
}
.dh-contact-done h2 {
outline: none;
}
.dh-contact-done p {
max-width: 300px;
color: var(--dh-muted, #5c5c5c);
}
.dh-contact-done p span {
color: var(--dh-fg, #171717);
overflow-wrap: anywhere;
}
.dh-contact-again {
margin-top: 16px;
background: var(--dh-raised, #fff);
color: inherit;
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
}
/* In a narrow space the fields stack and grow to a thumb's height. */
@container dh-contact (max-width: 440px) {
.dh-contact {
padding: 16px 12px;
}
.dh-contact-grid {
grid-template-columns: 1fr;
}
.dh-contact-field :is(input, textarea) {
font-size: 16px;
}
.dh-contact-field input,
.dh-contact :is(.dh-contact-send, .dh-contact-again) {
height: 44px;
}
.dh-contact-foot {
flex-direction: column-reverse;
align-items: stretch;
padding: 12px 20px;
text-align: center;
}
}
@keyframes dh-contact-spin {
to {
rotate: 1turn;
}
}
@keyframes dh-contact-in {
from {
opacity: 0;
transform: translateY(-4px);
filter: blur(4px);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-contact-done {
animation: dh-contact-fade 200ms both;
}
.dh-contact :is(.dh-contact-send, .dh-contact-again) {
transition: none;
}
}
@keyframes dh-contact-fade {
from {
opacity: 0;
}
}<section class="dh-contact">
<form class="dh-contact-card" novalidate>
<div class="dh-contact-top">
<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M3.500 4A2.500 2.500 0 0 0 1 6.500v.4l8.300 4.400a1.500 1.500 0 0 0 1.400 0L19 6.900v-.4A2.500 2.500 0 0 0 16.500 4h-13ZM19 8.600l-7.600 4a3 3 0 0 1-2.800 0L1 8.600v4.900A2.500 2.500 0 0 0 3.500 16h13a2.500 2.500 0 0 0 2.500-2.500V8.600Z" /></svg>
</div>
<div class="dh-contact-body">
<h2>Get in touch</h2>
<p class="dh-contact-lead">Tell us what you need and we will reply within one working day.</p>
<div class="dh-contact-grid">
<div class="dh-contact-field">
<label for="dh-contact-name">Name</label>
<input id="dh-contact-name" name="name" type="text" autocomplete="name" placeholder="Maya Okafor" aria-describedby="dh-contact-name-note" />
<small id="dh-contact-name-note" aria-live="polite"></small>
</div>
<div class="dh-contact-field">
<label for="dh-contact-email">Email</label>
<input id="dh-contact-email" name="email" type="email" autocomplete="email" placeholder="[email protected]" aria-describedby="dh-contact-email-note" />
<small id="dh-contact-email-note" aria-live="polite"></small>
</div>
<div class="dh-contact-field dh-contact-wide">
<label for="dh-contact-subject">Subject</label>
<input id="dh-contact-subject" name="subject" type="text" placeholder="A new website" aria-describedby="dh-contact-subject-note" />
<small id="dh-contact-subject-note" aria-live="polite"></small>
</div>
<div class="dh-contact-field dh-contact-wide">
<label for="dh-contact-message">Message <span class="dh-contact-count">0 / 500</span></label>
<textarea id="dh-contact-message" name="message" rows="4" maxlength="500" placeholder="What are you working on?" aria-describedby="dh-contact-message-note"></textarea>
<small id="dh-contact-message-note" aria-live="polite"></small>
</div>
</div>
<div class="dh-contact-foot">
<p>We only use this to reply to you.</p>
<button class="dh-contact-send" type="submit"><i aria-hidden="true"></i><span>Send message</span></button>
</div>
</div>
<div class="dh-contact-done" hidden>
<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M10 2a8 8 0 1 0 0 16 8 8 0 0 0 0-16Zm3.7 6.3a.9.9 0 0 0-1.3-1.2L9 10.700 7.600 9.300a.9.9 0 0 0-1.300 1.300l2.100 2a.9.9 0 0 0 1.300 0l4-4.300Z" fill-rule="evenodd" /></svg>
<h2 tabindex="-1">Message sent</h2>
<p>Thanks, <span data-fill="name">Maya</span>. We will reply to <span data-fill="email">[email protected]</span> within one working day.</p>
<button class="dh-contact-again" type="button">Send another message</button>
</div>
</form>
</section>/* A contact form on a raised card under a dithered band of the accent. Each
field checks itself when you leave it, and the card shows a confirmation in
place once the message is sent. */
.dh-contact {
container: dh-contact / inline-size;
display: grid;
grid-template-columns: minmax(0, 440px);
justify-content: center;
padding: 56px 20px;
background: var(--dh-bg, #f7f7f6);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-contact *,
.dh-contact *::before,
.dh-contact *::after {
box-sizing: border-box;
}
/* The form and the confirmation share one grid cell, so the card keeps its
size when one takes the place of the other. */
.dh-contact-card {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
display: grid;
grid-template-rows: auto 1fr;
overflow: hidden;
border-radius: 14px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
}
.dh-contact-top {
height: 76px;
padding: 18px 20px;
background: linear-gradient(color-mix(in srgb, var(--accent) 82%, #000), var(--accent));
color: var(--dh-on-accent, #fff);
-webkit-mask-image: linear-gradient(#000 46%, transparent), radial-gradient(circle, #000 1.3px, transparent 1.6px);
mask-image: linear-gradient(#000 46%, transparent), radial-gradient(circle, #000 1.3px, transparent 1.6px);
-webkit-mask-size: auto, 5px 5px;
mask-size: auto, 5px 5px;
}
.dh-contact-top svg {
display: block;
width: 20px;
height: 20px;
fill: currentColor;
}
.dh-contact-body,
.dh-contact-done {
grid-area: 2 / 1;
min-width: 0;
}
.dh-contact :is(h2, p) {
margin: 0;
}
.dh-contact h2 {
padding: 0 20px;
font-size: 16px;
font-weight: 500;
line-height: 24px;
letter-spacing: -0.02em;
}
.dh-contact-lead {
margin-top: 2px !important;
padding: 0 20px;
color: var(--dh-muted, #5c5c5c);
}
.dh-contact-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0 8px;
padding: 16px 20px 4px;
}
.dh-contact-field {
display: grid;
min-width: 0;
}
.dh-contact-wide {
grid-column: 1 / -1;
}
.dh-contact-field label {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-contact-count {
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.dh-contact-field :is(input, textarea) {
width: 100%;
height: 32px;
padding: 0 10px;
border: 0;
border-radius: 7px;
background: var(--dh-well, #ededeb);
color: inherit;
font: inherit;
outline: none;
transition: box-shadow 150ms var(--ease);
}
.dh-contact-field textarea {
display: block;
height: auto;
min-height: 84px;
padding: 6px 10px;
resize: vertical;
}
.dh-contact-field ::placeholder {
color: var(--dh-faint, #8a8a8a);
}
.dh-contact-field :is(input, textarea):focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-contact-field [aria-invalid="true"] {
box-shadow: 0 0 0 1px #d92d20;
}
.dh-contact-field [aria-invalid="true"]:focus-visible {
box-shadow:
0 0 0 1px #d92d20,
0 0 0 3px color-mix(in srgb, #d92d20 30%, transparent);
}
/* The message line keeps its height when empty, so nothing jumps. */
.dh-contact-field small {
min-height: 16px;
margin: 3px 0 4px;
color: color-mix(in srgb, #d92d20 82%, var(--dh-fg, #171717));
font-size: 11px;
line-height: 16px;
}
/* The foot is a quiet strip: a note on the left, the one action on the right. */
.dh-contact-foot {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 10px 12px 10px 20px;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
}
.dh-contact-foot p {
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
}
.dh-contact :is(.dh-contact-send, .dh-contact-again) {
display: inline-flex;
flex: none;
align-items: center;
justify-content: center;
gap: 7px;
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 8px;
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
transition:
transform 150ms var(--ease),
opacity 150ms var(--ease);
}
.dh-contact :is(.dh-contact-send, .dh-contact-again):active {
transform: scale(0.97);
}
.dh-contact :is(.dh-contact-send, .dh-contact-again):focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
.dh-contact-send {
min-width: 112px;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
color: var(--dh-on-accent, #fff);
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
}
/* While sending, the label gives way to a spinner and "Sending…". */
.dh-contact-send i {
display: none;
width: 12px;
height: 12px;
border: 1.5px solid color-mix(in srgb, currentColor 30%, transparent);
border-top-color: currentColor;
border-radius: 50%;
animation: dh-contact-spin 0.7s linear infinite;
}
.dh-contact-card[data-state="busy"] .dh-contact-send {
opacity: 0.7;
pointer-events: none;
}
.dh-contact-card[data-state="busy"] .dh-contact-send i {
display: block;
}
.dh-contact-card[data-state="done"] .dh-contact-body {
visibility: hidden;
}
.dh-contact-done {
display: grid;
place-content: center;
justify-items: center;
gap: 2px;
padding: 0 20px 20px;
text-align: center;
animation: dh-contact-in 280ms var(--ease) both;
}
.dh-contact-done[hidden] {
display: none;
}
/* The tick sits in a small tinted tile, like an app icon. */
.dh-contact-done svg {
width: 36px;
height: 36px;
margin-bottom: 10px;
padding: 8px;
border-radius: 10px;
background: color-mix(in srgb, #1fa750 12%, transparent);
box-shadow: inset 0 0 0 1px color-mix(in srgb, #1fa750 18%, transparent);
fill: #1fa750;
}
.dh-contact-done h2 {
outline: none;
}
.dh-contact-done p {
max-width: 300px;
color: var(--dh-muted, #5c5c5c);
}
.dh-contact-done p span {
color: var(--dh-fg, #171717);
overflow-wrap: anywhere;
}
.dh-contact-again {
margin-top: 16px;
background: var(--dh-raised, #fff);
color: inherit;
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
}
/* In a narrow space the fields stack and grow to a thumb's height. */
@container dh-contact (max-width: 440px) {
.dh-contact {
padding: 16px 12px;
}
.dh-contact-grid {
grid-template-columns: 1fr;
}
.dh-contact-field :is(input, textarea) {
font-size: 16px;
}
.dh-contact-field input,
.dh-contact :is(.dh-contact-send, .dh-contact-again) {
height: 44px;
}
.dh-contact-foot {
flex-direction: column-reverse;
align-items: stretch;
padding: 12px 20px;
text-align: center;
}
}
@keyframes dh-contact-spin {
to {
rotate: 1turn;
}
}
@keyframes dh-contact-in {
from {
opacity: 0;
transform: translateY(-4px);
filter: blur(4px);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-contact-done {
animation: dh-contact-fade 200ms both;
}
.dh-contact :is(.dh-contact-send, .dh-contact-again) {
transition: none;
}
}
@keyframes dh-contact-fade {
from {
opacity: 0;
}
}/* Each field is checked when you leave it and again when you send. Nothing is
sent anywhere: the send is faked with a short wait, so put your own request
where the timer is. */
document.querySelectorAll(".dh-contact-card").forEach((form) => {
const fields = [...form.querySelectorAll(".dh-contact-field")];
const body = form.querySelector(".dh-contact-body");
const done = form.querySelector(".dh-contact-done");
const send = form.querySelector(".dh-contact-send span");
const message = form.elements.message;
const count = form.querySelector(".dh-contact-count");
const rules = {
name: (value) => (value ? "" : "Enter your name."),
email: (value) => {
if (!value) return "Enter your email.";
return /^[^ @]+@[^ @]+[.][^ @]+$/.test(value) ? "" : "That email looks incomplete.";
},
subject: (value) => (value ? "" : "Add a subject."),
message: (value) => {
if (!value) return "Write a short message.";
return value.length < 10 ? "Add a little more detail." : "";
},
};
const check = (field) => {
const input = field.querySelector("input, textarea");
const problem = rules[input.name](input.value.trim());
field.querySelector("small").textContent = problem;
input.setAttribute("aria-invalid", String(Boolean(problem)));
return !problem;
};
fields.forEach((field) => {
const input = field.querySelector("input, textarea");
input.addEventListener("blur", () => check(field));
/* A field already marked wrong clears as soon as it is put right. */
input.addEventListener("input", () => {
if (input.getAttribute("aria-invalid") === "true") check(field);
});
});
message.addEventListener("input", () => {
count.textContent = message.value.length + " / " + message.maxLength;
});
form.addEventListener("submit", (event) => {
event.preventDefault();
if (form.dataset.state) return;
const wrong = fields.filter((field) => !check(field));
if (wrong.length) {
wrong[0].querySelector("input, textarea").focus();
return;
}
form.dataset.state = "busy";
send.textContent = "Sending…";
setTimeout(() => {
done.querySelector('[data-fill="name"]').textContent = form.elements.name.value.trim().split(" ")[0];
done.querySelector('[data-fill="email"]').textContent = form.elements.email.value.trim();
form.dataset.state = "done";
send.textContent = "Send message";
body.inert = true;
done.hidden = false;
done.querySelector("h2").focus();
}, 900);
});
form.querySelector(".dh-contact-again").addEventListener("click", () => {
form.reset();
count.textContent = "0 / " + message.maxLength;
delete form.dataset.state;
body.inert = false;
done.hidden = true;
form.elements.name.focus();
});
});Contact form is not in the Figma file yet.
Use the React or HTML code for now.