menuAim()
Keep a submenu open while moving to it. Nested menus and mega menus.
Try it
Move diagonally from Products towards the lower links, then try moving directly down. Click a trigger or use Enter to activate immediately.
Desktop check: The diagonal hover behavior can only be checked with a desktop mouse or trackpad. Click and keyboard activation still work on touch devices.
Products selected
<div id="your-component">
<nav class="menu-demo" data-ix="menu-aim" data-ix-side="right" aria-label="Example navigation"><div class="triggers"><button data-ix-menu-trigger="products" aria-expanded="true" aria-controls="products">Products ›</button><button data-ix-menu-trigger="solutions" aria-expanded="false" aria-controls="solutions">Solutions ›</button><button data-ix-menu-trigger="resources" aria-expanded="false" aria-controls="resources">Resources ›</button></div><div class="menu-panel" id="products" data-ix-menu-panel="products"><b>Products</b><a href="#example">Overview</a><a href="#example">Analytics</a><a href="#example">Integrations</a><a href="#example">Developer tools</a></div><div class="menu-panel" id="solutions" data-ix-menu-panel="solutions" hidden><b>Solutions</b><a href="#example">Business</a><a href="#example">Teams</a><a href="#example">Enterprise</a></div><div class="menu-panel" id="resources" data-ix-menu-panel="resources" hidden><b>Resources</b><a href="#example">Guides</a><a href="#example">Documentation</a></div></nav><p class="demo-status" aria-live="polite">Products selected</p>
</div>import * as kit from 'subtly';
// Start once at application level.
const interactions = kit.createInteractions();
interactions.start();
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 nav = el.querySelector('nav');
// The adapter emits intent. This component controls visibility.
nav.addEventListener('ix:menu-change', ({ detail }) => {
for (const panel of nav.querySelectorAll('[data-ix-menu-panel]')) {
panel.hidden = panel.dataset.ixMenuPanel !== detail.key;
}
for (const trigger of nav.querySelectorAll('[data-ix-menu-trigger]')) {
trigger.setAttribute('aria-expanded', String(trigger.dataset.ixMenuTrigger === detail.key));
}
status(detail.key + ' selected');
}, { signal });
}
const stop = setup();
// Call when this component is removed.
function cleanup() {
listeners.abort();
stop?.();
interactions.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
Create one instance per sibling submenu group. remember(event) records the pointer on the active trigger; request(event, panel, activate, side, key) delays a competing panel only while the mouse is heading toward the current submenu.
- Interface
delay=300, tolerance=8; remember pointer on active trigger, request on competing trigger movement, cancel on panel entry or exit, destroy on unmount.- Lifecycle
- Call cancel() when the pointer enters the panel or leaves the menu. destroy() clears pending work and the remembered pointer. Pass the actual side if the submenu flips position.
Accessibility and safety
Implementation notes
This selects panels, not a complete ARIA menu. Add appropriate dismissal, keyboard and touch behavior. The declarative adapter uses a fixed side; use the programmatic API for placement flips.
This manages pointer timing, not a complete ARIA menu. Click and keyboard activation should bypass the delay; add touch activation, dismissal and correct semantics separately.
What to check
Move diagonally into the submenu, then directly toward another trigger. Check keyboard and tap activation are immediate and test flipped submenu positions.
View documented module source
/** Safe triangle for sibling submenu switching. All coordinates are viewport pixels.
* Call remember() while over the active trigger; call request() on competing hover.
* Call cancel() on entering the panel or leaving the menu. Keyboard/click bypass it.
*/
export function menuAim({ delay = 300, tolerance = 8 } = {}) {
let anchor, timer, pending;
const cross = (a,b,p) => (b.x-a.x)*(p.y-a.y)-(b.y-a.y)*(p.x-a.x);
function cancel() { clearTimeout(timer); timer = undefined; pending = undefined; }
function remember(e) { cancel(); anchor = {x:e.clientX,y:e.clientY}; }
function request(e, panel, activate, side = 'right', key = activate) {
const p = {x:e.clientX,y:e.clientY};
const r = panel.getBoundingClientRect(), t = tolerance;
// Use the edge facing the trigger, including a small tolerance.
const edges = {right:[{x:r.left,y:r.top-t},{x:r.left,y:r.bottom+t}],left:[{x:r.right,y:r.top-t},{x:r.right,y:r.bottom+t}],bottom:[{x:r.left-t,y:r.top},{x:r.right+t,y:r.top}],top:[{x:r.left-t,y:r.bottom},{x:r.right+t,y:r.bottom}]};
const [b,c] = edges[side] ?? edges.right;
const toward = anchor && ({right:p.x>anchor.x,left:p.x<anchor.x,bottom:p.y>anchor.y,top:p.y<anchor.y}[side]);
const signs = anchor ? [cross(anchor,b,p),cross(b,c,p),cross(c,anchor,p)] : [];
const safe = e.pointerType === 'mouse' && toward && Math.abs(cross(anchor,b,c))>0 && !(signs.some(v=>v<0)&&signs.some(v=>v>0));
if (!safe) { cancel(); activate(); return; }
// Do not restart the deadline on every pointermove.
if (pending === key) return;
cancel(); pending = key;
timer = setTimeout(()=>{cancel(); activate();},delay);
}
return {remember,request,cancel,destroy(){cancel();anchor=undefined;}};
}