Snackbar
A single bottom message with one action, typically Undo, that replaces the previous one.
AI prompt
Build a list of three email rows with a delete icon on each. Deleting a row removes it from the list with a 180ms height collapse and shows a snackbar at the bottom of the demo area reading "Conversation archived" with an "Undo" button and a close icon. The snackbar slides up over 240ms with cubic-bezier(0.2,0.8,0.2,1), stays 6 seconds, pauses while hovered or focused, and then disappears; the deletion stands. Pressing Undo (also reachable by keyboard) restores the row at its original position. If another row is deleted while a snackbar is visible, finalise the previous deletion and replace the bar. Use role="status" on the bar. Under prefers-reduced-motion use opacity only.About this pattern
Definition
A narrow bar that slides up from the bottom to report an action and offer one response, most often Undo. Only one is shown at a time; a new one replaces the current one.
Purpose & UX principles
- Makes destructive actions reversible without a confirmation dialog, which is faster and less annoying.
- The action must stay available long enough to be read and used: at least 5 seconds, longer for longer text.
- Pause the timer while the user hovers or focuses it.
Interaction states
- Hidden, entering, visible, action pressed, dismissed.
- Pressing Undo restores the item and closes the bar; ignoring it lets the deletion stand.
- Announced politely through
role="status".
Motion specification
- Enter: translateY(24px) to 0 with opacity, 240ms cubic-bezier(0.2, 0.8, 0.2, 1).
- Exit: 180ms ease-in.
- Replacement: the old bar leaves before the new one enters.
- Reduced motion: fade only.
Responsive behaviour
- Phones: spans the width with 12px margins above any bottom navigation.
- Desktop: centred, maximum width 480px.
Code
<div class="box"><ul id="list">
<li data-id="1"><span>Welcome to the team</span><button class="del" aria-label="Archive Welcome to the team">🗑</button></li>
<li data-id="2"><span>Your invoice is ready</span><button class="del" aria-label="Archive Your invoice is ready">🗑</button></li>
<li data-id="3"><span>Weekly digest</span><button class="del" aria-label="Archive Weekly digest">🗑</button></li>
</ul>
<div class="snack" id="snack" role="status" hidden><span>Conversation archived</span><button id="undo">Undo</button><button id="cls" aria-label="Dismiss">×</button></div></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}
.box{position:relative;width:min(380px,100%);height:340px;border:1px solid var(--line);border-radius:18px;background:var(--surface);overflow:hidden;padding:16px}
ul{list-style:none;display:grid;gap:8px}
li{display:flex;align-items:center;justify-content:space-between;padding:12px 14px;border-radius:12px;background:var(--surface-2);font-size:14px;transition:opacity .18s,transform .18s}
li.gone{opacity:0;transform:translateX(16px)}
.del{background:none;border:0;cursor:pointer;font-size:16px;padding:4px 8px;border-radius:8px}
.del:hover{background:rgba(0,0,0,.06)}
.snack{position:absolute;left:12px;right:12px;bottom:12px;display:flex;align-items:center;gap:8px;padding:10px 12px 10px 16px;border-radius:12px;background:var(--text);color:var(--bg);font-size:14px;animation:up .24s cubic-bezier(.2,.8,.2,1)}
.snack.out{opacity:0;transform:translateY(12px);transition:opacity .18s ease-in,transform .18s ease-in}
.snack span{flex:1}
.snack button{background:none;border:0;color:#171717;font:inherit;font-weight:700;cursor:pointer;padding:6px 10px;border-radius:8px}
.snack #cls{color:var(--bg);font-size:18px;padding:2px 8px}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@keyframes up{from{opacity:0;transform:translateY(24px)}}
@media (prefers-reduced-motion:reduce){.snack{animation:none}li,li.gone,.snack.out{transform:none}}const list=document.getElementById('list'),snack=document.getElementById('snack');
let pending=null,timer,left=6000,start;
function hide(){clearTimeout(timer);snack.classList.add('out');setTimeout(()=>{snack.hidden=true;snack.classList.remove('out');},180);pending=null;}
function finalise(){if(pending){pending.li.remove();pending=null;}}
function begin(){start=Date.now();timer=setTimeout(hide,left);}
list.addEventListener('click',e=>{const b=e.target.closest('.del');if(!b)return;
finalise();const li=b.closest('li');li.classList.add('gone');
pending={li,next:li.nextElementSibling};
setTimeout(()=>{if(pending&&pending.li===li)li.hidden=true;},180);
snack.hidden=false;snack.style.animation='none';void snack.offsetWidth;snack.style.animation='';
clearTimeout(timer);left=6000;begin();});
document.getElementById('undo').onclick=()=>{if(pending){pending.li.hidden=false;void pending.li.offsetWidth;pending.li.classList.remove('gone');}hide();};
document.getElementById('cls').onclick=()=>{finalise();hide();};
snack.addEventListener('mouseenter',()=>{clearTimeout(timer);left-=Date.now()-start;});
snack.addEventListener('mouseleave',begin);snack.addEventListener('focusin',()=>{clearTimeout(timer);left-=Date.now()-start;});snack.addEventListener('focusout',begin);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>Snackbar — 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}}
.box{position:relative;width:min(380px,100%);height:340px;border:1px solid var(--line);border-radius:18px;background:var(--surface);overflow:hidden;padding:16px}
ul{list-style:none;display:grid;gap:8px}
li{display:flex;align-items:center;justify-content:space-between;padding:12px 14px;border-radius:12px;background:var(--surface-2);font-size:14px;transition:opacity .18s,transform .18s}
li.gone{opacity:0;transform:translateX(16px)}
.del{background:none;border:0;cursor:pointer;font-size:16px;padding:4px 8px;border-radius:8px}
.del:hover{background:rgba(0,0,0,.06)}
.snack{position:absolute;left:12px;right:12px;bottom:12px;display:flex;align-items:center;gap:8px;padding:10px 12px 10px 16px;border-radius:12px;background:var(--text);color:var(--bg);font-size:14px;animation:up .24s cubic-bezier(.2,.8,.2,1)}
.snack.out{opacity:0;transform:translateY(12px);transition:opacity .18s ease-in,transform .18s ease-in}
.snack span{flex:1}
.snack button{background:none;border:0;color:#171717;font:inherit;font-weight:700;cursor:pointer;padding:6px 10px;border-radius:8px}
.snack #cls{color:var(--bg);font-size:18px;padding:2px 8px}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@keyframes up{from{opacity:0;transform:translateY(24px)}}
@media (prefers-reduced-motion:reduce){.snack{animation:none}li,li.gone,.snack.out{transform:none}}
</style></head>
<body><div class="stage">
<div class="box"><ul id="list">
<li data-id="1"><span>Welcome to the team</span><button class="del" aria-label="Archive Welcome to the team">🗑</button></li>
<li data-id="2"><span>Your invoice is ready</span><button class="del" aria-label="Archive Your invoice is ready">🗑</button></li>
<li data-id="3"><span>Weekly digest</span><button class="del" aria-label="Archive Weekly digest">🗑</button></li>
</ul>
<div class="snack" id="snack" role="status" hidden><span>Conversation archived</span><button id="undo">Undo</button><button id="cls" aria-label="Dismiss">×</button></div></div>
</div>
<script>
const list=document.getElementById('list'),snack=document.getElementById('snack');
let pending=null,timer,left=6000,start;
function hide(){clearTimeout(timer);snack.classList.add('out');setTimeout(()=>{snack.hidden=true;snack.classList.remove('out');},180);pending=null;}
function finalise(){if(pending){pending.li.remove();pending=null;}}
function begin(){start=Date.now();timer=setTimeout(hide,left);}
list.addEventListener('click',e=>{const b=e.target.closest('.del');if(!b)return;
finalise();const li=b.closest('li');li.classList.add('gone');
pending={li,next:li.nextElementSibling};
setTimeout(()=>{if(pending&&pending.li===li)li.hidden=true;},180);
snack.hidden=false;snack.style.animation='none';void snack.offsetWidth;snack.style.animation='';
clearTimeout(timer);left=6000;begin();});
document.getElementById('undo').onclick=()=>{if(pending){pending.li.hidden=false;void pending.li.offsetWidth;pending.li.classList.remove('gone');}hide();};
document.getElementById('cls').onclick=()=>{finalise();hide();};
snack.addEventListener('mouseenter',()=>{clearTimeout(timer);left-=Date.now()-start;});
snack.addEventListener('mouseleave',begin);snack.addEventListener('focusin',()=>{clearTimeout(timer);left-=Date.now()-start;});snack.addEventListener('focusout',begin);
</script>
</body></html>