Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

refactor(useScrollLock): the page hold asks the native-scroll probe's edge test (#9812)

Christopher Chedeauaaacae935972
diff --git a/.changeset/scroll-lock-shared-edge-test.md b/.changeset/scroll-lock-shared-edge-test.md
new file mode 100644
index 0000000000..be3665235b
--- /dev/null
+++ b/.changeset/scroll-lock-shared-edge-test.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[chore] Internal: the scroll lock's touch guard asks the gesture system's native-scroll test whether an element under a finger can scroll, instead of keeping its own copy. The copy left out `overflow: overlay`, which current Chromium and WebKit compute as `auto`, so pages there behave the same.
+
+@vjeux
diff --git a/packages/core/src/hooks/scrollLockTouchGuard.ts b/packages/core/src/hooks/scrollLockTouchGuard.ts
index aaf478f95e..7df65c2d3b 100644
--- a/packages/core/src/hooks/scrollLockTouchGuard.ts
+++ b/packages/core/src/hooks/scrollLockTouchGuard.ts
@@ -5,7 +5,7 @@
/**
* @file scrollLockTouchGuard.ts
* @input The node under a finger and the pan's direction, while a scroll lock
- * holds the page
+ * holds the page; the native-scroll probe's edge test (gestures/nativeScroll)
* @output isPanAbsorbedBeforePage and isPageZoomedIn, for the page hold
* @position Internal hook utility; gestures/pageHold calls it for
* useScrollLock.
@@ -19,34 +19,8 @@
// element, whose containing block is the viewport, so its untaken pans go to
// the page.
+import {canElementScroll} from '../gestures/nativeScroll';
import {parentAcrossShadow} from '../utils/flatTree';
-import {isRtlElement} from './isRtlElement';
-
-const SCROLLABLE = new Set(['auto', 'scroll']);
-
-/** Overflow at or below this many px is a box that fits, not a scroller. */
-const EDGE_TOLERANCE = 1;
-
-function canMoveY(element: Element, dy: number, range: number): boolean {
- // A finger moving down drags the content down: scrollTop decreases.
- return dy > 0
- ? element.scrollTop > EDGE_TOLERANCE - 0.5
- : element.scrollTop < range - EDGE_TOLERANCE + 0.5;
-}
-
-function canMoveX(
- element: Element,
- dx: number,
- range: number,
- isRtl: boolean,
-): boolean {
- const min = isRtl ? -range : 0;
- const max = isRtl ? 0 : range;
- // A finger moving right drags the content right: scrollLeft decreases.
- return dx > 0
- ? element.scrollLeft > min + EDGE_TOLERANCE - 0.5
- : element.scrollLeft < max - EDGE_TOLERANCE + 0.5;
-}
/**
* Whether `element` itself takes a pan. It does when it can still scroll that
@@ -59,23 +33,16 @@ function takesPan(
dx: number,
dy: number,
): boolean {
- if (!SCROLLABLE.has(isVertical ? style.overflowY : style.overflowX)) {
- return false;
+ const x = isVertical ? 0 : dx;
+ const y = isVertical ? dy : 0;
+ if (canElementScroll(element, x, y)) {
+ return true;
}
- const range = isVertical
- ? element.scrollHeight - element.clientHeight
- : element.scrollWidth - element.clientWidth;
- // Content that fits has nothing to scroll. The pan goes on to the next
- // scroller in the chain, and at last to the page.
- if (range <= EDGE_TOLERANCE) {
+ // A scroller at its edge can still move the other way. A box that does not
+ // scroll, or whose content fits, cannot. Its pan goes on along the chain.
+ if (!canElementScroll(element, -x, -y)) {
return false;
}
- const canMove = isVertical
- ? canMoveY(element, dy, range)
- : canMoveX(element, dx, range, isRtlElement(element as HTMLElement));
- if (canMove) {
- return true;
- }
const behavior = isVertical
? style.overscrollBehaviorY
: style.overscrollBehaviorX;
diff --git a/packages/core/src/hooks/useScrollLock.test.ts b/packages/core/src/hooks/useScrollLock.test.ts
index 12edc92c67..b00bb06ee4 100644
--- a/packages/core/src/hooks/useScrollLock.test.ts
+++ b/packages/core/src/hooks/useScrollLock.test.ts
@@ -307,6 +307,15 @@ describe('useScrollLock', () => {
expect(pan(row, 200, 300)).toBe(false);
});
+ it('lets a pan through on a scroller whose overflow is `overlay`, as the browser scrolls it', () => {
+ const list = scroller(400, 100);
+ list.style.overflowY = 'overlay';
+ renderHook(() => useScrollLock(true));
+
+ expect(pan(list, 300, 200)).toBe(false);
+ expect(pan(list, 200, 300)).toBe(false);
+ });
+
it('cancels a pan on a scroller whose content fits', () => {
const body = scroller(0, 0);
renderHook(() => useScrollLock(true));
diff --git a/packages/core/src/hooks/useScrollLock.ts b/packages/core/src/hooks/useScrollLock.ts
index 45f37ec9c5..aef20bcd4f 100644
--- a/packages/core/src/hooks/useScrollLock.ts
+++ b/packages/core/src/hooks/useScrollLock.ts
@@ -24,11 +24,6 @@ import {holdPageScroll} from '../gestures/pageHold';
import {isPageZoomedIn, isPanAbsorbedBeforePage} from './scrollLockTouchGuard';
import {holdScrollbarGutter, type ScrollbarGutterHold} from './scrollbarGutter';
-// iOS Safari scrolls an `overflow: hidden` page under a finger.
-// So the lock also cancels each one-finger pan that would reach the page. The
-// scroll offset never changes, so unlock restores nothing, and iOS Safari's
-// toolbar keeps its state.
-
interface ActiveLock {
releaseRootStyle: () => void;
releasePageHold: () => void;
@@ -41,8 +36,9 @@ let activeLock: ActiveLock | null = null;
/**
* Locks page scroll while `isLocked` is true.
*
- * The page keeps its scroll offset, so nothing jumps on lock or unlock. Where
- * the primary pointer is coarse (phones and tablets), the lock cancels a
+ * The page keeps its scroll offset, so nothing jumps on lock or unlock. iOS
+ * Safari scrolls an `overflow: hidden` page under a finger. So where the
+ * primary pointer is coarse (phones and tablets), the lock cancels a
* one-finger pan that would reach the page.
* Pans that a scroller in the overlay takes, and two-finger zoom, go through.
* The gutter of a classic scrollbar stays open, so the page does not shift