Swipe to Delete
A list row that slides away when dragged past a threshold, and springs back if released early.
AI prompt
Create a list of four message rows (56px tall, 14px text) inside a 300px wide card. Each row is a foreground element on top of a background element containing the word "Delete" on the right. Use pointer events on the foreground with touch-action pan-y: while dragging left, translate the foreground by the horizontal delta (never right of 0) and fade in the background label proportionally. If on release the drag exceeds 40% of the row width, animate the row off to the left and collapse its height to 0 over 220ms, then remove it from the DOM; otherwise return it to 0 with a 260ms cubic-bezier(.2,.7,.2,1) transition. Rows are focusable (tabindex 0); pressing Delete or Backspace removes the focused row with the same animation. Add a Reset button that restores the list. Under prefers-reduced-motion remove animations.About this pattern
Definition
A list row that follows the finger or pointer horizontally. Dragging it past a threshold removes it; releasing earlier returns it to its place.
Purpose & UX principles
- A fast, direct way to remove items from a list on touch devices.
- Because gestures are hidden, a keyboard alternative is required: focus the row and press Delete.
- A red label behind the row reveals what the gesture will do while dragging.
Interaction states
- Rest, dragging (row follows pointer, label fades in), past threshold (label turns solid), removed, focus-visible ring.
Motion specification
- Following the pointer has no transition; release uses 260ms cubic-bezier(.2,.7,.2,1).
- Removal slides the row off and collapses its height in 220ms.
- Reduced motion: removal is instant and the row snaps back without animation.
Responsive behaviour
- The row fills the list width; the threshold is 40% of the row width.
- Vertical page scrolling stays possible because the row only captures horizontal movement (
touch-action: pan-y).
Code
<div class="box">
<ul class="list" id="list"></ul>
<button class="reset" id="reset" type="button">Reset</button>
</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{width:min(300px,100%);display:grid;gap:10px;justify-items:center}
.list{list-style:none;width:100%;background:var(--surface);border:1px solid var(--line);border-radius:14px;overflow:hidden}
.li{position:relative;height:56px;overflow:hidden;border-top:1px solid var(--line);transition:height .22s,opacity .22s}
.li:first-child{border-top:0}
.bg{position:absolute;inset:0;background:var(--danger);color:#fff;display:flex;align-items:center;justify-content:flex-end;padding-right:18px;font-size:13px;font-weight:600;opacity:0}
.fg{position:relative;height:100%;display:flex;align-items:center;padding:0 16px;background:var(--surface);font-size:14px;touch-action:pan-y;cursor:grab;user-select:none}
.fg.back{transition:transform .26s cubic-bezier(.2,.7,.2,1)}
.li:focus-visible{outline:2px solid var(--accent);outline-offset:-2px}
.li.gone{height:0;opacity:0;border-width:0}
.reset{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12.5px;cursor:pointer}
@media (prefers-reduced-motion:reduce){.fg.back,.li{transition:none}}const names=['Welcome to the team','Your invoice is ready','Weekly digest','Password changed'];
const list=document.getElementById('list');
function remove(li){li.classList.add('gone');setTimeout(()=>li.remove(),230);}
function build(){list.innerHTML='';names.forEach(n=>{
const li=document.createElement('li');li.className='li';li.tabIndex=0;li.innerHTML='<div class="bg">Delete</div><div class="fg"></div>';li.querySelector('.fg').textContent=n;list.append(li);
const fg=li.querySelector('.fg'),bg=li.querySelector('.bg');let sx=0,dx=0,on=false;
fg.addEventListener('pointerdown',e=>{on=true;sx=e.clientX;dx=0;fg.classList.remove('back');fg.setPointerCapture(e.pointerId);});
fg.addEventListener('pointermove',e=>{if(!on)return;dx=Math.min(0,e.clientX-sx);fg.style.transform='translateX('+dx+'px)';bg.style.opacity=Math.min(1,-dx/(li.offsetWidth*.4));});
const end=()=>{if(!on)return;on=false;if(-dx>li.offsetWidth*.4){fg.classList.add('back');fg.style.transform='translateX(-110%)';remove(li);}else{fg.classList.add('back');fg.style.transform='';bg.style.opacity=0;}};
fg.addEventListener('pointerup',end);fg.addEventListener('pointercancel',end);
li.addEventListener('keydown',e=>{if(e.key==='Delete'||e.key==='Backspace'){e.preventDefault();remove(li);}});
});}
build();document.getElementById('reset').addEventListener('click',build);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>Swipe to Delete — 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{width:min(300px,100%);display:grid;gap:10px;justify-items:center}
.list{list-style:none;width:100%;background:var(--surface);border:1px solid var(--line);border-radius:14px;overflow:hidden}
.li{position:relative;height:56px;overflow:hidden;border-top:1px solid var(--line);transition:height .22s,opacity .22s}
.li:first-child{border-top:0}
.bg{position:absolute;inset:0;background:var(--danger);color:#fff;display:flex;align-items:center;justify-content:flex-end;padding-right:18px;font-size:13px;font-weight:600;opacity:0}
.fg{position:relative;height:100%;display:flex;align-items:center;padding:0 16px;background:var(--surface);font-size:14px;touch-action:pan-y;cursor:grab;user-select:none}
.fg.back{transition:transform .26s cubic-bezier(.2,.7,.2,1)}
.li:focus-visible{outline:2px solid var(--accent);outline-offset:-2px}
.li.gone{height:0;opacity:0;border-width:0}
.reset{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12.5px;cursor:pointer}
@media (prefers-reduced-motion:reduce){.fg.back,.li{transition:none}}
</style></head>
<body><div class="stage">
<div class="box">
<ul class="list" id="list"></ul>
<button class="reset" id="reset" type="button">Reset</button>
</div>
</div>
<script>
const names=['Welcome to the team','Your invoice is ready','Weekly digest','Password changed'];
const list=document.getElementById('list');
function remove(li){li.classList.add('gone');setTimeout(()=>li.remove(),230);}
function build(){list.innerHTML='';names.forEach(n=>{
const li=document.createElement('li');li.className='li';li.tabIndex=0;li.innerHTML='<div class="bg">Delete</div><div class="fg"></div>';li.querySelector('.fg').textContent=n;list.append(li);
const fg=li.querySelector('.fg'),bg=li.querySelector('.bg');let sx=0,dx=0,on=false;
fg.addEventListener('pointerdown',e=>{on=true;sx=e.clientX;dx=0;fg.classList.remove('back');fg.setPointerCapture(e.pointerId);});
fg.addEventListener('pointermove',e=>{if(!on)return;dx=Math.min(0,e.clientX-sx);fg.style.transform='translateX('+dx+'px)';bg.style.opacity=Math.min(1,-dx/(li.offsetWidth*.4));});
const end=()=>{if(!on)return;on=false;if(-dx>li.offsetWidth*.4){fg.classList.add('back');fg.style.transform='translateX(-110%)';remove(li);}else{fg.classList.add('back');fg.style.transform='';bg.style.opacity=0;}};
fg.addEventListener('pointerup',end);fg.addEventListener('pointercancel',end);
li.addEventListener('keydown',e=>{if(e.key==='Delete'||e.key==='Backspace'){e.preventDefault();remove(li);}});
});}
build();document.getElementById('reset').addEventListener('click',build);
</script>
</body></html>