Open History to see the last commit
1export function isHorizontalWheel(
2 deltaY: number,
3 wheel?: Pick<WheelEvent, "deltaX" | "shiftKey">,
4): boolean {
5 // Shift-wheel scrolls sideways; trackpads can add vertical noise to a horizontal swipe.
6 return Boolean(wheel?.shiftKey || Math.abs(wheel?.deltaX ?? 0) > Math.abs(deltaY));
7}
8
9export function isTimelineScrollTarget(
10 target: EventTarget | null,
11 timeline: HTMLElement,
12 deltaY: number,
13 wheel?: Pick<WheelEvent, "deltaX" | "shiftKey">,
14): boolean {
15 if (!(target instanceof Element) || !timeline.contains(target) || deltaY === 0) return false;
16 if (isHorizontalWheel(deltaY, wheel)) return false;
17
18 for (
19 let element: Element | null = target;
20 element && element !== timeline;
21 element = element.parentElement
22 ) {
23 const style = getComputedStyle(element);
24 if (style.overflowY !== "auto" && style.overflowY !== "scroll") continue;
25
26 const canScroll =
27 deltaY < 0
28 ? element.scrollTop > 0
29 : element.scrollTop < element.scrollHeight - element.clientHeight;
30 if (
31 canScroll ||
32 style.overscrollBehaviorY === "contain" ||
33 style.overscrollBehaviorY === "none"
34 ) {
35 return false;
36 }
37 }
38 return true;
39}
40
41// Sandboxed iframe gestures are invisible to the parent; only their chained scroll arrives.
42export function createUpwardScrollDetector(initial: { top: number; height: number }) {
43 let baseline = initial;
44 return (next: { top: number; height: number; programmatic?: boolean }) => {
45 if (next.programmatic || next.height < baseline.height || next.top > baseline.top) {
46 baseline = next;
47 return false;
48 }
49 if (next.top < baseline.top - 1) {
50 baseline = next;
51 return true;
52 }
53 // A slow scroll can move under a pixel per event; let those moves add up.
54 baseline = { top: baseline.top, height: next.height };
55 return false;
56 };
57}