Skip to content

176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.

35.2 KB
Raw Blame
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Swipe to Dismiss — Animation Handbook</title>
  <meta name="description" content="A card dragged sideways flies off and the list closes up. Best for inboxes.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/swipe-to-dismiss/">
  <link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
  <link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
  <link rel="apple-touch-icon" href="../../../apple-touch-icon.png">
  <meta property="og:type" content="article">
  <meta property="og:site_name" content="Animation Handbook">
  <meta property="og:title" content="Swipe to Dismiss — Animation Handbook">
  <meta property="og:description" content="A card dragged sideways flies off and the list closes up. Best for inboxes.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/swipe-to-dismiss/">
  <meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
  <meta name="twitter:card" content="summary_large_image">
  <meta name="twitter:title" content="Swipe to Dismiss — Animation Handbook">
  <meta name="twitter:description" content="A card dragged sideways flies off and the list closes up. Best for inboxes.">
  <meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
  <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Swipe to Dismiss", "name": "Swipe to Dismiss", "description": "A card dragged sideways flies off and the list closes up. Best for inboxes.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/swipe-to-dismiss/", "inLanguage": "en", "articleSection": "Micro-Interactions", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Micro-Interactions, Swipe to Dismiss"}</script>
  <style>
    :root{--ui-accent:#ff9d5c;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-text:#f4f4f2;--ui-muted:#8a8a92;--del:#f4515b}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{display:flex;align-items:center;justify-content:center;padding:clamp(16px,4vw,32px);background:#0b0b0d;overflow:hidden}
    .list{width:100%;max-width:340px;display:flex;flex-direction:column;gap:8px;-webkit-user-select:none;user-select:none}
    /* The row collapses its own height when its card is dismissed: the list closing up is the effect itself, the one place this page animates height */
    .row{position:relative;height:66px;transition:height .28s cubic-bezier(.4,0,.2,1),opacity .2s;overflow:hidden}
    .row.collapsing{height:0!important;opacity:0}
    /* Reveal action sits behind the card */
    .reveal{position:absolute;inset:0;border-radius:10px;background:var(--del);display:flex;align-items:center;justify-content:flex-end;padding:0 20px;font-weight:700;font-size:12px;color:#1a0f08;opacity:0;transition:opacity .12s}
    .reveal.show{opacity:1}
    .reveal.from-left{justify-content:flex-start}
    .reveal.hidden{display:none}
    /* The draggable card */
    .card{position:absolute;inset:0;border-radius:10px;background:var(--ui-bg);border:1px solid var(--ui-border);display:flex;align-items:center;gap:12px;padding:0 16px;cursor:grab;touch-action:pan-y;-webkit-user-select:none;user-select:none;will-change:transform}
    .card.settling{transition:transform .32s cubic-bezier(.22,1,.36,1),opacity .32s}
    .card.flung{transition:transform .3s cubic-bezier(.4,0,1,1),opacity .3s}
    .avatar{width:36px;height:36px;border-radius:50%;flex-shrink:0;display:flex;align-items:center;justify-content:center;font-weight:700;font-size:14px;color:#1a0f08;background:var(--ui-accent)}
    .meta{min-width:0}
    .meta .t{font-size:12px;color:var(--ui-text);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
    .meta .s{font-size:11px;color:var(--ui-muted);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
    .empty{font-size:11px;color:var(--ui-muted);text-align:center}
    .sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
    /* A window this short gives the stage 260 to 310px, less than four rows of 66px need (288px): the rows give up some height and spacing */
    @media(min-width:601px) and (max-height:640px){.stage{padding:8px}.list{gap:6px}.row{height:56px}}
    @media(prefers-reduced-motion:reduce){.row,.card.settling,.card.flung{transition:none!important}}
    /* See inside and Compare: all they add stays hidden until the stage is marked with a mode */
    .stage,.list{position:relative}
    .ins,.lbl,.list.copy,.row.gone,.row.xtra{display:none}
    .lbl{padding:2px 7px;border-radius:5px;background:#111114;color:#f4f4f2;font:600 12px/1.25 var(--hb-font);white-space:nowrap;text-align:center}
    .ins .lbl{display:block;align-self:center;max-width:100%}
    .ins .lbl[hidden]{display:none}
    .lbl.k1{white-space:normal;text-wrap:balance}
    .stage.snap .row,.stage.posed .row{transition:none!important}
    /* See inside: a gauge reads the card that moves, a speed bar has a notch where a flick counts, the first card is see-through over its strip */
    .stage[data-hb-mode="inside"]{--rh:56px;flex-direction:column;justify-content:center;gap:4px;padding:8px clamp(12px,4vw,24px)}
    .stage[data-hb-mode="inside"] .ins{display:flex;flex-direction:column;gap:4px;width:100%;max-width:340px}
    .stage[data-hb-mode="inside"] .lbl.k4{display:block;align-self:center;max-width:100%}
    .stage[data-hb-mode="inside"] .list{gap:6px;min-height:var(--lh)}
    .stage[data-hb-mode="inside"] .row{height:var(--rh)}
    .stage[data-hb-mode="inside"] .row:first-child .reveal:not(.hidden)+.card{background:rgba(17,17,20,.55)}
    .stage[data-hb-mode="inside"] .row:first-child .reveal:not(.hidden)+.card .s{color:#b8b8c0}
    .stage[data-hb-mode="inside"] .row:first-child .reveal{opacity:.8;color:#f4f4f2;padding:0 8px}
    .stage[data-hb-mode="inside"] .row:first-child .reveal.show{opacity:1}
    @media(max-width:340px){.stage[data-hb-mode="inside"] .row:first-child .reveal:not(.show){color:transparent}} /* the strip's word would meet the first message's last words */
    .stage[data-hb-mode="inside"] .list::before{content:'';position:absolute;right:18px;top:calc(var(--rh) + 21px);bottom:8px;width:2px;background:var(--ui-accent);z-index:1}
    .stage[data-hb-mode="inside"] .list::after{content:'';position:absolute;right:12px;top:calc(var(--rh) + 14px);border:7px solid transparent;border-top:0;border-bottom:8px solid var(--ui-accent)}
    .gauge{--k:.47;position:relative;height:22px;border-radius:7px;border:1px solid var(--ui-border);
      background:linear-gradient(90deg,rgba(244,81,91,.42) calc(50% - var(--k)*(50% - 8px)),#1b1b20 0,#1b1b20 calc(50% + var(--k)*(50% - 8px)),rgba(244,81,91,.42) 0)}
    .gauge i{position:absolute;display:block}
    .gauge .lim{top:-3px;bottom:-3px;left:calc(50% - var(--k)*(50% - 8px));border-left:2px dashed #f4f4f2}
    .gauge .lim.r{left:calc(50% + var(--k)*(50% - 8px))}
    .gauge .ring,.gauge .dot{left:50%;top:50%;border-radius:50%}
    .gauge .ring{width:16px;height:16px;margin:-8px 0 0 -8px;border:1.5px solid rgba(255,255,255,.5)}
    .gauge .dot{width:12px;height:12px;margin:-6px 0 0 -6px;background:var(--ui-accent);box-shadow:0 0 0 2px #0b0b0d}
    .gauge .dot.settle{transition:transform .32s cubic-bezier(.22,1,.36,1)}
    .gauge .dot.fling{transition:transform .3s cubic-bezier(.4,0,1,1)}
    .spd{display:flex;align-items:center;gap:8px}
    .meter{position:relative;flex:0 0 34%;height:12px;border-radius:6px;border:1px solid var(--ui-border);background:#1b1b20;overflow:hidden}
    .meter .fill{position:absolute;inset:0;background:#6d6d78;transform:scaleX(0);transform-origin:0 50%;transition:transform .12s}
    .meter .fill.hot{background:var(--ui-accent)}
    .meter .notch{position:absolute;top:0;bottom:0;left:60%;width:2px;margin-left:-1px;background:#f4f4f2}
    .stage.moved .list::before,.stage.moved .list::after{opacity:0;transition:opacity .5s .2s} /* once a row has gone the arrow has nothing to point at */
    @media(prefers-reduced-motion:reduce){.gauge .dot,.meter .fill,.stage.moved .list::before,.stage.moved .list::after{transition:none!important}}
    /* Compare: a copy of the list that drops a message at once (Vanishes) beside the real one (Flies off), posed 0.1 s after letting go */
    .stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:auto 1fr;align-items:start;justify-items:center;
      column-gap:clamp(12px,3vw,28px);row-gap:6px;padding:clamp(8px,2vw,16px)}
    .stage[data-hb-mode="compare"]::before{content:'';position:absolute;left:50%;top:12px;bottom:12px;width:1px;background:var(--ui-border)}
    .stage[data-hb-mode="compare"] .lbl.half{display:block}
    .stage[data-hb-mode="compare"] .list{display:flex;min-height:var(--lh)}
    .stage.posed .list:not(.copy) .row:first-child::after{content:'';position:absolute;inset:0;border:1.5px dashed rgba(255,255,255,.4);border-radius:10px;pointer-events:none}
    @media(max-width:600px){
      .stage[data-hb-mode="compare"]{grid-template-columns:minmax(0,1fr);grid-template-rows:repeat(4,auto);row-gap:4px;padding:6px 12px}
      .stage[data-hb-mode="compare"]::before{left:12px;right:12px;top:50%;bottom:auto;width:auto;height:1px}
      .stage[data-hb-mode="compare"] .h2{order:2}
      .stage[data-hb-mode="compare"] #list{order:3}
      .stage[data-hb-mode="compare"] .row{height:52px}
    }
  </style>
  <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=11">
  <script src="../../../assets/js/demo-page.js?v=11" defer></script>
</head>
<body class="hb" data-hb-kind="do" data-hb-autoplay>
<nav class="hb-bar" aria-label="Animation Handbook">
  <a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>
  <span class="hb-pager"><a href="../error-shake/" rel="prev" aria-label="Previous: Error Shake"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Error Shake</span></a><a href="../hamburger-menu-toggle/" rel="next" aria-label="Next: Hamburger Menu Toggle"><span><span class="hb-dir">Next: </span>Hamburger Menu Toggle</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>
  <a class="hb-star" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener" aria-label="Star on GitHub"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg><span>Star on GitHub</span></a>
</nav>
<main class="hb-page">
  <header class="hb-head">
    <p class="hb-cat">04.22 · Micro-Interactions</p>
    <h1>Swipe to Dismiss</h1>
    <p class="hb-lede">A card dragged sideways flies off and the list closes up. Best for inboxes.</p>
  </header>

  <section class="hb-step hb-watch" aria-labelledby="hb-watch-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">1</span>
      <div><h2 id="hb-watch-title">Drag it</h2><p>Drag a message sideways and let go, or press Show me.</p></div>
    </div>
    <div class="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>
      <button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>
      <button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>
      <button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>
    </div>
    <div class="stage hb-dots">
      <div class="ins" aria-hidden="true">
        <p class="lbl k1" data-hb-caption>Let go past a dashed mark and it flies off; short of it, it springs back</p>
        <div class="gauge"><i class="ring"></i><i class="lim l"></i><i class="lim r"></i><i class="dot"></i></div>
        <div class="spd"><div class="meter"><i class="fill"></i><i class="notch"></i></div><p class="lbl k2" data-hb-caption>Or flick faster than this</p></div>
        <p class="lbl k3" id="k3" data-hb-caption>The Delete strip waits behind the card</p>
      </div>
      <p class="lbl half h1" aria-hidden="true" data-hb-caption>Vanishes</p>
      <p class="lbl half h2" aria-hidden="true" data-hb-caption>Flies off</p>
      <div class="list" id="list"></div>
      <p class="lbl k4" aria-hidden="true" data-hb-caption>Its row closes; the rows below glide up</p>
      <p class="sr-only" id="del-hint">Press Delete to remove this message.</p>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-demo" data-hb-demo><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/></svg>Show me</button>
      <button class="hb-play" type="button" id="btn-reset" data-hb-reset><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></svg>Reset</button>
    </div>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, the dashed marks are the delete distance: let go past one, or flick fast, and the card flies off; short of it, it springs back. Drag a message, or press Show me.</p>
      <p data-hb-for="compare" hidden>Vanishes: the message is gone the moment you let go and the list jumps up; Flies off: it slides away and the list closes up smoothly. Drag a message, or press Show me.</p>
    </div>
  </section>

  <section class="hb-step hb-try" aria-labelledby="hb-try-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">2</span>
      <div><h2 id="hb-try-title">Try it</h2><p>Change a setting, then try it again or press Show me.</p></div>
    </div>
    <div class="hb-settings">
      <div class="hb-setting">
        <p class="hb-setting-name" id="dist-lbl">Distance to delete</p>
        <div class="seg" id="dist-seg" role="group" aria-labelledby="dist-lbl">
          <button type="button" data-t="0.2" aria-pressed="false">Short</button>
          <button type="button" data-t="0.35" class="on" aria-pressed="true">Medium</button>
          <button type="button" data-t="0.5" aria-pressed="false">Long</button>
        </div>
        <p class="hb-hint">How far to drag before letting go deletes it.</p>
      </div>
      <div class="hb-setting">
        <label class="hb-switch-row"><span>Shows a Delete label</span><input class="hb-switch" type="checkbox" role="switch" id="reveal-tog" checked autocomplete="off"></label>
        <p class="hb-hint">A red Delete strip shows behind the card as you drag.</p>
      </div>
    </div>
  </section>

  <section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">3</span>
      <div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>
    </div>
    <div class="hb-card">
      <p class="hb-prompt">Add swipe to dismiss to [the list items people should be able to remove]. While an item is dragged sideways, it follows the finger exactly and fades a little; when the settings include it, a red Delete label shows behind it. On release, remove it if it went far enough or was flicked quickly: it slides off the edge and its row closes up so the list moves up smoothly. Otherwise it springs back into place. Use pointer events so mouse, touch and pen all work, and let up-and-down scrolling still pass through. Offer an undo, and a button for people who cannot drag. If the visitor has reduced motion turned on, remove the row at once without sliding. Match the settings listed below.</p>
      <div class="hb-chips-box" hidden>
        <p class="hb-chips-title">Your settings</p>
        <ul class="hb-chips"></ul>
      </div>
      <div class="hb-copy-row">
        <button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>
        <p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>
      </div>
    </div>
    <div class="hb-star-row"><p>Free and open source. If this helped, a star on GitHub helps other people find it.</p><a class="hb-star-btn" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>Star on GitHub</a></div>
  </section>

  <section class="hb-about" aria-labelledby="hb-what-title">
    <div class="hb-what">
      <h2 id="hb-what-title">What it is</h2>
      <div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>
    </div>
    <div class="hb-fit">
      <h2>Good for</h2>
      <ul class="hb-tags hb-good"><li>Inboxes</li><li>Notifications</li><li>Chat lists</li><li>To-do lists</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Permanent deletes</li><li>Mouse-only tools</li></ul>
    </div>
  </section>

  <section class="hb-related" aria-labelledby="hb-related-title" hidden>
    <h2 id="hb-related-title">Similar animations</h2>
    <div class="hb-rel-list"></div>
  </section>
</main>
<footer class="hb-foot"><span>Animation Handbook</span><a href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener">Source on GitHub</a><a href="#top">Back to top ↑</a></footer>
<script>
  const listEl=document.getElementById('list'),stage=document.querySelector('.stage');
  const resetBtn=document.getElementById('btn-reset'),reduceMq=window.matchMedia('(prefers-reduced-motion:reduce)');
  let threshold=0.35, showReveal=true;
  const FLICK=0.6,FLICK_MIN=24; // letting go commits when the card moves faster than FLICK px per ms, after at least FLICK_MIN px
  let mode='normal',holding=0; // holding: cards held by a pointer right now
  const DATA=[
    ['AV','Ava Reyes','Lunch tomorrow? I found a spot'],
    ['KM','Kai Morgan','Booked the tickets for June'],
    ['NP','Noor Patel','Sent the revised mockups'],
    ['SL','Sol Lindqvist','Are we still on for Friday?']
  ];

  function build(){
    const was=listEl.contains(document.activeElement)&&document.activeElement.dataset.i; // which message has the focus, if the list does
    listEl.innerHTML='';
    DATA.forEach((d,i)=>{
      const row=document.createElement('div');row.className='row';
      row.innerHTML=`<div class="reveal ${showReveal?'':'hidden'}" aria-hidden="true">Delete</div>`+
        `<div class="card" tabindex="0" role="group" data-i="${i}" aria-label="${d[1]}: ${d[2]}" aria-describedby="del-hint"><div class="avatar" aria-hidden="true">${d[0]}</div>`+
        `<div class="meta"><div class="t">${d[1]}</div><div class="s">${d[2]}</div></div></div>`;
      listEl.appendChild(row);
      wire(row);
    });
    if(was)listEl.querySelector(`.card[data-i="${was}"]`).focus({preventScroll:true}); // a rebuild never takes the focus away from its message
    if(mode!=='normal'){if(mode==='compare')syncCopy();else homeGauge('settle',0);layout();}
  }

  // The card follows the pointer 1:1 and fades a little; the red strip shows once it has moved a few pixels, its label on the side the card uncovers.
  // The visitor's drag and Show me's drag both draw each frame with this.
  function draw(card,reveal,x){
    card.style.transform=`translateX(${x}px)`;
    card.style.opacity=String(Math.max(0.3,1-Math.abs(x)/(card.offsetWidth*0.9)));
    reveal.classList.toggle('show',showReveal&&Math.abs(x)>8);
    reveal.classList.toggle('from-left',x>0);
  }
  function follow(card,reveal,x){
    draw(card,reveal,x);
    if(mode==='inside')readGauge(x,card.offsetWidth);
    const t=mode==='compare'&&twins.get(card); // Compare's copy shows the same offset for the same message
    if(t){t.classList.remove('settling','flung');draw(t,t.previousElementSibling,x);} // its spring's transition would trail the drag
  }

  function wire(row){
    const card=row.querySelector('.card');
    const reveal=row.querySelector('.reveal');
    let startX=0,dx=0,dragging=false,pid=0,lastX=0,lastT=0,vel=0;

    card.addEventListener('pointerdown',e=>{
      if(dragging||row.classList.contains('collapsing'))return; // already dismissed
      if(e.pointerType==='mouse'&&e.button!==0)return;
      const off=new DOMMatrix(getComputedStyle(card).transform).m41; // where the card is now: it may be on its way back to rest
      dragging=true;holding++;pid=e.pointerId;dx=off;startX=e.clientX-off;lastX=e.clientX;lastT=performance.now();vel=0;
      card.classList.remove('settling','flung');
      follow(card,reveal,off); // stays where it is
      card.setPointerCapture(e.pointerId);
      card.style.cursor='grabbing';
    });
    card.addEventListener('pointermove',e=>{
      if(!dragging||e.pointerId!==pid)return;
      dx=e.clientX-startX;
      const now=performance.now();
      if(now>lastT){vel=(e.clientX-lastX)/(now-lastT);lastX=e.clientX;lastT=now;} // px per ms
      follow(card,reveal,dx);
    });
    // Letting go decides by distance or flick; a cancelled pointer (the browser took the gesture over for scrolling) always springs back
    function end(e,cancelled){
      if(!dragging||e.pointerId!==pid)return;dragging=false;holding--;card.style.cursor='';
      if(performance.now()-lastT>100)vel=0; // finger stopped before release: no flick
      if(vel&&mode==='inside')holdSpeed(700);
      const w=card.offsetWidth;
      const pastDist=Math.abs(dx)>w*threshold;
      const flick=Math.abs(vel)>FLICK&&Math.abs(dx)>FLICK_MIN&&Math.sign(vel)===Math.sign(dx); // velocity commit, only when the flick points away from rest
      if(!cancelled&&(pastDist||flick))dismiss(row,card,dx>=0?1:-1);else spring(card,reveal);
      poseLater();
    }
    card.addEventListener('pointerup',e=>end(e,false));
    card.addEventListener('pointercancel',e=>end(e,true));
    // The keyboard way: a focused message goes with Delete or Backspace, and focus moves on to the next message (or to Reset)
    card.addEventListener('keydown',e=>{
      if((e.key!=='Delete'&&e.key!=='Backspace')||row.classList.contains('collapsing'))return;
      e.preventDefault();
      const rows=[...listEl.children].filter(r=>!r.classList.contains('collapsing')&&!r.classList.contains('xtra')),i=rows.indexOf(row);
      const next=rows[i+1]||rows[i-1];
      dismiss(row,card,1);
      (next?next.querySelector('.card'):resetBtn).focus({preventScroll:true});
    });
  }

  function spring(card,reveal){
    card.classList.add('settling');
    card.style.transform='translateX(0)';
    card.style.opacity='1';
    reveal.classList.remove('show');
    if(mode==='inside')homeGauge('settle',0);
    const t=mode==='compare'&&twins.get(card);
    if(t)spring(t,t.previousElementSibling);
  }

  function dismiss(row,card,dir){
    if(mode==='inside'){homeGauge('fling',dir);stage.classList.add('moved');}
    if(mode==='compare')dropTwin(card); // Vanishes: the copy's row goes at once
    if(reduceMq.matches){row.remove();checkEmpty();return;} // no fly-off flourish — collapse immediately
    card.classList.add('flung');
    card.style.transform=`translateX(${dir*card.offsetWidth*1.25}px)`;
    card.style.opacity='0';
    row.classList.add('collapsing');
    row.addEventListener('transitionend',ev=>{if(ev.propertyName==='height'){row.remove();checkEmpty();}});
  }

  function checkEmpty(){
    if(!listEl.querySelector('.row'))listEl.innerHTML='<div class="empty">All cleared. Press Reset to bring them back.</div>';
  }

  // Show me: one run at a time. Every timer of the run goes through later(); stopRun() cancels the whole run.
  // later(fn, move, hold) waits move*(slow?3:1) + (hold||0) ms: Slow motion triples the movement, never the hold.
  const slowTog=document.getElementById('slow-tog');      // only on pages with Slow motion
  let runTimers=[],runFrame=0,runDrag=null;               // runDrag: the card the run is dragging right now
  function later(fn,move,hold){
    const slow=!!(slowTog&&slowTog.checked);
    const id=setTimeout(()=>{ runTimers=runTimers.filter(t=>t!==id); fn(); }, move*(slow?3:1)+(hold||0));
    runTimers.push(id);
  }
  function running(){ return runTimers.length>0||runFrame!==0; }
  function stopRun(){
    runTimers.forEach(clearTimeout); runTimers=[];
    cancelAnimationFrame(runFrame); runFrame=0;
    if(runDrag){ spring(runDrag.card,runDrag.reveal); runDrag=null; } // a card still being dragged by the run springs back
  }
  // A run starts with all four messages in place; when one is missing (or still closing up) the list is rebuilt.
  function toRest(){
    if(listEl.querySelectorAll('.row').length!==DATA.length||listEl.querySelector('.collapsing'))build();
  }
  // Show me drags a card the way a pointer would: it draws every frame with the visitor's follow(), then lets go
  function drag(card,reveal,x,ms,letGo){
    const t0=performance.now();
    card.classList.remove('settling','flung');
    runDrag={card,reveal};
    (function frame(now){
      const k=Math.min(1,Math.max(0,(now-t0)/ms));
      follow(card,reveal,x*k*k*(3-2*k));
      if(k<1){runFrame=requestAnimationFrame(frame);return;}
      runFrame=0;runDrag=null;letGo();
    })(t0);
  }
  // The first message is dragged halfway to where letting go deletes it, and let go: it springs back; then dragged past that point and let go: it flies off and its row closes up. Then the list is rebuilt.
  document.getElementById('btn-demo').addEventListener('click',()=>{
    stopRun();
    toRest();
    lift();
    const row=[...listEl.children].find(r=>!r.contains(document.activeElement)),card=row.querySelector('.card'),reveal=row.querySelector('.reveal'); // the first message, unless the visitor is on it
    drag(card,reveal,card.offsetWidth*threshold/2,400,()=>spring(card,reveal));
    later(()=>drag(card,reveal,card.offsetWidth*(threshold+0.15),600,()=>dismiss(row,card,1)),0,1100);
    later(build,0,2600);
  });
  resetBtn.addEventListener('click',()=>{ stopRun(); lift(); build(); });
  // The visitor pressed, typed, scrolled or touched inside the stage: the run stops and the list stays as it is.
  document.addEventListener('hb:input',()=>{ if(running()) stopRun(); lift(); poseLater(); });
  // A Tab press from outside the stage sends no hb:input, but a run never moves the focus, so focus arriving in the stage while one runs is the visitor's.
  stage.addEventListener('focusin',e=>{ if(e.isTrusted&&running()) stopRun(); if(e.target===listEl.querySelector('.card'))lift(); }); // a posed message that gets the focus comes back to rest

  function choices(id,pick){
    const buttons=document.querySelectorAll('#'+id+' button');
    buttons.forEach(b=>b.addEventListener('click',()=>{
      buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
      pick(b.dataset);
    }));
  }
  choices('dist-seg',d=>{threshold=+d.t;layout();});
  document.getElementById('reveal-tog').addEventListener('change',e=>{
    showReveal=e.target.checked;
    stage.querySelectorAll('.reveal').forEach(r=>r.classList.toggle('hidden',!showReveal));
    document.getElementById('k3').hidden=!showReveal;
    layout();
  });

  // ── See inside and Compare ──
  // Compare's left half: an inert copy of the list, made once
  const copyEl=document.createElement('div');
  copyEl.className='list copy';copyEl.inert=true;copyEl.setAttribute('aria-hidden','true');
  copyEl.innerHTML=DATA.map(d=>`<div class="row"><div class="reveal ${showReveal?'':'hidden'}">Delete</div><div class="card"><div class="avatar">${d[0]}</div>`+
    `<div class="meta"><div class="t">${d[1]}</div><div class="s">${d[2]}</div></div></div></div>`).join('')+'<div class="empty" hidden>All cleared. Press Reset to bring them back.</div>';
  listEl.before(copyEl);
  const copyRows=[...copyEl.querySelectorAll('.row')],copyEmpty=copyEl.querySelector('.empty'),twins=new Map(); // twins: a real card → its copy
  const gaugeEl=stage.querySelector('.gauge'),dot=gaugeEl.querySelector('.dot'),fill=stage.querySelector('.fill');
  const noteIn=document.querySelector('[data-hb-for="inside"]'),noteCmp=document.querySelector('[data-hb-for="compare"]');
  const RANGE=0.75,SPEED_FULL=FLICK/0.6; // the gauge shows a card up to RANGE of its width each way; the speed bar is full at SPEED_FULL px per ms
  const POSE_FLY=0.2,POSE_CLOSE=0.5;     // 0.1 s after letting go the card has flown this share of its way and the row has closed this share
  let half=0,gx=0,gw=300,speedX=0,speedT=0,holdT=0,poseT=0;
  const STILL='Shown still, just after letting go: Vanishes has already dropped the message and jumped up; Flies off is still sliding it away and closing the gap.';
  const FREE='With reduced motion, Vanishes and Flies off both drop a message at once. Reset brings the still back.';
  if(reduceMq.matches){
    noteIn.textContent='Inside, the dashed marks are the delete distance: let go past one, or flick fast, and the message goes; short of it, it comes back. Drag a message, or press Show me.';
    noteCmp.textContent=STILL;
  }
  function note(posed){ if(reduceMq.matches&&mode==='compare'&&noteCmp.textContent!==(posed?STILL:FREE))noteCmp.textContent=posed?STILL:FREE; }

  function placeDot(){ dot.style.transform=`translateX(${(Math.max(-1,Math.min(1,gx/(RANGE*gw)))*half).toFixed(1)}px)`; }
  function setSpeed(v){ fill.style.transform=`scaleX(${Math.min(1,v/SPEED_FULL).toFixed(3)})`;fill.classList.toggle('hot',v>FLICK); }
  function holdSpeed(ms){ clearTimeout(holdT);holdT=setTimeout(()=>setSpeed(0),ms); }
  // Every follow() call: the marker goes where the card is, and the speed bar reads how far it moved since the call before
  function readGauge(x,w){
    gx=x;gw=w;dot.classList.remove('settle','fling');placeDot();
    const now=performance.now(),dt=now-speedT;
    if(dt>0){ setSpeed(dt<100?Math.abs(x-speedX)/dt:0);speedX=x;speedT=now; }
    holdSpeed(holding?100:700); // a stopped finger reads zero, as its release does; after letting go the reading is held a moment
  }
  // The card springs back (dir 0) or flies off (dir 1 or -1): the marker does the same
  function homeGauge(kind,dir){
    gx=dir*9e3;dot.classList.remove('settle','fling');dot.classList.add(kind);placeDot();
    if(dir)setTimeout(()=>{ if(mode==='inside'&&Math.abs(gx)>=9e3)homeGauge('settle',0); },600);
  }

  function dropTwin(card){
    const t=twins.get(card);
    if(t){ t.parentElement.classList.add('gone');copyEmpty.hidden=copyRows.some(r=>!r.classList.contains('gone')); }
  }
  // Draws the copy as the real list is now (on entering Compare and after a rebuild)
  function syncCopy(){
    twins.clear();
    copyRows.forEach((cr,i)=>{
      const card=listEl.querySelector(`.card[data-i="${i}"]`),c=cr.querySelector('.card');
      cr.classList.toggle('gone',!card||card.parentElement.classList.contains('collapsing'));
      c.className=card?card.className:'card';c.style.cssText=card?card.style.cssText:'';
      cr.querySelector('.reveal').className=card?card.previousElementSibling.className:'reveal';
      if(card)twins.set(card,c);
    });
    copyEmpty.hidden=copyRows.some(r=>!r.classList.contains('gone'));
  }
  // A mode shows as many messages as fit under what it adds (never fewer than two); the others stay hidden in both halves
  function fit(){
    const modal=mode!=='normal',cs=getComputedStyle(stage);
    let n=DATA.length;
    if(modal){
      const pad=parseFloat(cs.paddingTop)+parseFloat(cs.paddingBottom),gap=parseFloat(cs.rowGap)||0,rg=parseFloat(getComputedStyle(listEl).rowGap)||0;
      const rh=parseFloat(getComputedStyle(copyRows[0]).height),nameH=stage.querySelector('.h1').offsetHeight;
      const room=mode==='inside'?stage.clientHeight-pad-stage.querySelector('.ins').offsetHeight-stage.querySelector('.k4').offsetHeight-2*gap
        :cs.gridTemplateColumns.split(' ').length===1?(stage.clientHeight-pad-2*nameH-3*gap)/2:stage.clientHeight-pad-nameH-gap;
      n=Math.max(2,Math.min(DATA.length,Math.floor((room+rg)/(rh+rg))));
      stage.style.setProperty('--lh',(n*rh+(n-1)*rg)+'px');
    }
    listEl.querySelectorAll('.card').forEach(c=>c.parentElement.classList.toggle('xtra',modal&&+c.dataset.i>=n));
    copyRows.forEach((r,i)=>r.classList.toggle('xtra',modal&&i>=n));
  }

  // Compare's rest picture: whole lists, nothing moving, no run pending. Returns whether it stands.
  function pose(){
    if(mode!=='compare'||holding||running()||listEl.querySelectorAll('.row').length!==DATA.length||listEl.querySelector('.collapsing')||listEl.querySelector('.card').matches(':focus-visible'))return stage.classList.contains('posed');
    const row=listEl.querySelector('.row'),card=row.querySelector('.card'),w=card.offsetWidth,x0=(threshold+0.15)*w;
    card.classList.remove('settling','flung'); // a still, dealt at once: a spring's transition would slide the card into the pose
    stage.classList.add('posed');
    row.style.height=Math.round(parseFloat(getComputedStyle(copyRows[0]).height)*(1-POSE_CLOSE))+'px';
    draw(card,row.querySelector('.reveal'),x0+POSE_FLY*(1.25*w-x0));
    card.style.opacity='0.5';
    copyRows[0].classList.add('gone');
    return true;
  }
  // Any input, Show me and Reset take the pose away before a card reads where it is
  function lift(){
    if(!stage.classList.contains('posed'))return;
    const row=listEl.querySelector('.row'),card=row.querySelector('.card');
    card.classList.remove('settling','flung'); // back to rest at once, so a press reads the card there
    row.style.height='';card.style.transform='';card.style.opacity='';row.querySelector('.reveal').classList.remove('show','from-left');
    copyRows[0].classList.remove('gone');
    void row.offsetHeight; // the rows' transition is still off: the pose goes at once
    stage.classList.remove('posed');
    note(false);
  }
  function poseLater(){ if(mode==='compare'){ clearTimeout(poseT);poseT=setTimeout(()=>note(pose()),420); } }
  function layout(){
    fit();
    if(mode==='inside'){
      half=gaugeEl.clientWidth/2-8;gw=listEl.querySelector('.card')?listEl.querySelector('.card').offsetWidth:gw;
      gaugeEl.style.setProperty('--k',(threshold/RANGE).toFixed(3));placeDot();
      stage.classList.toggle('moved',listEl.querySelectorAll('.row').length<DATA.length);
    }
    if(mode==='compare')note(pose());
  }
  document.addEventListener('hb:mode',e=>{
    mode=e.detail.mode;
    stage.classList.add('snap'); // the rows change height with the mode at once, not over a third of a second
    lift();
    if(mode==='compare')syncCopy();
    layout();
    stage.classList.remove('snap');
  });
  addEventListener('resize',()=>{ if(mode!=='normal')layout(); });
  if(document.fonts)document.fonts.ready.then(()=>{ if(mode!=='normal')layout(); });

  build();
</script>
</body>
</html>