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.

36.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>Drawer / Panel Slide — Animation Handbook</title>
  <meta name="description" content="A side panel slides in over a dimmed page. Best for mobile menus.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/drawer-slide/">
  <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="Drawer / Panel Slide — Animation Handbook">
  <meta property="og:description" content="A side panel slides in over a dimmed page. Best for mobile menus.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/drawer-slide/">
  <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="Drawer / Panel Slide — Animation Handbook">
  <meta name="twitter:description" content="A side panel slides in over a dimmed page. Best for mobile menus.">
  <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": "Drawer / Panel Slide", "name": "Drawer / Panel Slide", "description": "A side panel slides in over a dimmed page. Best for mobile menus.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/drawer-slide/", "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, Drawer / Panel Slide"}</script>
  <style>
    :root{--ui-accent:#ff9d5c;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-text:#f4f4f2;--ui-muted:#8a8a92;--drawer-w:min(280px,80%);--open-dur:280ms;--close-dur:220ms;--open-ease:ease-out;--close-ease:ease-in;--backdrop-op:0.5}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{position:relative;overflow:hidden;display:flex;flex-direction:column;background:#0b0b0d}
    /* App header */
    .app-header{display:flex;align-items:center;gap:12px;padding:14px 16px;border-bottom:1px solid var(--ui-border);background:var(--ui-bg);flex-shrink:0}
    .hamburger{background:none;border:1px solid var(--ui-border);color:var(--ui-text);font-family:inherit;font-size:16px;width:44px;height:44px;border-radius:6px;cursor:pointer;display:flex;align-items:center;justify-content:center;transition:border-color .15s;flex-shrink:0}
    @media(hover:hover){.hamburger:hover{border-color:var(--ui-accent)}}
    .app-title{font-size:13px;font-weight:bold;letter-spacing:1px}
    /* App body */
    .app-body{flex:1;padding:24px 16px;display:flex;flex-direction:column;gap:12px}
    .content-line{height:10px;background:rgba(255,255,255,.06);border-radius:4px}
    .content-line.w100{width:100%}.content-line.w80{width:80%}.content-line.w60{width:60%}.content-line.w40{width:40%}
    /* Backdrop */
    .backdrop{position:absolute;inset:0;background:rgba(0,0,0,var(--backdrop-op));opacity:0;transition:opacity var(--close-dur) var(--close-ease);pointer-events:none;z-index:5}
    .backdrop.visible{opacity:1;pointer-events:auto;transition:opacity var(--open-dur) var(--open-ease)}
    /* Drawer. No text selection: a mouse swipe would select the list's words, and the next swipe would become a native text drag and fail. */
    .drawer{position:absolute;top:0;left:0;bottom:0;width:var(--drawer-w);background:var(--ui-bg);border-right:1px solid var(--ui-border);z-index:10;display:flex;flex-direction:column;transform:translateX(-100%);transition:transform var(--close-dur) var(--close-ease);will-change:transform;overflow:hidden;touch-action:pan-y;-webkit-user-select:none;user-select:none}
    .drawer.right{left:auto;right:0;border-right:none;border-left:1px solid var(--ui-border);transform:translateX(100%)}
    .drawer.top{width:auto;left:0;right:0;top:0;bottom:auto;height:200px;transform:translateY(-100%);touch-action:pan-x}
    .drawer.bottom{width:auto;left:0;right:0;top:auto;bottom:0;height:200px;transform:translateY(100%);touch-action:pan-x}
    .drawer.open{transform:translate(0,0);transition:transform var(--open-dur) var(--open-ease)}
    .drawer-header{display:flex;align-items:center;justify-content:space-between;padding:16px;border-bottom:1px solid var(--ui-border);flex-shrink:0}
    .drawer-title{font-size:12px;font-weight:bold;text-transform:uppercase;letter-spacing:1px}
    .close-btn{background:none;border:none;color:var(--ui-muted);font-family:inherit;font-size:18px;cursor:pointer;width:44px;height:44px;display:flex;align-items:center;justify-content:center;border-radius:4px}
    @media(hover:hover){.close-btn:hover{color:var(--ui-text)}}
    .drawer-body{padding:12px;display:flex;flex-direction:column;gap:4px;flex:1;overflow-y:auto;touch-action:pan-y}
    .nav-item{padding:10px 12px;border-radius:6px;font-size:12px;cursor:pointer;transition:background .15s;min-height:44px;display:flex;align-items:center}
    @media(hover:hover){.nav-item:hover{background:rgba(255,255,255,.05)}}
    .nav-item.active{color:var(--ui-accent)}
    /* A window this short gives the stage 260px, less than the four menu items need: the drawer gives up some spacing, the items keep their size */
    @media(min-width:601px) and (max-height:620px){.drawer-header{padding:8px 16px}.drawer-body{padding:8px 12px;gap:0}}
    @media(prefers-reduced-motion:reduce){.drawer,.backdrop,.arrow{transition:none!important}}
    /* The app sits in a box that fills the stage; See inside and Compare lay the box out their own way */
    .box{position:relative;display:flex;flex-direction:column;flex:1;min-height:0;min-width:0;overflow:hidden}
    .cap,.half{padding:2px 6px;border-radius:5px;border:1px solid rgba(255,255,255,.14);background:#111114;color:var(--ui-text);font:600 12px/1.25 var(--hb-font);white-space:nowrap}
    .cap{display:flex;align-items:center;gap:5px}
    .cap .more{font-style:normal}
    @media(max-width:480px){.cap .more{display:none}}
    .cap b{font-weight:600;color:#c9c9cf}
    .twin,.park,.arrow,.keys,.half{display:none}
    .twin .drawer,.twin .backdrop{transition:none}
    .ico{flex:none;width:16px;height:12px}
    .ico-park{border:2px dashed var(--ui-accent);border-radius:2px}
    .ico-dim{border-radius:3px;border:1px solid rgba(255,255,255,.4);background:linear-gradient(rgba(0,0,0,var(--backdrop-op)),rgba(0,0,0,var(--backdrop-op))),#e8e8ea}
    .ico-in,.ico-out{background:var(--ui-accent);clip-path:polygon(0 35%,68% 35%,68% 0,100% 50%,68% 100%,68% 65%,0 65%)}
    .arrow{background:var(--ui-accent);clip-path:polygon(0 35%,calc(100% - 14px) 35%,calc(100% - 14px) 0,100% 50%,calc(100% - 14px) 100%,calc(100% - 14px) 65%,0 65%)}
    .ico-in{transform:rotate(calc(var(--r)*1deg))}
    .ico-out{background:#f4f4f2;transform:rotate(calc((var(--r) + 180)*1deg))}
    /* Where each arrow sits and points, for each edge the drawer comes in from: centered on the screen's edge and as long as the
       slide (--mv, measured), on a side edge between the menu items */
    .stage[data-side="left"]{--r:0;--ax:calc(var(--mv)/-2);--ay:min(109px,calc(100% - 70px));--bx:calc(var(--mv)/-2);--by:min(153px,calc(100% - 14px))}
    .stage[data-side="right"]{--r:180;--ax:calc(100% - var(--mv)/2);--ay:min(109px,calc(100% - 70px));--bx:calc(100% - var(--mv)/2);--by:min(153px,calc(100% - 14px))}
    .stage[data-side="top"]{--r:90;--ax:calc(62% - var(--mv)/2);--ay:-6px;--bx:calc(82% - var(--mv)/2);--by:-6px}
    .stage[data-side="bottom"]{--r:-90;--ax:calc(62% - var(--mv)/2);--ay:calc(100% - 6px);--bx:calc(82% - var(--mv)/2);--by:calc(100% - 6px)}
    /* See inside: the screen is laid out smaller on the side away from the drawer and stops clipping, so the closed drawer shows in
       its parked place beyond the edge. A dashed outline stays there when it opens; the key below names each part. */
    .stage[data-hb-mode="inside"]{display:grid;grid-template-rows:minmax(0,1fr) auto;gap:6px;padding:6px;--drawer-w:min(280px,76%);--dh:62%}
    .stage[data-hb-mode="inside"] .box{overflow:visible;width:55%;outline:2px solid rgba(244,244,242,.4)}
    .stage[data-hb-mode="inside"][data-side="left"] .box{justify-self:end}
    .stage[data-hb-mode="inside"][data-side="right"] .box{justify-self:start}
    .stage[data-hb-mode="inside"][data-side="top"] .box,.stage[data-hb-mode="inside"][data-side="bottom"] .box{width:auto;height:60%}
    .stage[data-hb-mode="inside"][data-side="top"] .box{align-self:end}
    .stage[data-hb-mode="inside"][data-side="bottom"] .box{align-self:start}
    .stage[data-hb-mode="inside"] .app-header,.stage[data-hb-mode="inside"] .drawer-header{padding-block:6px}
    .stage[data-hb-mode="inside"] .app-body{overflow:hidden;min-height:0}
    .stage[data-hb-mode="inside"] .content-line{flex-shrink:0}
    .stage[data-hb-mode="inside"] .drawer.top,.stage[data-hb-mode="inside"] .drawer.bottom{height:var(--dh)}
    .stage[data-hb-mode="inside"] .drawer{opacity:.35;transition:transform var(--close-dur) var(--close-ease),opacity var(--close-dur) var(--close-ease)}
    .stage[data-hb-mode="inside"] .drawer.open{opacity:1;transition:transform var(--open-dur) var(--open-ease),opacity var(--open-dur) var(--open-ease)}
    .stage[data-hb-mode="inside"] .park{display:block;position:absolute;z-index:12;pointer-events:none;border:0 dashed var(--ui-accent);top:0;bottom:0;left:0;width:var(--drawer-w);border-width:2px 0 2px 2px;transform:translateX(-100%)}
    .stage[data-hb-mode="inside"][data-side="right"] .park{left:auto;right:0;border-width:2px 2px 2px 0;transform:translateX(100%)}
    .stage[data-hb-mode="inside"][data-side="top"] .park{bottom:auto;right:0;width:auto;height:var(--dh);border-width:2px 2px 0;transform:translateY(-100%)}
    .stage[data-hb-mode="inside"][data-side="bottom"] .park{top:auto;bottom:0;right:0;width:auto;height:var(--dh);border-width:0 2px 2px;transform:translateY(100%)}
    .stage[data-hb-mode="inside"] .arrow{display:block;position:absolute;z-index:13;pointer-events:none;width:var(--mv);height:12px;left:var(--ax);top:var(--ay);transform:rotate(calc(var(--r)*1deg));transition:opacity .15s var(--close-dur)}
    .stage[data-hb-mode="inside"] .arrow.out{left:var(--bx);top:var(--by);background:#f4f4f2;transform:rotate(calc((var(--r) + 180)*1deg))}
    .stage[data-hb-mode="inside"].shown .arrow{opacity:0;transition-delay:0s}
    .stage[data-hb-mode="inside"] .keys{display:flex;flex-direction:column;align-items:center;gap:4px}
    .kr{display:flex;flex-wrap:wrap;justify-content:center;gap:4px 6px}
    .stage[data-hb-mode="inside"] .app-header{padding-inline:12px;gap:8px}
    .stage[data-hb-mode="inside"] .app-title{white-space:nowrap}
    @media(max-width:360px){.stage[data-hb-mode="inside"]{padding:4px;gap:4px}}
    @media(min-width:761px){.stage[data-hb-mode="inside"] .box{width:58%}.stage[data-hb-mode="inside"] .keys{flex-direction:row;flex-wrap:wrap;justify-content:center;gap:4px 6px}}
    /* Compare: on the left a copy whose menu is simply there, on the right the real app. At rest the real drawer and sheet are caught
       part way in, as far as the page's own opening curve takes them in a tenth of a second, with an arrow in front of the drawer. */
    .stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:2px;background:#2a2a30}
    .stage[data-hb-mode="compare"] .twin{display:flex}
    .stage[data-hb-mode="compare"] .box{background:#0b0b0d}
    .stage[data-hb-mode="compare"] .app-header{padding:6px 8px;gap:8px}
    .stage[data-hb-mode="compare"] .app-title{font-size:12px;letter-spacing:.3px;white-space:nowrap}
    .stage[data-hb-mode="compare"] .app-body{padding:16px 8px}
    .stage[data-hb-mode="compare"] .drawer-header{padding:8px}
    .stage[data-hb-mode="compare"] .drawer-body{padding:8px;gap:0}
    .stage[data-hb-mode="compare"] .half{display:block;position:absolute;z-index:11;bottom:8px;right:8px;pointer-events:none}
    .stage[data-hb-mode="compare"][data-side="right"] .half,.stage[data-hb-mode="compare"][data-side="top"] .half{right:auto;left:8px}
    .stage[data-hb-mode="compare"][data-side="bottom"] .half{bottom:auto;top:64px;right:auto;left:8px}
    .stage[data-hb-mode="compare"].posed .twin .drawer{transform:none}
    .stage[data-hb-mode="compare"].posed .twin .backdrop{opacity:1}
    .stage[data-hb-mode="compare"].posed .box:not(.twin) .drawer,.stage[data-hb-mode="compare"].posed .box:not(.twin) .backdrop{transition:none}
    .stage[data-hb-mode="compare"].posed .box:not(.twin) .backdrop{opacity:var(--pose)}
    .stage[data-hb-mode="compare"].posed .box:not(.twin) .drawer{transform:translateX(calc((var(--pose) - 1)*100%))}
    .stage[data-hb-mode="compare"].posed .box:not(.twin) .drawer.right{transform:translateX(calc((1 - var(--pose))*100%))}
    .stage[data-hb-mode="compare"].posed .box:not(.twin) .drawer.top{transform:translateY(calc((var(--pose) - 1)*100%))}
    .stage[data-hb-mode="compare"].posed .box:not(.twin) .drawer.bottom{transform:translateY(calc((1 - var(--pose))*100%))}
    .stage[data-hb-mode="compare"].posed .box:not(.twin) .arrow.in{display:block;position:absolute;z-index:11;pointer-events:none;width:28px;height:12px;transform:rotate(calc(var(--r)*1deg));top:calc(50% - 6px);left:calc(var(--pose)*var(--drawer-w) + 6px)}
    .stage[data-hb-mode="compare"][data-side="right"].posed .box:not(.twin) .arrow.in{left:auto;right:calc(var(--pose)*var(--drawer-w) + 6px)}
    .stage[data-hb-mode="compare"][data-side="top"].posed .box:not(.twin) .arrow.in{left:calc(50% - 14px);top:calc(var(--pose)*200px + 14px)}
    .stage[data-hb-mode="compare"][data-side="bottom"].posed .box:not(.twin) .arrow.in{left:calc(50% - 14px);top:auto;bottom:calc(var(--pose)*200px + 14px)}
    /* The real menu button stays drawn above the caught drawer, so it can be seen and pressed. A Top menu keeps a gutter for it, and on
       narrow stages, where the drawer's own × would meet it, the caught drawer leaves the × out. */
    .stage[data-hb-mode="compare"].posed .box:not(.twin) .hamburger{position:relative;z-index:11;background:var(--ui-bg)}
    .stage[data-hb-mode="compare"][data-side="top"] .drawer-body{padding-left:56px}
    @media(max-width:760px){.stage[data-hb-mode="compare"][data-side="left"].posed .box:not(.twin) .close-btn,.stage[data-hb-mode="compare"][data-side="top"].posed .box:not(.twin) .close-btn{visibility:hidden}}
    @media(min-width:601px){.stage[data-hb-mode="compare"][data-side="bottom"] .half{top:18px;left:auto;right:8px}}
    /* A short stage cannot hold the open menu and a half's name below it: each drawer gives up some width, so the name sits beside it */
    @media(min-width:601px) and (max-height:620px){.stage[data-hb-mode="compare"]{--drawer-w:min(280px,58%)}}
  </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="../tooltip-reveal/" rel="prev" aria-label="Previous: Tooltip Reveal"><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>Tooltip Reveal</span></a><a href="../modal-expand/" rel="next" aria-label="Next: Modal Expand"><span><span class="hb-dir">Next: </span>Modal Expand</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.17 · Micro-Interactions</p>
    <h1>Drawer / Panel Slide</h1>
    <p class="hb-lede">A side panel slides in over a dimmed page. Best for mobile menus.</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">Click it</h2><p>Press the menu button, then close the drawer with × or a swipe, 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">
      <div class="box" id="box">
        <div class="app-header">
          <button class="hamburger" id="open-btn" type="button" aria-label="Open menu" aria-expanded="false" aria-controls="drawer">☰</button>
          <div class="app-title">Atlas App</div>
        </div>
        <div class="app-body">
          <div class="content-line w100"></div><div class="content-line w80"></div>
          <div class="content-line w60"></div><div class="content-line w100"></div>
          <div class="content-line w40"></div><div class="content-line w80"></div>
        </div>
        <div class="backdrop" id="backdrop" aria-hidden="true"></div>
        <div class="drawer" id="drawer" role="dialog" aria-modal="true" aria-label="Navigation menu" tabindex="-1" inert>
          <div class="drawer-header">
            <div class="drawer-title">Menu</div>
            <button class="close-btn" id="close-btn" type="button" aria-label="Close menu">×</button>
          </div>
          <div class="drawer-body">
            <div class="nav-item active">Home</div>
            <div class="nav-item">Gallery</div>
            <div class="nav-item">About</div>
            <div class="nav-item">Contact</div>
          </div>
        </div>
        <span class="park" aria-hidden="true"></span><span class="arrow in" aria-hidden="true"></span><span class="arrow out" aria-hidden="true"></span>
        <p class="half" aria-hidden="true" data-hb-caption>Slides in</p>
      </div>
      <div class="keys" aria-hidden="true">
        <div class="kr">
          <p class="cap" data-hb-caption><i class="ico ico-park"></i><span>Waits <i class="more">here, </i>off the screen</span></p>
          <p class="cap" id="cap-dim" data-hb-caption><i class="ico ico-dim"></i><span>Dims the page</span></p>
        </div>
        <div class="kr">
          <p class="cap" data-hb-caption><i class="ico ico-in"></i><span>Slides in, slowing<i class="more"> down</i><br><b id="t-in">a quarter of a second</b></span></p>
        <p class="cap" data-hb-caption><i class="ico ico-out"></i><span>Slides out, speeding<i class="more"> up</i><br><b id="t-out">a fifth of a second</b></span></p>
        </div>
      </div>
    </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>
      <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo="css" autocomplete="off"><span>Slow motion</span></label>
    </div>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, the drawer waits just off the screen, outlined where it parks; opening slides it in over the page, slowing as it lands. Press the menu button, or Show me.</p>
      <p data-hb-for="compare" hidden>Appears at once: the menu is simply there; Slides in: it comes in from the edge, so you see where it lives. Press the menu button, or 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="side-lbl">Slides in from</p>
        <div class="seg" id="side-seg" role="group" aria-labelledby="side-lbl">
          <button type="button" data-side="left" class="on" aria-pressed="true">Left</button>
          <button type="button" data-side="right" aria-pressed="false">Right</button>
          <button type="button" data-side="top" aria-pressed="false">Top</button>
          <button type="button" data-side="bottom" aria-pressed="false">Bottom</button>
        </div>
        <p class="hb-hint">The edge the drawer comes in from.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="open-lbl">Opening speed</p>
        <div class="seg" id="open-seg" role="group" aria-labelledby="open-lbl">
          <button type="button" data-ms="450" aria-pressed="false">Slow</button>
          <button type="button" data-ms="280" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-ms="170" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">It slows down as it arrives, like a panel landing.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="close-lbl">Closing speed</p>
        <div class="seg" id="close-seg" role="group" aria-labelledby="close-lbl">
          <button type="button" data-ms="350" aria-pressed="false">Slow</button>
          <button type="button" data-ms="220" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-ms="130" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">It speeds up as it leaves, so closing feels quick.</p>
      </div>
    </div>
    <details class="hb-options">
      <summary><span class="hb-more-label">More options</span><span class="hb-less">Fewer options</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></summary>
      <div class="hb-settings">
        <div class="hb-setting">
          <p class="hb-setting-name" id="dim-lbl">Page dimming</p>
          <div class="seg" id="dim-seg" role="group" aria-labelledby="dim-lbl">
            <button type="button" data-op="0" aria-pressed="false">None</button>
            <button type="button" data-op="0.25" aria-pressed="false">Light</button>
            <button type="button" data-op="0.5" class="on" aria-pressed="true">Medium</button>
            <button type="button" data-op="0.7" aria-pressed="false">Dark</button>
          </div>
          <p class="hb-hint">How dark the page behind the drawer gets.</p>
        </div>
      </div>
    </details>
  </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 a slide-in drawer to [the menu or panel you want to open]. It waits just beyond one edge of the screen and slides in over the page when its button is pressed; when the settings include it, the page behind it dims. Let it slow down as it arrives, like a panel sliding to a stop, and speed up as it leaves, so closing feels quicker than opening. Close it with a close button, a tap on the page beside it, the Escape key or a swipe back toward its edge. Move only its position, and keep keyboard focus inside it while it is open. If the visitor has reduced motion turned on, show and hide it 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>Mobile menus</li><li>Filters</li><li>Settings panels</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Short messages</li><li>Main content</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 drawer=document.getElementById('drawer');
  const backdrop=document.getElementById('backdrop');
  const openBtn=document.getElementById('open-btn');
  const closeBtn=document.getElementById('close-btn');
  const vars=document.documentElement.style;
  let isOpen=false,side='left',dragging=false,dragStartX=0,dragStartY=0,openDur=280,closeDur=220,dimOp=0.5;
  const stage=document.querySelector('.stage'),box=document.getElementById('box');
  const motionOk=!matchMedia('(prefers-reduced-motion:reduce)').matches;
  stage.dataset.side=side;

  // Compare's first half: a copy of the app whose drawer and sheet show the moment open() runs and go the moment close() runs. It has
  // no ids, no buttons (spans) and no dialog role, and it is out of the Tab order and hidden from screen readers.
  const twin=box.cloneNode(true);
  twin.classList.add('twin');twin.inert=true;twin.setAttribute('aria-hidden','true');twin.removeAttribute('id');
  twin.querySelectorAll('[id]').forEach(e=>e.removeAttribute('id'));
  twin.querySelectorAll('.park,.arrow').forEach(e=>e.remove());
  twin.querySelectorAll('button').forEach(b=>{const t=document.createElement('span');t.className=b.className;t.textContent=b.textContent;b.replaceWith(t)});
  const tDrawer=twin.querySelector('.drawer'),tBackdrop=twin.querySelector('.backdrop');
  ['role','aria-modal','aria-label','tabindex','inert'].forEach(a=>tDrawer.removeAttribute(a));
  twin.querySelector('.half').textContent='Appears at once';
  box.before(twin);

  function open(){
    settleAt=0;unpose();
    drawer.inert=false;
    drawer.classList.add('open');backdrop.classList.add('visible');
    tDrawer.classList.add('open');tBackdrop.classList.add('visible');
    openBtn.setAttribute('aria-expanded','true');isOpen=true;
    stage.classList.add('shown');notes();
  }
  function close(){
    drawer.classList.remove('open');backdrop.classList.remove('visible');
    tDrawer.classList.remove('open');tBackdrop.classList.remove('visible');
    openBtn.setAttribute('aria-expanded','false');isOpen=false;
    drawer.inert=true; // a closed drawer cannot be reached with Tab
    stage.classList.remove('shown');
    settleAt=Date.now()+(motionOk?closeDur*(slowTog.checked?3:1):0);pose();
  }
  // The visitor's own way in and out. Keyboard focus goes into the drawer when it opens and back to the menu button when it
  // closes, unless the visitor has moved on to another control in the meantime. Show me calls open() and close() alone: a run
  // never moves the focus.
  function dismiss(){
    const a=document.activeElement,mine=a===document.body||drawer.contains(a);
    close();
    if(mine)openBtn.focus({preventScroll:true});
  }
  openBtn.addEventListener('click',()=>{open();closeBtn.focus({preventScroll:true})});
  closeBtn.addEventListener('click',dismiss);
  backdrop.addEventListener('click',dismiss);
  document.addEventListener('keydown',e=>{if(e.key==='Escape'&&isOpen)dismiss()});
  // Tab stays inside the open drawer: past its last control it goes back to the first, and Shift+Tab goes the other way.
  drawer.addEventListener('keydown',e=>{
    if(e.key!=='Tab')return;
    const f=[...drawer.querySelectorAll('button')],i=f.indexOf(document.activeElement);
    e.preventDefault();
    f[e.shiftKey?(i<=0?f.length-1:i-1):(i<0||i===f.length-1?0:i+1)].focus({preventScroll:true});
  });

  // Swipe to close
  drawer.addEventListener('pointerdown',e=>{dragging=true;dragStartX=e.clientX;dragStartY=e.clientY});
  document.addEventListener('pointercancel',()=>{dragging=false});
  document.addEventListener('pointerup',e=>{
    if(!dragging)return;dragging=false;
    const dx=e.clientX-dragStartX,dy=e.clientY-dragStartY;
    if(side==='left'&&dx<-50||side==='right'&&dx>50||side==='top'&&dy<-50||side==='bottom'&&dy>50)dismiss();
  });

  // 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=[];
  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; }
  function stopRun(){ runTimers.forEach(clearTimeout); runTimers=[]; }
  // A run starts with the drawer closed, put there without animating: the transitions are turned off, the drawer is closed,
  // a reflow makes it stick, and the transitions come back.
  function snap(fn){
    const parts=[drawer,backdrop];
    parts.forEach(p=>p.style.setProperty('transition','none','important'));
    fn();
    void drawer.offsetWidth;
    parts.forEach(p=>p.style.removeProperty('transition'));
  }
  function toRest(){snap(()=>{unpose();close()})}
  // The drawer opens as the menu button opens it, waits 1.5 s, and closes (the slide in is the movement the wait counts).
  document.getElementById('btn-demo').addEventListener('click',()=>{
    stopRun();
    toRest();
    open();
    later(close,openDur,1500);
  });
  // The visitor pressed, typed, scrolled or touched inside the stage: the run stops and the drawer stays as it is.
  document.addEventListener('hb:input',()=>{ if(running()){ stopRun(); pose(); } });
  // A Tab press from outside the stage sends no hb:input (the key goes to the control that had the focus), but a run never
  // moves the focus, so focus arriving in the stage while one runs is the visitor's: the drawer must not close under it.
  document.querySelector('.stage').addEventListener('focusin',e=>{ if(e.isTrusted&&running()) stopRun(); });

  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);
      words();pose();measure();
    }));
  }
  // An open drawer closes first; the class then changes without a transition, so it never slides across the stage
  choices('side-seg',d=>{
    if(isOpen)close();
    side=d.side;
    drawer.style.transition='none';
    drawer.className='drawer';
    if(side!=='left')drawer.classList.add(side);
    tDrawer.className=drawer.className;stage.dataset.side=side;
    void drawer.offsetWidth;
    drawer.style.transition='';
  });
  choices('open-seg',d=>{openDur=+d.ms;vars.setProperty('--open-dur',d.ms+'ms')});
  choices('close-seg',d=>{closeDur=+d.ms;vars.setProperty('--close-dur',d.ms+'ms')});
  choices('dim-seg',d=>{dimOp=+d.op;vars.setProperty('--backdrop-op',d.op)});

  // The two views. See inside draws the key from the page's own numbers; Compare's rest pose catches the real drawer and sheet where
  // the page's own opening curve has taken them a tenth of a second after the press (judged by progress, so Slow motion leaves it be).
  let mode='normal',posed=false,settleAt=0,poseTimer=0;
  const SECS={130:'an eighth of a second',170:'a sixth of a second',220:'a fifth of a second',280:'a quarter of a second',350:'a third of a second',390:'under half a second',450:'nearly half a second',510:'half a second',660:'over half a second',840:'nearly a second',1050:'about a second',1350:'over a second'};
  const tIn=document.getElementById('t-in'),tOut=document.getElementById('t-out'),capDim=document.getElementById('cap-dim');
  const probe=document.body.appendChild(document.createElement('i'));
  probe.style.cssText='position:absolute;visibility:hidden;width:0;height:0';
  // How far the drawer slides (its width, or its height at the top and bottom), for the length of See inside's arrows
  function measure(){stage.style.setProperty('--mv',(side==='top'||side==='bottom'?drawer.offsetHeight:drawer.offsetWidth)+'px')}
  function words(){
    const k=slowTog.checked?3:1;
    tIn.textContent=SECS[openDur*k];tOut.textContent=SECS[closeDur*k];capDim.hidden=dimOp===0;
  }
  function progressAt(ms,dur){
    const a=probe.animate({opacity:[0,1]},{duration:dur,easing:getComputedStyle(document.documentElement).getPropertyValue('--open-ease').trim(),fill:'both'});
    a.pause();a.currentTime=ms;
    const v=+getComputedStyle(probe).opacity;a.cancel();return v;
  }
  function unpose(){if(posed){posed=false;stage.classList.remove('posed')}}
  // At rest in Compare: the drawer is closed, no Show me run is waiting and the last slide out has finished
  function pose(){
    clearTimeout(poseTimer);poseTimer=0;
    let on=mode==='compare'&&!isOpen&&!running();
    const wait=settleAt-Date.now();
    if(on&&wait>0){on=false;poseTimer=setTimeout(pose,wait+20)}
    stage.style.setProperty('--pose',progressAt(100,openDur).toFixed(3));
    if(on&&!posed){posed=true;stage.classList.add('posed')}
    else if(!on&&posed)snap(unpose);
    notes();
  }
  // Under reduced motion each note says what its still shows, and changes with it
  const nIn=document.querySelector('[data-hb-for="inside"]'),nCmp=document.querySelector('[data-hb-for="compare"]');
  function setText(el,t){if(el.textContent!==t)el.textContent=t}
  function notes(){
    if(motionOk)return;
    setText(nIn,isOpen?'Inside, the outline marks where the drawer waits; your device is set to reduce motion, so it appears over the page at once.':'Inside, shown still: the drawer waits just off the screen, outlined where it parks; with motion on, it slides in over the page, slowing as it lands.');
    setText(nCmp,posed||!isOpen?'Shown still: Appears at once is already open; Slides in is caught part way in from its edge.':'Appears at once and Slides in both show the open menu, because your device is set to reduce motion. Close it to see the still again.');
  }
  document.addEventListener('hb:mode',e=>{mode=e.detail.mode;words();pose();measure()});
  addEventListener('resize',measure);
  slowTog.addEventListener('change',words);
  words();notes();
</script>
</body>
</html>