A clearer home for your next release.
In progresshoverIntent()
Open a preview after a deliberate hover. Hover cards, navigation.
Try it
Move quickly across the project button, then rest on it. The project preview opens after 150 ms of deliberate hover. You can also focus or click the button.
Desktop check: The hover delay can only be checked with a desktop mouse or trackpad. Keyboard focus and click can also activate the example.
Preview closed. Hover, focus or click to open.
<div id="your-component">
<div class="preview-project">
<button class="project-trigger" aria-expanded="false" aria-controls="project-preview">Website redesign <span class="muted">Project preview</span></button>
<article id="project-preview" class="project-preview" hidden><span class="sample-art" aria-hidden="true"></span><strong>Website redesign</strong><p>A clearer home for your next release.</p><span class="tag">In progress</span></article>
</div><p class="demo-status" role="status">Preview closed. Hover, focus or click to open.</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 trigger = el.querySelector('button');
const preview = el.querySelector('.project-preview');
function show(open) {
preview.hidden = !open;
trigger.setAttribute('aria-expanded', String(open));
status(open ? 'Project preview open' : 'Project preview closed');
}
const stop = kit.hoverIntent(trigger, {
delay: 150,
onEnter: () => show(true),
onLeave: () => { if (document.activeElement !== trigger) show(false); }
});
trigger.addEventListener('focus', () => show(true), { signal });
trigger.addEventListener('blur', () => show(false), { signal });
trigger.addEventListener('click', () => show(true), { signal });
trigger.addEventListener('keydown', event => {
if (event.key === 'Escape') show(false);
}, { signal });
return stop;
}
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
Attach hoverIntent(element, callbacks) to delay a mouse preview until pointer movement settles. Defaults are 150 ms opening delay, 120 ms closing delay and a 6 px movement threshold.
- Interface
delay=150, closeDelay=120, distance=6; returns cleanup.- Lifecycle
- The returned cleanup cancels timers and removes pointer listeners. Mount once per trigger and clean up when the trigger or its view is removed.
Accessibility and safety
Implementation notes
Hover is an enhancement. Keep click, focus and keyboard alternatives. Do not hide interactive content merely because a pointer leaves a trigger.
The utility listens to mouse pointer events. Add focus and click/tap activation separately, and keep interactive preview content reachable when moving off the trigger.
What to check
Sweep across the trigger quickly, then hover deliberately. Check focus, tap and click alternatives, and moving the pointer into the preview.
View documented module source
/** Delay hover activation until pointer movement settles. Touch uses click instead.
* Returns cleanup; call it from Alpine destroy(). */
export function hoverIntent(element, {onEnter, onLeave, delay=150, closeDelay=120, distance=6}={}) {
let timer, active=false, origin;
const clear=()=>clearTimeout(timer);
function schedule(e){ clear(); origin={x:e.clientX,y:e.clientY}; timer=setTimeout(()=>{active=true;onEnter?.(e);},delay); }
function enter(e){if(e.pointerType!=='mouse')return;clear();if(!active)schedule(e);}
function move(e){if(e.pointerType==='mouse'&&!active&&origin&&Math.hypot(e.clientX-origin.x,e.clientY-origin.y)>distance)schedule(e);}
function leave(e){if(e.pointerType!=='mouse')return;clear();origin=null;if(active)timer=setTimeout(()=>{active=false;onLeave?.(e);},closeDelay);}
element.addEventListener('pointerenter',enter);element.addEventListener('pointermove',move);element.addEventListener('pointerleave',leave);
return ()=>{clear();element.removeEventListener('pointerenter',enter);element.removeEventListener('pointermove',move);element.removeEventListener('pointerleave',leave);};
}