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.

37.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>Tooltip Reveal — Animation Handbook</title>
  <meta name="description" content="A small label fades in after a short pause. Best for icon buttons.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/tooltip-reveal/">
  <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="Tooltip Reveal — Animation Handbook">
  <meta property="og:description" content="A small label fades in after a short pause. Best for icon buttons.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/tooltip-reveal/">
  <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="Tooltip Reveal — Animation Handbook">
  <meta name="twitter:description" content="A small label fades in after a short pause. Best for icon 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": "Tooltip Reveal", "name": "Tooltip Reveal", "description": "A small label fades in after a short pause. Best for icon buttons.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/tooltip-reveal/", "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, Tooltip Reveal"}</script>
  <style>
    :root{--ui-accent:#ff9d5c;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-text:#f4f4f2;--ui-muted:#8a8a92;--tip-bg:#1c1c22;--tip-dur:150ms}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:28px;padding:12px 16px;background:#0b0b0d}
    /* Tooltip base */
    .tip-wrap{position:relative;display:inline-flex}
    .tooltip{position:absolute;background:var(--tip-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-size:11px;padding:7px 10px;border-radius:6px;pointer-events:none;opacity:0;transform:scale(.95);transition:opacity var(--tip-dur) ease,transform var(--tip-dur) ease;z-index:10;width:max-content;max-width:200px;line-height:1.5}
    .tooltip.visible{opacity:1;transform:scale(1)}
    /* Positions */
    .tip-top{bottom:calc(100% + 8px);left:50%;transform:translateX(-50%) scale(.95);transform-origin:bottom center}
    .tip-top.visible{transform:translateX(-50%) scale(1)}
    .tip-bottom{top:calc(100% + 8px);left:50%;transform:translateX(-50%) scale(.95);transform-origin:top center}
    .tip-bottom.visible{transform:translateX(-50%) scale(1)}
    .tip-right{left:calc(100% + 8px);top:50%;transform:translateY(-50%) scale(.95);transform-origin:left center}
    .tip-right.visible{transform:translateY(-50%) scale(1)}
    /* Arrow */
    .has-arrow .tooltip::after{content:'';position:absolute;border:5px solid transparent}
    .has-arrow .tip-bottom::after{top:-10px;left:50%;transform:translateX(-50%);border-bottom-color:var(--tip-bg)}
    .has-arrow .tip-top::after{bottom:-10px;left:50%;transform:translateX(-50%);border-top-color:var(--tip-bg)}
    .has-arrow .tip-right::after{left:-10px;top:50%;transform:translateY(-50%);border-right-color:var(--tip-bg)}
    /* Trigger elements */
    .icon-btn{width:52px;height:52px;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px;display:flex;align-items:center;justify-content:center;font-size:22px;cursor:pointer}
    .trunc-text{font-size:13px;max-width:120px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;cursor:pointer;padding:10px 12px;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:6px;color:var(--ui-muted)}
    .help-input{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:inherit;font-size:12px;padding:10px 12px;border-radius:6px;outline:none;min-height:44px;width:200px;transition:border-color .15s}
    .help-input::placeholder{color:var(--ui-muted);opacity:1}
    .help-input:focus{border-color:var(--ui-accent)}
    /* The field's own focus look (the border) replaces the site focus ring */
    .stage .help-input:focus-visible{outline:none}
    .avatar-sm{width:44px;height:44px;border-radius:50%;background:linear-gradient(135deg,#1a3060,#2a5090);display:flex;align-items:center;justify-content:center;font-size:18px;cursor:pointer;flex-shrink:0}
    /* A stage 260px tall (a laptop window under about 600px, a phone held sideways) is shorter than the four items with their usual spacing */
    @media(min-width:601px) and (max-height:640px){.stage{gap:16px;padding:8px 16px}}
    /* Touch screens: the cut-off text is a tap target. Narrow screens: there is no room to the right of the field for its tooltip, so it opens below the field */
    @media(max-width:720px),(max-height:500px),(pointer:coarse){.trunc-text{min-height:44px;line-height:24px}}
    @media(max-width:720px){
      .tip-right{left:50%;right:auto;top:calc(100% + 8px);transform:translateX(-50%) scale(.95);transform-origin:top center}
      .tip-right.visible{transform:translateX(-50%) scale(1)}
      .has-arrow .tip-right::after{left:50%;top:-10px;transform:translateX(-50%);border-right-color:transparent;border-bottom-color:var(--tip-bg)}
    }
    /* At 16px a text field does not make iOS zoom in when it gets the focus */
    @media(pointer:coarse){.help-input{font-size:16px}}
    @media(prefers-reduced-motion:reduce){.tooltip{transition:opacity var(--tip-dur) ease!important}}
    /* See inside and Compare, drawn from the page's own numbers. See inside: the tip's life as bars on one time line (a marker rides
       it), and the hidden tip outlined. Compare: a copy of the items on the left whose tips show with no wait. */
    .strip,.views,.cp-wrap,.tip-ol,.lbl{display:none}
    .lbl{position:absolute;padding:2px 7px;border-radius:5px;background:rgba(11,11,13,.92);color:var(--ui-text);font:600 12px/1.25 var(--hb-font);white-space:nowrap}
    .views{position:absolute;inset:0;z-index:11;pointer-events:none}
    .views svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible}
    .pt path{fill:var(--ui-text);stroke:#0b0b0d;stroke-width:1.5;stroke-linejoin:round}
    .gh{opacity:.7}
    .trail{fill:none;stroke:rgba(244,244,242,.6);stroke-width:2;stroke-dasharray:3 5}
    .tip-ol{color:transparent;background:rgba(28,28,34,.78);border:1px dashed rgba(255,157,92,.85);--tip-bg:rgba(255,157,92,.8);transition:none;z-index:9;user-select:none}
    .strip{padding:6px 10px 7px;background:#111114;border:1px solid var(--ui-border);border-radius:8px}
    .tl{position:relative;height:24px}
    .f-wait{--edge:1px dashed rgba(244,244,242,.6)}
    .f-in{--fill:linear-gradient(90deg,rgba(255,157,92,.1),var(--ui-accent))}
    .f-out{--fill:linear-gradient(90deg,var(--ui-accent),rgba(255,157,92,.1))}
    .f-stay{--fill:repeating-linear-gradient(135deg,var(--ui-accent) 0 3px,rgba(255,157,92,.28) 3px 6px)}
    .bar,.kl::before{background:var(--fill,none);border:var(--edge,0)}
    .bar{position:absolute;top:3px;height:18px;border-radius:3px}
    .bar.f-stay::after{content:'';position:absolute;inset:0;background:linear-gradient(90deg,transparent 30%,#111114)}
    .mk{position:absolute;left:0;top:0;width:2px;height:24px;margin-left:-1px;background:var(--ui-text);box-shadow:0 0 0 1px #0b0b0d}
    .key{display:flex;flex-wrap:wrap;gap:2px 14px;margin-top:4px}
    .kl{font:600 12px/1.25 var(--hb-font);color:var(--ui-text)}
    .kl::before{content:'';display:inline-block;width:16px;height:10px;margin-right:6px;border-radius:2px;vertical-align:-1px}
    .stage[data-hb-mode="inside"],.stage[data-hb-mode="compare"]{position:relative}
    .stage[data-hb-mode="inside"] .views,.stage[data-hb-mode="compare"] .views{display:block}
    .stage[data-hb-mode="inside"].three #tw2,.stage[data-hb-mode="compare"].three #tw2{display:none}
    .stage[data-hb-mode="inside"]{gap:8px;padding:8px 16px}
    .stage[data-hb-mode="inside"] .strip{display:block;align-self:stretch;flex:none}
    .stage[data-hb-mode="inside"] .in-cap:not([hidden]){display:block}
    .stage[data-hb-mode="inside"] .tip-ol.on{display:block}
    /* Compare: the real items move to the middle of the right half (left, not translate: a transform would trap each tip under the
       items after it) and their copies stand at the same heights in the left half. The field's tip opens below it in both halves,
       as Normal's does on a narrow screen. */
    .stage[data-hb-mode="compare"]{gap:26px;padding:30px 16px 8px}
    .stage[data-hb-mode="compare"]::after{content:'';position:absolute;left:50%;top:0;bottom:0;width:1px;background:rgba(255,255,255,.14)}
    .stage[data-hb-mode="compare"] .cmp-cap:not([hidden]){display:block}
    .stage[data-hb-mode="compare"] .tip-wrap{left:var(--half,0px)}
    .stage[data-hb-mode="compare"] .cp-wrap{display:inline-flex;position:absolute;pointer-events:none}
    .stage[data-hb-mode="compare"].three .cp2{display:none}
    .stage[data-hb-mode="compare"] .tooltip{max-width:var(--tw,200px)}
    .stage[data-hb-mode="compare"] .tooltip.pose{opacity:var(--po,0);transform:translateX(-50%) scale(var(--ps,.95));transition:none}
    .stage[data-hb-mode="compare"] .tip-right{left:50%;right:auto;top:calc(100% + 8px);transform:translateX(-50%) scale(.95);transform-origin:top center}
    .stage[data-hb-mode="compare"] .tip-right.visible{transform:translateX(-50%) scale(1)}
    .stage[data-hb-mode="compare"] .has-arrow .tip-right::after{left:50%;top:-10px;transform:translateX(-50%);border-right-color:transparent;border-bottom-color:var(--tip-bg)}
    .n-l{left:8px;top:8px}
    .n-r{left:calc(50% + 8px);top:8px}
    @media(max-width:600px){.stage[data-hb-mode="inside"]{gap:6px;padding:6px}.strip{padding:5px 6px 6px}}
    /* A stage under 300px tall has no room for the strip under three items, so it stands to their right */
    .stage[data-hb-mode="inside"].side{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.7fr);align-content:center;gap:8px 16px}
    .stage[data-hb-mode="inside"].side .tip-wrap{grid-column:1;justify-self:center}
    .stage[data-hb-mode="inside"].side .strip{grid-column:2;grid-row:1/span 3;align-self:center}
  </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="../badge-pulse/" rel="prev" aria-label="Previous: Notification Badge Pulse"><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>Notification Badge Pulse</span></a><a href="../drawer-slide/" rel="next" aria-label="Next: Drawer / Panel Slide"><span><span class="hb-dir">Next: </span>Drawer / Panel Slide</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.16 · Micro-Interactions</p>
    <h1>Tooltip Reveal</h1>
    <p class="hb-lede">A small label fades in after a short pause. Best for icon 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">Hover it</h2><p>Point at an item, tab to it or tap it on a phone, 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="tip-wrap" id="tw0">
        <div class="icon-btn" tabindex="0" aria-describedby="tt0">⚙️</div>
        <div class="tooltip tip-bottom" id="tt0" role="tooltip">Settings — configure your workspace preferences</div>
      </div>
      <div class="tip-wrap" id="tw1">
        <div class="trunc-text" tabindex="0" aria-describedby="tt1">This text is truncat…</div>
        <div class="tooltip tip-top" id="tt1" role="tooltip">This text is truncated because it is too long for the available space in this layout.</div>
      </div>
      <div class="tip-wrap" id="tw2">
        <input class="help-input" type="text" placeholder="Username" aria-label="Username" aria-describedby="tt2" autocomplete="off">
        <div class="tooltip tip-right" id="tt2" role="tooltip">3–20 characters. Letters, numbers and underscores only.</div>
      </div>
      <div class="tip-wrap" id="tw3">
        <div class="avatar-sm" tabindex="0" aria-describedby="tt3">🧑</div>
        <div class="tooltip tip-top" id="tt3" role="tooltip">@atlas — joined 2019 · 140 projects</div>
      </div>
      <div class="strip" aria-hidden="true">
        <div class="tl"><span class="bar f-wait" id="b1"></span><span class="bar f-in" id="b2"></span><span class="bar f-stay" id="b3"></span><span class="bar f-wait" id="b4"></span><span class="bar f-out" id="b5"></span><span class="mk" id="mk"></span></div>
        <p class="key"><span class="kl f-wait" id="k1" data-hb-caption>Waits about a third of a second</span><span class="kl f-in" id="k2" data-hb-caption>Fades in, about a sixth of a second</span><span class="kl f-stay" data-hb-caption>Stays while you point</span><span class="kl f-out" id="k4" data-hb-caption>Waits a tenth of a second, then fades out</span></p>
      </div>
      <div class="views" aria-hidden="true">
        <span class="lbl in-cap" id="l-ol" data-hb-caption hidden>Hidden here until it shows</span>
        <span class="lbl cmp-cap n-l" data-hb-caption>Shows at once</span>
        <span class="lbl cmp-cap n-r" data-hb-caption>Waits a moment</span>
        <span class="lbl cmp-cap" id="l-pass" data-hb-caption hidden>Passing by</span>
        <!-- The trails, the pretend pointer and its ghost over the copy: after the labels, so a pointer is never hidden under one -->
        <svg>
          <path class="trail" id="tr0"/><path class="trail" id="tr1"/>
          <g class="pt" id="pt"><path d="M0 0V19L5 14.5L8.5 22L11.5 20.7L8 13.3H14.5Z"/></g>
          <g class="pt gh" id="gh"><path d="M0 0V19L5 14.5L8.5 22L11.5 20.7L8 13.3H14.5Z"/></g>
        </svg>
      </div>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-demo" data-hb-demo><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/></svg>Show me</button>
      <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo="css" autocomplete="off"><span>Slow motion</span></label>
    </div>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, the dashed box is the hidden tip; the bars are its life: it waits, fades in, stays while you point, then waits and fades out. Point at an item, or press Show me.</p>
      <p data-hb-for="compare" hidden>Shows at once: every tip the pointer passes flashes up; Waits a moment: only the item the pointer rests on shows its tip. Move across the items, or press 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="show-lbl">Delay before showing</p>
        <div class="seg" id="show-seg" role="group" aria-labelledby="show-lbl">
          <button type="button" data-ms="0" aria-pressed="false">None</button>
          <button type="button" data-ms="150" aria-pressed="false">Short</button>
          <button type="button" data-ms="300" class="on" aria-pressed="true">Medium</button>
          <button type="button" data-ms="600" aria-pressed="false">Long</button>
        </div>
        <p class="hb-hint">The pause stops tips flashing as the pointer passes.</p>
      </div>
      <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="250" aria-pressed="false">Slow</button>
          <button type="button" data-ms="150" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-ms="90" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">How quickly the label fades and grows in.</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="hide-lbl">Delay before hiding</p>
          <div class="seg" id="hide-seg" role="group" aria-labelledby="hide-lbl">
            <button type="button" data-ms="0" aria-pressed="false">None</button>
            <button type="button" data-ms="100" class="on" aria-pressed="true">Short</button>
            <button type="button" data-ms="300" aria-pressed="false">Long</button>
          </div>
          <p class="hb-hint">How long the tip stays after the pointer leaves.</p>
        </div>
        <div class="hb-setting">
          <label class="hb-switch-row"><span>Shows an arrow</span><input class="hb-switch" type="checkbox" role="switch" id="arrow-tog" autocomplete="off"></label>
          <p class="hb-hint">A small point that aims the tip at its item.</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 tooltips to [the icon buttons, shortened text or fields that need a short explanation]. When the pointer rests on an item, or keyboard focus reaches it, wait a moment, then fade the tooltip in beside it while it grows very slightly from the side facing the item, so it feels light rather than like a pop-up. The wait keeps tooltips from flashing while the pointer only passes by. When the pointer leaves, hide it again, after a pause if the settings include one, and let the Escape key close it at once. On touch screens, show and hide it with a tap. If the visitor has reduced motion turned on, fade it without growing. Match the settings listed below.</p>
      <div class="hb-chips-box" hidden>
        <p class="hb-chips-title">Your settings</p>
        <ul class="hb-chips"></ul>
      </div>
      <div class="hb-copy-row">
        <button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>
        <p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>
      </div>
    </div>
    <div class="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>Icon buttons</li><li>Cut-off text</li><li>Form hints</li><li>Chart values</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Key information</li><li>Long text</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 stage=document.querySelector('.stage');
  const wraps=[...document.querySelectorAll('.tip-wrap')];
  const tips=wraps.map(w=>w.querySelector('.tooltip'));
  const slowTog=document.getElementById('slow-tog');
  const canHover=window.matchMedia('(hover: hover)');
  const vars=document.documentElement.style;
  let showDelay=300,hideDelay=100,fadeMs=150,runTimers=[];

  // See inside and Compare: the strip of bars with its marker, a dashed outline of each hidden tip, an inert copy of the items for
  // Compare, and the pretend pointer of Show me's pass. The mode is Normal until the switch says otherwise.
  const $=id=>document.getElementById(id);
  const strip=stage.querySelector('.strip'),tl=strip.querySelector('.tl'),mk=$('mk'),barEls=[1,2,3,4,5].map(n=>$('b'+n));
  const pt=$('pt'),gh=$('gh'),trails=[$('tr0'),$('tr1')],capOl=$('l-ol'),capPass=$('l-pass');
  const reduceMQ=matchMedia('(prefers-reduced-motion: reduce)'),narrowMQ=matchMedia('(max-width:720px)'),phoneMQ=matchMedia('(max-width:600px)');
  const compareNote=document.querySelector('[data-hb-for="compare"]'),COMPARE_NOTE=compareNote.textContent,insideNote=document.querySelector('[data-hb-for="inside"]');
  const NONE_NOTE='Delay before showing is set to None, so Shows at once and Waits a moment both flash every tip the pointer passes. Pick another delay in Try it.';
  let mode='normal',cur=0,M={i:-1,ph:'rest'},K=0,X=[],D=[],rp=-1,posed=false,passed=[],mouse=null,mouseIn=false,markRaf=0;
  const copies=[],ols=[];

  // Each item shows and hides its own tooltip: the visitor's pointer, focus and tap and the Show me run all call the same two
  // functions. A delay of 0 shows or hides at once (a tap does), and dismiss() clears both timers and hides at once (Escape does).
  wraps.forEach((wrap,i)=>{
    const tip=tips[i];
    let showTimer=0,hideTimer=0,waiting=false;
    function show(delay=showDelay){
      const was=tip.classList.contains('visible');
      clearTimeout(hideTimer);clearTimeout(showTimer);waiting=false;
      if(delay>0){waiting=true;showTimer=setTimeout(()=>{waiting=false;tip.classList.add('visible');sync()},delay)}
      else tip.classList.add('visible');
      note(i,'show',delay,was);
    }
    function hide(delay=hideDelay){
      const was=tip.classList.contains('visible'),cut=waiting;
      clearTimeout(showTimer);clearTimeout(hideTimer);waiting=false;
      if(delay>0)hideTimer=setTimeout(()=>{tip.classList.remove('visible');sync()},delay);
      else tip.classList.remove('visible');
      note(i,'hide',delay,was,cut);
    }
    wrap.show=show;
    wrap.hide=hide;
    wrap.dismiss=()=>hide(0);
    // Hover only where a pointer can hover, and never for a finger: a tap shows the tooltip through touchstart below
    wrap.addEventListener('pointerenter',e=>{if(e.pointerType!=='touch'&&canHover.matches)show()});
    wrap.addEventListener('pointerleave',e=>{if(e.pointerType!=='touch'&&canHover.matches)hide()});
    wrap.addEventListener('focusin',()=>show());
    wrap.addEventListener('focusout',()=>hide());
    // A tap hides a shown tooltip only when the item already has the focus. Without it (a tooltip Show me shows) the tap keeps it,
    // or the focus the tap brings would show it again a moment later: a blink.
    wrap.addEventListener('touchstart',()=>{if(tip.classList.contains('visible')&&wrap.contains(document.activeElement))hide(0);else show(0)},{passive:true});
  });
  // Escape closes every shown tooltip at once; one comes back only when the pointer or the focus leaves its item and returns
  document.addEventListener('keydown',e=>{if(e.key==='Escape')wraps.forEach(w=>w.dismiss())});

  // The copies, made once from the real items (which stay the four in wraps): no ids, no roles, inert and hidden from screen readers.
  // Each real item also holds a twin of its tip for See inside, drawn as a dashed outline, with its text clear so it has the tip's size.
  function plain(el){
    [el,...el.querySelectorAll('*')].forEach(n=>['id','role','tabindex','aria-describedby','aria-label'].forEach(a=>n.removeAttribute(a)));
    el.setAttribute('aria-hidden','true');el.inert=true;return el;
  }
  wraps.forEach((w,i)=>{
    const c=plain(w.cloneNode(true));
    c.classList.replace('tip-wrap','cp-wrap');c.classList.add('cp'+i);
    stage.insertBefore(c,strip);
    copies.push({wrap:c,tip:c.querySelector('.tooltip'),t:0});
    const o=plain(tips[i].cloneNode(true));
    o.classList.add('tip-ol','visible');w.append(o);ols.push(o);
  });
  // The field's copy shows what is typed in the real one
  wraps[2].querySelector('input').addEventListener('input',e=>{copies[2].wrap.querySelector('input').value=e.target.value});

  // A call of show() or hide() reaches the views. Compare's copy shows its tip at once (no wait) and hides it after the delay before
  // hiding. See inside's marker starts at the wait and runs through the fade, and on hide() jumps to the second wait; a show() cut
  // off before its wait ended (cut) sends it back to the start, and a tap (delay 0) starts it at the fade.
  function note(i,kind,delay,was,cut){
    if(mode==='compare'){
      const c=copies[i];clearTimeout(c.t);
      if(kind==='show'){c.tip.classList.remove('pose');c.tip.classList.add('visible')}
      else if(delay>0)c.t=setTimeout(()=>{c.tip.classList.remove('visible');sync()},delay);
      else c.tip.classList.remove('visible');
    }else if(mode==='inside'){
      const t0=performance.now();
      if(kind==='show'){cur=i;M={i,ph:was?'stay':'in',t0,off:showDelay-delay}}
      else if(i===M.i)M=cut?{i:-1,ph:'rest'}:was?{i,ph:'out',t0,off:hideDelay-delay}:M;
      ride();
    }
    sync();
  }

  // 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}
  // A stopped run lets go of the tooltips it showed (the gear's, and in the views the ones its pass touched), unless the visitor's
  // pointer or focus is on that item
  function stopRun(){
    runTimers.forEach(clearTimeout);runTimers=[];
    [0,...passed].forEach(i=>{if(!wraps[i].matches(':hover,:focus-within'))wraps[i].hide()});
    passed=[];rp=-1;sync();
  }
  // A run starts with every tooltip hidden at once: the transitions are turned off, a reflow makes it stick, and they come back.
  function toRest(){
    const all=[...stage.querySelectorAll('.tooltip')];
    all.forEach(t=>t.style.setProperty('transition','none','important'));
    wraps.forEach(w=>w.dismiss());
    void stage.offsetWidth;
    all.forEach(t=>t.style.removeProperty('transition'));
  }
  // One hover of the gear: its tooltip shows after the delay before showing, stays 1.6 s, then hides after the delay before hiding.
  // At the defaults it is back at rest 2.3 s after the press. In See inside and Compare a pass comes first: a pretend pointer
  // jumps up the items a tenth of a second apart, on each one show() and then hide(), so the wait is seen keeping the tips from
  // flashing. The pass is a hold (Slow motion does not lengthen it) and adds a tenth of a second per item.
  $('btn-demo').addEventListener('click',()=>{
    stopRun();
    toRest();
    const path=mode==='normal'?[0]:[...[3,2,1].filter(i=>wraps[i].offsetParent),0],lead=(path.length-1)*100;
    path.forEach((i,k)=>{
      const go=()=>{
        if(k)wraps[path[k-1]].hide();
        if(k<path.length-1)passed.push(i);
        rp=i;
        wraps[i].show();
        if(mode!=='normal')pointers();
      };
      if(k)later(go,0,k*100);else go();
    });
    later(()=>wraps[0].hide(),fadeMs,lead+showDelay+1600);
    later(()=>{rp=-1;sync()},fadeMs,lead+showDelay+1600);
    sync();
  });
  // The visitor pressed, typed, scrolled or touched inside the stage, moved a pointer over it or tabbed into it: the run stops.
  document.addEventListener('hb:input',()=>{if(running())stopRun()});
  stage.addEventListener('pointermove',e=>{
    if(e.isTrusted&&running())stopRun();
    if(e.pointerType!=='mouse'||mode==='normal')return;
    const s=stage.getBoundingClientRect();
    mouse={x:e.clientX-s.left-stage.clientLeft,y:e.clientY-s.top-stage.clientTop};
    if(!mouseIn){mouseIn=true;sync()}else pointers();
  });
  stage.addEventListener('pointerleave',e=>{if(e.pointerType==='mouse'){mouseIn=false;mouse=null;sync()}});
  stage.addEventListener('focusin',e=>{if(e.isTrusted&&running())stopRun();sync()});
  stage.addEventListener('focusout',()=>setTimeout(sync));

  // ---- See inside and Compare ----
  const say=ms=>[[90,'a tenth of a second'],[150,'about a sixth of a second'],[250,'a quarter of a second'],[300,'about a third of a second'],
    [450,'about half a second'],[600,'over half a second'],[750,'about three quarters of a second']].reduce((m,w)=>Math.abs(w[0]-ms)<Math.abs(m[0]-ms)?w:m)[1];
  const write=(el,text)=>{if(el.textContent!==text)el.textContent=text};
  // The box of an element, and the point where the pointer lands on an item, in the stage's padding box
  function box(el){const s=stage.getBoundingClientRect(),b=el.getBoundingClientRect(),l=b.left-s.left-stage.clientLeft,t=b.top-s.top-stage.clientTop;return{l,t,r:l+b.width,b:t+b.height}}
  function hot(i){const b=box(wraps[i]);return{x:(b.l+b.r)/2+18,y:b.t+(b.b-b.t)*.55}}
  const below=()=>wraps[2].offsetParent?2:3;
  // How far a tip has faded in after a tenth of a second: a curve close to the CSS "ease" the fade uses
  const ease=t=>t*(2-t);
  // The bars, one per step of a tip's life, on a time line drawn at one scale (the stay is a made-up length that fades out, since it
  // lasts as long as the pointer stays); with Slow motion the fades are three times as long, as the CSS fade is
  function barsDraw(){
    const f=fadeMs*(slowTog.checked?3:1);
    D=[showDelay,f,800,hideDelay,f];
    K=tl.clientWidth/Math.max(2200,D.reduce((a,b)=>a+b));
    let x=0;X=[];
    D.forEach((ms,j)=>{X.push(x*K);barEls[j].style.left=x*K+'px';barEls[j].style.width=ms*K+'px';barEls[j].hidden=!ms;x+=ms});
    X.push(x*K);
    write($('k1'),showDelay?'Waits '+say(showDelay):'No wait: shows at once');
    write($('k2'),'Fades in, '+say(f));
    write($('k4'),hideDelay?'Waits '+say(hideDelay)+', then fades out':'Fades out as soon as you leave');
  }
  // The marker's place at this moment. It runs in real time (the waits as they are, the fades as long as they last); under reduced
  // motion it jumps to the start of each step instead of gliding.
  function markX(){
    const t=M.off+performance.now()-M.t0,r=reduceMQ.matches;
    if(M.ph==='in'){if(t>=D[0]+D[1]){M.ph='stay';return X[2]}return r?(t<D[0]?0:X[1]):t*K}
    if(M.ph==='out'){if(t>=D[3]+D[4]){M.ph='rest';return 0}return X[3]+(r?(t<D[3]?0:D[3]*K):t*K)}
    return M.ph==='stay'?X[2]:0;
  }
  function ride(){
    cancelAnimationFrame(markRaf);
    const step=()=>{mk.style.transform=`translateX(${markX()}px)`;markRaf=(M.ph==='in'||M.ph==='out')&&mode==='inside'?requestAnimationFrame(step):0};
    step();
  }
  // The dashed outline of the tip shown last (the gear's at rest), and its words beside it where there is room
  function outline(){
    ols.forEach((o,k)=>o.classList.toggle('on',k===cur));
    capOl.hidden=mode!=='inside'||phoneMQ.matches||tips[cur].classList.contains('visible');
    if(capOl.hidden)return;
    const o=box(ols[cur]),W=stage.clientWidth,H=stage.clientHeight,w=capOl.offsetWidth,h=capOl.offsetHeight,y=(o.t+o.b-h)/2;
    const others=[strip,...wraps.filter(v=>v.offsetParent)].map(box);
    const at=[o.r+8,o.l-8-w].find(x=>x>=6&&x+w<=W-6&&y>=6&&y+h<=H-6&&!others.some(b=>x<b.r&&x+w>b.l&&y<b.b&&y+h>b.t));
    capOl.hidden=at===undefined;
    if(at!==undefined){capOl.style.left=at+'px';capOl.style.top=y+'px'}
  }
  // The pretend pointer (Show me's pass, and Compare's pose) with its ghost over the copy, and the trail of the pose
  const put=(g,p)=>{g.style.display=p?'':'none';if(p)g.setAttribute('transform',`translate(${p.x} ${p.y})`)};
  function pointers(){
    const W=stage.clientWidth;
    let p=null,from=null;
    if(mode!=='normal'){if(rp>=0)p=hot(rp);else if(posed){p=hot(1);from=hot(below())}}
    put(pt,p);
    put(gh,mode!=='compare'?null:p?{x:p.x-W/2,y:p.y}:mouse&&mouse.x>W/2?{x:mouse.x-W/2,y:mouse.y}:null);
    trails.forEach((t,k)=>t.setAttribute('d',from?`M${from.x-k*W/2} ${from.y}L${p.x-k*W/2} ${p.y}`:''));
    capPass.hidden=!from;
    if(from){
      const t=box(wraps[1]),n=box(wraps[below()]);
      capPass.style.left=p.x-6-capPass.offsetWidth+'px';capPass.style.top=(t.b+n.t-capPass.offsetHeight)/2+'px';
    }
  }
  // Compare's rest pose, until the real side gets input or a run starts: the pretend pointer has just come up from the item below
  // onto the cut-off text. The left shows its tip as far faded in as a tenth of a second of the fade gets it (at None the right
  // does too), the right nothing yet. The pose goes by progress, so Slow motion does not change it.
  function sync(){
    const was=posed;
    posed=mode==='compare'&&!running()&&!mouseIn&&!stage.matches(':focus-within')&&![...tips,...copies.map(c=>c.tip)].some(t=>t.classList.contains('visible'));
    if(mode==='normal'&&!was&&rp<0)return;
    if(mode==='inside')outline();
    const p=posed?ease(Math.min(1,100/fadeMs)):0;
    stage.style.setProperty('--po',p);stage.style.setProperty('--ps',.95+.05*p);
    copies[1].tip.classList.toggle('pose',posed);
    tips[1].classList.toggle('pose',posed&&showDelay===0);
    pointers();
  }
  function layout(){
    if(mode!=='normal'){
      const W=stage.clientWidth;
      stage.classList.toggle('three',stage.clientHeight<340||(mode==='inside'&&narrowMQ.matches));
      stage.classList.toggle('side',mode==='inside'&&stage.clientHeight<300&&W>=600);
      if(cur===2&&!wraps[2].offsetParent)cur=0;
      stage.style.setProperty('--half',W/4+'px');stage.style.setProperty('--tw',Math.min(200,W/2-16)+'px');
      copies.forEach((c,i)=>{c.wrap.style.top=wraps[i].offsetTop+'px';c.wrap.style.left=W/4-c.wrap.offsetWidth/2+'px'});
      if(mode==='inside')barsDraw();
      ride();
    }
    write(insideNote,'Inside, the dashed box is the hidden tip; the bars are its life: it '+(showDelay?'waits, fades in':'fades in')+', stays while you point, then '+(hideDelay?'waits and fades out':'fades out')+'. Point at an item, or press Show me.');
    write(compareNote,showDelay===0?NONE_NOTE:COMPARE_NOTE);
    sync();
  }
  document.addEventListener('hb:mode',e=>{
    mode=e.detail.mode;
    copies.forEach(c=>{clearTimeout(c.t);c.tip.classList.remove('visible')});
    // A run goes on across a mode press, so the marker and the outline start from the tip showing now, if one shows
    const live=tips.findIndex(t=>t.classList.contains('visible'));
    if(mode==='inside'){M=live<0?{i:-1,ph:'rest'}:{i:live,ph:'stay'};if(live>=0)cur=live}
    layout();
  });
  addEventListener('resize',layout);
  document.fonts.ready.then(layout);
  slowTog.addEventListener('change',()=>{if(mode==='inside'){barsDraw();ride()}});

  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);
      layout();
    }));
  }
  choices('show-seg',d=>{showDelay=+d.ms});
  choices('speed-seg',d=>{fadeMs=+d.ms;vars.setProperty('--tip-dur',fadeMs+'ms')});
  choices('hide-seg',d=>{hideDelay=+d.ms});
  document.getElementById('arrow-tog').addEventListener('change',e=>{
    wraps.forEach((w,i)=>{w.classList.toggle('has-arrow',e.target.checked);copies[i].wrap.classList.toggle('has-arrow',e.target.checked)});
  });
</script>
</body>
</html>