Skip to content
IndexRequest a UIAboutContact

Loading Button States

One button that goes idle, loading, success or error and keeps its width, so the layout never jumps.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
Create a primary button component with four states. Idle: near-black pill with the label "Save changes". Loading: the button is disabled, shows a small white spinner and the text "Saving…", aria-busy true. Success: turns green with a check mark and "Saved" for two seconds, then returns to idle. Error: turns red, shakes 4 pixels left and right and shows "Try again". The button keeps a constant width of 140 pixels throughout. A switch "Fail next time" lets you test the error. Announce results via an aria-live region. Light minimal UI, 40 pixel height, 150 millisecond colour transitions, prefers-reduced-motion version with no shake and a blinking dot instead of a spinner. Plain HTML, CSS and JavaScript.

About this pattern

Definition

An async button reflects the life of an action: it is ready, then working (disabled with a spinner), then shows success or error before returning to ready.

Purpose & UX principles

  • Prevents double submits and gives instant feedback that the click was received.
  • Keep the label and width stable between states so surrounding content does not shift.
  • Show success briefly (1.5 to 2 seconds) then reset; on error explain what happened and allow retry.

Interaction states

  • Idle ("Save changes"), loading ("Saving…" with spinner, aria-busy, disabled), success (check, "Saved"), error (shake, "Try again"). The "Fail next time" switch tests the error path.

Motion specification

  • Spinner rotates 0.7s linear; success check draws in 250ms; error shakes 4px for 350ms.
  • Reduced motion: spinner becomes a blinking dot; no shake, only a red state.

Responsive behaviour

  • Minimum tap size 44px on touch; the button may become full width on small screens.
  • Use text plus icon, never icon alone, in the loading state.

Code

HTML
<div class="bx"><button class="b" id="b" type="button"><span class="sp" aria-hidden="true"></span><span class="lab" id="lab">Save changes</span></button><p class="sr" id="sr" aria-live="polite"></p><label class="sw"><input id="fl" type="checkbox"> Fail next time</label></div>

Related patterns

Primary & Secondary Button

↗
Primary & Secondary ButtonButtons & Actions

Press Feedback

↗
Press FeedbackButtons & Actions

Animated Checkmark Button

↗
Animated Checkmark ButtonMicro Interactions