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.

32.8 KB
Raw Blame
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Copy to Clipboard — Animation Handbook</title>
  <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">
  <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="Copy to Clipboard — Animation Handbook">
  <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="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": "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>
    :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}
    .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;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}
    .copy-btn.copied::after{animation:pulse 520ms var(--ease)}
    @keyframes pulse{0%{opacity:.28;transform:scale(.6)}100%{opacity:0;transform:scale(1.08)}}
    /* icon stack: two icons crossfade in place */
    .ic{position:relative;width:16px;height:16px;flex-shrink:0}
    .ic svg{position:absolute;inset:0;width:16px;height:16px;stroke:currentColor;fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;transition:opacity var(--dur) var(--ease),transform var(--dur) var(--ease)}
    .ic .clip{opacity:1;transform:scale(1)}
    .ic .chk{opacity:0;transform:scale(.5)}
    .ic .chk path{stroke-dasharray:24;stroke-dashoffset:24;transition:stroke-dashoffset var(--dur) var(--ease)}
    .copy-btn.copied .clip{opacity:0;transform:scale(.5)}
    .copy-btn.copied .chk{opacity:1;transform:scale(1)}
    .copy-btn.copied .chk path{stroke-dashoffset:0;transition-delay:80ms}
    /* label crossfade */
    .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)}
    /* 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}
    @media(prefers-reduced-motion:reduce){.copy-btn,.copy-btn::after,.ic svg,.ic .chk path,.labels span{transition:none!important;animation:none!important}}
    /* See inside and Compare, drawn around the real button from the page's own numbers (the script says how) */
    .stage{position:relative}
    .views,.leads,.tbar,.cmp-one,.cmp-name,.pose{display:none}
    .cap{padding:3px 7px;border-radius:5px;background:rgba(11,11,13,.88);color:#f4f4f2;font:600 13px/1.25 var(--hb-font);text-wrap:balance}
    .leads{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none}
    .leads line{stroke:rgba(244,244,242,.75);stroke-width:1.5}
    .leads .l-flash{opacity:0}
    .stage[data-hb-mode="inside"]{padding:clamp(10px,2vw,16px)}
    .stage[data-hb-mode="inside"] .copy-wrap{display:grid;width:100%;grid-template-columns:minmax(0,1fr) auto minmax(0,1fr);align-items:center;justify-items:center;gap:6px 12px}
    .stage[data-hb-mode="inside"] .copy-row,.stage[data-hb-mode="inside"] .views{display:contents}
    .stage[data-hb-mode="inside"] .leads{display:block}
    .stage[data-hb-mode="inside"] .code-box{grid-area:1/2}
    .stage[data-hb-mode="inside"] .copy-btn{grid-area:2/2;margin:var(--my,22px) var(--mx,50px);transform:scale(2)}
    .stage[data-hb-mode="inside"] .ic::after,.stage[data-hb-mode="inside"] .labels::after{content:"";position:absolute;inset:-3px;border:1px dashed rgba(244,244,242,.9);border-radius:4px}
    .stage[data-hb-mode="inside"] .views .cap{display:block;max-width:100%}
    .stage[data-hb-mode="inside"] .c-icons{grid-area:2/1;justify-self:end;text-align:right}
    .stage[data-hb-mode="inside"] .c-words{grid-area:2/3;justify-self:start}
    .stage[data-hb-mode="inside"] .c-flash{grid-area:1/3;justify-self:start;align-self:end;opacity:0}
    .stage[data-hb-mode="inside"] .c-swap{grid-area:3/1/4/-1;text-align:center}
    .stage[data-hb-mode="inside"] .tbar{display:block;grid-area:4/1/5/-1;position:relative;width:min(100%,320px);height:12px}
    .stage[data-hb-mode="inside"] .c-bar{grid-area:5/1/6/-1;text-align:center}
    .stage[data-hb-mode="inside"] .fail{position:static;transform:none;grid-area:3/1/4/-1;justify-self:center;max-width:100%;text-align:center}
    .tbar i{position:absolute;left:0;right:0;top:3px;height:6px;border-radius:3px}
    .t-track{background:rgba(244,244,242,.2)}
    .t-bar{background:var(--ok);transform-origin:0 50%}
    .t-mark{right:auto;left:-6px;top:0;width:12px;height:12px;border-radius:50%;background:#f4f4f2}
    .stage[data-hb-mode="inside"]:has(#copyBtn.copied) .c-flash,.stage[data-hb-mode="inside"]:has(#copyBtn.copied) .l-flash{animation:flashcap 900ms linear both}
    @keyframes flashcap{0%,65%{opacity:1}100%{opacity:0}}
    @media(max-width:600px),(max-height:500px){.stage[data-hb-mode="inside"] .views .c-flash,.stage[data-hb-mode="inside"] .l-flash{display:none}}
    .stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));align-items:center;justify-items:center;padding:0 8px}
    .stage[data-hb-mode="compare"]::after{content:"";position:absolute;top:0;bottom:0;left:50%;border-left:1px solid rgba(244,244,242,.12);pointer-events:none}
    .stage[data-hb-mode="compare"] .cmp-one,.stage[data-hb-mode="compare"] .copy-wrap{margin-top:30px;max-width:100%}
    .stage[data-hb-mode="compare"] .cmp-one{display:block}
    .stage[data-hb-mode="compare"] .cmp-name{display:block;position:absolute;top:10px;left:12px}
    .stage[data-hb-mode="compare"] .cmp-name.second{left:calc(50% + 12px)}
    .stage[data-hb-mode="compare"] .fail{top:calc(100% + 6px)}
    .cmp-one .copy-btn{cursor:default;text-align:center}
    @media(max-width:340px){.stage[data-hb-mode="compare"] .code-box{padding:8px 10px}.stage[data-hb-mode="compare"] .code{font-size:17px}}
    @media(max-width:600px),(max-height:500px){.stage[data-hb-mode="compare"] .copy-row{gap:8px}.stage[data-hb-mode="compare"] .code-box{padding:8px 12px}}
    .stage[data-hb-mode="compare"] .pose:not([hidden]){display:inline-flex;position:absolute;pointer-events:none;text-align:center}
    .pose.snap,.pose.snap *,.pose.snap::after{transition:none!important;animation:none!important}
    .copy-wrap:has(.copy-btn:focus-visible) .pose{outline:2px solid var(--hb-accent);outline-offset:-3px}
    /* phones: both views, after the rules they change */
    @media(max-width:600px){
      .stage[data-hb-mode="inside"] .copy-wrap{grid-template-columns:minmax(0,1fr)}
      .stage[data-hb-mode="inside"] .code-box{display:none}
      .stage[data-hb-mode="inside"] .c-icons{grid-area:1/1;justify-self:center;text-align:center}
      .stage[data-hb-mode="inside"] .copy-btn{grid-area:2/1}
      .stage[data-hb-mode="inside"] .c-words{grid-area:3/1;justify-self:center;text-align:center}
      .stage[data-hb-mode="inside"] .c-swap,.stage[data-hb-mode="inside"] .fail{grid-area:4/1}
      .stage[data-hb-mode="inside"] .tbar{grid-area:5/1;justify-self:center}
      .stage[data-hb-mode="inside"] .c-bar{grid-area:6/1}
      .stage[data-hb-mode="compare"]{grid-template-columns:minmax(0,1fr);grid-template-rows:repeat(2,minmax(0,1fr))}
      .stage[data-hb-mode="compare"]::after{top:50%;bottom:auto;left:0;right:0;border-left:0;border-top:1px solid rgba(244,244,242,.12)}
      .stage[data-hb-mode="compare"] .cmp-name.second{left:12px;top:calc(50% + 8px)}
    }
    @media(prefers-reduced-motion:reduce){.stage[data-hb-mode="inside"] .c-flash,.stage[data-hb-mode="inside"] .l-flash{animation:none!important}}
  </style>
  <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=11">
  <script src="../../../assets/js/demo-page.js?v=11" defer></script>
</head>
<body class="hb" data-hb-kind="do" data-hb-autoplay>
<nav class="hb-bar" aria-label="Animation Handbook">
  <a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>
  <span class="hb-pager"><a href="../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>
  <a class="hb-star" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener" aria-label="Star on GitHub"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg><span>Star on GitHub</span></a>
</nav>
<main class="hb-page">
  <header class="hb-head">
    <p class="hb-cat">04.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="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>
      <button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>
      <button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>
      <button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>
    </div>
    <div class="stage hb-dots">
      <div class="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>
        <div class="views" aria-hidden="true">
          <span class="cap c-icons" data-hb-caption>So do the clipboard and the tick</span>
          <span class="cap c-words" data-hb-caption>Copy and Copied! share this spot</span>
          <span class="cap c-flash" data-hb-caption>A green flash spreads and fades</span>
          <span class="cap c-swap" data-hb-caption>Swaps in about a quarter of a second, with a small bounce</span>
          <span class="tbar"><i class="t-track"></i><i class="t-bar"></i><i class="t-mark"></i></span>
          <span class="cap c-bar" data-hb-caption>Changes back after a second and a half</span>
        </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>
      <svg class="leads" aria-hidden="true"><line class="l-icons"/><line class="l-words"/><line class="l-flash"/><line class="l-swap"/><line class="l-bar"/></svg>
      <span class="cap cmp-name first" aria-hidden="true" data-hb-caption>No sign</span>
      <span class="cap cmp-name second" aria-hidden="true" data-hb-caption>Confirms</span>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-demo" data-hb-demo><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/></svg>Show me</button>
      <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo="css" autocomplete="off"><span>Slow motion</span></label>
    </div>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, both icons and both words share one spot each, so the button never changes size; the bar shows how long the confirmation stays. Press Copy, or Show me.</p>
      <p data-hb-for="compare" hidden>No sign: the code is copied but nothing shows it; Confirms: a tick, a green flash and Copied tell you it worked. Press the button under Confirms, or Show me.</p>
    </div>
  </section>

  <section class="hb-step hb-try" aria-labelledby="hb-try-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">2</span>
      <div><h2 id="hb-try-title">Try it</h2><p>Change a setting, then try it again or press Show me.</p></div>
    </div>
    <div class="hb-settings">
      <div class="hb-setting">
        <p class="hb-setting-name" id="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>
    <div class="hb-star-row"><p>Free and open source. If this helped, a star on GitHub helps other people find it.</p><a class="hb-star-btn" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>Star on GitHub</a></div>
  </section>

  <section class="hb-about" aria-labelledby="hb-what-title">
    <div class="hb-what">
      <h2 id="hb-what-title">What it is</h2>
      <div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>
    </div>
    <div class="hb-fit">
      <h2>Good for</h2>
      <ul class="hb-tags hb-good"><li>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="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener">Source on GitHub</a><a href="#top">Back to top ↑</a></footer>
<script>
  const btn=document.getElementById('copyBtn');
  const code=document.getElementById('snippet');
  const live=document.getElementById('live');
  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
  const NAME='Copy invite code',DONE='Copied: invite code'; // the button's name, which holds the visible word while Copied! shows
  let timer=0,failTimer=0,revert=1500;
  // How long Copied! stays: the pulse counts as movement (tripled in Slow motion) and the rest of the wait as a hold
  const stays=()=>PULSE*(slowTog.checked?3:1)+(revert-PULSE);

  function legacyCopy(text){
    return new Promise((res,rej)=>{
      // 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){
    // 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; the button changes back on its own timer (stays() long).
  function showCopied(){
    btn.classList.add('copied');
    btn.setAttribute('aria-label',DONE);
    hideFail();
    clearTimeout(timer);
    lift();
    runStart=performance.now();runTotal=stays();
    timer=setTimeout(()=>{btn.classList.remove('copied');btn.setAttribute('aria-label',NAME);live.textContent='';settle()},runTotal);
    draw();
  }
  // 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');
    btn.setAttribute('aria-label',NAME);
    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);
    lift();settle();
  }
  function hideFail(){
    clearTimeout(failTimer);
    fail.hidden=true;
    if(live.textContent===fail.textContent)live.textContent='';
    settle();
  }

  btn.addEventListener('click',()=>{
    copying=true;
    copyText(code.innerText).then(()=>{
      copying=false;
      live.textContent='Copied to clipboard';
      showCopied();
    },()=>{copying=false;showFail()});
  });

  // 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');
    btn.setAttribute('aria-label',NAME);
    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,i)=>b.addEventListener('click',()=>{
      buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
      pick(b.dataset,i);
      draw();
    }));
  }
  choices('speed-seg',(d,i)=>{vars.setProperty('--dur',d.ms+'ms');speedAt=i});
  choices('revert-seg',d=>{revert=+d.ms});
  choices('feel-seg',(d,i)=>{vars.setProperty('--ease',d.ease);feelAt=i});

  // See inside and Compare, drawn from the numbers above (--dur, --ease, stays()); they read the button and never change it.
  const $=s=>document.querySelector(s);
  const stage=$('.stage'),wrap=$('.copy-wrap'),row=$('.copy-row'),ic=btn.querySelector('.ic'),labs=btn.querySelector('.labels');
  const cap={},ln={};
  ['icons','words','flash','swap','bar'].forEach(k=>{cap[k]=$('.c-'+k);ln[k]=$('.l-'+k)});
  const tbar=$('.tbar'),bar=$('.t-bar'),mark=$('.t-mark');
  const motionOk=!matchMedia('(prefers-reduced-motion:reduce)').matches,phoneMQ=matchMedia('(max-width:600px)');
  const SCALE=4000,POSE_MS=120; // the bar's fixed scale (four seconds); how far into the swap Compare's copy is caught (the tick has begun to draw)
  const SWAP=[['almost half a second','more than a second'],['about a quarter of a second','almost a second'],['about a sixth of a second','about half a second']];
  const FEEL=['with a small bounce','slowing to a stop','at one steady speed'];
  const HALVES=['','half a second','a second','a second and a half','two seconds','two and a half seconds','three seconds','three and a half seconds','four seconds'];
  let mode='normal',speedAt=1,feelAt=0,copying=false,runStart=0,runTotal=0,markRaf=0,backTimer=0;
  if(!motionOk)$('[data-hb-for="compare"]').textContent='No sign: the code is copied but nothing shows it; Confirms: a tick and Copied tell you it worked. Press the button under Confirms, or Show me.';

  // Compare's copies, made once: the first half (a button that is only a picture) and the button caught in its swap
  const one=document.createElement('div'),row1=row.cloneNode(true),still=document.createElement('span');
  one.className='cmp-one';one.inert=true;one.setAttribute('aria-hidden','true');
  row1.querySelectorAll('[id]').forEach(e=>e.removeAttribute('id'));
  still.className=btn.className;still.innerHTML=btn.innerHTML;row1.querySelector('.copy-btn').replaceWith(still);
  one.append(row1);wrap.before(one);
  const pose=document.createElement('span');
  pose.className=btn.className+' pose';pose.innerHTML=btn.innerHTML;pose.inert=true;pose.hidden=true;pose.setAttribute('aria-hidden','true');
  row.after(pose);

  const say=(el,t)=>{if(el.textContent!==t)el.textContent=t};
  const total=()=>runStart?runTotal:stays(); // the run on show, else what the settings give
  function draw(){
    if(mode==='inside')drawInside();
    else if(mode==='compare'){if(canPose())drawPose();else pose.hidden=true}
    else pose.hidden=true;
  }
  document.addEventListener('hb:mode',e=>{mode=e.detail.mode;draw()});
  addEventListener('resize',draw);
  slowTog.addEventListener('change',draw);
  if(document.fonts)document.fonts.ready.then(draw);

  // See inside. The button is twice as large (margins make room), its two spots outlined; the bar is a fixed four seconds long
  // with the part Copied! stays filled and a marker that runs along it by the clock from the timer's start (the same sum).
  function drawInside(){
    btn.style.setProperty('--mx',btn.offsetWidth/2+'px');btn.style.setProperty('--my',btn.offsetHeight/2+'px');
    const t=total();
    say(cap.swap,'Swaps in '+SWAP[speedAt][+slowTog.checked]+', '+FEEL[feelAt]);
    say(cap.bar,'Changes back after '+(t%500?'about ':'')+HALVES[Math.round(t/500)]);
    bar.style.transform=`scaleX(${t/SCALE})`;
    place(t/SCALE);
    drawMark();
  }
  function drawMark(){
    const p=runStart&&motionOk?Math.min(1,(performance.now()-runStart)/runTotal):0;
    mark.style.transform=`translateX(${p*total()/SCALE*tbar.clientWidth}px)`;
    cancelAnimationFrame(markRaf);
    if(runStart&&motionOk&&p<1&&mode==='inside')markRaf=requestAnimationFrame(drawMark);
  }
  // Words go beside the button on a wide stage, over and under it on a phone's; a short line joins each to its part.
  function place(f){
    const s=stage.getBoundingClientRect(),ox=s.left+stage.clientLeft,oy=s.top+stage.clientTop,W=stage.clientWidth,ph=phoneMQ.matches;
    const R=el=>{const r=el.getBoundingClientRect();return{l:r.left-ox,t:r.top-oy,r:r.right-ox,b:r.bottom-oy,x:(r.left+r.right)/2-ox,y:(r.top+r.bottom)/2-oy}};
    const seg=(k,a,b,c,d)=>{const e=ln[k];e.setAttribute('x1',a);e.setAttribute('y1',b);e.setAttribute('x2',c);e.setAttribute('y2',d)};
    const mid=(v,a,b)=>Math.min(Math.max(v,a),b);
    // a label centered on x, kept inside the box
    const over=(el,x)=>{el.style.translate='';const r=R(el),nx=mid(x-(r.r-r.l)/2,6,W-6-(r.r-r.l));el.style.translate=(nx-r.l)+'px';return R(el)};
    const grow=o=>({l:o.l-6,t:o.t-6,r:o.r+6,b:o.b+6,x:o.x,y:o.y}); // a spot's dashed line stands 3px out, twice as large
    const B=R(btn),Io=grow(R(ic)),Wo=grow(R(labs)),T=R(tbar);
    let L;
    if(ph){
      L=over(cap.icons,Io.x);seg('icons',mid(Io.x,L.l+4,L.r-4),L.b,mid(Io.x,L.l+4,L.r-4),Io.t);
      L=over(cap.words,Wo.x);seg('words',mid(Wo.x,L.l+4,L.r-4),L.t,mid(Wo.x,L.l+4,L.r-4),Wo.b);
    }else{
      cap.icons.style.translate=cap.words.style.translate='';L=R(cap.icons);seg('icons',L.r,L.y,Io.l,Io.y);
      L=R(cap.words);seg('words',L.l,L.y,Wo.r,Wo.y);
      L=R(cap.flash);seg('flash',L.l,L.b,B.r-26,B.t+22);
    }
    const sw=fail.hidden&&!ph;L=R(cap.swap);cap.swap.style.visibility=fail.hidden?'':'hidden';
    ln.swap.style.display=sw?'':'none';seg('swap',mid(B.x,L.l+8,L.r-8),L.t,mid(B.x,L.l+8,L.r-8),B.b);
    const end=T.l+f*(T.r-T.l);L=over(cap.bar,end);seg('bar',mid(end,L.l+4,L.r-4),T.t+9,mid(end,L.l+4,L.r-4),L.t);
  }

  // Compare. The real button is never touched: a copy laid over it is caught POSE_MS into the swap (its transitions and flash are
  // paused there, so Speed and Feel set how far along it is). A press on the real button, or Show me, lifts it before the real
  // one starts; it comes back once the real one is at rest and nothing is pending.
  const canPose=()=>mode==='compare'&&!btn.classList.contains('copied')&&!copying&&fail.hidden;
  function drawPose(){
    pose.style.cssText=`left:${btn.offsetLeft}px;top:${btn.offsetTop}px;width:${btn.offsetWidth}px;height:${btn.offsetHeight}px`;
    pose.hidden=false;
    pose.classList.remove('copied');pose.classList.add('snap');void pose.offsetWidth;
    pose.classList.remove('snap');pose.classList.add('copied');void pose.offsetWidth;
    pose.getAnimations({subtree:true}).forEach(a=>{a.pause();a.currentTime=POSE_MS});
  }
  function lift(){
    if(mode!=='compare')return;
    pose.hidden=true;
    clearTimeout(backTimer);backTimer=setTimeout(draw,900); // a press that came to nothing
  }
  const early={capture:true,passive:true};
  stage.addEventListener('keydown',e=>{if((e.key==='Enter'||e.key===' ')&&e.target===btn)lift()},early);
  ['pointerdown','touchstart','click'].forEach(type=>stage.addEventListener(type,e=>{if(btn.contains(e.target))lift()},early));
  // The button is back at rest (or a refused copy has been shown): the bar's marker waits at its start and the views redraw
  function settle(){
    Promise.allSettled(btn.getAnimations({subtree:true}).map(a=>a.finished)).then(()=>{
      if(btn.classList.contains('copied'))return;
      runStart=0;draw();
    });
  }
</script>
</body>
</html>