// A tap is a touch that stays put on something still. A click that ends a // touch which moved more than a few pixels, or that came down while what // holds a list was still scrolling (a tap that stops a fling, which a // phone's browser may end as a click on whatever scrolled under it), is no // tap: `strays`, on a list, drops it before it reaches the item, its link or // its buttons. A scroller is still scrolling until the browser says it came // to rest (`scrollend`) or it has not moved for a frame or so: one gliding // moves every frame, so a tap once the glide has stopped, however soon, // opens. A mouse's clicks and a keyboard's are left as they are. Its // listeners are the browser's alone: attached as a list is, never on a server. /** How far a touch may move and still be a tap, in CSS pixels. */ const SLOP = 10; /** How long a scroller that has not moved may still be scrolling: one gliding moves every frame (a 30 Hz one too), so longer is a scroll at rest. */ const MOVING = 100; /** The touch last put down: where, when, and whether it has moved past the slop or been taken for a scroll. */ let down: { x: number; y: number; at: number; touch: boolean; moved: boolean } | null = null; /** When each scroller last moved, and last came to rest where the browser says so, by its event's target (the document for the page's own). */ const scrolled = new WeakMap(); const rested = new WeakMap(); let listening = false; function listen() { if (listening) return; listening = true; const options = { capture: true, passive: true }; window.addEventListener( 'pointerdown', (event) => { if (event.isPrimary) down = { x: event.clientX, y: event.clientY, at: event.timeStamp, touch: event.pointerType !== 'mouse', moved: false }; }, options ); window.addEventListener( 'pointermove', (event) => { if (down?.touch && event.isPrimary && Math.hypot(event.clientX - down.x, event.clientY - down.y) > SLOP) down.moved = true; }, options ); // The browser took the touch for a scroll. window.addEventListener('pointercancel', () => down && (down.moved = true), options); window.addEventListener('scroll', (event) => event.target && scrolled.set(event.target, event.timeStamp), options); // The browser says it came to rest, where it does (a touch that stops a glide brings one too, after it came down). window.addEventListener('scrollend', (event) => event.target && rested.set(event.target, event.timeStamp), options); } /** Whether `event`, a click in `list`, ends a touch that was no tap. */ function stray(list: Element, event: MouseEvent): boolean { // A keyboard's click (or a script's) has no press count. if (!down?.touch || event.detail === 0) return false; if (down.moved || Math.hypot(event.clientX - down.x, event.clientY - down.y) > SLOP) return true; for (let at: Node | null = list; at; at = at.parentNode) { const moved = scrolled.get(at); const rest = rested.get(at); const resting = rest !== undefined && moved !== undefined && rest >= moved && rest <= down.at; if (moved !== undefined && moved > down.at - MOVING && !resting) return true; } return false; } /** An attachment: a click in `list` that ends no tap is dropped, the link it landed on not followed. */ export function strays(list: HTMLElement) { listen(); const drop = (event: MouseEvent) => { if (!stray(list, event)) return; event.preventDefault(); event.stopPropagation(); }; list.addEventListener('click', drop, true); return () => list.removeEventListener('click', drop, true); }