From ca30677fe8d8a68cdcadccf24a3b650e78f99d23 Mon Sep 17 00:00:00 2001 From: Ivor Zhou Date: Tue, 10 Nov 2026 17:12:08 -0800 Subject: [PATCH] fix(BottomSheet): a swipe that starts slowly on the sheet's content moves the sheet In WebKit, a touch on a sheet whose content fits arms both scroll edges. A first move of 8 px or less disarmed both edges, so the pull never reached the sheet: the sheet stayed open, or jumped a few pixels and fell back. A fast swipe, or a swipe on the grab bar, still worked. An edge now goes back to the native scroll only when the content has room to scroll that way. Content that fits has nothing to scroll, so both edges stay armed until the pull passes the drag slop. At the edge of a list that scrolls, a move away from the edge takes the touch for the scroll only once it passes a tap's wobble, 5 px. A shorter move commits no native pan, so a pull down that follows a small wobble up still moves the sheet. The first move past the drag slop on either axis now decides the touch, once. A mostly vertical pull against an armed edge is the sheet's drag. A mostly sideways move is the row's for the rest of the touch. Any other move is the content's scroll. So a swipe along a row that drifts, or that turns into a pull, never moves the sheet late. --- .../bottom-sheet-fitting-content-slow-pull.md | 7 + .../src/BottomSheet/useSheetGestures.test.ts | 321 ++++++++++++++++++ .../core/src/BottomSheet/useSheetGestures.ts | 51 ++- 3 files changed, 370 insertions(+), 9 deletions(-) create mode 100644 .changeset/bottom-sheet-fitting-content-slow-pull.md diff --git a/.changeset/bottom-sheet-fitting-content-slow-pull.md b/.changeset/bottom-sheet-fitting-content-slow-pull.md new file mode 100644 index 0000000000000..c1d72a4532298 --- /dev/null +++ b/.changeset/bottom-sheet-fitting-content-slow-pull.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[fix] BottomSheet: in Safari, and in every browser on iPhone and iPad, a swipe on the sheet's content that starts slowly now moves the sheet, so a slow swipe down can close it. Before, on content that fits, when the finger's first move was 8 px or less, in either direction, the sheet stopped answering that swipe: it stayed open, or moved a few pixels and fell back. Fast swipes and swipes on the grab bar already worked, and Chromium browsers were not affected. The fix applies wherever the touched content has nothing to scroll: a sheet whose content fits, and the fixed header or footer around a list. That includes the PowerSearch and Tokenizer sheets. At the top of a list that scrolls, a pull down now also moves the sheet when the finger first wobbles up by less than 5 px; a move up of 5 px or more still scrolls the list. In every browser, the sheet now decides who owns a touch on its content once, at the first move past 8 px. A mostly vertical pull at a scroll edge is the sheet's. A mostly sideways move, such as a swipe along a row of chips, is not the sheet's for the rest of that touch. Before, at the top of a list, a sideways swipe that drifted more than 8 px down, or a pull that began more sideways than down, also moved the sheet. + +@imdreamrunner diff --git a/packages/core/src/BottomSheet/useSheetGestures.test.ts b/packages/core/src/BottomSheet/useSheetGestures.test.ts index 948e422ed3b23..39beb5dac170f 100644 --- a/packages/core/src/BottomSheet/useSheetGestures.test.ts +++ b/packages/core/src/BottomSheet/useSheetGestures.test.ts @@ -1629,6 +1629,327 @@ describe('useSheetGestures', () => { } }); + describe('content that fits', () => { + // Nothing scrolls, so a touch arms both edges at touch-down. + function fittingSheet() { + const {hook} = setup({ + snapHeights: () => [200], + offscreenBlockEndInset: TOUCH_RESERVE, + }); + const el = makeScroller({ + scrollTop: 0, + clientHeight: 200, + scrollHeight: 200, + }); + act(() => hook.result.current.sheetRef(el)); + act(() => hook.result.current.bodyProps.ref(el)); + return {hook, el}; + } + + it.each([ + {first: 1, isFirstCancelled: false}, + {first: 4, isFirstCancelled: false}, + {first: 5, isFirstCancelled: true}, + {first: 8, isFirstCancelled: true}, + ])( + 'promotes a pull-down whose first move is $first px, under the drag slop', + ({first, isFirstCancelled}) => { + const {hook, el} = fittingSheet(); + let start!: Event; + let pull!: Event; + act(() => { + touch(el, 'touchstart', 100); + start = touch(el, 'touchmove', 100 + first); + }); + // A tap's wobble keeps its click. Past the wobble, the sheet cancels + // the pan. + expect(start.defaultPrevented).toBe(isFirstCancelled); + expect(hook.result.current.isDragging).toBe(false); + act(() => { + pull = touch(el, 'touchmove', 140); + }); + expect(pull.defaultPrevented).toBe(true); + expect(hook.result.current.isDragging).toBe(true); + // The drag starts from the touch-down point, so the sheet loses no + // travel. + expect(liveOffset(hook)).toBe(40); + }, + ); + + it('promotes a pull-down that creeps a pixel at a time through the slop', () => { + const {hook, el} = fittingSheet(); + const moves: Event[] = []; + act(() => { + touch(el, 'touchstart', 100); + for (let y = 101; y <= 108; y += 1) { + moves.push(touch(el, 'touchmove', y)); + } + }); + expect(hook.result.current.isDragging).toBe(false); + expect(moves.map(move => move.defaultPrevented)).toEqual([ + false, + false, + false, + false, + true, + true, + true, + true, + ]); + act(() => { + touch(el, 'touchmove', 109); + }); + expect(hook.result.current.isDragging).toBe(true); + expect(liveOffset(hook)).toBe(9); + }); + + it('promotes a pull-down after the finger drifts up as it lands', () => { + const {hook, el} = fittingSheet(); + let drift!: Event; + let pull!: Event; + act(() => { + touch(el, 'touchstart', 100); + drift = touch(el, 'touchmove', 99); + }); + expect(drift.defaultPrevented).toBe(false); + expect(hook.result.current.isDragging).toBe(false); + act(() => { + pull = touch(el, 'touchmove', 140); + }); + expect(pull.defaultPrevented).toBe(true); + expect(hook.result.current.isDragging).toBe(true); + expect(liveOffset(hook)).toBe(40); + }); + + it('claims a pull-up after the finger drifts down as it lands', () => { + const {hook, el} = fittingSheet(); + let pull!: Event; + act(() => { + touch(el, 'touchstart', 300); + touch(el, 'touchmove', 303); + pull = touch(el, 'touchmove', 250); + }); + // The sheet takes the pull from the touch-down, and cancels its pan. + expect(pull.defaultPrevented).toBe(true); + expect(hook.result.current.isDragging).toBe(true); + expect(-liveOffset(hook)).toBeCloseTo( + rubberBand(50, SHEET_HEIGHT - TOUCH_RESERVE), + 6, + ); + }); + }); + + it('gives a list at its top to its scroll on an upward first move, and takes no later pull-down', () => { + const {hook} = setup({snapHeights: () => [200]}); + const el = makeScroller({ + scrollTop: 0, + clientHeight: 200, + scrollHeight: 800, + }); + act(() => hook.result.current.sheetRef(el)); + act(() => hook.result.current.bodyProps.ref(el)); + let back!: Event; + act(() => { + touch(el, 'touchstart', 100); + // A kept move of 5 px commits iOS Safari to the list's pan. + touch(el, 'touchmove', 95); + back = touch(el, 'touchmove', 150); + }); + // The scroll owns this touch, so the drag does not change hands. + expect(back.defaultPrevented).toBe(false); + expect(hook.result.current.isDragging).toBe(false); + }); + + it('gives a list at its end to its scroll on a downward first move, and claims no later pull-up', () => { + const {hook} = setup({snapHeights: () => [200]}); + const el = makeScroller({ + scrollTop: 600, + clientHeight: 200, + scrollHeight: 800, + }); + act(() => hook.result.current.sheetRef(el)); + act(() => hook.result.current.bodyProps.ref(el)); + // Settle at the lower detent, where the sheet claims a pull up at the + // end. + down(hook, 0, 0, el); + move(hook, 180, 700, el); + up(hook, 180, 1100, el); + expect(hook.result.current.settledOffset).toBe(200); + let back!: Event; + act(() => { + touch(el, 'touchstart', 300); + touch(el, 'touchmove', 305); + back = touch(el, 'touchmove', 250); + }); + expect(back.defaultPrevented).toBe(false); + }); + + it('promotes a pull-down at the top of a list after the finger wobbles up less than a tap does', () => { + const {hook} = setup({snapHeights: () => [200]}); + const el = makeScroller({ + scrollTop: 0, + clientHeight: 200, + scrollHeight: 800, + }); + act(() => hook.result.current.sheetRef(el)); + act(() => hook.result.current.bodyProps.ref(el)); + let wobble!: Event; + let pull!: Event; + act(() => { + touch(el, 'touchstart', 100); + // A kept move of 4 px commits no native pan, so it decides nothing. + wobble = touch(el, 'touchmove', 96); + pull = touch(el, 'touchmove', 106); + }); + expect(wobble.defaultPrevented).toBe(false); + expect(pull.defaultPrevented).toBe(true); + expect(hook.result.current.isDragging).toBe(false); + act(() => { + touch(el, 'touchmove', 140); + }); + expect(hook.result.current.isDragging).toBe(true); + expect(liveOffset(hook)).toBe(40); + }); + + it('claims a pull-up at the end of a list after the finger wobbles down less than a tap does', () => { + const {hook} = setup({snapHeights: () => [200]}); + const el = makeScroller({ + scrollTop: 600, + clientHeight: 200, + scrollHeight: 800, + }); + act(() => hook.result.current.sheetRef(el)); + act(() => hook.result.current.bodyProps.ref(el)); + // Settle at the lower detent, where the sheet claims a pull up at the + // end. + down(hook, 0, 0, el); + move(hook, 180, 700, el); + up(hook, 180, 1100, el); + expect(hook.result.current.settledOffset).toBe(200); + let wobble!: Event; + let pull!: Event; + act(() => { + touch(el, 'touchstart', 300); + wobble = touch(el, 'touchmove', 304); + pull = touch(el, 'touchmove', 250); + }); + expect(wobble.defaultPrevented).toBe(false); + // The sheet takes the pull from the touch-down, and cancels its pan. + expect(pull.defaultPrevented).toBe(true); + expect(hook.result.current.isDragging).toBe(true); + expect(liveOffset(hook)).toBe(150); + }); + + it.each([ + {content: 'content that fits', scrollHeight: 200}, + {content: 'a list at its top', scrollHeight: 800}, + ])( + 'leaves a sideways swipe on $content to the row for the whole touch, also when it drifts down, turns into a pull, or comes back up', + ({scrollHeight}) => { + const {hook} = setup({snapHeights: () => [200]}); + const el = makeScroller({ + scrollTop: 0, + clientHeight: 200, + scrollHeight, + }); + act(() => hook.result.current.sheetRef(el)); + act(() => hook.result.current.bodyProps.ref(el)); + const moves: Event[] = []; + act(() => { + touch(el, 'touchstart', 100, 1, 'direct', undefined, 0); + // A swipe along a row of chips whose finger drifts 40 px down. + moves.push(touch(el, 'touchmove', 103, 1, 'direct', undefined, 20)); + moves.push(touch(el, 'touchmove', 109, 1, 'direct', undefined, 60)); + moves.push(touch(el, 'touchmove', 140, 1, 'direct', undefined, 150)); + // The finger then pulls down, farther than it went sideways. + moves.push(touch(el, 'touchmove', 300, 1, 'direct', undefined, 152)); + // It then comes back up, past where it began. + moves.push(touch(el, 'touchmove', 60, 1, 'direct', undefined, 154)); + }); + expect(moves.map(move => move.defaultPrevented)).toEqual([ + false, + false, + false, + false, + false, + ]); + expect(hook.result.current.isDragging).toBe(false); + }, + ); + + it('takes no pull whose first move past the slop is more sideways than down, also when the pull then turns down', () => { + const {hook} = setup({snapHeights: () => [200]}); + const el = makeScroller({ + scrollTop: 0, + clientHeight: 200, + scrollHeight: 800, + }); + act(() => hook.result.current.sheetRef(el)); + act(() => hook.result.current.bodyProps.ref(el)); + let diagonal!: Event; + let down!: Event; + act(() => { + touch(el, 'touchstart', 100, 1, 'direct', undefined, 0); + diagonal = touch(el, 'touchmove', 120, 1, 'direct', undefined, 30); + down = touch(el, 'touchmove', 250, 1, 'direct', undefined, 34); + }); + expect(diagonal.defaultPrevented).toBe(false); + expect(down.defaultPrevented).toBe(false); + // A drag that began here would move the sheet 150 px at once. + expect(hook.result.current.isDragging).toBe(false); + }); + + it('decides at the drag slop, so a pull on content that fits whose first move is diagonal starts the drag', () => { + const {hook} = setup({snapHeights: () => [200]}); + const el = makeScroller({ + scrollTop: 0, + clientHeight: 200, + scrollHeight: 200, + }); + act(() => hook.result.current.sheetRef(el)); + act(() => hook.result.current.bodyProps.ref(el)); + let first!: Event; + act(() => { + touch(el, 'touchstart', 100, 1, 'direct', undefined, 0); + // 6 px sideways and 5 px down: under the slop, so nothing is decided. + first = touch(el, 'touchmove', 105, 1, 'direct', undefined, 6); + }); + expect(first.defaultPrevented).toBe(false); + expect(hook.result.current.isDragging).toBe(false); + act(() => { + touch(el, 'touchmove', 112, 1, 'direct', undefined, 7); + }); + expect(hook.result.current.isDragging).toBe(true); + // A sideways drift after the drag begins does not end it. + act(() => { + touch(el, 'touchmove', 140, 1, 'direct', undefined, 90); + }); + expect(hook.result.current.isDragging).toBe(true); + expect(liveOffset(hook)).toBe(40); + }); + + it('gives the touch to the content when a list is off its top at the drag slop, and takes no later pull-down', () => { + const {hook} = setup({snapHeights: () => [200]}); + const el = makeScroller({ + scrollTop: 0, + clientHeight: 200, + scrollHeight: 800, + }); + act(() => hook.result.current.sheetRef(el)); + act(() => hook.result.current.bodyProps.ref(el)); + let back!: Event; + act(() => { + touch(el, 'touchstart', 100); + // The list scrolls under the finger before the pull passes the slop. + el.scrollTop = 12; + touch(el, 'touchmove', 110); + el.scrollTop = 0; + back = touch(el, 'touchmove', 150); + }); + expect(back.defaultPrevented).toBe(false); + expect(hook.result.current.isDragging).toBe(false); + }); + it("keeps a stylus pulling up on the body at the tallest detent on a pen's stretch, capped at 48 px", () => { const {hook} = setup({ snapHeights: () => [200], diff --git a/packages/core/src/BottomSheet/useSheetGestures.ts b/packages/core/src/BottomSheet/useSheetGestures.ts index 3930c060725fb..e0d5ec75ca470 100644 --- a/packages/core/src/BottomSheet/useSheetGestures.ts +++ b/packages/core/src/BottomSheet/useSheetGestures.ts @@ -1749,16 +1749,17 @@ export function useSheetGestures({ // A pull against the armed edge can never scroll: the chain is at that // edge. Its native pan is cancelled before the promotion slop, once the // pull passes a tap's wobble, because a later cancel no longer stops a - // pan iOS Safari has committed. Before the sheet's drag begins, a mostly - // sideways move is a horizontal row's scroll, so it is not cancelled. A - // move past the drag's 8 px slop starts the drag in any direction. + // pan iOS Safari has committed. A mostly sideways move is a horizontal + // row's scroll, so the sheet does not cancel it. const opposesTop = armed.top && delta > 0 && chainAtTop(armed.scrollers); const opposesBottom = armed.bottom && delta < 0 && chainAtBottom(armed.scrollers); + const sideways = Math.abs(t.clientX - armed.startX); + const isMostlyVertical = Math.abs(delta) > sideways; if ( (opposesTop || opposesBottom) && Math.abs(delta) >= PAN_CANCEL_SLOP && - Math.abs(delta) > Math.abs(t.clientX - armed.startX) && + isMostlyVertical && event.cancelable ) { event.preventDefault(); @@ -1767,8 +1768,10 @@ export function useSheetGestures({ // no longer scroll that way: at the top, a downward pull (delta > 0) // collapses; at the bottom, an upward pull (delta < 0) expands. The // opposite direction is a real scroll, so disarm and let it through. - const pullDownAtTop = opposesTop && delta > DRAG_PROMOTION_SLOP; - const pullUpAtBottom = opposesBottom && delta < -DRAG_PROMOTION_SLOP; + const pullDownAtTop = + opposesTop && isMostlyVertical && delta > DRAG_PROMOTION_SLOP; + const pullUpAtBottom = + opposesBottom && isMostlyVertical && delta < -DRAG_PROMOTION_SLOP; if (pullDownAtTop || pullUpAtBottom) { if (event.cancelable) { event.preventDefault(); @@ -1783,10 +1786,40 @@ export function useSheetGestures({ pointerMoveRef.current(asPointer(t, scroller, event)); return; } - if ((armed.top && delta < 0) || (armed.bottom && delta > 0)) { - // Scrolling away from the armed edge; hand back to native scroll. The - // touch stays tracked: this is the swipe that may reach the far edge. + // The first move past the drag slop on either axis decides the touch, + // once. A drag that started later would move the sheet by the whole + // travel at once. A mostly sideways move is a row's scroll or swipe, so + // the sheet stops tracking the touch. Any other move that did not start + // the drag is the content's scroll, and both edges go back to it. + const isPastDragSlop = + Math.max(Math.abs(delta), sideways) > DRAG_PROMOTION_SLOP; + if (isPastDragSlop && (armed.top || armed.bottom)) { + if (!isMostlyVertical) { + releaseContentHold(armed); + touchDragRef.current = null; + return; + } + armed.top = false; + armed.bottom = false; + } + // Under the drag slop, an armed edge goes back to the native scroll when + // a move leaves it, on two conditions. The content has room to scroll + // that way. The move has passed a tap's wobble, because a shorter move + // commits no native pan. The touch stays tracked, because this swipe can + // reach the far edge. Content that fits has nothing to scroll, so both + // edges stay armed. + if ( + armed.top && + delta <= -PAN_CANCEL_SLOP && + !chainAtBottom(armed.scrollers) + ) { armed.top = false; + } + if ( + armed.bottom && + delta >= PAN_CANCEL_SLOP && + !chainAtTop(armed.scrollers) + ) { armed.bottom = false; } // Reaching the end of the content mid-gesture. Arming at `touchstart`