Sub topic 6
Designing the Pending State: Disabling, Labels, Spinners, and Screen Readers
Earlier chapters showed that useFormStatus returns pending, and that you must call it from a component rendered inside the form. This chapter is about what you do with that boolean. A pending state has to prevent duplicate submissions, tell sighted users that work is happening, and tell people who use assistive technology the same thing.
This chapter assumes you can already:
- Write a form with a function passed to its
actionprop. - Call
useFormStatusfrom a child of the form, as in the reusableSubmitButton. - Read basic JSX and CSS.
It uses React 19 and react-dom, where useFormStatus is exported.
What a Pending State Must Accomplish
A good pending state does four jobs:
- Prevent duplicate submissions. A second click should not start a second request.
- Confirm that the click registered. The user should see a change within a fraction of a second.
- Keep the interface stable. The button should not jump in size, and focus should not vanish.
- Reach non-visual users. A color or animation change means nothing to a screen reader.
Each technique below serves some of these jobs and fails others. The diagram shows the two states the interface moves between. Every technique in this chapter changes something in one of them.
Disabling the Submit Button
The simplest pending UI is disabled={pending}. It stops duplicate clicks and gives a visual cue, because browsers style disabled buttons differently.
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
Save
</button>
);
}The disabled attribute has side effects you should know about:
- A disabled button is removed from the keyboard tab order.
- If the button had focus when it was disabled, focus can fall back to the page body in some browsers. A keyboard user who pressed Enter on the button then has to tab from the top of the page again.
- Many screen readers skip disabled controls when navigating by control type, so the user may not find the button to check on it.
- Default disabled styling often has low contrast. If you also change the label, make sure the new text is still readable.
Keeping Focus With aria-disabled
If keyboard focus matters, as it does in long forms or forms that users resubmit often, use aria-disabled instead. It marks the button as unavailable to assistive technology but leaves it focusable. Because the browser no longer blocks activation, you must block it yourself.
function SubmitButton({ children, pendingLabel }) {
const { pending } = useFormStatus();
function handleClick(event) {
if (pending) {
event.preventDefault();
}
}
return (
<button type="submit" aria-disabled={pending} onClick={handleClick}>
{pending ? pendingLabel : children}
</button>
);
}Calling preventDefault on the click cancels the submission. This also covers pressing Enter inside a text field, because the browser performs implicit submission by dispatching a click on the form's default submit button.
| Approach | Blocks clicks | Stays in tab order | Stays visible to screen readers |
|---|---|---|---|
| `disabled` | Yes, automatically | No | Often skipped |
| `aria-disabled` plus `preventDefault` | Only because your handler does it | Yes | Yes, announced as unavailable |
Pick disabled as the default because it is hard to get wrong. Pick aria-disabled when losing focus would hurt, and test the Enter-key path.
Changing the Label
A label change is the most useful single signal because it works for sighted users and for screen reader users who are focused on the button. Change it to describe the work in progress, not just the fact of waiting.
- Prefer "Saving..." over "Please wait...". It names what is happening.
- Use the same verb as the idle label: "Save" becomes "Saving...", "Publish" becomes "Publishing...".
- Let the component accept the pending label as a prop, so the same
SubmitButtonworks for every form.
function SubmitButton({ children, pendingLabel = 'Working...' }) {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? pendingLabel : children}
</button>
);
}Usage: <SubmitButton pendingLabel="Publishing...">Publish</SubmitButton>.
Avoiding Layout Shift
"Save" and "Saving..." have different widths, so the button resizes and the controls beside it shift. Reserve space with a minimum width in CSS, and center the content so both labels look deliberate.
.submit-button {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
min-width: 8rem;
}Set the width to fit your longest label. Do not hard-code a pixel width that would break with larger user font sizes. A min-width in rem scales with the text.
Adding a Spinner
A spinner shows that the app is still working, which a static label cannot do. Treat it as decoration. The meaning is already in the label, so hide the spinner from assistive technology with aria-hidden="true". Otherwise a screen reader may announce an empty element or a stray image name.
This spinner is pure CSS and needs no icon library:
.spinner {
width: 1em;
height: 1em;
border: 2px solid currentColor;
border-right-color: transparent;
border-radius: 50%;
opacity: 0;
animation:
spin 0.7s linear infinite,
reveal 0s linear 300ms forwards;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@keyframes reveal {
to {
opacity: 1;
}
}
@media (prefers-reduced-motion: reduce) {
.spinner {
animation:
spin 3s linear infinite,
reveal 0s linear 300ms forwards;
}
}Two details matter here:
- The spinner appears after 300 ms. The
revealanimation holds the spinner atopacity: 0for 300 ms, then shows it. Fast actions finish before the spinner appears, so users do not see a distracting flash.useFormStatusitself has no delay, so a CSS delay is the cheapest way to add one. - Reduced motion is respected. Users who enable "reduce motion" in their operating system get a much slower rotation. Slowing keeps a sense of activity without fast movement. Removing the animation entirely would leave a static ring that looks broken.
The component renders the spinner only while pending:
function SubmitButton({ children, pendingLabel = 'Working...' }) {
const { pending } = useFormStatus();
return (
<button type="submit" className="submit-button" disabled={pending}>
{pending && <span className="spinner" aria-hidden="true" />}
<span>{pending ? pendingLabel : children}</span>
</button>
);
}Communicating With Screen Readers
Screen reader users do not see the spinner, and a label change is announced only in some situations. Browsers and screen readers do not reliably announce a change to the name of a control that already has focus. If the user moved on to read other parts of the page, they will hear nothing. A live region fixes this.
A live region is an element whose text changes are announced automatically. role="status" creates a polite one. "Polite" means the screen reader waits until it finishes the current speech before announcing the change.
Render the Region Before It Has Content
Live regions work most reliably when the element already exists in the page and its content changes later. An element that is inserted into the DOM already containing text is often missed. The SubmissionNotice example from the earlier chapter returned null when not pending, which mounts and unmounts the whole element. For accessibility, keep the element mounted and change only its text:
function PendingAnnouncer({ message = 'Saving your changes' }) {
const { pending } = useFormStatus();
return (
<p role="status" className="visually-hidden">
{pending ? message : ''}
</p>
);
}The visually-hidden class hides the text from sight but keeps it available to screen readers. Do not use display: none or the hidden attribute, because both remove the element from the accessibility tree and nothing would be announced.
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}If you want users to see the message too, drop the hiding class and style the paragraph normally. The same element then serves everyone.
Choosing a Live Region Role
| Attribute | Use for | Behavior |
|---|---|---|
| `role="status"` | Progress and success messages | Polite, waits for a pause |
| `role="alert"` | Errors that need immediate attention | Assertive, interrupts speech |
| `aria-live="polite"` on any element | Custom containers | Same as `status`, without the role's semantics |
Use status for pending messages. Interrupting the user to say that saving has started is more disruptive than helpful.
What useFormStatus Cannot Announce
useFormStatus reports only that a submission is in flight. When pending returns to false, the hook does not say whether the action succeeded or failed. Announcing the outcome ("Profile saved" or "Email already in use") needs the action's result, which comes from useActionState, covered in a later chapter. Until then, remember that a pending message that vanishes silently leaves screen reader users guessing about what happened.
aria-busy on the Form
You can also set aria-busy on a container to say its content is being updated. Support is inconsistent across screen readers, and some ignore it. Treat it as an optional extra and rely on the live region for the announcement.
Disabling the Rest of the Form
Disabling only the button still lets users edit fields while the request runs. If you want fields locked during submission, a child component can wrap them in a <fieldset>. Setting disabled on a fieldset disables every control inside it.
function FormFields({ children }) {
const { pending } = useFormStatus();
return <fieldset disabled={pending}>{children}</fieldset>;
}FormFields is a child of the form, so the hook works. Consider the costs before using it:
- Focus is lost if the user was typing in a field when the fieldset became disabled. Submitting by pressing Enter in a text field is common, so this case happens often.
- Disabled controls are left out of
FormData. This is safe here because React reads the form data at submission time, beforependingbecomes true. If you ever buildFormDatamanually later, gather it before disabling anything. - For a quick action that finishes in under a second, a whole-form lock produces a visible flicker. Use it for slow operations only.
Putting It Together
This example combines a stable button, a delayed spinner, a pending label, a live announcement, and a locked fieldset. Add the CSS from the sections above.
'use client';
import { useFormStatus } from 'react-dom';
function FormFields({ children }) {
const { pending } = useFormStatus();
return <fieldset disabled={pending}>{children}</fieldset>;
}
function SubmitButton({ children, pendingLabel = 'Working...' }) {
const { pending } = useFormStatus();
return (
<button type="submit" className="submit-button" disabled={pending}>
{pending && <span className="spinner" aria-hidden="true" />}
<span>{pending ? pendingLabel : children}</span>
</button>
);
}
function PendingAnnouncer({ message }) {
const { pending } = useFormStatus();
return (
<p role="status" className="visually-hidden">
{pending ? message : ''}
</p>
);
}
export default function ProfileForm() {
async function saveProfile(formData) {
await new Promise((resolve) => setTimeout(resolve, 1500));
console.log('Saved', formData.get('name'));
}
return (
<form action={saveProfile}>
<FormFields>
<label htmlFor="name">Name</label>
<input id="name" name="name" />
<SubmitButton pendingLabel="Saving...">Save</SubmitButton>
</FormFields>
<PendingAnnouncer message="Saving your profile" />
</form>
);
}Each of the three helper components calls useFormStatus, and all three render inside the <form> element. Each one reads the same form's status, so splitting responsibilities this way costs nothing extra. PendingAnnouncer sits outside the fieldset because a live region is not a control, and keeping it outside makes clear that its content is not part of the locked group.
Common Mistakes in Pending UI
- Hiding the status region with
display: none. The announcement never reaches screen readers. Use the visually hidden pattern. - Mounting the region only while pending. The announcement is unreliable. Keep the element in the DOM and change its text.
- Using
aria-live="assertive"for a routine progress message. It interrupts whatever the user was hearing. - Letting the label change resize the button. Surrounding controls jump. Set a
min-width. - Using
aria-disabledwithoutpreventDefault. The button looks unavailable but still submits again. - Showing a spinner with no text. The state is invisible to anyone who cannot see the animation.
- Adding a delay in JavaScript to hide flicker. A CSS animation delay is simpler and does not add state to your component.
Exercise
A keyboard user reports that after pressing Enter in the name field of your form, focus jumps to the top of the page while the request runs. The submit button uses disabled={pending}. Name the likely cause and give one fix that keeps the form protected against double submission.
Show answer
Disabling the focused element (here the button, or a fieldset containing the focused field) can make the browser drop focus to the page body, so keyboard users lose their place. One fix is to use aria-disabled={pending} on the button and call event.preventDefault() in its onClick handler when pending is true. The button stays focusable and duplicate submissions are still blocked. Not wrapping the fields in a disabled fieldset also helps keep focus in the text field.
Exercise
Your PendingAnnouncer is written as if (!pending) return null; followed by return <p role="status">Saving...</p>;. Screen reader users say they sometimes hear nothing. What changes would make the announcement more reliable, and which CSS must you avoid when hiding the text visually?
Show answer
Always render the <p role="status"> element and change only its text content, for example {pending ? 'Saving...' : ''}. A live region that already exists in the DOM is announced more reliably than one inserted with its text already inside. If you hide it visually, use a visually-hidden pattern (absolute positioning, 1px size, clipped) and avoid display: none, visibility: hidden, and the hidden attribute, which remove it from the accessibility tree so nothing is announced.
Exercise
Write a SubmitButton that takes children and pendingLabel props, shows a spinner element hidden from assistive technology only while pending, and swaps its label while pending. Why is the spinner marked aria-hidden="true"?
Show answer
Example: function SubmitButton({ children, pendingLabel = 'Working...' }) { const { pending } = useFormStatus(); return ( <button type="submit" disabled={pending}> {pending && <span className="spinner" aria-hidden="true" />} <span>{pending ? pendingLabel : children}</span> </button> ); } The spinner is purely decorative. The label already carries the meaning, so hiding the spinner prevents screen readers from announcing a meaningless empty element.