Optimistic Update & Rollback
Checking a task updates instantly and saves in the background; if the save fails, it rolls back with a message.
AI prompt
Clean UI mockup of a to-do list with four rows: the first checked with a faint small spinner on its right to show it is saving, the others unchecked, and below the list a small red message Couldn't save. Change reverted., rounded rows with thin borders, light theme, flat modern style. Variants: the saved state with a grey check, and the failed row shown with a subtle red outline. Negative: blocking spinners, modal dialogs, heavy colour, text errors, clutter. Reduced motion: static design.About this pattern
Definition
Optimistic UI assumes success: the interface changes the moment the user acts, and the request finishes in the background. Most requests succeed, so the app feels instant. When one fails, the change is reverted and the user is told why.
Purpose & UX principles
- Update the interface immediately and show a quiet pending cue, such as a small spinner or dimmed text.
- On failure, restore the previous state and explain in one sentence, with a way to retry.
- Use it only for actions that rarely fail and are easy to undo.
Interaction states
- Four tasks you can tick, and a Fail the next save checkbox to see the rollback.
Motion specification
- The tick appears at once; saving takes 900ms; a failure shakes the row for 300ms.
- Reduced motion: no shake, only the message.
Responsive behaviour
- Never apply optimism to payments or destructive actions.
- Keep the pending cue small so the instant feeling is not lost.
Code
<div class="bx"><ul class="ls" id="ls"></ul><label class="sw"><input type="checkbox" id="fl"> Fail the next save</label><p class="out" id="out" role="status"></p></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}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.ls{list-style:none;display:grid;gap:5px}.ls li{display:flex;align-items:center;gap:10px;border:1px solid var(--line);border-radius:10px;background:var(--surface);padding:8px 12px;font-size:13px}.ls li.s span{opacity:.6}.ls li.e{border-color:#e5484d;animation:sk .3s}.ls li label{display:flex;align-items:center;gap:10px;flex:1;cursor:pointer}.ls li input{width:17px;height:17px}.ls li.d span{text-decoration:line-through;color:var(--muted)}
.sp{width:12px;height:12px;border:2px solid #bbb;border-top-color:#171717;border-radius:50%;animation:rot .7s linear infinite;margin-left:auto}@keyframes rot{to{transform:rotate(360deg)}}@keyframes sk{25%{transform:translateX(-4px)}75%{transform:translateX(4px)}}
.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}.out{min-height:18px;color:#c62f2f}
@media (prefers-reduced-motion:reduce){.ls li.e{animation:none}.sp{animation:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const T=['Draft the brief','Review the design','Update the roadmap','Send the invoice'];const st=T.map(()=>({d:0,p:0,e:0}));const ls=document.getElementById('ls'),fl=document.getElementById('fl'),out=document.getElementById('out');
function draw(){ls.innerHTML=T.map((t,i)=>'<li class="'+(st[i].d?'d ':'')+(st[i].p?'s ':'')+(st[i].e?'e':'')+'"><label><input type="checkbox" data-i="'+i+'"'+(st[i].d?' checked':'')+'><span>'+t+'</span></label>'+(st[i].p?'<i class="sp" aria-label="Saving"></i>':'')+'</li>').join('');}
ls.addEventListener('change',e=>{const i=+e.target.dataset.i,s=st[i],prev=s.d;s.d=e.target.checked?1:0;s.p=1;s.e=0;out.textContent='';draw();const fail=fl.checked;fl.checked=false;
setTimeout(()=>{s.p=0;if(fail){s.d=prev;s.e=1;out.textContent='Couldn\u2019t save \u201C'+T[i]+'\u201D. Change reverted.';draw();setTimeout(()=>{s.e=0;draw();},1500);}else draw();},900);});draw();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>Optimistic Update & Rollback — 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}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.ls{list-style:none;display:grid;gap:5px}.ls li{display:flex;align-items:center;gap:10px;border:1px solid var(--line);border-radius:10px;background:var(--surface);padding:8px 12px;font-size:13px}.ls li.s span{opacity:.6}.ls li.e{border-color:#e5484d;animation:sk .3s}.ls li label{display:flex;align-items:center;gap:10px;flex:1;cursor:pointer}.ls li input{width:17px;height:17px}.ls li.d span{text-decoration:line-through;color:var(--muted)}
.sp{width:12px;height:12px;border:2px solid #bbb;border-top-color:#171717;border-radius:50%;animation:rot .7s linear infinite;margin-left:auto}@keyframes rot{to{transform:rotate(360deg)}}@keyframes sk{25%{transform:translateX(-4px)}75%{transform:translateX(4px)}}
.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}.out{min-height:18px;color:#c62f2f}
@media (prefers-reduced-motion:reduce){.ls li.e{animation:none}.sp{animation:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><ul class="ls" id="ls"></ul><label class="sw"><input type="checkbox" id="fl"> Fail the next save</label><p class="out" id="out" role="status"></p></div>
</div>
<script>
const T=['Draft the brief','Review the design','Update the roadmap','Send the invoice'];const st=T.map(()=>({d:0,p:0,e:0}));const ls=document.getElementById('ls'),fl=document.getElementById('fl'),out=document.getElementById('out');
function draw(){ls.innerHTML=T.map((t,i)=>'<li class="'+(st[i].d?'d ':'')+(st[i].p?'s ':'')+(st[i].e?'e':'')+'"><label><input type="checkbox" data-i="'+i+'"'+(st[i].d?' checked':'')+'><span>'+t+'</span></label>'+(st[i].p?'<i class="sp" aria-label="Saving"></i>':'')+'</li>').join('');}
ls.addEventListener('change',e=>{const i=+e.target.dataset.i,s=st[i],prev=s.d;s.d=e.target.checked?1:0;s.p=1;s.e=0;out.textContent='';draw();const fail=fl.checked;fl.checked=false;
setTimeout(()=>{s.p=0;if(fail){s.d=prev;s.e=1;out.textContent='Couldn\u2019t save \u201C'+T[i]+'\u201D. Change reverted.';draw();setTimeout(()=>{s.e=0;draw();},1500);}else draw();},900);});draw();
</script>
</body></html>