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.

feat: move Theme Toggle Morph to the guided-steps page

matinb6be12517bae
diff --git a/animations/04-micro-interactions/theme-toggle-morph/README.md b/animations/04-micro-interactions/theme-toggle-morph/README.md
index 7c9bffed..ee357bf0 100644
--- a/animations/04-micro-interactions/theme-toggle-morph/README.md
+++ b/animations/04-micro-interactions/theme-toggle-morph/README.md
@@ -1,7 +1,7 @@
# Theme Toggle Morph
## What it is
-A single icon that animates between a sun and a crescent moon to signal a light/dark theme switch. Instead of swapping two static glyphs, the sun's rays retract and an SVG mask slides across the disc to carve a crescent, so the two states are visibly the same shape transforming. Clicking it also flips a small preview card between a light and dark palette so the theme change reads at a glance.
+A theme toggle morph is one icon that turns from a sun into a crescent moon when you switch a site from light to dark. The sun's rays shrink into its center while a hidden circle slides across the sun and cuts it into a crescent, so the two states are clearly the same shape changing. In the demo, a small preview card flips to its dark side at the same time.
## When to use it
- The header or settings control that switches an interface between light and dark mode.
@@ -30,16 +30,13 @@ The icon is one inline SVG: a filled `disc`, a group of eight `line` rays, and a
| Parameter | Default | Effect |
|-----------|---------|--------|
-| `--morph-dur` | `500ms` | Length of the ray retraction, mask slide, and card flip. Under ~200ms the crescent forms too fast to read; over ~800ms it drags. |
-| `--morph-ease` | `cubic-bezier(.4,0,.2,1)` | Timing curve. An overshoot curve adds a slight settle; linear feels mechanical. |
-| `--toggle-size` | `104px` | Rendered icon size; the button padding scales with it. |
-| cutout offset | `translate(7px,-4px)` | Final mask position. Larger offset yields a thinner crescent; smaller leaves a gibbous shape. |
-| ray `transform-origin` | `12px 12px` | The point rays collapse toward. Off-center origins make them slide rather than shrink in place. |
+| Speed | Normal | How long the change takes: slow is 800ms, normal 500ms and fast 300ms; under about 200ms the crescent forms too fast to see, and over 800ms it drags |
+| Feel | Gentle | Gentle eases in and out; Springy overshoots a little, then settles; Even keeps one steady speed |
## Production notes
The control is a real `<button>` with `aria-pressed` reflecting the current mode and an `aria-label` that names the *action* ("Switch to dark theme"), updated on each toggle so screen readers announce the change. The preview card is `aria-hidden` since it only illustrates the effect. On first load, read the user's stored preference and fall back to `prefers-color-scheme`: `const dark = localStorage.getItem('theme') === 'dark' || (localStorage.getItem('theme') === null && matchMedia('(prefers-color-scheme: dark)').matches)`, then set the class and `aria-pressed` before the button is interactive to avoid a flash. Persist the choice to `localStorage` on click so it survives reloads. Respect `prefers-reduced-motion: reduce` by dropping the transitions — the icon still snaps to the correct state, just without the morph. Icon sets like Feather ship the sun and moon as separate SVGs; the mask-based morph here is what libraries such as Framer Motion or GSAP would drive by tweening the mask offset and ray scale on the same timeline.
## See also
-- [Toggle / Switch Slide](../toggle-switch/)
-- [Hamburger Menu Toggle](../hamburger-menu-toggle/)
-- [Checkmark Draw](../checkmark-draw/)
+- [Toggle / Switch Slide](../toggle-switch/) — a switch that slides between on and off
+- [Hamburger Menu Toggle](../hamburger-menu-toggle/) — three lines that turn into an X
+- [Checkmark Draw](../checkmark-draw/) — a tick that draws itself
diff --git a/animations/04-micro-interactions/theme-toggle-morph/index.html b/animations/04-micro-interactions/theme-toggle-morph/index.html
index 28b7dfff..b4edfb63 100644
--- a/animations/04-micro-interactions/theme-toggle-morph/index.html
+++ b/animations/04-micro-interactions/theme-toggle-morph/index.html
@@ -4,39 +4,28 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Theme Toggle Morph — Animation Handbook</title>
- <meta name="description" content="A sun retracts its rays and slides a mask into a crescent moon — one icon animating between light and dark.">
+ <meta name="description" content="A sun turns into a moon as the colors switch to dark. Best for theme buttons.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/theme-toggle-morph/">
<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="Theme Toggle Morph — Animation Handbook — Animation Handbook">
- <meta property="og:description" content="A sun retracts its rays and slides a mask into a crescent moon — one icon animating between light and dark.">
+ <meta property="og:title" content="Theme Toggle Morph — Animation Handbook">
+ <meta property="og:description" content="A sun turns into a moon as the colors switch to dark. Best for theme buttons.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/theme-toggle-morph/">
<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="Theme Toggle Morph — Animation Handbook — Animation Handbook">
- <meta name="twitter:description" content="A sun retracts its rays and slides a mask into a crescent moon — one icon animating between light and dark.">
+ <meta name="twitter:title" content="Theme Toggle Morph — Animation Handbook">
+ <meta name="twitter:description" content="A sun turns into a moon as the colors switch to dark. Best for theme buttons.">
<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": "Theme Toggle Morph — Animation Handbook", "name": "Theme Toggle Morph — Animation Handbook", "description": "A sun retracts its rays and slides a mask into a crescent moon — one icon animating between light and dark.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/theme-toggle-morph/", "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, Theme Toggle Morph — Animation Handbook"}</script>
+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Theme Toggle Morph", "name": "Theme Toggle Morph", "description": "A sun turns into a moon as the colors switch to dark. Best for theme buttons.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/theme-toggle-morph/", "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, Theme Toggle Morph"}</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;--morph-dur:500ms;--morph-ease:cubic-bezier(.4,0,.2,1);--toggle-size:104px}
+ :root{--ui-accent:#ff9d5c;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--morph-dur:500ms;--morph-ease:cubic-bezier(.4,0,.2,1);--toggle-size:clamp(72px,12vw,104px)}
*,*::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-wrap{flex:1;min-width:0}
- .stage{height:var(--stage-h);border:1px solid var(--ui-border);border-radius:8px;background:var(--bg);display:flex;flex-direction:column;align-items:center;justify-content:center;gap:clamp(28px,5vh,52px);padding:32px}
+ .stage{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:clamp(20px,4vh,40px);padding:32px;background:#0b0b0d}
/* The toggle button */
- .toggle{width:calc(var(--toggle-size) + 28px);height:calc(var(--toggle-size) + 28px);border-radius:50%;border:1px solid var(--ui-border);background:var(--ui-bg);color:var(--ui-accent);display:flex;align-items:center;justify-content:center;cursor:pointer;flex-shrink:0;transition:color var(--morph-dur) var(--morph-ease),background var(--morph-dur) var(--morph-ease)}
- .toggle:focus-visible{outline:2px solid var(--ui-accent);outline-offset:3px}
+ .toggle{width:calc(var(--toggle-size) + 28px);height:calc(var(--toggle-size) + 28px);border-radius:50%;border:1px solid var(--ui-border);background:var(--ui-bg);color:var(--ui-accent);display:flex;align-items:center;justify-content:center;cursor:pointer;flex-shrink:0;font-family:inherit;transition:color var(--morph-dur) var(--morph-ease),background var(--morph-dur) var(--morph-ease)}
.icon{width:var(--toggle-size);height:var(--toggle-size);display:block}
.icon line{stroke:currentColor;stroke-width:2;stroke-linecap:round;transform-origin:12px 12px;transition:transform var(--morph-dur) var(--morph-ease),opacity var(--morph-dur) var(--morph-ease)}
.icon .disc{fill:currentColor;transform-origin:12px 12px;transition:transform var(--morph-dur) var(--morph-ease)}
@@ -47,7 +36,7 @@
.demo.dark .icon .disc{transform:scale(1.28)}
.demo.dark .icon .cutout{transform:translate(7px,-4px)}
/* Flip preview card */
- .card{width:min(260px,80%);aspect-ratio:8/5;perspective:1000px}
+ .card{flex-shrink:0;width:min(220px,80%);aspect-ratio:8/5;perspective:1000px}
.card-inner{position:relative;width:100%;height:100%;transform-style:preserve-3d;transition:transform var(--morph-dur) var(--morph-ease)}
.demo.dark .card-inner{transform:rotateY(180deg)}
.face{position:absolute;inset:0;backface-visibility:hidden;border-radius:10px;border:1px solid;padding:16px;display:flex;flex-direction:column;justify-content:space-between}
@@ -55,49 +44,37 @@
.dot{width:12px;height:12px;border-radius:50%}
.bar{height:8px;border-radius:4px}
.face .tt{font-size:12px;font-weight:bold}
- .face .st{font-size:9px}
+ .face .st{font-size:11px}
.face.light{background:#f4f4f2;border-color:#d9d9d6;color:#1a1a1c}
.face.light .bar{background:#c9c9c4}.face.light .dot{background:#ff9d5c}
.face.dark-face{background:#141418;border-color:rgba(255,255,255,.12);color:#f4f4f2;transform:rotateY(180deg)}
.face.dark-face .bar{background:#33333a}.face.dark-face .dot{background:#cdd3ff}
- .state-label{font-size:11px;color:var(--ui-muted);text-align:center;min-height:20px}
- /* 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)}
- .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:52px;text-align:right;color:var(--ui-text)}
- select{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:var(--mono);font-size:10px;padding:8px 6px;border-radius:4px;width:100%;min-height:44px}
- button.rst{background:transparent;border:1px solid var(--ui-border);color:var(--ui-text);font-family:var(--mono);font-size:10px;padding:14px 8px;border-radius:4px;cursor:pointer;width:100%;min-height:44px}
- @media(hover:hover){button.rst:hover{border-color:var(--ui-accent);color:var(--ui-accent)}.toggle:hover{background:#17171b}}
- @media(max-width:600px){.layout{flex-direction:column}.stage-wrap{width:100%}aside{width:100%}:root{--stage-h:auto}.stage{height:auto;min-height:440px}}
+ @media(hover:hover){.toggle:hover{background:#17171b}}
+ /* A window this short gives the stage 260px, less than the button and the card need: the icon, the card and the gap shrink together */
+ @media(min-width:601px) and (max-height:640px){.stage{--toggle-size:80px;gap:16px}.card{width:min(190px,80%)}}
@media(prefers-reduced-motion:reduce){.icon line,.icon .disc,.icon .cutout,.card-inner,.toggle{transition:none!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="../hamburger-menu-toggle/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../copy-to-clipboard/" 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/theme-toggle-morph/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>
-<header>
- <h1>Theme Toggle Morph</h1>
- <p>A sun retracts its rays and slides a mask into a crescent moon — one icon animating between light and dark.</p>
-</header>
-<div class="layout">
- <div class="stage-wrap">
- <div class="stage demo" id="demo">
+<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="../hamburger-menu-toggle/" rel="prev" aria-label="Previous: Hamburger Menu Toggle"><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>Hamburger Menu Toggle</span></a><a href="../copy-to-clipboard/" rel="next" aria-label="Next: Copy to Clipboard"><span><span class="hb-dir">Next: </span>Copy to Clipboard</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.24 · Micro-Interactions</p>
+ <h1>Theme Toggle Morph</h1>
+ <p class="hb-lede">A sun turns into a moon as the colors switch to dark. Best for theme buttons.</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 sun to switch to dark and the moon to switch back, or press Show me.</p></div>
+ </div>
+ <div class="stage demo hb-dots" id="demo">
<button class="toggle" id="toggle" type="button" aria-pressed="false" aria-label="Switch to dark theme">
<svg class="icon" viewBox="0 0 24 24" aria-hidden="true">
<defs>
@@ -131,53 +108,129 @@ body{padding-top:46px}
</div>
</div>
</div>
- <div class="state-label" id="state-label">Light theme active</div>
</div>
- </div>
- <aside>
- <p class="note">The sun's rays scale to zero from the center while an SVG mask circle slides across the disc, carving out a crescent. The card flips on the same clock.</p>
- <div class="lbl">Morph Duration</div>
- <div class="sr"><input type="range" id="dur-sl" min="150" max="1200" value="500" step="50"><span class="sv" id="dur-v">500ms</span></div>
- <div class="lbl">Easing</div>
- <select id="ease-sel">
- <option value="cubic-bezier(.4,0,.2,1)" selected>Standard</option>
- <option value="cubic-bezier(.34,1.56,.64,1)">Overshoot</option>
- <option value="ease-in-out">Ease in-out</option>
- <option value="linear">Linear</option>
- </select>
- <div class="lbl">Toggle Size</div>
- <div class="sr"><input type="range" id="size-sl" min="64" max="140" value="104" step="4"><span class="sv" id="size-v">104px</span></div>
- <div class="div"></div>
- <button class="rst" id="reset-btn">↺ Reset to light</button>
- </aside>
-</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="800" aria-pressed="false">Slow</button>
+ <button type="button" data-ms="500" class="on" aria-pressed="true">Normal</button>
+ <button type="button" data-ms="300" aria-pressed="false">Fast</button>
+ </div>
+ <p class="hb-hint">How long the sun takes to become a moon.</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(.4,0,.2,1)" class="on" aria-pressed="true">Gentle</button>
+ <button type="button" data-ease="cubic-bezier(.34,1.56,.64,1)" aria-pressed="false">Springy</button>
+ <button type="button" data-ease="linear" aria-pressed="false">Even</button>
+ </div>
+ <p class="hb-hint">Springy overshoots a little, then settles.</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 a theme toggle whose icon turns from a sun into a moon to [the button that switches your site between light and dark]. When it is pressed, the sun's rays shrink into its center while a circle slides across the sun and cuts it into a crescent, so the sun becomes the moon instead of being swapped for another icon. Switch the page's colors at the same moment and at the same pace. Make it a real button whose label says what pressing it will do, and remember the visitor's choice for their next visit. If the visitor has reduced motion turned on, switch the icon and the colors without movement. 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>
+ </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>Theme buttons</li><li>Settings</li><li>Headers</li></ul>
+ <h3>Avoid on</h3>
+ <ul class="hb-tags hb-avoid"><li>Dense forms</li><li>Icons without labels</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 demo=document.getElementById('demo');
+ const stage=document.getElementById('demo');
const toggle=document.getElementById('toggle');
- const label=document.getElementById('state-label');
- const root=document.documentElement;
+ const slowTog=document.getElementById('slow-tog');
+ const vars=document.documentElement.style;
+ // Everything on the stage that moves or changes color, so a run can start from rest without animating
+ const moving=[...stage.querySelectorAll('.toggle,.icon line,.icon .disc,.icon .cutout,.card-inner')];
+ let dur=500,runTimers=[];
+ // The button and Show me both switch the theme through setDark().
function setDark(on){
- demo.classList.toggle('dark',on);
+ stage.classList.toggle('dark',on);
toggle.setAttribute('aria-pressed',String(on));
toggle.setAttribute('aria-label',on?'Switch to light theme':'Switch to dark theme');
- label.textContent=on?'Dark theme active':'Light theme active';
}
- toggle.addEventListener('click',()=>setDark(!demo.classList.contains('dark')));
- document.getElementById('reset-btn').addEventListener('click',()=>setDark(false));
+ toggle.addEventListener('click',()=>setDark(!stage.classList.contains('dark')));
- document.getElementById('dur-sl').addEventListener('input',e=>{
- document.getElementById('dur-v').textContent=e.target.value+'ms';
- root.style.setProperty('--morph-dur',e.target.value+'ms');
- });
- document.getElementById('ease-sel').addEventListener('change',e=>{
- root.style.setProperty('--morph-ease',e.target.value);
- });
- document.getElementById('size-sl').addEventListener('input',e=>{
- document.getElementById('size-v').textContent=e.target.value+'px';
- root.style.setProperty('--toggle-size',e.target.value+'px');
+ // 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 ms: Slow motion triples the movement, never the hold.
+ function later(fn,move,hold){
+ const id=setTimeout(()=>{runTimers=runTimers.filter(t=>t!==id);fn()},move*(slowTog.checked?3:1)+(hold||0));
+ runTimers.push(id);
+ }
+ function running(){return runTimers.length>0}
+ function stopRun(){runTimers.forEach(clearTimeout);runTimers=[]}
+ // A run starts in the light theme, put there without animating: the transitions are turned off, the theme is switched,
+ // a reflow makes it stick, and the transitions come back.
+ function toRest(){
+ moving.forEach(el=>el.style.setProperty('transition','none','important'));
+ setDark(false);
+ void stage.offsetWidth;
+ moving.forEach(el=>el.style.removeProperty('transition'));
+ }
+ // The sun becomes the moon as a press makes it, stays a moment, and after one change plus a 1000ms hold the moon becomes the sun again.
+ document.getElementById('btn-demo').addEventListener('click',()=>{
+ stopRun();
+ toRest();
+ setDark(true);
+ later(()=>setDark(false),dur,1000);
});
+ // The visitor pressed, typed, scrolled or touched inside the stage: the run stops and the theme stays as it is.
+ document.addEventListener('hb:input',()=>{if(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('--morph-dur',dur+'ms')});
+ choices('feel-seg',d=>{vars.setProperty('--morph-ease',d.ease)});
</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.html
index 52a365fd..2107d91a 100644
--- a/index.html
+++ b/index.html
@@ -974,7 +974,7 @@ const CATS=[
['error-shake','Error Shake','An invalid field shakes horizontally with a decaying wobble and flashes red — the universal "no".'],
['swipe-to-dismiss','Swipe to Dismiss','Drag a card past a threshold and it flies off as the row collapses — pointer-driven, touch-ready.'],
['hamburger-menu-toggle','Hamburger Menu Toggle','Three lines turn into an X as the menu opens. Best for mobile menus.'],
- ['theme-toggle-morph','Theme Toggle Morph','A sun morphs into a crescent moon as the interface flips between light and dark.'],
+ ['theme-toggle-morph','Theme Toggle Morph','A sun turns into a moon as the colors switch to dark. Best for theme buttons.'],
['copy-to-clipboard','Copy to Clipboard','The copy button swaps to a checkmark and a Copied confirmation, then reverts after a moment.'],
['star-rating','Star Rating','Stars fill toward the pointer and pop on commit — an accessible five-star control.'],
['toast-notification','Toast Notification','Notification cards slide in from a corner, stack, and auto-dismiss with a progress bar — swipe or click to dismiss.'],