Field states
One text field in four states: with helper text, with an error, confirmed and switched off. The message keeps its place under the field, so nothing jumps when a field is checked.
Account details
Options
Code
import { useId } from "react";
import "./field-states.css";
const marks = {
error:
"M8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13Zm0 3a.75.75 0 0 1 .75.75v3a.75.75 0 0 1-1.5 0v-3A.75.75 0 0 1 8 4.5Zm0 5.5a.9.9 0 1 1 0 1.800.9.9 0 0 1 0-1.800Z",
success:
"M8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13Zm3.030 4.970a.75.75 0 0 1 0 1.060l-3.500 3.500a.75.75 0 0 1-1.060 0l-1.500-1.500a.75.75 0 1 1 1.060-1.060l.970.970 2.970-2.970a.75.75 0 0 1 1.060 0Z",
};
/* A field with a message under it. state is "error", "success" or left out;
message is the helper text, the error or the confirmation.
<Field label="Email" state="error" message="Enter a full email address." /> */
export function Field({ label, state, message, ...props }) {
const id = useId();
return (
<label className="dh-state" data-state={state}>
<span>{label}</span>
<input type="text" aria-invalid={state === "error" || undefined} aria-describedby={id} {...props} />
<small id={id} role={state === "error" ? "alert" : undefined}>
{marks[state] && (
<svg viewBox="0 0 16 16" aria-hidden="true">
<path d={marks[state]} />
</svg>
)}
{message}
</small>
</label>
);
}
export function Example() {
return (
<section className="dh-state-card">
<h3>Account details</h3>
<div className="dh-state-stack">
<Field label="Company" defaultValue="Logoipsum" message="As it should appear on invoices." />
<Field
label="Email"
type="email"
defaultValue="maya@example"
state="error"
message="Enter a full email address, like [email protected]."
/>
<Field label="Username" defaultValue="maya-okafor" state="success" message="This name is free." />
<Field label="Plan" defaultValue="Team, yearly" disabled message="Ask an owner to change the plan." />
</div>
<footer>
<button type="button">Cancel</button>
<button type="button">Save changes</button>
</footer>
</section>
);
}/* One text field in four states: with helper text, with an error, confirmed
and switched off. The message keeps its place under the field, so nothing
jumps when a field is checked. Shown here as a small account card. */
.dh-state {
--hue: var(--dh-muted, #5c5c5c);
display: grid;
align-content: start;
gap: 5px;
min-width: 0;
color: var(--dh-fg, #171717);
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-state > span {
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-state input {
width: 100%;
height: 32px;
box-sizing: border-box;
padding: 0 10px;
border: 0;
border-radius: 7px;
background: var(--dh-well, #ededeb);
color: inherit;
font: inherit;
outline: none;
transition:
box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1),
background-color 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-state input::placeholder {
color: var(--dh-faint, #8a8a8a);
}
.dh-state input:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-state small {
display: flex;
gap: 5px;
min-height: 16px;
color: var(--hue);
font-size: 11px;
line-height: 16px;
}
.dh-state small svg {
flex: none;
width: 12px;
height: 12px;
margin-top: 2px;
fill: currentColor;
animation: dh-state-in 280ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-state[data-state="error"] {
--hue: #d92d20;
}
.dh-state[data-state="error"] input {
background: color-mix(in srgb, #d92d20 7%, var(--dh-well, #ededeb));
box-shadow: 0 0 0 1px #d92d20;
}
.dh-state[data-state="error"] input:focus-visible {
box-shadow:
0 0 0 1px #d92d20,
0 0 0 3px color-mix(in srgb, #d92d20 28%, transparent);
}
.dh-state[data-state="success"] {
--hue: #1fa750;
}
.dh-state[data-state="success"] small {
color: color-mix(in srgb, #1fa750 80%, var(--dh-fg, #171717));
}
.dh-state[data-state="error"] small {
color: color-mix(in srgb, #d92d20 88%, var(--dh-fg, #171717));
}
.dh-state:has(input:disabled) > span,
.dh-state:has(input:disabled) small {
color: var(--dh-faint, #8a8a8a);
}
.dh-state input:disabled {
color: var(--dh-faint, #8a8a8a);
background: color-mix(in srgb, var(--dh-well, #ededeb) 55%, transparent);
cursor: not-allowed;
}
@keyframes dh-state-in {
from {
opacity: 0;
filter: blur(4px);
transform: translateY(-4px);
}
}
/* The card around them in the example. */
.dh-state-card {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
container: dh-state-card / inline-size;
width: 460px;
max-width: 100%;
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);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-state-card h3 {
margin: 0;
padding: 14px 16px 12px;
font-size: 14px;
font-weight: 500;
line-height: 20px;
letter-spacing: -0.015em;
}
.dh-state-stack {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px 12px;
padding: 0 16px 14px;
}
@container dh-state-card (max-width: 380px) {
.dh-state-stack {
grid-template-columns: minmax(0, 1fr);
}
}
.dh-state-card footer {
display: flex;
justify-content: flex-end;
gap: 8px;
padding: 10px;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
}
.dh-state-card button {
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 8px;
background: var(--dh-raised, #fff);
color: inherit;
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-state-card button:last-child {
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);
}
.dh-state-card button:active {
transform: scale(0.97);
}
.dh-state-card button:focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
@media (pointer: coarse) {
.dh-state input {
height: 44px;
font-size: 16px;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-state input,
.dh-state-card button {
transition: none;
}
.dh-state small svg {
animation: none;
}
}<section class="dh-state-card">
<h3>Account details</h3>
<div class="dh-state-stack">
<label class="dh-state">
<span>Company</span>
<input type="text" value="Logoipsum" aria-describedby="dh-state-help" />
<small id="dh-state-help">As it should appear on invoices.</small>
</label>
<label class="dh-state" data-state="error">
<span>Email</span>
<input type="email" value="maya@example" aria-invalid="true" aria-describedby="dh-state-error" />
<small id="dh-state-error" role="alert">
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13Zm0 3a.75.75 0 0 1 .75.75v3a.75.75 0 0 1-1.5 0v-3A.75.75 0 0 1 8 4.5Zm0 5.5a.9.9 0 1 1 0 1.800.9.9 0 0 1 0-1.800Z" /></svg>
Enter a full email address, like [email protected].
</small>
</label>
<label class="dh-state" data-state="success">
<span>Username</span>
<input type="text" value="maya-okafor" aria-describedby="dh-state-good" />
<small id="dh-state-good">
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13Zm3.030 4.970a.75.75 0 0 1 0 1.060l-3.500 3.500a.75.75 0 0 1-1.060 0l-1.500-1.500a.75.75 0 1 1 1.060-1.060l.970.970 2.970-2.970a.75.75 0 0 1 1.060 0Z" /></svg>
This name is free.
</small>
</label>
<label class="dh-state">
<span>Plan</span>
<input type="text" value="Team, yearly" disabled aria-describedby="dh-state-off" />
<small id="dh-state-off">Ask an owner to change the plan.</small>
</label>
</div>
<footer>
<button type="button">Cancel</button>
<button type="button">Save changes</button>
</footer>
</section>/* One text field in four states: with helper text, with an error, confirmed
and switched off. The message keeps its place under the field, so nothing
jumps when a field is checked. Shown here as a small account card. */
.dh-state {
--hue: var(--dh-muted, #5c5c5c);
display: grid;
align-content: start;
gap: 5px;
min-width: 0;
color: var(--dh-fg, #171717);
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-state > span {
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-state input {
width: 100%;
height: 32px;
box-sizing: border-box;
padding: 0 10px;
border: 0;
border-radius: 7px;
background: var(--dh-well, #ededeb);
color: inherit;
font: inherit;
outline: none;
transition:
box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1),
background-color 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-state input::placeholder {
color: var(--dh-faint, #8a8a8a);
}
.dh-state input:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-state small {
display: flex;
gap: 5px;
min-height: 16px;
color: var(--hue);
font-size: 11px;
line-height: 16px;
}
.dh-state small svg {
flex: none;
width: 12px;
height: 12px;
margin-top: 2px;
fill: currentColor;
animation: dh-state-in 280ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-state[data-state="error"] {
--hue: #d92d20;
}
.dh-state[data-state="error"] input {
background: color-mix(in srgb, #d92d20 7%, var(--dh-well, #ededeb));
box-shadow: 0 0 0 1px #d92d20;
}
.dh-state[data-state="error"] input:focus-visible {
box-shadow:
0 0 0 1px #d92d20,
0 0 0 3px color-mix(in srgb, #d92d20 28%, transparent);
}
.dh-state[data-state="success"] {
--hue: #1fa750;
}
.dh-state[data-state="success"] small {
color: color-mix(in srgb, #1fa750 80%, var(--dh-fg, #171717));
}
.dh-state[data-state="error"] small {
color: color-mix(in srgb, #d92d20 88%, var(--dh-fg, #171717));
}
.dh-state:has(input:disabled) > span,
.dh-state:has(input:disabled) small {
color: var(--dh-faint, #8a8a8a);
}
.dh-state input:disabled {
color: var(--dh-faint, #8a8a8a);
background: color-mix(in srgb, var(--dh-well, #ededeb) 55%, transparent);
cursor: not-allowed;
}
@keyframes dh-state-in {
from {
opacity: 0;
filter: blur(4px);
transform: translateY(-4px);
}
}
/* The card around them in the example. */
.dh-state-card {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
container: dh-state-card / inline-size;
width: 460px;
max-width: 100%;
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);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-state-card h3 {
margin: 0;
padding: 14px 16px 12px;
font-size: 14px;
font-weight: 500;
line-height: 20px;
letter-spacing: -0.015em;
}
.dh-state-stack {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px 12px;
padding: 0 16px 14px;
}
@container dh-state-card (max-width: 380px) {
.dh-state-stack {
grid-template-columns: minmax(0, 1fr);
}
}
.dh-state-card footer {
display: flex;
justify-content: flex-end;
gap: 8px;
padding: 10px;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
}
.dh-state-card button {
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 8px;
background: var(--dh-raised, #fff);
color: inherit;
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-state-card button:last-child {
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);
}
.dh-state-card button:active {
transform: scale(0.97);
}
.dh-state-card button:focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
@media (pointer: coarse) {
.dh-state input {
height: 44px;
font-size: 16px;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-state input,
.dh-state-card button {
transition: none;
}
.dh-state small svg {
animation: none;
}
}Field states is not in the Figma file yet.
Use the React or HTML code for now.