tooltipGroup()
Show hints on toolbar buttons. Adjacent toolbar tips.
Try it
Hover a formatting button. The first hint waits 500 ms; moving to a neighboring button shows its hint immediately. Keyboard focus always shows the hint immediately.
Desktop check: The hover timing can only be checked with a desktop mouse or trackpad. Keyboard focus can also reveal each toolbar hint.
Good writing starts with a small idea.
Hover or focus a formatting button.
<div id="your-component">
<div class="demo-toolbar" aria-label="Formatting actions">
<span class="hint-wrap"><button aria-describedby="tip-bold">Bold</button><span id="tip-bold" role="tooltip" hidden>Make selected text bold</span></span>
<span class="hint-wrap"><button aria-describedby="tip-italic">Italic</button><span id="tip-italic" role="tooltip" hidden>Emphasize selected text</span></span>
<span class="hint-wrap"><button aria-describedby="tip-link">Link</button><span id="tip-link" role="tooltip" hidden>Insert a link</span></span>
</div><p class="sample-document">Good writing starts with a small idea.</p><p class="demo-status" role="status">Hover or focus a formatting button.</p>
</div>import * as kit from 'subtly';
const el = document.querySelector('#your-component');
const listeners = new AbortController();
const { signal } = listeners;
const status = text => {
el.querySelector('.demo-status').textContent = text;
};
// Local simulation only. Replace with your real data operation.
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
function setup() {
const group = kit.tooltipGroup({ delay: 500, cooldown: 300 });
for (const [key, wrapper] of [...el.querySelectorAll('.hint-wrap')].entries()) {
const button = wrapper.querySelector('button');
const tooltip = wrapper.querySelector('[role="tooltip"]');
const hide = () => { tooltip.hidden = true; };
const enter = immediate => group.enter(key, () => {
tooltip.hidden = false;
status(tooltip.textContent);
}, hide, { immediate });
button.addEventListener('pointerenter', e => {
if (e.pointerType === 'mouse') enter(false);
}, { signal });
button.addEventListener('pointerleave', () => {
if (document.activeElement !== button) group.leave(key);
}, { signal });
button.addEventListener('focus', () => enter(true), { signal });
button.addEventListener('blur', () => group.leave(key), { signal });
button.addEventListener('keydown', e => {
if (e.key === 'Escape') group.leave(key);
}, { signal });
button.addEventListener('click', () => status(button.textContent + ' selected (demo)'), { signal });
}
return () => group.destroy();
}
const stop = setup();
// Call when this component is removed.
function cleanup() {
listeners.abort();
stop?.();
}Add it to your project
- Copy the HTML. Use the HTML tab above and place the markup inside a dedicated component root. Layout classes belong to this documentation website; use your own styles.
- Connect the behavior. Copy the JavaScript tab, update
#your-componentand initialize it after the elements exist. Replace simulated operations with your own data. - Clean up on unmount. Call the example’s
cleanup()when the component is removed. It aborts application listeners and releases the utility’s timers or observers.
The preview and JavaScript tab use the same utility wiring. status() writes plain text into the example’s status element. wait() is a simulated local delay. For examples with data-ix attributes, start the declarative runtime once at application level, as shown in the code. Use your framework guide to place setup and cleanup correctly.
API and lifecycle
Share one timing instance across neighboring tooltip triggers. enter(key, show, hide) opens the chosen tooltip; the initial delay is 500 ms, followed by a 300 ms warm period for adjacent hints.
- Interface
delay=500, cooldown=300; enter(key,show,hide,{immediate}), leave(key), destroy.- Lifecycle
- leave(key) cancels or closes the matching tooltip. Pass immediate:true for keyboard focus. destroy() clears timing state and closes the current tooltip.
Accessibility and safety
Implementation notes
Timing only: provide accessible descriptions, Escape and hoverable tooltip content when required.
The helper controls timing only. Your tooltip needs an accessible description, Escape behavior where appropriate, and content that stays reachable when hover is required.
What to check
Hover the first trigger, move to its neighbor and compare the delay. Focus a trigger with the keyboard and verify the hint appears immediately and can be dismissed.
View documented module source
/** Share one instance across neighbouring tooltip triggers. Focus should open immediately.
* show() and hide() are callbacks controlling your existing tooltip component. */
export function tooltipGroup({delay=500, cooldown=300}={}) {
let timer, current, warmUntil=0;
function close(){if(current){current.hide();current=null;warmUntil=performance.now()+cooldown;}}
return {
enter(key,show,hide,{immediate=false}={}){clearTimeout(timer);if(current?.key===key)return;close();timer=setTimeout(()=>{current={key,hide};show();},immediate||performance.now()<warmUntil?0:delay);},
leave(key){clearTimeout(timer);if(current?.key===key)close();},
destroy(){clearTimeout(timer);close();warmUntil=0;}
};
}