|
| 1 | +import * as React from "react"; |
| 2 | + |
| 3 | +const BOUNDARY_EPSILON_PX = 1; |
| 4 | +const CONVERSATION_SCROLL_SELECTOR = "[data-buzz-conversation-scroll]"; |
| 5 | +const SCROLLABLE_OVERFLOW_VALUES = new Set(["auto", "scroll", "overlay"]); |
| 6 | + |
| 7 | +function isHTMLElement(value: EventTarget | null): value is HTMLElement { |
| 8 | + return value instanceof HTMLElement; |
| 9 | +} |
| 10 | + |
| 11 | +function isDocumentElement(element: HTMLElement) { |
| 12 | + return element === document.body || element === document.documentElement; |
| 13 | +} |
| 14 | + |
| 15 | +function isScrollableY(element: HTMLElement) { |
| 16 | + if (isDocumentElement(element)) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + const style = window.getComputedStyle(element); |
| 21 | + if (!SCROLLABLE_OVERFLOW_VALUES.has(style.overflowY)) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + return element.scrollHeight > element.clientHeight + BOUNDARY_EPSILON_PX; |
| 26 | +} |
| 27 | + |
| 28 | +function canScrollY(element: HTMLElement, deltaY: number) { |
| 29 | + if (deltaY < 0) { |
| 30 | + return element.scrollTop > BOUNDARY_EPSILON_PX; |
| 31 | + } |
| 32 | + |
| 33 | + const maxScrollTop = element.scrollHeight - element.clientHeight; |
| 34 | + return element.scrollTop < maxScrollTop - BOUNDARY_EPSILON_PX; |
| 35 | +} |
| 36 | + |
| 37 | +function isConversationScroller(element: HTMLElement) { |
| 38 | + return Boolean(element.closest(CONVERSATION_SCROLL_SELECTOR)); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Stops macOS/WKWebView rubber-band gestures from escaping into the viewport. |
| 43 | + * |
| 44 | + * Buzz is laid out as fixed-height nested panes. On macOS, a wheel/trackpad |
| 45 | + * gesture that starts over a non-scrollable pane (or over a scrollable pane at |
| 46 | + * its boundary) can still be handed to the WKWebView viewport, which rubber- |
| 47 | + * bands the entire app and reveals a blank strip above/below the UI. CSS |
| 48 | + * `overscroll-behavior` is not enough for all of the empty/header/footer hit |
| 49 | + * targets in the webview, so this capture listener consumes only gestures that |
| 50 | + * otherwise have nowhere app-local to scroll. |
| 51 | + * |
| 52 | + * Real scrolling is left alone: if any scroll container under the pointer can |
| 53 | + * move in the wheel direction, the browser handles it normally. At boundaries, |
| 54 | + * only containers marked with `data-buzz-conversation-scroll` are allowed to |
| 55 | + * receive the gesture so their own local elastic affordance can remain; every |
| 56 | + * other boundary is locked and cannot chain to the viewport. |
| 57 | + */ |
| 58 | +export function useWebviewScrollBoundaryLock() { |
| 59 | + React.useEffect(() => { |
| 60 | + function handleWheel(event: WheelEvent) { |
| 61 | + if (event.defaultPrevented || event.deltaY === 0 || event.ctrlKey) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const path = event.composedPath(); |
| 66 | + let firstScrollable: HTMLElement | null = null; |
| 67 | + |
| 68 | + for (const target of path) { |
| 69 | + if (!isHTMLElement(target) || !isScrollableY(target)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + firstScrollable ??= target; |
| 74 | + if (canScrollY(target, event.deltaY)) { |
| 75 | + return; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (firstScrollable && isConversationScroller(firstScrollable)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + event.preventDefault(); |
| 84 | + event.stopPropagation(); |
| 85 | + } |
| 86 | + |
| 87 | + window.addEventListener("wheel", handleWheel, { |
| 88 | + capture: true, |
| 89 | + passive: false, |
| 90 | + }); |
| 91 | + return () => { |
| 92 | + window.removeEventListener("wheel", handleWheel, { capture: true }); |
| 93 | + }; |
| 94 | + }, []); |
| 95 | +} |
0 commit comments