Loading Button States
One button that goes idle, loading, success or error and keeps its width, so the layout never jumps.
AI prompt
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
<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>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.bx{display:grid;gap:12px;justify-items:center}
.b{width:150px;height:40px;border-radius:99px;border:0;background:var(--accent);color:var(--accent-ink);font-size:13.5px;font-weight:600;cursor:pointer;display:flex;align-items:center;justify-content:center;gap:8px;transition:background .15s,transform .1s}
.b:active:not(:disabled){transform:scale(.97)}
.b:disabled{cursor:progress}
.sp{display:none;width:14px;height:14px;border-radius:50%;border:2px solid rgba(255,255,255,.35);border-top-color:#fff;animation:sp .7s linear infinite}
@keyframes sp{to{transform:rotate(360deg)}}
.b.load .sp{display:block}.b.ok{background:#1a8f4a}.b.err{background:var(--danger);animation:sk .35s}
@keyframes sk{25%{transform:translateX(-4px)}75%{transform:translateX(4px)}}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
@media (prefers-reduced-motion:reduce){.b.err{animation:none}.sp{width:8px;height:8px;border:0;background:#fff;animation:bk 1s steps(2) infinite}@keyframes bk{50%{opacity:.2}}}const b=document.getElementById('b'),lab=document.getElementById('lab'),sr=document.getElementById('sr'),fl=document.getElementById('fl');let t;
function set(c,txt,busy,dis){b.className='b '+c;lab.textContent=txt;b.setAttribute('aria-busy',String(busy));b.disabled=dis;}
b.addEventListener('click',()=>{clearTimeout(t);set('load','Saving…',true,true);sr.textContent='Saving';t=setTimeout(()=>{if(fl.checked){fl.checked=false;set('err','Try again',false,false);sr.textContent='Save failed';t=setTimeout(()=>set('', 'Save changes',false,false),2500);}
else{set('ok','✓ Saved',false,true);sr.textContent='Saved';t=setTimeout(()=>set('', 'Save changes',false,false),1800);}},1300);});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Loading Button States — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.bx{display:grid;gap:12px;justify-items:center}
.b{width:150px;height:40px;border-radius:99px;border:0;background:var(--accent);color:var(--accent-ink);font-size:13.5px;font-weight:600;cursor:pointer;display:flex;align-items:center;justify-content:center;gap:8px;transition:background .15s,transform .1s}
.b:active:not(:disabled){transform:scale(.97)}
.b:disabled{cursor:progress}
.sp{display:none;width:14px;height:14px;border-radius:50%;border:2px solid rgba(255,255,255,.35);border-top-color:#fff;animation:sp .7s linear infinite}
@keyframes sp{to{transform:rotate(360deg)}}
.b.load .sp{display:block}.b.ok{background:#1a8f4a}.b.err{background:var(--danger);animation:sk .35s}
@keyframes sk{25%{transform:translateX(-4px)}75%{transform:translateX(4px)}}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
@media (prefers-reduced-motion:reduce){.b.err{animation:none}.sp{width:8px;height:8px;border:0;background:#fff;animation:bk 1s steps(2) infinite}@keyframes bk{50%{opacity:.2}}}
</style></head>
<body><div class="stage">
<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>
</div>
<script>
const b=document.getElementById('b'),lab=document.getElementById('lab'),sr=document.getElementById('sr'),fl=document.getElementById('fl');let t;
function set(c,txt,busy,dis){b.className='b '+c;lab.textContent=txt;b.setAttribute('aria-busy',String(busy));b.disabled=dis;}
b.addEventListener('click',()=>{clearTimeout(t);set('load','Saving…',true,true);sr.textContent='Saving';t=setTimeout(()=>{if(fl.checked){fl.checked=false;set('err','Try again',false,false);sr.textContent='Save failed';t=setTimeout(()=>set('', 'Save changes',false,false),2500);}
else{set('ok','✓ Saved',false,true);sr.textContent='Saved';t=setTimeout(()=>set('', 'Save changes',false,false),1800);}},1300);});
</script>
</body></html>