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 Copy to Clipboard to the guided-steps page

matin1b55c4b88231
diff --git a/animations/04-micro-interactions/copy-to-clipboard/README.md b/animations/04-micro-interactions/copy-to-clipboard/README.md
index 74447017..7f336d6e 100644
--- a/animations/04-micro-interactions/copy-to-clipboard/README.md
+++ b/animations/04-micro-interactions/copy-to-clipboard/README.md
@@ -1,7 +1,7 @@
# Copy to Clipboard
## What it is
-Copy-to-clipboard feedback is the transient confirmation a button gives after copying text. The moment the copy succeeds, the icon morphs from a clipboard glyph to a checkmark, the label crossfades from "Copy" to "Copied!", and a brief color pulse acknowledges the event. After about 1.5 seconds the button reverts to its resting state, ready to use again. It is the canonical example of transient success feedback: confirm, then get out of the way.
+Copy feedback is the short confirmation a Copy button gives after it copies something. The clipboard icon turns into a tick, the label changes from Copy to Copied! and a brief flash of color marks the moment. After a second or two the button changes back, ready to use again.
## When to use it
- Code blocks and API keys in documentation
@@ -33,15 +33,14 @@ btn.addEventListener('click', () => {
.copied .chk { opacity: 1; transform: scale(1); } /* checkmark in */
```
-The two labels are stacked in the same grid cell so they crossfade in place without shifting layout, and the two icons are absolutely positioned on top of each other so the clipboard scales out as the checkmark scales in. Only `opacity` and `transform` animate; the checkmark path adds a short `stroke-dashoffset` draw for a hand-drawn finish.
+The two labels are stacked in the same grid cell so they crossfade in place without shifting layout, and the two icons are absolutely positioned on top of each other so the clipboard scales out as the checkmark scales in. The icons and labels animate `opacity` and `transform`, the checkmark path adds a short `stroke-dashoffset` draw for a hand-drawn finish, the button's border and text fade to green with a color transition, and the pulse ring is a one-shot animation of `opacity` and `transform`. If the browser refuses the copy, the button stays as it is and a short message says so.
## Key parameters
| Parameter | Default | Effect |
|-----------|---------|--------|
-| Revert delay | 1500ms | How long "Copied!" stays before reverting. Under ~1s feels rushed; over ~3s lingers |
-| Animation duration | 280ms | Speed of the icon morph and label crossfade. 150–300ms reads as snappy |
-| Easing | overshoot | A slight spring overshoot on the checkmark gives the confirmation a bit of pop |
-| Pulse | ~520ms | One-shot color ring; runs once per copy, independent of the revert timer |
+| Speed | Normal | How long the icon and label take to change: slow is 450ms, normal 280ms and fast 170ms |
+| Time before it changes back | Medium | How long Copied! stays: short is 1 second, medium 1.5 and long 2.5; under a second feels rushed, and over about three seconds it lingers |
+| Feel | Springy | Springy gives the tick a small bounce; Smooth slows to a stop; Even keeps one steady speed |
## Production notes
- **Clipboard API support and fallback**: `navigator.clipboard.writeText` requires a secure context (HTTPS or `localhost`) and a user gesture. Feature-detect it and fall back to a hidden `<textarea>` plus `document.execCommand('copy')` for older or insecure-context browsers, as the demo does.
@@ -51,6 +50,6 @@ The two labels are stacked in the same grid cell so they crossfade in place with
- **Reduced motion**: under `prefers-reduced-motion: reduce`, drop the morph, pulse, and stroke draw — swap the icon and label instantly. The confirmation still lands, just without movement.
## See also
-- [Checkmark Draw](../checkmark-draw/) — the stroke-dashoffset technique behind the success tick
-- [Button Press Scale](../button-press-scale/) — the press feedback that precedes the copy
-- [Success Confetti](../success-confetti/) — a louder success celebration for higher-stakes actions
+- [Checkmark Draw](../checkmark-draw/) — a tick that draws itself
+- [Button Press Scale](../button-press-scale/) — a button that shrinks as you press it
+- [Success Confetti](../success-confetti/) — a bigger celebration for a bigger moment
diff --git a/animations/04-micro-interactions/copy-to-clipboard/index.html b/animations/04-micro-interactions/copy-to-clipboard/index.html
index f9c1cacb..b306f8f4 100644
--- a/animations/04-micro-interactions/copy-to-clipboard/index.html
+++ b/animations/04-micro-interactions/copy-to-clipboard/index.html
@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Copy to Clipboard — Animation Handbook</title>
- <meta name="description" content="A copy button morphs to a checkmark and &quot;Copied!&quot; on success, then reverts after a short delay.">
+ <meta name="description" content="Copy turns into a tick and Copied, then changes back. Best for codes and links.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/copy-to-clipboard/">
<link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
<link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
@@ -12,33 +12,26 @@
<meta property="og:type" content="article">
<meta property="og:site_name" content="Animation Handbook">
<meta property="og:title" content="Copy to Clipboard — Animation Handbook">
- <meta property="og:description" content="A copy button morphs to a checkmark and &quot;Copied!&quot; on success, then reverts after a short delay.">
+ <meta property="og:description" content="Copy turns into a tick and Copied, then changes back. Best for codes and links.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/copy-to-clipboard/">
<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="Copy to Clipboard — Animation Handbook">
- <meta name="twitter:description" content="A copy button morphs to a checkmark and &quot;Copied!&quot; on success, then reverts after a short delay.">
+ <meta name="twitter:description" content="Copy turns into a tick and Copied, then changes back. Best for codes and links.">
<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": "Copy to Clipboard", "name": "Copy to Clipboard", "description": "A copy button morphs to a checkmark and \"Copied!\" on success, then reverts after a short delay.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/copy-to-clipboard/", "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, Copy to Clipboard"}</script>
+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Copy to Clipboard", "name": "Copy to Clipboard", "description": "Copy turns into a tick and Copied, then changes back. Best for codes and links.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/copy-to-clipboard/", "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, Copy to Clipboard"}</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;--dur:280ms;--ease:cubic-bezier(.34,1.4,.5,1);--revert:1500ms;--ok:#56d364}
+ :root{--ui-accent:#ff9d5c;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-text:#f4f4f2;--ui-muted:#8a8a92;--bg:#0b0b0d;--ok:#56d364;--dur:280ms;--ease:cubic-bezier(.34,1.4,.5,1)}
*,*::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{border:1px solid var(--ui-border);border-radius:8px;background:var(--bg);padding:clamp(16px,3vw,28px);display:flex;flex-direction:column;gap:16px}
- .code-head{display:flex;align-items:center;justify-content:space-between;gap:12px;font-size:10px;color:var(--ui-muted);text-transform:uppercase;letter-spacing:1px}
- pre{background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:6px;padding:16px;overflow-x:auto;font-family:var(--mono);font-size:clamp(11px,1.5vw,13px);line-height:1.7;color:var(--ui-text)}
- pre .k{color:var(--ui-accent)}
+ .stage{display:flex;align-items:center;justify-content:center;padding:24px;background:var(--bg)}
+ .copy-wrap{position:relative}
+ .copy-row{display:flex;flex-wrap:wrap;align-items:center;justify-content:center;gap:12px}
+ /* the invite code */
+ .code-box{display:flex;flex-direction:column;gap:4px;padding:12px 16px;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px}
+ .code-label{font-size:12px;color:var(--ui-muted)}
+ .code{font-size:20px;font-weight:700;color:var(--ui-text)}
/* copy button */
- .copy-btn{position:relative;display:inline-flex;align-items:center;gap:8px;min-height:44px;padding:0 16px;align-self:flex-start;background:var(--ui-bg);color:var(--ui-text);border:1px solid var(--ui-border);border-radius:8px;font-family:var(--mono);font-size:12px;font-weight:500;cursor:pointer;overflow:hidden;transition:border-color var(--dur) var(--ease),color var(--dur) var(--ease)}
+ .copy-btn{position:relative;display:inline-flex;align-items:center;gap:8px;min-height:44px;padding:0 16px;background:var(--ui-bg);color:var(--ui-text);border:1px solid var(--ui-border);border-radius:8px;font:inherit;font-size:12px;font-weight:500;cursor:pointer;overflow:hidden;transition:border-color var(--dur) var(--ease),color var(--dur) var(--ease)}
.copy-btn.copied{border-color:var(--ok);color:var(--ok)}
/* pulse ring */
.copy-btn::after{content:"";position:absolute;inset:0;border-radius:8px;background:var(--ok);opacity:0;transform:scale(.6);pointer-events:none}
@@ -54,134 +47,235 @@
.copy-btn.copied .chk{opacity:1;transform:scale(1)}
.copy-btn.copied .chk path{stroke-dashoffset:0;transition-delay:80ms}
/* label crossfade */
- .lbls{position:relative;display:inline-grid}
- .lbls span{grid-area:1/1;transition:opacity var(--dur) var(--ease),transform var(--dur) var(--ease)}
+ .labels{position:relative;display:inline-grid}
+ .labels span{grid-area:1/1;transition:opacity var(--dur) var(--ease),transform var(--dur) var(--ease)}
.l-copied{opacity:0;transform:translateY(4px)}
.copy-btn.copied .l-copy{opacity:0;transform:translateY(-4px)}
.copy-btn.copied .l-copied{opacity:1;transform:translateY(0)}
- .hint{font-size:10px;color:var(--ui-muted)}
+ /* shown only when the copy did not work: below the row, so nothing else moves */
+ .fail{position:absolute;top:calc(100% + 12px);left:50%;transform:translateX(-50%);width:max-content;max-width:min(300px,80vw);font-size:12px;line-height:1.4;text-align:center;color:var(--ui-text)}
.sr-only{position:absolute;width:1px;height:1px;padding:0;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0}
- /* 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}
- @media(max-width:600px){.layout{flex-direction:column}aside{width:100%}}
- @media(prefers-reduced-motion:reduce){.copy-btn,.copy-btn::after,.ic svg,.ic .chk path,.lbls span{transition:none!important;animation:none!important}}
+ @media(prefers-reduced-motion:reduce){.copy-btn,.copy-btn::after,.ic svg,.ic .chk path,.labels span{transition:none!important;animation: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="../theme-toggle-morph/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../star-rating/" 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/copy-to-clipboard/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>
-<header>
- <h1>Copy to Clipboard</h1>
- <p>A copy button morphs to a checkmark and "Copied!" on success, then reverts after a short delay.</p>
-</header>
-<div class="layout">
- <div class="stage-wrap">
- <div class="stage">
- <div class="code-head"><span>snippet.js</span><span>click copy to grab it</span></div>
- <pre id="snippet"><span class="k">const</span> el = document.querySelector(<span class="k">'.card'</span>);
-el.animate(
- [{ opacity: <span class="k">0</span> }, { opacity: <span class="k">1</span> }],
- { duration: <span class="k">300</span>, easing: <span class="k">'ease-out'</span> }
-);</pre>
- <button class="copy-btn" id="copyBtn" type="button" aria-label="Copy code snippet">
- <span class="ic" aria-hidden="true">
- <svg class="clip" viewBox="0 0 16 16"><rect x="5" y="5" width="9" height="9" rx="1.5"/><path d="M11 5V3.5A1.5 1.5 0 0 0 9.5 2h-6A1.5 1.5 0 0 0 2 3.5v6A1.5 1.5 0 0 0 3.5 11H5"/></svg>
- <svg class="chk" viewBox="0 0 16 16"><path d="M3.5 8.5l3 3 6-6.5"/></svg>
- </span>
- <span class="lbls"><span class="l-copy">Copy</span><span class="l-copied">Copied!</span></span>
- </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="../theme-toggle-morph/" rel="prev" aria-label="Previous: Theme Toggle Morph"><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>Theme Toggle Morph</span></a><a href="../star-rating/" rel="next" aria-label="Next: Star Rating"><span><span class="hb-dir">Next: </span>Star Rating</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.25 · Micro-Interactions</p>
+ <h1>Copy to Clipboard</h1>
+ <p class="hb-lede">Copy turns into a tick and Copied, then changes back. Best for codes and links.</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 Copy and watch the button confirm, then change back, or press Show me.</p></div>
+ </div>
+ <div class="stage hb-dots">
+ <div class="copy-wrap">
+ <div class="copy-row">
+ <div class="code-box">
+ <span class="code-label">Your invite code</span>
+ <span class="code" id="snippet">MOTION-2026</span>
+ </div>
+ <button class="copy-btn" id="copyBtn" type="button" aria-label="Copy invite code">
+ <span class="ic" aria-hidden="true">
+ <svg class="clip" viewBox="0 0 16 16"><rect x="5" y="5" width="9" height="9" rx="1.5"/><path d="M11 5V3.5A1.5 1.5 0 0 0 9.5 2h-6A1.5 1.5 0 0 0 2 3.5v6A1.5 1.5 0 0 0 3.5 11H5"/></svg>
+ <svg class="chk" viewBox="0 0 16 16"><path d="M3.5 8.5l3 3 6-6.5"/></svg>
+ </span>
+ <span class="labels"><span class="l-copy">Copy</span><span class="l-copied">Copied!</span></span>
+ </button>
+ </div>
+ <p class="fail" id="fail" aria-hidden="true" hidden>Couldn't copy. Select the code and copy it yourself.</p>
+ </div>
<span class="sr-only" id="live" role="status" aria-live="polite"></span>
- <span class="hint">Uses navigator.clipboard.writeText with a document.execCommand fallback.</span>
</div>
- </div>
- <aside>
- <p class="note">Success feedback should be transient: confirm the action, then quietly revert so the control is ready to use again.</p>
- <div class="lbl">Revert delay</div>
- <div class="sr"><input type="range" id="rv" min="600" max="3000" value="1500" step="100"><span class="sv" id="rvv">1500ms</span></div>
- <div class="lbl">Animation duration</div>
- <div class="sr"><input type="range" id="du" min="120" max="600" value="280" step="20"><span class="sv" id="duv">280ms</span></div>
- <div class="lbl">Easing</div>
- <select id="es">
- <option value="cubic-bezier(.34,1.4,.5,1)" selected>Overshoot (spring)</option>
- <option value="ease-out">Ease out</option>
- <option value="ease-in-out">Ease in-out</option>
- <option value="linear">Linear</option>
- </select>
- <div class="div"></div>
- <p class="note" style="border-color:var(--ui-border)">The checkmark and label share one <code>.copied</code> class toggle; a timer removes it to revert.</p>
- </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="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">How quickly the icon and label change.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="revert-lbl">Time before it changes back</p>
+ <div class="seg" id="revert-seg" role="group" aria-labelledby="revert-lbl">
+ <button type="button" data-ms="1000" aria-pressed="false">Short</button>
+ <button type="button" data-ms="1500" class="on" aria-pressed="true">Medium</button>
+ <button type="button" data-ms="2500" aria-pressed="false">Long</button>
+ </div>
+ <p class="hb-hint">How long Copied! stays before the button resets.</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="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.4,.5,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 gives the tick a small bounce.</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 copy feedback to [the Copy button next to your code, link or key]. When the copy succeeds, turn the button's icon into a tick and its label into Copied!, with a brief flash of color, then change it back on its own after a moment so it is ready to use again. Stack both icons and both labels in the same place so the change never shifts the layout. Show the confirmation only once the copy has really worked, tell screen readers with a short status message, and say so if copying fails. If the visitor has reduced motion turned on, swap the icon and label 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>Share links</li><li>Invite codes</li><li>Code blocks</li><li>API keys</li></ul>
+ <h3>Avoid on</h3>
+ <ul class="hb-tags hb-avoid"><li>Slow actions</li><li>Important saves</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 btn=document.getElementById('copyBtn');
- const snippet=document.getElementById('snippet');
+ const code=document.getElementById('snippet');
const live=document.getElementById('live');
- const root=document.documentElement;
- let timer=null;
- let revert=1500;
+ const fail=document.getElementById('fail');
+ const slowTog=document.getElementById('slow-tog');
+ const vars=document.documentElement.style;
+ // Everything on the button that changes with .copied, so a run can start from rest without animating
+ const moving=[btn,...btn.querySelectorAll('.ic svg,.ic .chk path,.labels span')];
+ const PULSE=520; // length of the pulse ring in ms
+ let timer=0,failTimer=0,revert=1500;
function legacyCopy(text){
return new Promise((res,rej)=>{
- const ta=document.createElement('textarea');
- ta.value=text;ta.style.position='fixed';ta.style.opacity='0';
- document.body.appendChild(ta);ta.select();
- try{document.execCommand('copy')?res():rej();}catch(e){rej(e);}
- document.body.removeChild(ta);
+ // a temporary text box takes the focus to copy from; the button gets it back afterwards
+ const before=document.activeElement;
+ const box=document.createElement('textarea');
+ box.value=text;box.setAttribute('readonly','');
+ box.style.cssText='position:fixed;top:-1000px;opacity:0';
+ document.body.appendChild(box);
+ box.focus({preventScroll:true});box.select();box.setSelectionRange(0,text.length);
+ let ok=false;
+ try{ok=document.execCommand('copy')}catch(e){}
+ document.body.removeChild(box);
+ if(before&&before.focus)before.focus({preventScroll:true});
+ ok?res():rej(new Error('copy refused'));
});
}
function copyText(text){
- if(navigator.clipboard&&navigator.clipboard.writeText){
- // the API can exist yet reject (permission denied, embedded frame) — fall back then too
- return navigator.clipboard.writeText(text).catch(()=>legacyCopy(text));
- }
- return legacyCopy(text);
+ // the API can exist yet reject (permission denied, page not focused) — fall back then too
+ const viaApi=navigator.clipboard&&navigator.clipboard.writeText?navigator.clipboard.writeText(text):Promise.reject();
+ return viaApi.catch(()=>legacyCopy(text));
+ }
+
+ // A real copy and Show me both show the confirmation here, and the button changes back on its own timer. The timer counts the
+ // pulse as movement (tripled while Slow motion is on) and the rest of the wait as a hold, so the button never changes back
+ // before a slowed pulse has ended.
+ function showCopied(){
+ btn.classList.add('copied');
+ hideFail();
+ clearTimeout(timer);
+ timer=setTimeout(()=>{btn.classList.remove('copied');live.textContent=''},PULSE*(slowTog.checked?3:1)+(revert-PULSE));
+ }
+ // The copy did not work: say so, and never leave "Copied!" showing (a Show me run may have put it there).
+ function showFail(){
+ clearTimeout(timer);
+ btn.classList.remove('copied');
+ live.textContent=fail.textContent;
+ fail.hidden=false;
+ const range=document.createRange();range.selectNodeContents(code);
+ const picked=getSelection();picked.removeAllRanges();picked.addRange(range);
+ clearTimeout(failTimer);
+ failTimer=setTimeout(hideFail,5000);
+ }
+ function hideFail(){
+ clearTimeout(failTimer);
+ fail.hidden=true;
+ if(live.textContent===fail.textContent)live.textContent='';
}
btn.addEventListener('click',()=>{
- const text=snippet.innerText;
- copyText(text).then(()=>{
- btn.classList.add('copied');
+ copyText(code.innerText).then(()=>{
live.textContent='Copied to clipboard';
- clearTimeout(timer);
- timer=setTimeout(()=>{
- btn.classList.remove('copied');
- live.textContent='';
- },revert);
- }).catch(()=>{
- live.textContent='Copy failed — select and copy manually';
- });
+ showCopied();
+ },showFail);
});
- document.getElementById('rv').addEventListener('input',e=>{
- revert=+e.target.value;
- document.getElementById('rvv').textContent=e.target.value+'ms';
- });
- document.getElementById('du').addEventListener('input',e=>{
- document.getElementById('duv').textContent=e.target.value+'ms';
- root.style.setProperty('--dur',e.target.value+'ms');
- });
- document.getElementById('es').addEventListener('change',e=>{
- root.style.setProperty('--ease',e.target.value);
+ // Show me plays the confirmation without copying: writing to the clipboard would replace what the visitor copied, and
+ // browsers refuse a copy nobody pressed for. The run is only this one step, so it leaves no timer of its own (hb:input has
+ // nothing to stop); the button changes back on its own timer, just as after a real copy.
+ // A run starts from rest, put there without animating: the transitions are turned off, .copied goes, a reflow makes it
+ // stick, and the transitions come back.
+ function toRest(){
+ clearTimeout(timer);
+ hideFail();
+ live.textContent='';
+ moving.forEach(el=>el.style.setProperty('transition','none','important'));
+ btn.classList.remove('copied');
+ void btn.offsetWidth;
+ moving.forEach(el=>el.style.removeProperty('transition'));
+ }
+ document.getElementById('btn-demo').addEventListener('click',()=>{
+ toRest();
+ showCopied();
});
+
+ 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=>vars.setProperty('--dur',d.ms+'ms'));
+ choices('revert-seg',d=>{revert=+d.ms});
+ choices('feel-seg',d=>vars.setProperty('--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 2107d91a..7b51f177 100644
--- a/index.html
+++ b/index.html
@@ -975,7 +975,7 @@ const CATS=[
['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 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.'],
+ ['copy-to-clipboard','Copy to Clipboard','Copy turns into a tick and Copied, then changes back. Best for codes and links.'],
['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.'],
['segmented-control','Segmented Control','A highlighted pill slides beneath the selected segment as the active label crossfades — the iOS control.'],