Tooltip
Quiet labels on hover, with shortcuts and a richer preview card. Free and MIT licensed, as HTML, a typed React component or an Astro component, in three styles with light and dark mode.
Need this designed into your product? Book a call
Install
Add it to your project with the CLI.
HTML, CSS and JS
npx designme add tooltipReact
npx designme add tooltip --reactAstro
npx designme add tooltip --astroshadcn, React
npx shadcn@latest add https://designme.dev/r/shadcn/tooltip.json shadcn, Astro
npx shadcn@latest add https://designme.dev/r/shadcn/astro/tooltip.json The CLI copies the source into your project, so you own it and can change anything. Where each version lands, what it needs and the shadcn theme bridge: Installation.
About
What it is for and what it does.
Tooltip is a toolbar of icon buttons whose labels appear after a short pause and then follow the pointer from button to button without waiting again. Avatars at the end open a larger card about each person, including the agent, with what they are doing right now. It shows a team and its agent working side by side in the same file.
- When a toolbar is all icons and each one needs a name and a keyboard shortcut
- When people and agents share a workspace and you want to show who is doing what
- When an agent's presence should read as a teammate, with its own status line
- When onboarding should point at a specific control by showing its tip on cue
- Tools come in
groups, with a thin divider between each group. - Each tip shows the tool's
tiptext, or itslabel, plus an optionalkbdshortcut. - The first tip waits a moment, and moving straight to the next button shows its tip at once.
- The avatars sit side by side, never stacked over each other.
- Hovering an avatar opens a person card after
cardDelayms, 300 by default, with name, role and status. The card sits under its avatar, or over it when there is no room below, centred on it or right-aligned to it at the edge, and follows the avatar while it shows. - A person marked
workingshows the agent's thinking dots instead of a green dot, and the status shimmers as loader text does. openTipandopenCardshow a tip or card by key from your own code, for a guided tour.
- Each icon button gets its
labelas anaria-label, and avatar buttons are named with the person's name. - Tips render with
role="tooltip"and also open on keyboard focus, not only on hover. - Person cards open when an avatar receives keyboard focus and close on blur.
- The card is marked
aria-hiddenwhile it is not showing.
Usage
Import it in React and drive it from your own state, or place it in an Astro page.
React
import { Tooltip } from '@/components/designme/tooltip/Tooltip';React example
The demo above as a React component, 31 lines you can copy.
'use client';
// The tooltip card from designme.dev: a toolbar with shortcut tips and two people with a card each.
import { useRef, useState } from 'react';
import { useScript, useTakeover } from '../lib/useScript';
import { Tooltip, type TooltipPerson } from './Tooltip';
const PEOPLE: TooltipPerson[] = [
{ key: 'mk', initials: 'MK', color: 'var(--dm-av-l)', name: 'Maya Kim', role: 'Design lead', status: 'Editing Pricing, frame 2' },
{ key: 'da', initials: 'DA', color: 'var(--dm-av-b)', name: 'Design agent', role: 'Working for Maya', status: 'Auditing contrast, 2 of 6 checks', working: true }
];
export default function TooltipDemo() {
const [tip, setTip] = useState<string | null>();
const [card, setCard] = useState<string | null>();
// once it has appeared it shows what it does: a shortcut tip, then the agent's card. Pointing at it takes over, as does
// clicking, focusing or typing in it, and puts away what the demo had open unless the pointer is on it
const touched = useRef(false), ref = useRef<HTMLDivElement>(null), mine = useTakeover(ref, () => setTip(undefined));
const yours = () => touched.current || mine();
useScript(async ({ wait }) => {
await wait(1300); if (yours()) return;
setTip('ask'); await wait(1700); setTip(undefined); if (yours()) return;
await wait(500); if (yours()) return;
setCard('da'); await wait(2600); setCard(null);
});
return (
<div ref={ref} style={{ width: '100%' }} onPointerEnter={() => { touched.current = true; setTip(undefined); setCard(c => (c ? null : c)); }}>
<Tooltip people={PEOPLE} openTip={tip} openCard={card} />
</div>
);
}
Astro
In a page in src/pages. It plays the demo above; change its tooltip.js to show your own data. Attributes like class go on its root element.
---
import Tooltip from '../components/designme/tooltip/Tooltip.astro';
---
<Tooltip />
<Tooltip data-style="soft" class="my-class" />Props
Everything Tooltip takes.
| Prop | Type | What it does |
|---|---|---|
groups | TooltipTool[][] | The toolbar's buttons, in groups with a line between them |
people | TooltipPerson[] | Avatars at the end of the toolbar, side by side. Hovering one shows a card about the person |
cardDelay | number | How long the person card waits before it shows |
openTip | string | null | Shows this tool's tip, by key, as if it were hovered. Leave it out to let the pointer decide |
openCard | string | null | Shows this person's card, by key, as if they were hovered. Leave it out to let the pointer decide |
className | string |
Code
The full source, as the CLI installs it.
Tooltip.tsxReact · 137 lines
'use client';
import { useEffect, useRef, useState } from 'react';
import { cx } from '../lib/cx';
import { useIsoLayoutEffect } from '../lib/hooks';
import { Icon, type IconName } from '../lib/icons';
import { Avatar, Dot, Think, Tip } from '../lib/motion';
import { useHoverGlide } from '../lib/glide';
import '../designme.css';
import './tooltip.css';
export interface TooltipTool {
key: string;
icon: IconName;
/** The button's name, read out by screen readers */
label: string;
/** The tooltip, when it should say more than the name, like "Ask the agent". Defaults to the label */
tip?: string;
/** A shortcut shown after the tip, like "⌘Z" */
kbd?: string;
onClick?: () => void;
}
export interface TooltipPerson {
key: string;
initials: string;
/** The avatar's colour, like 'var(--dm-av-l)' */
color: string;
name: string;
/** The quiet line under the name, like "Design lead" */
role: string;
/** What they are doing now, like "Editing Pricing, frame 2" */
status: string;
/** Shows the agent's thinking dots instead of the green dot, and the status shimmers as loader text does */
working?: boolean;
}
export interface TooltipProps {
/** The toolbar's buttons, in groups with a line between them */
groups?: TooltipTool[][];
/** Avatars at the end of the toolbar, side by side. Hovering one shows a card about the person */
people?: TooltipPerson[];
/** How long the person card waits before it shows */
cardDelay?: number;
/** Shows this tool's tip, by key, as if it were hovered. Leave it out to let the pointer decide */
openTip?: string | null;
/** Shows this person's card, by key, as if they were hovered. Leave it out to let the pointer decide */
openCard?: string | null;
className?: string;
}
const DEFAULT_GROUPS: TooltipTool[][] = [
[{ key: 'undo', icon: 'undo', label: 'Undo', kbd: '⌘Z' }, { key: 'comment', icon: 'edit', label: 'Comment', kbd: 'C' }, { key: 'ask', icon: 'spark', label: 'Ask agent', tip: 'Ask the agent', kbd: '⌘J' }],
[{ key: 'share', icon: 'users', label: 'Share', tip: 'Share with your team' }]
];
/** A toolbar whose labels wait a moment, then follow you instantly between buttons, with a richer card for people. */
export function Tooltip({ groups = DEFAULT_GROUPS, people = [], cardDelay = 300, openTip, openCard, className }: TooltipProps) {
const root = useRef<HTMLDivElement>(null), box = useRef<HTMLDivElement>(null);
// Studio's one hover: a highlight that glides from button to button
const barRef = useRef<HTMLDivElement>(null);
useHoverGlide(barRef, '.dm-ib,.dm-ttp-av');
const timer = useRef<ReturnType<typeof setTimeout>>(undefined);
// the card keeps its last person while it fades out
const [card, setCard] = useState<{ person: TooltipPerson; el: HTMLElement } | null>(null);
const [show, setShow] = useState(false);
useEffect(() => () => clearTimeout(timer.current), []);
const open = (person: TooltipPerson, el: HTMLElement, now = false) => {
clearTimeout(timer.current);
timer.current = setTimeout(() => { setCard({ person, el }); setShow(true); }, now ? 0 : cardDelay);
};
const close = () => { clearTimeout(timer.current); setShow(false); };
// the card sits under its avatar, or over it when there is no room below; centred on it, or right-aligned to it (left-aligned
// on the left) at the window's edge. While it shows it goes where its avatar goes, so a toolbar that moves carries it along
useIsoLayoutEffect(() => {
if (!show || !card) return;
let raf = 0, at = '';
const place = () => {
const r = root.current, c = box.current; if (!r || !c) return;
const rr = r.getBoundingClientRect(), br = card.el.getBoundingClientRect(), w = c.offsetWidth, h = c.offsetHeight;
let x = br.left + br.width / 2 - w / 2;
if (x + w > innerWidth - 8) x = Math.max(8, br.right - w); else if (x < 8) x = Math.min(innerWidth - 8 - w, br.left);
const down = br.bottom + 10 + h <= innerHeight - 8 || br.top - 10 - h < 8;
c.classList.toggle('dm-up', !down);
c.style.left = Math.round(x - rr.left) + 'px'; c.style.top = Math.round((down ? br.bottom + 10 : br.top - 10 - h) - rr.top) + 'px';
};
const follow = () => { const b = card.el.getBoundingClientRect(), k = `${b.left},${b.top}`; if (k !== at) { at = k; place(); } raf = requestAnimationFrame(follow); };
follow();
return () => cancelAnimationFrame(raf);
}, [show, card]);
// the avatar under the pointer: a card opened from code and then let go stays while the pointer is on its avatar
const hovered = useRef<string | null>(null);
// a card asked for by key opens at once under its avatar, and closes when the key is cleared
const avatars = useRef(new Map<string, HTMLButtonElement>());
useEffect(() => {
if (openCard === undefined) return;
const person = people.find(x => x.key === openCard), el = person && avatars.current.get(person.key);
if (person && el) open(person, el, true); else if (!hovered.current) close();
}, [openCard]); // eslint-disable-line react-hooks/exhaustive-deps
const p = card?.person;
return (
<div data-dm="" ref={root} className={cx('dm-ttp', className)}>
<div ref={barRef} className="dm-ttp-bar dm-panel">
{groups.map((g, i) => [
i > 0 && <span key={'s' + i} className="dm-ttp-sep" />,
...g.map(t => (
<Tip key={t.key} label={t.tip ?? t.label} kbd={t.kbd} open={openTip === t.key || undefined}>
<button type="button" className="dm-ib" aria-label={t.label} onClick={t.onClick}><Icon name={t.icon} /></button>
</Tip>
))
])}
{people.length > 0 && <>
<span className="dm-ttp-sep" />
<span className="dm-ttp-avs">
{people.map(person => (
<button key={person.key} ref={el => { if (el) avatars.current.set(person.key, el); else avatars.current.delete(person.key); }} type="button" className="dm-ttp-av" aria-label={person.name}
onMouseEnter={e => { hovered.current = person.key; open(person, e.currentTarget); }} onMouseLeave={() => { hovered.current = null; close(); }}
onFocus={e => { if (e.currentTarget.matches(':focus-visible')) open(person, e.currentTarget, true); }} onBlur={close}>
<Avatar initials={person.initials} color={person.color} />
</button>
))}
</span>
</>}
</div>
<div ref={box} className={cx('dm-ttp-card dm-panel', show && 'dm-show')} aria-hidden={!show}>
<div className="dm-ttp-ci">
{p && <>
<div className="dm-ttp-ch"><Avatar initials={p.initials} color={p.color} /><div><b>{p.name}</b><span>{p.role}</span></div></div>
<div className="dm-ttp-cs">{p.working ? <><span style={{ display: 'inline-flex', color: 'var(--dm-accent-mark, var(--dm-accent))' }}><Think /></span><span className="dm-shim">{p.status}</span></> : <><Dot />{p.status}</>}</div>
</>}
</div>
</div>
</div>
);
}
tooltip.cssReact · 39 lines
/* Generated from designme.dev by scripts/sync.mjs. Edit the site, then sync. */
/* ---------- Tooltip ---------- */
/* a toolbar of icon buttons whose labels (the core tooltip, data-tip) wait a moment, then follow the pointer from button to
button, and the people at its end, each opening a card about them on hover or keyboard focus. The component is the toolbar:
the card opens over the stage, so it keeps no room for it */
.dm-ttp{position:relative;display:flex;flex-direction:column;align-items:center}
.dm-ttp-bar{display:flex;align-items:center;gap:2px;padding:5px;border-radius:var(--dm-r-pill)}
.dm-ttp-sep{width:1px;height:18px;background:var(--dm-line);margin:0 var(--dm-s-1)}
/* the people side by side, never stacked over each other (owner, 18 Nov) */
.dm-ttp-avs{display:flex;gap:2px}
/* a person under the pointer stays where they are (owner, 21 Nov: "you hover it up but show tooltip down"): the one quiet hover
fill, a ring in their round shape, easing in and out */
.dm-ttp-av{display:grid;place-items:center;width:30px;height:30px;border-radius:50%;transition:background-color var(--dm-t-fast) var(--dm-ease),box-shadow var(--dm-t-fast) var(--dm-ease)}
@media (hover:hover) and (pointer:fine){.dm-ttp-av:hover{background:var(--dm-row);box-shadow:0 0 0 2px var(--dm-row)}}
.dm-ttp-av .dm-av{width:28px;height:28px;font-size:11px}
/* the card about a person: centred on their avatar (right-aligned to it at the stage's edge), under it when there is room and
over it when there isn't, and it goes where the avatar goes while it shows (placed by the script) */
.dm-ttp-card{position:absolute;top:0;left:0;width:230px;padding:var(--dm-s-3);box-shadow:var(--dm-shadow-pop);opacity:0;transform:translateY(6px) scale(.97);filter:blur(4px);pointer-events:none;transition:opacity .25s var(--dm-ease),transform .35s var(--dm-ease),filter .25s;z-index:5}
/* the card opens from the avatar toward where it shows: down from it when it opens under it, up from it when it opens over it */
.dm-ttp-card:not(.dm-up){transform:translateY(-6px) scale(.97);transform-origin:50% 0}
.dm-ttp-card.dm-up{transform:translateY(6px) scale(.97);transform-origin:50% 100%}
.dm-ttp-card.dm-show{opacity:1;transform:none;filter:none}
.dm-ttp-ch{display:flex;align-items:center;gap:10px}
.dm-ttp-ch .dm-av{width:34px;height:34px;font-size:12px}
.dm-ttp-ch div{display:flex;flex-direction:column;min-width:0;font-size:13px}
.dm-ttp-ch b{font-weight:500}
.dm-ttp-ch div span{font-size:12px;color:var(--dm-muted)}
.dm-ttp-cs{display:flex;align-items:center;gap:6px;margin-top:10px;font-size:12px;color:var(--dm-fg2)}
.dm-ttp-cs .dm-dot{color:var(--dm-ok-mark)}
/* Studio: the card's content 16px in from every edge (owner, 18 Nov); it scales and fades in, never blurs */
:root[data-style="studio"] .dm-ttp-card.dm-panel,:root [data-style="studio"] .dm-ttp-card.dm-panel,:root [data-style="studio"].dm-ttp-card.dm-panel{width:246px;padding:16px;filter:none;transition:opacity var(--dm-t-fast) var(--dm-ease),transform var(--dm-t) var(--dm-ease)}
/* Studio (owner, 21 Nov): the bar, as Canvas toolbar's, is a pill drawn by a half-strength hairline with only a contact shadow,
its dividers half-strength hairlines, and its hovers pills (a circle round a person, with its ring) under one gliding highlight
(hoverGlide). The card about a person is Studio's popover, 22px corners and its shadow, with what they are doing now in an
off-white foot strip under a hairline, edge to edge, its words 16px in */
:root[data-style="studio"] .dm-ttp-bar.dm-panel,:root [data-style="studio"] .dm-ttp-bar.dm-panel,:root [data-style="studio"].dm-ttp-bar.dm-panel{box-shadow:0 0 0 1px var(--dm-hair),0 1px 2px rgba(0,0,0,.06)}
:root[data-style="studio"] .dm-ttp-sep,:root [data-style="studio"] .dm-ttp-sep,:root [data-style="studio"].dm-ttp-sep{background:color-mix(in srgb,var(--dm-line) 50%,transparent)}
:root[data-style="studio"] .dm-ttp-cs,:root [data-style="studio"] .dm-ttp-cs,:root [data-style="studio"].dm-ttp-cs{min-height:calc(28px + 2 * var(--dm-strip-y) + 1px);margin:16px -16px -16px;padding:var(--dm-strip-y) 16px;line-height:16px;background:var(--dm-strip);border-top:1px solid var(--dm-line);border-radius:0 0 var(--dm-r-card) var(--dm-r-card)}
Tooltip.astroAstro · 35 lines
---
// Generated from designme.dev by scripts/sync.mjs. Edit the site, then sync.
// Tooltip: Quiet labels on hover, with shortcuts and a richer preview card.
// The markup, CSS and script are designme.dev's own, run by the shared core in ../core, which mounts the component when it scrolls
// into view and plays its demo. To show your own data, change tooltip.js. The markup is kept exactly as it is (is:raw).
import type { HTMLAttributes } from 'astro/types';
import '../core/designme.css';
import './tooltip.css';
export interface Props extends HTMLAttributes<'div'> {
/** "soft" or "studio" draws it in that style. Leave it out for Blueprint, or for the style set on <html> (data-style="soft" or "studio") */
'data-style'?: 'soft' | 'studio';
}
const { class: className, ...rest } = Astro.props;
---
<div class:list={['card', className]} data-c="tooltip" {...rest}>
<div class="stage-in"><Fragment is:raw>
<div class="ttp">
<div class="ttp-bar panel">
<button class="ib" aria-label="Undo" data-tip="Undo" data-tip-k="⌘Z"><svg class="i s"><use href="#i-undo"/></svg></button>
<button class="ib" aria-label="Comment" data-tip="Comment" data-tip-k="C"><svg class="i s"><use href="#i-edit"/></svg></button>
<button class="ib" aria-label="Ask agent" data-tip="Ask the agent" data-tip-k="⌘J"><svg class="i s"><use href="#i-spark"/></svg></button>
<span class="ttp-sep"></span>
<button class="ib" aria-label="Share" data-tip="Share with your team"><svg class="i s"><use href="#i-users"/></svg></button>
<span class="ttp-sep"></span>
<span class="ttp-avs"><button class="ttp-av" data-card="0" aria-label="Maya Kim"><span class="av" style="background:var(--dm-av-l);color:var(--dm-av-l-ink)" data-pic="MK-g03">MK</span></button><button class="ttp-av" data-card="1" aria-label="Design agent"><span class="av" style="background:var(--dm-av-b);color:var(--dm-av-b-ink)">DA</span></button></span>
</div>
<div class="ttp-card panel" aria-hidden="true"><div class="ttp-ci"></div></div>
</div>
</Fragment></div>
</div>
<script>
import './tooltip.js';
</script>
tooltip.htmlHTML · 17 lines
<div class="card" data-c="tooltip">
<div class="stage-in">
<div class="ttp">
<div class="ttp-bar panel">
<button class="ib" aria-label="Undo" data-tip="Undo" data-tip-k="⌘Z"><svg class="i s"><use href="#i-undo"/></svg></button>
<button class="ib" aria-label="Comment" data-tip="Comment" data-tip-k="C"><svg class="i s"><use href="#i-edit"/></svg></button>
<button class="ib" aria-label="Ask agent" data-tip="Ask the agent" data-tip-k="⌘J"><svg class="i s"><use href="#i-spark"/></svg></button>
<span class="ttp-sep"></span>
<button class="ib" aria-label="Share" data-tip="Share with your team"><svg class="i s"><use href="#i-users"/></svg></button>
<span class="ttp-sep"></span>
<span class="ttp-avs"><button class="ttp-av" data-card="0" aria-label="Maya Kim"><span class="av" style="background:var(--dm-av-l);color:var(--dm-av-l-ink)" data-pic="MK-g03">MK</span></button><button class="ttp-av" data-card="1" aria-label="Design agent"><span class="av" style="background:var(--dm-av-b);color:var(--dm-av-b-ink)">DA</span></button></span>
</div>
<div class="ttp-card panel" aria-hidden="true"><div class="ttp-ci"></div></div>
</div>
</div>
</div>
tooltip.cssHTML · 38 lines
/* ---------- Tooltip ---------- */
/* a toolbar of icon buttons whose labels (the core tooltip, data-tip) wait a moment, then follow the pointer from button to
button, and the people at its end, each opening a card about them on hover or keyboard focus. The component is the toolbar:
the card opens over the stage, so it keeps no room for it */
.ttp{position:relative;display:flex;flex-direction:column;align-items:center}
.ttp-bar{display:flex;align-items:center;gap:2px;padding:5px;border-radius:var(--dm-r-pill)}
.ttp-sep{width:1px;height:18px;background:var(--dm-line);margin:0 var(--dm-s-1)}
/* the people side by side, never stacked over each other (owner, 18 Nov) */
.ttp-avs{display:flex;gap:2px}
/* a person under the pointer stays where they are (owner, 21 Nov: "you hover it up but show tooltip down"): the one quiet hover
fill, a ring in their round shape, easing in and out */
.ttp-av{display:grid;place-items:center;width:30px;height:30px;border-radius:50%;transition:background-color var(--dm-t-fast) var(--dm-ease),box-shadow var(--dm-t-fast) var(--dm-ease)}
@media (hover:hover) and (pointer:fine){.ttp-av:hover{background:var(--dm-row);box-shadow:0 0 0 2px var(--dm-row)}}
.ttp-av .av{width:28px;height:28px;font-size:11px}
/* the card about a person: centred on their avatar (right-aligned to it at the stage's edge), under it when there is room and
over it when there isn't, and it goes where the avatar goes while it shows (placed by the script) */
.ttp-card{position:absolute;top:0;left:0;width:230px;padding:var(--dm-s-3);box-shadow:var(--dm-shadow-pop);opacity:0;transform:translateY(6px) scale(.97);filter:blur(4px);pointer-events:none;transition:opacity .25s var(--dm-ease),transform .35s var(--dm-ease),filter .25s;z-index:5}
/* the card opens from the avatar toward where it shows: down from it when it opens under it, up from it when it opens over it */
.ttp-card:not(.up){transform:translateY(-6px) scale(.97);transform-origin:50% 0}
.ttp-card.up{transform:translateY(6px) scale(.97);transform-origin:50% 100%}
.ttp-card.show{opacity:1;transform:none;filter:none}
.ttp-ch{display:flex;align-items:center;gap:10px}
.ttp-ch .av{width:34px;height:34px;font-size:12px}
.ttp-ch div{display:flex;flex-direction:column;min-width:0;font-size:13px}
.ttp-ch b{font-weight:500}
.ttp-ch div span{font-size:12px;color:var(--dm-muted)}
.ttp-cs{display:flex;align-items:center;gap:6px;margin-top:10px;font-size:12px;color:var(--dm-fg2)}
.ttp-cs .dot{color:var(--dm-ok-mark)}
/* Studio: the card's content 16px in from every edge (owner, 18 Nov); it scales and fades in, never blurs */
:root[data-style="studio"] .ttp-card.panel,:root [data-style="studio"] .ttp-card.panel,:root [data-style="studio"].ttp-card.panel{width:246px;padding:16px;filter:none;transition:opacity var(--dm-t-fast) var(--dm-ease),transform var(--dm-t) var(--dm-ease)}
/* Studio (owner, 21 Nov): the bar, as Canvas toolbar's, is a pill drawn by a half-strength hairline with only a contact shadow,
its dividers half-strength hairlines, and its hovers pills (a circle round a person, with its ring) under one gliding highlight
(hoverGlide). The card about a person is Studio's popover, 22px corners and its shadow, with what they are doing now in an
off-white foot strip under a hairline, edge to edge, its words 16px in */
:root[data-style="studio"] .ttp-bar.panel,:root [data-style="studio"] .ttp-bar.panel,:root [data-style="studio"].ttp-bar.panel{box-shadow:0 0 0 1px var(--dm-hair),0 1px 2px rgba(0,0,0,.06)}
:root[data-style="studio"] .ttp-sep,:root [data-style="studio"] .ttp-sep,:root [data-style="studio"].ttp-sep{background:color-mix(in srgb,var(--dm-line) 50%,transparent)}
:root[data-style="studio"] .ttp-cs,:root [data-style="studio"] .ttp-cs,:root [data-style="studio"].ttp-cs{min-height:calc(28px + 2 * var(--dm-strip-y) + 1px);margin:16px -16px -16px;padding:var(--dm-strip-y) 16px;line-height:16px;background:var(--dm-strip);border-top:1px solid var(--dm-line);border-radius:0 0 var(--dm-r-card) var(--dm-r-card)}
tooltip.jsHTML · 45 lines
DM.def('tooltip', (r, c) => {
r.innerHTML = r.innerHTML.replace('{undo}', I('undo')).replace('{chat}', I('edit')).replace('{spark}', I('spark')).replace('{users}', I('users')).replace('{a1}', AV('MK','var(--dm-av-l)','MK-g03')).replace('{a2}', AV('DA','var(--dm-av-b)'));
const P = [['MK','var(--dm-av-l)','Maya Kim','Design lead','Editing Pricing, frame 2','MK-g03'],['DA','var(--dm-av-b)','Design agent','Working for Maya','Auditing contrast, 2 of 6 checks']];
const card = $('.ttp-card', r), ci = $('.ttp-ci', r), root = $('.ttp', r); let t, on = null, raf = 0, at = '';
// Studio's one hover: a highlight that glides from button to button (hoverGlide)
c.offs.push(hoverGlide($('.ttp-bar', r), '.ib,.ttp-av'));
/* the card sits centred on its avatar, or right-aligned to it (left-aligned on the left) where the stage's edge is near,
under it when there is room in the stage and over it when there isn't */
const place = b => {
const rr = root.getBoundingClientRect(), br = b.getBoundingClientRect(), w = card.offsetWidth, h = card.offsetHeight;
const st = root.closest('.stage-in'), sb = st ? (st.closest('.stage-inner') || st.parentElement).getBoundingClientRect() : { left:0, right:innerWidth, top:0, bottom:innerHeight };
const lo = Math.max(0, sb.left) + 8, hi = Math.min(innerWidth, sb.right) - 8;
let x = br.left + br.width / 2 - w / 2; if (x + w > hi) x = Math.max(lo, br.right - w); else if (x < lo) x = Math.min(hi - w, br.left);
const down = br.bottom + 10 + h <= Math.min(innerHeight, sb.bottom) - 8 || br.top - 10 - h < Math.max(0, sb.top) + 8;
card.classList.toggle('up', !down);
card.style.left = Math.round(x - rr.left) + 'px'; card.style.top = Math.round((down ? br.bottom + 10 : br.top - 10 - h) - rr.top) + 'px';
};
// while it shows it goes where its avatar goes (owner, 19 Nov: nothing places a tooltip once and leaves it)
const follow = () => { raf = 0; if (!on || c.dead) return; const b = on.getBoundingClientRect(), k = `${b.left},${b.top}`; if (k !== at){ at = k; place(on); } raf = requestAnimationFrame(follow); };
const open = (b, now) => { clearTimeout(t); t = setTimeout(() => { const p = P[+b.dataset.card];
ci.innerHTML = `<div class="ttp-ch">${AV(p[0], p[1], p[5])}<div><b>${p[2]}</b><span>${p[3]}</span></div></div><div class="ttp-cs">${p[0] === 'DA' ? '<span class="think on" style="color:var(--dm-accent-mark,var(--dm-accent))"><i></i><i></i><i></i><i></i></span><span class="shim">' + p[4] + '</span>' : '<span class="dot"></span>' + p[4]}</div>`;
on = b; at = ''; place(b); card.classList.add('show'); card.removeAttribute('aria-hidden'); cancelAnimationFrame(raf); raf = requestAnimationFrame(follow); }, now ? 0 : 300); };
const close = () => { clearTimeout(t); on = null; cancelAnimationFrame(raf); card.classList.remove('show'); card.setAttribute('aria-hidden', 'true'); };
// a person's card opens on hover after a short wait, and at once on keyboard focus; it closes as the pointer or focus leaves
$$('.ttp-av', r).forEach(b => { b.onmouseenter = () => open(b); b.onmouseleave = () => { if (b !== document.activeElement || !b.matches(':focus-visible')) close(); };
b.onfocus = () => { if (b.matches(':focus-visible')) open(b, true); }; b.onblur = close; });
c.offs.push(() => { clearTimeout(t); cancelAnimationFrame(raf); });
// once it has entered, it shows what it does: a shortcut tip, then the agent's card. Pointing at it takes over, as does
// clicking, focusing or typing in it
// what the demo opened goes when the pointer comes in, unless it is on that very control: the visitor's own hover decides
// from then on, so no tip or card is left over from the demo over the one they point at
let touched = false, shown = null;
c.on(root, 'pointerenter', () => { touched = true;
if (shown === 'tip' && tipFor && !tipNext && root.contains(tipFor) && !tipFor.matches(':hover')) hideTip();
if (shown === 'card' && !$('.ttp-av:hover', r)) close();
shown = null; });
const go = () => { if (touched || c.mine) throw 'stop'; };
c.run(async () => {
await c.wait(1300); go(); if (tipFor) return;
const ask = $('[data-tip-k="⌘J"]', r); showTip(ask); shown = 'tip'; await c.wait(1700); go(); if (tipFor === ask) hideTip(true); shown = null;
await c.wait(500); go();
const av = $('.ttp-av[data-card="1"]', r); open(av); shown = 'card'; await c.wait(2600); go(); close(); shown = null;
});
});
Questions
What people ask about it.
- Can the label read differently from the button's accessible name?
- Yes.
labelis the name screen readers hear, andtipis the visible tooltip text when you want it to say more, like "Ask the agent" for a button named "Ask agent". - How do I show what the agent is doing in the person card?
- Add the agent to
peoplewith astatuslike "Auditing contrast, 2 of 6 checks" and setworking: true. The card then shows the thinking dots next to the status. - How do I open a tip from code, for a walkthrough?
- Set
openTipto the tool's key, oropenCardto a person's key. Clear it to close, and leave it undefined to let the pointer decide again.
More in Forms and inputs
Buttons, fields, pickers and sign-in flows for any product.