Sub topic 6
Designing the pending state: disabling, labels, spinners, and screen readers
This chapter assumes you can already build a SubmitButton that calls useFormStatus from a child of the form and reads pending. Here the question is what that button should do while pending is true. Sighted mouse users, keyboard users, and screen reader users each need something different.
What the pending state has to do
A good pending state does three jobs:
- It prevents duplicate submissions.
- It shows visibly that work is in progress.
- It tells assistive technology the same thing.
Each technique below covers some of these jobs, but none covers all three.
| Technique | Prevents duplicates | Visible feedback | Screen reader feedback |
|---|---|---|---|
| `disabled` attribute | Yes | Dimmed button | Weak, and focus can be lost |
| `aria-disabled` plus a click guard | Yes | Needs styling | Announced as "dimmed" or "unavailable" |
| Changing the label | No | Yes | Often not announced |
| Spinner | No | Yes | None if hidden from assistive technology |
| Live region (`role="status"`) | No | Optional | Yes |
Disabling the button
disabled={pending} is the simplest choice and works for most forms. It has one side effect. A disabled button is removed from the tab order, and browsers differ in what happens to focus on a button that is focused when it becomes disabled. Keyboard users may find that focus has jumped to the page body.
The alternative is aria-disabled. The button stays focusable, and you block the extra submission yourself:
<button
type="submit"
aria-disabled={pending}
onClick={(event) => {
if (pending) event.preventDefault();
}}
>
Save
</button>aria-disabled only changes what assistive technology reports. It does not stop clicks, so the preventDefault guard is required. Style the state yourself, for example with button[aria-disabled="true"].
Changing the label
Replace the idle label with a progress label such as "Saving...". Use a verb in the present tense and keep the wording close to the idle label. Avoid a label that is much longer than the original, because the button will change width and shift the layout. A min-width on the button avoids that.
Adding a spinner
A spinner is decoration. The text already says what is happening, so hide the spinner from assistive technology with aria-hidden="true". Keep it in a separate element, and animate it with CSS only.
.spinner {
display: inline-block;
width: 1em;
height: 1em;
margin-right: 0.5em;
border: 2px solid currentColor;
border-right-color: transparent;
border-radius: 50%;
animation: spin 0.7s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
.spinner { animation-duration: 3s; }
}
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}The prefers-reduced-motion rule slows the spinner for people who have asked their system to reduce motion. The .visually-hidden class hides text from view but leaves it available to screen readers. You will use it in the next component.
Announcing the state to screen readers
Changing a button's text while it has focus is not reliably announced. A live region is. A live region is an element that screen readers watch and read aloud when its text changes. role="status" makes an element a polite live region, so the announcement waits until the reader finishes speaking.
The region must already be in the DOM before its text changes. If you render it only when pending becomes true, many screen readers will not announce it. Render it always and change only its content.
'use client';
import { useFormStatus } from 'react-dom';
export default function SubmitButton({
idleLabel = 'Save',
pendingLabel = 'Saving...',
}) {
const { pending } = useFormStatus();
return (
<>
<button
type="submit"
aria-disabled={pending}
onClick={(event) => {
if (pending) event.preventDefault();
}}
>
{pending && <span className="spinner" aria-hidden="true" />}
{pending ? pendingLabel : idleLabel}
</button>
<span role="status" className="visually-hidden">
{pending ? pendingLabel : ''}
</span>
</>
);
}Use it inside any form, as in the earlier chapters:
import SubmitButton from './SubmitButton';
export default function ProfileForm({ saveProfile }) {
return (
<form action={saveProfile}>
<label>
Name
<input name="name" />
</label>
<SubmitButton idleLabel="Save profile" pendingLabel="Saving profile..." />
</form>
);
}The SubmitButton is still a child of the form, so useFormStatus reads the right status.
Common mistakes
- Setting
disabledon the whole form's inputs during submission. Disabled fields are left out ofFormDatain some flows, so the values can be lost. - Forgetting the click guard when using
aria-disabled. The button then looks busy but still submits again. - Rendering the
role="status"element conditionally. Keep it mounted. - Announcing every state change. Announce the start of the submission. Report the result separately, for example with an error message tied to the field.
Exercise
A teammate switches SubmitButton from disabled={pending} to aria-disabled={pending} and removes the onClick handler. Users now report duplicate submissions during slow requests. What went wrong, and what is the fix?
Show answer
aria-disabled only changes how assistive technology describes the button. It does not block clicks or Enter presses, so the form can submit again while pending is true. The fix is to restore a guard such as onClick={(event) => { if (pending) event.preventDefault(); }}, or to go back to disabled={pending} if the focus behavior is acceptable.
Exercise
Why should the role="status" element be rendered all the time and not only when pending is true?
Show answer
Screen readers announce changes to the contents of a live region that already exists in the page. An element that is added with its text already inside it is often not announced. Keeping the element mounted and changing only its text makes the announcement reliable.