Optimistic UI

낙관적 UI

Show the expected success before the server responds, then roll back on failure.

···
html
<div class="scene"><div class="heart">LIKE <b id="count">12</b></div><div class="server" id="server">server: waiting</div><div class="branches"><span id="yes">confirm</span><span id="no">rollback</span></div><div class="label" id="note">tap → show 13 now</div></div>
css
.scene{width:min(94vw,800px);height:min(86vh,320px);position:relative;overflow:hidden;display:flex;flex-direction:column;justify-content:space-between;border:1px solid var(--line);border-radius:14px;background:var(--surface);padding:clamp(9px,2.4vmin,20px);font:500 clamp(15px,4vmin,18px)/1.3 var(--font-sans,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif)}.scene b,.scene strong{font-weight:700}.label{color:var(--muted)}.code{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.pill{border:1px solid var(--line);border-radius:8px;padding:.4em .65em;background:var(--bg)}.active{border-color:var(--accent)!important;color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 10%,var(--surface))!important}.heart{text-align:center;font:700 clamp(28px,11vmin,64px) ui-monospace,monospace;color:var(--accent);margin:clamp(7px,3vmin,20px)}.server{text-align:center;padding:.4em;background:var(--bg);border-radius:6px}.branches{display:flex;justify-content:space-evenly;margin:clamp(12px,4vmin,24px) 0}.branches span{border:1px solid var(--line);border-radius:6px;padding:.3em .6em}.branches .on{border-color:var(--accent);color:var(--accent)}.label{text-align:center}
js
let n=0;function tick(){const s=n%4;document.getElementById("count").textContent=s===0?12:13;document.getElementById("server").textContent=["server: waiting","request in flight","server: accepted","server: rejected"][s];document.getElementById("yes").classList.toggle("on",s===2);document.getElementById("no").classList.toggle("on",s===3);document.getElementById("note").textContent=["tap → show 13 now","optimistic 13","confirmed 13","rollback → 12"][s];if(s===3)document.getElementById("count").textContent=12;n++}tick();setInterval(tick,800)

Optimistic UI merges temporary state into the screen immediately after an action. Success confirms it; failure removes it or shows an error.

The demo increments a reaction immediately, then follows either confirmation or rollback. Design the feedback carefully for hard-to-reverse operations such as payment or inventory.

When to use

Use it for likely-to-succeed, reversible actions such as reactions, checks, and comments.

Open as page ↗