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.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
feat: move Modal Expand to the guided-steps page
matinf86bf64c0d35
diff --git a/animations/04-micro-interactions/modal-expand/README.md b/animations/04-micro-interactions/modal-expand/README.mdindex ba0e8ea6..ab719bbf 100644--- a/animations/04-micro-interactions/modal-expand/README.md+++ b/animations/04-micro-interactions/modal-expand/README.md@@ -1,7 +1,7 @@# Modal Expand## What it is-Modal expand is a transition where a modal dialog scales into view from the position of the button that triggered it. By setting `transform-origin` to the trigger button's location, the modal appears to "grow from" that point — maintaining spatial continuity between trigger and result. This gives users a clear mental model of where the modal came from and, by extension, where it will return to when dismissed.+Modal expand makes a dialog grow out of the button that opened it. The page measures where that button is and grows the window from that exact spot to its full size in the middle of the screen, so it is clear where the window came from and where it goes back to when it closes.## When to use it- Card actions where tapping a card opens a detail modal@@ -10,27 +10,17 @@ Modal expand is a transition where a modal dialog scales into view from the posi- App-like interfaces mimicking iOS long-press → context menu expansion## How it works-On click, measure the button's position with `getBoundingClientRect()`, then compute the origin point relative to the modal's final centered position:+On click, measure the button's position with `getBoundingClientRect()`, then compute the same point relative to the modal's own corner:```js-function openModal(triggerBtn) {- const stageRect = stage.getBoundingClientRect();- const btnRect = triggerBtn.getBoundingClientRect();- const modalRect = modal.getBoundingClientRect();-- // Button center relative to stage- const bx = btnRect.left + btnRect.width/2 - stageRect.left;- const by = btnRect.top + btnRect.height/2 - stageRect.top;-- // Modal center in stage- const mx = stageRect.width / 2;- const my = stageRect.height / 2;-- // Origin point relative to modal's top-left corner- const ox = bx - mx + modalRect.width/2;- const oy = by - my + modalRect.height/2;-- modal.style.transformOrigin = `${ox}px ${oy}px`;+function openModal(btn) {+ const sr = stage.getBoundingClientRect();+ const br = btn.getBoundingClientRect();+ // The button's centre, relative to the stage+ const bx = br.left + br.width / 2 - sr.left;+ const by = br.top + br.height / 2 - sr.top;+ // The same point, relative to the modal's laid-out corner (offsetLeft/Top ignore its scale)+ modal.style.transformOrigin = `${bx - modal.offsetLeft}px ${by - modal.offsetTop}px`;modal.classList.add('open');}```@@ -44,18 +34,14 @@ function openModal(triggerBtn) {.modal {position: absolute;- top: 50%; left: 50%;- transform: translate(-50%, -50%) scale(var(--start-scale));+ inset: 0; margin: auto; /* centred without a transform */+ width: clamp(240px, 55%, 320px); height: fit-content;+ transform: scale(var(--start-scale));opacity: 0;- transition: transform var(--modal-dur) var(--modal-ease),- opacity 200ms ease;+ transition: transform var(--modal-dur) var(--modal-ease), opacity 200ms ease;pointer-events: none;}-.modal.open {- transform: translate(-50%, -50%) scale(1);- opacity: 1;- pointer-events: auto;-}+.modal.open { transform: scale(1); opacity: 1; pointer-events: auto; }```The springy easing (`cubic-bezier(.34, 1.3, .64, 1)`) causes a slight overshoot, giving the modal a satisfying "pop" as it settles into position.@@ -63,10 +49,10 @@ The springy easing (`cubic-bezier(.34, 1.3, .64, 1)`) causes a slight overshoot,## Key parameters| Parameter | Default | Effect ||-----------|---------|--------|-| Duration | 320ms | 200–400ms — longer than hover, shorter than a page transition |-| Start scale | 0.1 | 0.05–0.3; too large and the grow effect is invisible; too small and it flickers |-| Easing | Springy | Overshoot makes the arrival feel physical; ease-out is more conservative |-| Backdrop fade | 200ms | Independent from modal — the backdrop can fade in faster |+| Speed | Normal | How long the window takes to grow: slow is 500ms, normal 320ms and fast 200ms |+| Starting size | Small | How big the window is as it leaves the button: tiny is 5%, small 10% and medium 30% of its full size; too big and the growth is hard to see |+| Feel | Springy | Springy goes a little past full size, then settles; Smooth slows to a stop; Even keeps one steady speed |+| Marks where it grows from | on | Shows a small dot on the point the window grows out of |## Production notes- **Recalculate `transform-origin` on resize**: if the user resizes the window between opens, the cached origin is stale. Recalculate in the click handler, not at mount time.@@ -77,6 +63,6 @@ The springy easing (`cubic-bezier(.34, 1.3, .64, 1)`) causes a slight overshoot,- **GSAP**: `gsap.fromTo(modal, { scale: 0.1, transformOrigin: `${ox}px ${oy}px` }, { scale: 1, duration: 0.32, ease: "back.out(1.5)" })`.## See also-- [Drawer / Panel Slide](../drawer-slide/) — for content that slides from an edge rather than expanding from a point-- [Tooltip Reveal](../tooltip-reveal/) — for small supplementary content that doesn't need a full modal-- [FLIP Technique](../../03-page-transitions/flip-technique/) — the positional measurement technique underlying this animation+- [Drawer / Panel Slide](../drawer-slide/) — a panel that slides in from an edge instead+- [Tooltip Reveal](../tooltip-reveal/) — a small label for a short explanation+- [FLIP Technique](../../03-page-transitions/flip-technique/) — the same measure-then-move idea for any layout changediff --git a/animations/04-micro-interactions/modal-expand/index.html b/animations/04-micro-interactions/modal-expand/index.htmlindex aab9ba49..fcf1c249 100644--- a/animations/04-micro-interactions/modal-expand/index.html+++ b/animations/04-micro-interactions/modal-expand/index.html@@ -4,7 +4,7 @@<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Modal Expand — Animation Handbook</title>- <meta name="description" content="Modal scales from the trigger button's position — transform-origin set to where you clicked.">+ <meta name="description" content="A window grows out of the button you pressed. Best for detail views."><link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/modal-expand/"><link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg">@@ -12,179 +12,273 @@<meta property="og:type" content="article"><meta property="og:site_name" content="Animation Handbook"><meta property="og:title" content="Modal Expand — Animation Handbook">- <meta property="og:description" content="Modal scales from the trigger button's position — transform-origin set to where you clicked.">+ <meta property="og:description" content="A window grows out of the button you pressed. Best for detail views."><meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/modal-expand/"><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="Modal Expand — Animation Handbook">- <meta name="twitter:description" content="Modal scales from the trigger button's position — transform-origin set to where you clicked.">+ <meta name="twitter:description" content="A window grows out of the button you pressed. Best for detail views."><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": "Modal Expand", "name": "Modal Expand", "description": "Modal scales from the trigger button's position — transform-origin set to where you clicked.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/modal-expand/", "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, Modal Expand"}</script>+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Modal Expand", "name": "Modal Expand", "description": "A window grows out of the button you pressed. Best for detail views.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/modal-expand/", "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, Modal Expand"}</script><style>- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-700.woff2') format('woff2');font-weight:700;font-display:swap}- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-800.woff2') format('woff2');font-weight:800;font-display:swap}- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-400.woff2') format('woff2');font-weight:400;font-display:swap}- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-500.woff2') format('woff2');font-weight:500;font-display:swap}- :root{--bg:#0b0b0d;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-accent:#ff9d5c;--ui-text:#f4f4f2;--ui-muted:#77777e;--disp:'Bricolage','Arial Narrow',sans-serif;--mono:'PlexMono',ui-monospace,Menlo,Consolas,monospace;--stage-h:620px;--modal-dur:320ms;--modal-ease:cubic-bezier(.34,1.3,.64,1);--start-scale:0.1;--backdrop-dur:200ms}+ :root{--ui-accent:#ff9d5c;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-text:#f4f4f2;--ui-muted:#8a8a92;--modal-dur:320ms;--modal-ease:cubic-bezier(.34,1.3,.64,1);--start-scale:0.1;--backdrop-dur:200ms}*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}- body{background:var(--bg);color:var(--ui-text);font-family:var(--mono);-webkit-font-smoothing:antialiased;padding:clamp(12px,3vw,24px)}- header{margin-bottom:20px}- header h1{font-family:var(--disp);font-weight:700;font-size:clamp(11px,1.8vw,15px);letter-spacing:-.01em}- header p{font-size:clamp(10px,1.2vw,12px);color:var(--ui-muted);margin-top:4px}- .layout{display:flex;gap:clamp(12px,2vw,24px);align-items:flex-start}- .stage{flex:1;min-width:0;height:var(--stage-h);border:1px solid var(--ui-border);border-radius:8px;background:var(--bg);overflow:hidden;position:relative;display:flex;align-items:center;justify-content:center}+ .stage{position:relative;overflow:hidden;display:flex;align-items:center;justify-content:center;background:#0b0b0d}/* Trigger buttons scattered */.triggers{display:flex;flex-direction:column;align-items:center;gap:16px;width:100%;padding:24px}.trigger-row{display:flex;gap:16px;width:100%;justify-content:space-between}- .trig-btn{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:monospace;font-size:11px;padding:0 16px;height:44px;border-radius:6px;cursor:pointer;transition:border-color .15s;white-space:nowrap}+ .trig-btn{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:inherit;font-size:11px;padding:0 16px;height:44px;border-radius:6px;cursor:pointer;transition:border-color .15s;white-space:nowrap}@media(hover:hover){.trig-btn:hover{border-color:var(--ui-accent);color:var(--ui-accent)}}/* Backdrop */.modal-backdrop{position:absolute;inset:0;background:rgba(0,0,0,.6);opacity:0;transition:opacity var(--backdrop-dur) ease;pointer-events:none;z-index:5}.modal-backdrop.visible{opacity:1;pointer-events:auto}- /* Modal */- .modal{position:absolute;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:10px;width:clamp(240px,55%,320px);padding:24px;z-index:10;opacity:0;transform:scale(var(--start-scale));transition:opacity var(--backdrop-dur) ease,transform var(--modal-dur) var(--modal-ease);pointer-events:none;top:50%;left:50%;margin-left:calc(clamp(240px,55%,320px)*-0.5);margin-top:-80px}+ /* Modal, centred without a transform (inset, auto margins and its own height), so its scale is the only transform on it */+ .modal{position:absolute;inset:0;margin:auto;height:fit-content;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:10px;width:clamp(240px,55%,320px);padding:24px;z-index:10;opacity:0;transform:scale(var(--start-scale));transition:opacity var(--backdrop-dur) ease,transform var(--modal-dur) var(--modal-ease);pointer-events:none}.modal.open{opacity:1;transform:scale(1);pointer-events:auto}.modal-title{font-size:14px;font-weight:bold;margin-bottom:8px}.modal-body{font-size:11px;color:var(--ui-muted);line-height:1.7;margin-bottom:20px}.modal-actions{display:flex;gap:8px;justify-content:flex-end}- .modal-close{background:transparent;border:1px solid var(--ui-border);color:var(--ui-text);font-family:monospace;font-size:11px;padding:0 14px;height:36px;border-radius:4px;cursor:pointer}- .modal-confirm{background:var(--ui-accent);color:#000;border:none;font-family:monospace;font-size:11px;font-weight:bold;padding:0 14px;height:36px;border-radius:4px;cursor:pointer}+ .modal-close{background:transparent;border:1px solid var(--ui-border);color:var(--ui-text);font-family:inherit;font-size:11px;padding:0 14px;height:36px;border-radius:4px;cursor:pointer}+ .modal-confirm{background:var(--ui-accent);color:#000;border:none;font-family:inherit;font-size:11px;font-weight:bold;padding:0 14px;height:36px;border-radius:4px;cursor:pointer}+ /* The site focus ring is orange, like this button's fill: on it the ring turns dark */+ .hb-page .stage .modal-confirm:focus-visible{outline-color:#0b0b0d}/* Debug origin dot */.origin-dot{position:absolute;width:8px;height:8px;border-radius:50%;background:var(--ui-accent);transform:translate(-50%,-50%);pointer-events:none;opacity:0;transition:opacity .2s;z-index:15}.origin-dot.show{opacity:.8}- /* Panel */- aside{width:clamp(200px,22vw,240px);flex-shrink:0;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px;padding:16px;display:flex;flex-direction:column;gap:10px}- .note{font-size:10px;color:var(--ui-muted);line-height:1.6;border-left:2px solid var(--ui-accent);padding-left:8px}- .lbl{font-size:10px;text-transform:uppercase;letter-spacing:1px;color:var(--ui-accent)}- .kv{display:flex;justify-content:space-between;font-size:10px;color:var(--ui-muted)}- .kv .v{color:var(--ui-text);font-size:10px}- .div{height:1px;background:var(--ui-border)}- .sr{display:flex;align-items:center;gap:6px;font-size:10px}- .sr input[type=range]{flex:1;accent-color:var(--ui-accent);min-height:44px}- .sv{width:44px;text-align:right;color:var(--ui-text)}- select{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:monospace;font-size:10px;padding:8px 6px;border-radius:4px;width:100%;min-height:44px}- .tog{display:flex;align-items:center;gap:8px;font-size:10px;cursor:pointer;min-height:44px}- .tog input{accent-color:var(--ui-accent);width:16px;height:16px;flex-shrink:0}- @media(max-width:600px){.layout{flex-direction:column}.stage{flex:none;width:100%}aside{width:100%}:root{--stage-h:480px}.modal-close,.modal-confirm{height:44px}}+ @media(max-width:600px),(max-height:500px){.modal-close,.modal-confirm{height:44px}}@media(prefers-reduced-motion:reduce){.modal,.modal-backdrop{transition:none!important}.modal.open{transform:scale(1)!important;opacity:1!important}}</style>-<style id="ah-inject">-:root{--ah-bg:rgba(17,17,20,.82);--ah-line:rgba(255,255,255,.1);--ah-ink:#f4f4f2;--ah-muted:#9a9aa2;--ah-accent:#6ea8ff}-body{padding-top:46px}-.ah-bar{position:fixed;top:0;left:0;right:0;height:46px;z-index:2147483000;display:flex;align-items:center;justify-content:space-between;gap:10px;padding:0 clamp(10px,3vw,18px);background:var(--ah-bg);backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);border-bottom:1px solid var(--ah-line);font-family:ui-monospace,Menlo,Consolas,monospace;font-size:12px;box-sizing:border-box}-.ah-bar a,.ah-bar button{color:var(--ah-muted);text-decoration:none;background:none;border:0;font:inherit;cursor:pointer;display:inline-flex;align-items:center;gap:6px;min-height:34px;padding:0 7px;border-radius:6px;transition:color .18s,background .18s;white-space:nowrap}-.ah-bar a:hover,.ah-bar button:hover{color:var(--ah-ink)}-.ah-home{color:var(--ah-ink);font-weight:600}-.ah-actions{display:flex;align-items:center;gap:2px}-.ah-sep{width:1px;height:16px;background:var(--ah-line);margin:0 4px}-.ah-copied{color:var(--ah-accent)!important}-@media(max-width:600px){.ah-bar .ah-hideSm{display:none}}-@media print{.ah-bar{display:none}body{padding-top:0}}-</style>+ <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=4">+ <script src="../../../assets/js/demo-page.js?v=4" defer></script></head>-<body>-<div class="ah-bar" role="navigation" aria-label="Animation Handbook"><a class="ah-home" href="../../../">← Animation Handbook</a><span class="ah-actions"><a class="ah-hideSm" href="../drawer-slide/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../accordion/" rel="next" title="Next">Next ›</a><span class="ah-sep ah-hideSm"></span><button type="button" id="ah-copy" class="ah-hideSm">Copy source</button><a class="ah-hideSm" href="https://github.com/matinmonshizadeh/animation-handbook/blob/main/animations/04-micro-interactions/modal-expand/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>-<header>- <h1>Modal Expand</h1>- <p>Modal scales from the trigger button's position — transform-origin set to where you clicked.</p>-</header>-<div class="layout">- <div class="stage" id="stage">- <div class="triggers">- <div class="trigger-row">- <button class="trig-btn" data-label="Top Left">Open ↗</button>- <button class="trig-btn" data-label="Top Right">Open ↗</button>+<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="../drawer-slide/" rel="prev" aria-label="Previous: Drawer / Panel Slide"><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>Drawer / Panel Slide</span></a><a href="../accordion/" rel="next" aria-label="Next: Accordion Open/Close"><span><span class="hb-dir">Next: </span>Accordion Open/Close</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>+</nav>+<main class="hb-page">+ <header class="hb-head">+ <p class="hb-cat">04.18 · Micro-Interactions</p>+ <h1>Modal Expand</h1>+ <p class="hb-lede">A window grows out of the button you pressed. Best for detail views.</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 any of the five buttons and watch the window grow out of it, or press Show me.</p></div>+ </div>+ <div class="stage hb-dots" id="stage">+ <div class="triggers">+ <div class="trigger-row">+ <button class="trig-btn" type="button" data-label="Top Left" aria-label="Open from the top left">Open ↗</button>+ <button class="trig-btn" type="button" data-label="Top Right" aria-label="Open from the top right">Open ↗</button>+ </div>+ <div class="trigger-row" style="justify-content:center">+ <button class="trig-btn" type="button" data-label="Center">Open Center</button>+ </div>+ <div class="trigger-row">+ <button class="trig-btn" type="button" data-label="Bottom Left" aria-label="Open from the bottom left">Open ↗</button>+ <button class="trig-btn" type="button" data-label="Bottom Right" aria-label="Open from the bottom right">Open ↗</button>+ </div></div>- <div class="trigger-row" style="justify-content:center">- <button class="trig-btn" data-label="Center">Open Center</button>+ <div class="modal-backdrop" id="backdrop" aria-hidden="true"></div>+ <div class="modal" id="modal" role="dialog" aria-modal="true" aria-labelledby="modal-title" tabindex="-1" inert>+ <div class="modal-title" id="modal-title">Confirm action</div>+ <div class="modal-body">This window grew out of the button you pressed, so it feels connected to where it came from.</div>+ <div class="modal-actions">+ <button class="modal-close" id="modal-close" type="button">Cancel</button>+ <button class="modal-confirm" id="modal-confirm" type="button">Confirm</button>+ </div>+ </div>+ <div class="origin-dot" id="origin-dot" aria-hidden="true"></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>+ </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="speed-lbl">Speed</p>+ <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">+ <button type="button" data-ms="500" aria-pressed="false">Slow</button>+ <button type="button" data-ms="320" class="on" aria-pressed="true">Normal</button>+ <button type="button" data-ms="200" aria-pressed="false">Fast</button>+ </div>+ <p class="hb-hint">How long the window takes to grow to full size.</p></div>- <div class="trigger-row">- <button class="trig-btn" data-label="Bottom Left">Open ↗</button>- <button class="trig-btn" data-label="Bottom Right">Open ↗</button>+ <div class="hb-setting">+ <p class="hb-setting-name" id="size-lbl">Starting size</p>+ <div class="seg" id="size-seg" role="group" aria-labelledby="size-lbl">+ <button type="button" data-scale="0.05" aria-pressed="false">Tiny</button>+ <button type="button" data-scale="0.1" class="on" aria-pressed="true">Small</button>+ <button type="button" data-scale="0.3" aria-pressed="false">Medium</button>+ </div>+ <p class="hb-hint">How small it is as it leaves the button.</p></div>+ <div class="hb-setting">+ <p class="hb-setting-name" id="feel-lbl">Feel</p>+ <div class="seg" id="feel-seg" role="group" aria-labelledby="feel-lbl">+ <button type="button" data-ease="cubic-bezier(.34,1.3,.64,1)" class="on" aria-pressed="true">Springy</button>+ <button type="button" data-ease="ease-out" aria-pressed="false">Smooth</button>+ <button type="button" data-ease="linear" aria-pressed="false">Even</button>+ </div>+ <p class="hb-hint">Springy goes a little past full size, then settles.</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">+ <label class="hb-switch-row"><span>Marks where it grows from</span><input class="hb-switch" type="checkbox" role="switch" id="debug-tog" checked autocomplete="off"></label>+ <p class="hb-hint">A dot shows the point the window grows out of.</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="modal-backdrop" id="backdrop"></div>- <div class="modal" id="modal" role="dialog" aria-modal="true" aria-labelledby="modal-title">- <div class="modal-title" id="modal-title">Confirm action</div>- <div class="modal-body">This modal expanded from the button you clicked. The transform-origin is set to that exact position — making the modal feel connected to its source.</div>- <div class="modal-actions">- <button class="modal-close" id="modal-close">Cancel</button>- <button class="modal-confirm" id="modal-confirm">Confirm</button>+ <div class="hb-card">+ <p class="hb-prompt">Add a modal that grows out of its button to [the button or card that opens a dialog or detail view]. When it is pressed, measure where that button sits and grow the modal from that spot to full size in the middle of the screen while the page behind it dims, so it clearly comes from what was pressed. Close it with its buttons, a tap on the dimmed page or the Escape key, and let it shrink back toward the same spot. Animate only its scale and opacity. Move keyboard focus into it, and back to the button when it closes. If the visitor has reduced motion turned on, show it at full size without growing. 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="origin-dot" id="origin-dot"></div>- </div>- <aside>- <p class="note">transform-origin is set to the button's center relative to the modal's center. This creates the illusion the modal "grows from" the button.</p>- <div class="lbl">Duration</div>- <div class="sr"><input type="range" id="dur-sl" min="100" max="600" value="320" step="20"><span class="sv" id="dur-v">320ms</span></div>- <div class="lbl">Start Scale</div>- <div class="sr"><input type="range" id="scale-sl" min="1" max="40" value="10" step="1"><span class="sv" id="scale-v">0.10</span></div>- <div class="lbl">Easing</div>- <select id="ease-sel">- <option value="cubic-bezier(.34,1.3,.64,1)" selected>Springy</option>- <option value="ease-out">Ease out</option>- <option value="cubic-bezier(.2,.7,.3,1)">Smooth</option>- <option value="linear">Linear</option>- </select>- <label class="tog"><input type="checkbox" id="debug-tog" checked><span>Show origin point</span></label>- <div class="div"></div>- <div class="kv"><span>Origin</span><span class="v" id="origin-label">—</span></div>- </aside>-</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>Detail views</li><li>Confirmations</li><li>Quick forms</li><li>Action buttons</li></ul>+ <h3>Avoid on</h3>+ <ul class="hb-tags hb-avoid"><li>Long content</li><li>Full pages</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="#top">Back to top ↑</a></footer><script>+ const stage=document.getElementById('stage');const modal=document.getElementById('modal');const backdrop=document.getElementById('backdrop');const originDot=document.getElementById('origin-dot');- const originLabel=document.getElementById('origin-label');- let isOpen=false;+ const dotTog=document.getElementById('debug-tog');+ const cancelBtn=document.getElementById('modal-close');+ const btns=[...document.querySelectorAll('.trig-btn')];+ const vars=document.documentElement.style;+ let isOpen=false,opener=null,dur=320,tour=0;function openModal(btn){- const stage=document.getElementById('stage');const sr=stage.getBoundingClientRect();const br=btn.getBoundingClientRect();- // Button center relative to stage+ // The button's centre, relative to the stageconst bx=br.left+br.width/2-sr.left;const by=br.top+br.height/2-sr.top;- // Origin relative to the modal's laid-out top-left corner.- // offsetLeft/Top ignore the closed-state scale transform, unlike getBoundingClientRect.- const ox=bx-modal.offsetLeft;- const oy=by-modal.offsetTop;- modal.style.transformOrigin=`${ox.toFixed(0)}px ${oy.toFixed(0)}px`;- originLabel.textContent=`${ox.toFixed(0)}px, ${oy.toFixed(0)}px`;+ // The same point, relative to the modal's laid-out corner (offsetLeft/Top ignore its scale)+ modal.style.transformOrigin=`${bx-modal.offsetLeft}px ${by-modal.offsetTop}px`;originDot.style.left=bx+'px';originDot.style.top=by+'px';- originDot.classList.toggle('show',document.getElementById('debug-tog').checked);- backdrop.classList.add('visible');modal.classList.add('open');isOpen=true;+ originDot.classList.toggle('show',dotTog.checked);+ modal.inert=false;+ backdrop.classList.add('visible');modal.classList.add('open');+ opener=btn;isOpen=true;}function closeModal(){backdrop.classList.remove('visible');modal.classList.remove('open');- originDot.classList.remove('show');isOpen=false;+ originDot.classList.remove('show');+ modal.inert=true;isOpen=false;}-- document.querySelectorAll('.trig-btn').forEach(btn=>{- btn.addEventListener('click',()=>openModal(btn));+ // The visitor's own way in and out. Keyboard focus goes into the window when it opens and back to the button that opened it+ // when it closes, unless the visitor has moved on to another control in the meantime. Show me calls openModal() and+ // closeModal() alone: a run never moves the focus.+ function dismiss(){+ const a=document.activeElement,mine=a===document.body||modal.contains(a);+ closeModal();+ if(mine&&opener)opener.focus({preventScroll:true});+ }+ btns.forEach(btn=>btn.addEventListener('click',()=>{openModal(btn);cancelBtn.focus({preventScroll:true})}));+ backdrop.addEventListener('click',dismiss);+ cancelBtn.addEventListener('click',dismiss);+ document.getElementById('modal-confirm').addEventListener('click',dismiss);+ document.addEventListener('keydown',e=>{if(e.key==='Escape'&&isOpen)dismiss()});+ // Tab stays inside the open window: past its last button it goes back to the first, and Shift+Tab goes the other way.+ modal.addEventListener('keydown',e=>{+ if(e.key!=='Tab')return;+ const f=[...modal.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});});- backdrop.addEventListener('click',closeModal);- document.getElementById('modal-close').addEventListener('click',closeModal);- document.getElementById('modal-confirm').addEventListener('click',closeModal);- document.addEventListener('keydown',e=>{if(e.key==='Escape'&&isOpen)closeModal()});- document.getElementById('dur-sl').addEventListener('input',e=>{- document.getElementById('dur-v').textContent=e.target.value+'ms';- document.documentElement.style.setProperty('--modal-dur',e.target.value+'ms');- });- document.getElementById('scale-sl').addEventListener('input',e=>{- const v=(e.target.value/100).toFixed(2);- document.getElementById('scale-v').textContent=v;- document.documentElement.style.setProperty('--start-scale',v);- });- document.getElementById('ease-sel').addEventListener('change',e=>{- document.documentElement.style.setProperty('--modal-ease',e.target.value);- });- document.getElementById('debug-tog').addEventListener('change',e=>{- originDot.classList.toggle('show',e.target.checked&&isOpen);+ // 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 window closed, put there without animating: the transitions are turned off, the window is closed,+ // a reflow makes it stick, and the transitions come back.+ function toRest(){+ const parts=[modal,backdrop,originDot];+ parts.forEach(p=>p.style.setProperty('transition','none','important'));+ closeModal();+ void stage.offsetWidth;+ parts.forEach(p=>p.style.removeProperty('transition'));+ }+ // Each press of Show me uses the next button in turn, so the window starts from a different point every time: top right,+ // bottom left, the middle, top left, bottom right. The window grows (the movement the wait counts), stays 1.5 s and shrinks back.+ const tourOrder=['Top Right','Bottom Left','Center','Top Left','Bottom Right'];+ document.getElementById('btn-demo').addEventListener('click',()=>{+ stopRun();+ toRest();+ const label=tourOrder[tour++%tourOrder.length];+ openModal(btns.find(b=>b.dataset.label===label));+ later(closeModal,dur,1500);});+ // The visitor pressed, typed, scrolled or touched inside the stage: the run stops and the window stays open.+ document.addEventListener('hb:input',()=>{ if(running()) stopRun(); });+ // 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 window must not close under it.+ 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);+ }));+ }+ choices('speed-seg',d=>{dur=+d.ms;vars.setProperty('--modal-dur',d.ms+'ms')});+ choices('size-seg',d=>{vars.setProperty('--start-scale',d.scale)});+ choices('feel-seg',d=>{vars.setProperty('--modal-ease',d.ease)});+ dotTog.addEventListener('change',()=>{originDot.classList.toggle('show',dotTog.checked&&isOpen)});</script>-<script>(function(){var b=document.getElementById("ah-copy");if(!b)return;b.addEventListener("click",function(){fetch(location.href).then(function(r){return r.text();}).catch(function(){return document.documentElement.outerHTML;}).then(function(t){return navigator.clipboard.writeText(t);}).then(function(){b.textContent="Copied!";b.classList.add("ah-copied");setTimeout(function(){b.textContent="Copy source";b.classList.remove("ah-copied");},1500);}).catch(function(){b.textContent="Copy failed";setTimeout(function(){b.textContent="Copy source";},1500);});});})();</script></body></html>diff --git a/index.html b/index.htmlindex ab99bd73..87205d83 100644--- a/index.html+++ b/index.html@@ -968,7 +968,7 @@ const CATS=[['badge-pulse','Notification Badge Pulse','Dot on icon pulses to draw peripheral attention — scale, halo, or both.'],['tooltip-reveal','Tooltip Reveal','Info box fades in after a 300ms delay — long enough to avoid triggering on mouse pass-through.'],['drawer-slide','Drawer / Panel Slide','A side panel slides in over a dimmed page. Best for mobile menus.'],- ['modal-expand','Modal Expand','Modal scales from the trigger button\'s position — transform-origin set to where you clicked.'],+ ['modal-expand','Modal Expand','A window grows out of the button you pressed. Best for detail views.'],['accordion','Accordion Open/Close','Height animates between 0 and auto — JS measured height vs CSS grid-template-rows compared.'],['cursor-follower','Cursor Follower','Custom element follows mouse with lerp lag — mix-blend-mode: difference inverts any background.'],['error-shake','Error Shake','An invalid field shakes horizontally with a decaying wobble and flashes red — the universal "no".'],