Skip to content

Commit c9b425f

Browse files
committed
fix(BottomSheet): a swipe that starts slowly on content that fits moves the sheet
On a touch screen, 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. A list that scrolls keeps its rule.
1 parent 7eab4d5 commit c9b425f

3 files changed

Lines changed: 170 additions & 3 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] BottomSheet: on a touch screen, a swipe that starts slowly on a sheet whose content fits now moves the sheet, so a slow swipe down can close it. Before, 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. 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. A list that scrolls keeps its behavior.
6+
7+
@imdreamrunner

‎packages/core/src/BottomSheet/useSheetGestures.test.ts‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,160 @@ describe('useSheetGestures', () => {
16291629
}
16301630
});
16311631

1632+
describe('content that fits', () => {
1633+
// Nothing scrolls, so a touch arms both edges at touch-down.
1634+
function fittingSheet() {
1635+
const {hook} = setup({
1636+
snapHeights: () => [200],
1637+
offscreenBlockEndInset: TOUCH_RESERVE,
1638+
});
1639+
const el = makeScroller({
1640+
scrollTop: 0,
1641+
clientHeight: 200,
1642+
scrollHeight: 200,
1643+
});
1644+
act(() => hook.result.current.sheetRef(el));
1645+
act(() => hook.result.current.bodyProps.ref(el));
1646+
return {hook, el};
1647+
}
1648+
1649+
it.each([
1650+
{first: 1, isFirstCancelled: false},
1651+
{first: 4, isFirstCancelled: false},
1652+
{first: 5, isFirstCancelled: true},
1653+
{first: 8, isFirstCancelled: true},
1654+
])(
1655+
'promotes a pull-down whose first move is $first px, under the drag slop',
1656+
({first, isFirstCancelled}) => {
1657+
const {hook, el} = fittingSheet();
1658+
let start!: Event;
1659+
let pull!: Event;
1660+
act(() => {
1661+
touch(el, 'touchstart', 100);
1662+
start = touch(el, 'touchmove', 100 + first);
1663+
});
1664+
// A tap's wobble keeps its click. Past the wobble, the sheet cancels
1665+
// the pan.
1666+
expect(start.defaultPrevented).toBe(isFirstCancelled);
1667+
expect(hook.result.current.isDragging).toBe(false);
1668+
act(() => {
1669+
pull = touch(el, 'touchmove', 140);
1670+
});
1671+
expect(pull.defaultPrevented).toBe(true);
1672+
expect(hook.result.current.isDragging).toBe(true);
1673+
// The drag starts from the touch-down point, so the sheet loses no
1674+
// travel.
1675+
expect(liveOffset(hook)).toBe(40);
1676+
},
1677+
);
1678+
1679+
it('promotes a pull-down that creeps a pixel at a time through the slop', () => {
1680+
const {hook, el} = fittingSheet();
1681+
const moves: Event[] = [];
1682+
act(() => {
1683+
touch(el, 'touchstart', 100);
1684+
for (let y = 101; y <= 108; y += 1) {
1685+
moves.push(touch(el, 'touchmove', y));
1686+
}
1687+
});
1688+
expect(hook.result.current.isDragging).toBe(false);
1689+
expect(moves.map(move => move.defaultPrevented)).toEqual([
1690+
false,
1691+
false,
1692+
false,
1693+
false,
1694+
true,
1695+
true,
1696+
true,
1697+
true,
1698+
]);
1699+
act(() => {
1700+
touch(el, 'touchmove', 109);
1701+
});
1702+
expect(hook.result.current.isDragging).toBe(true);
1703+
expect(liveOffset(hook)).toBe(9);
1704+
});
1705+
1706+
it('promotes a pull-down after the finger drifts up as it lands', () => {
1707+
const {hook, el} = fittingSheet();
1708+
let drift!: Event;
1709+
let pull!: Event;
1710+
act(() => {
1711+
touch(el, 'touchstart', 100);
1712+
drift = touch(el, 'touchmove', 99);
1713+
});
1714+
expect(drift.defaultPrevented).toBe(false);
1715+
expect(hook.result.current.isDragging).toBe(false);
1716+
act(() => {
1717+
pull = touch(el, 'touchmove', 140);
1718+
});
1719+
expect(pull.defaultPrevented).toBe(true);
1720+
expect(hook.result.current.isDragging).toBe(true);
1721+
expect(liveOffset(hook)).toBe(40);
1722+
});
1723+
1724+
it('claims a pull-up after the finger drifts down as it lands', () => {
1725+
const {hook, el} = fittingSheet();
1726+
let pull!: Event;
1727+
act(() => {
1728+
touch(el, 'touchstart', 300);
1729+
touch(el, 'touchmove', 303);
1730+
pull = touch(el, 'touchmove', 250);
1731+
});
1732+
// The sheet takes the pull from the touch-down, and cancels its pan.
1733+
expect(pull.defaultPrevented).toBe(true);
1734+
expect(hook.result.current.isDragging).toBe(true);
1735+
expect(-liveOffset(hook)).toBeCloseTo(
1736+
rubberBand(50, SHEET_HEIGHT - TOUCH_RESERVE),
1737+
6,
1738+
);
1739+
});
1740+
});
1741+
1742+
it('gives a list at its top to its scroll on an upward first move, and takes no later pull-down', () => {
1743+
const {hook} = setup({snapHeights: () => [200]});
1744+
const el = makeScroller({
1745+
scrollTop: 0,
1746+
clientHeight: 200,
1747+
scrollHeight: 800,
1748+
});
1749+
act(() => hook.result.current.sheetRef(el));
1750+
act(() => hook.result.current.bodyProps.ref(el));
1751+
let back!: Event;
1752+
act(() => {
1753+
touch(el, 'touchstart', 100);
1754+
touch(el, 'touchmove', 97);
1755+
back = touch(el, 'touchmove', 150);
1756+
});
1757+
// The scroll owns this touch, so the drag does not change hands.
1758+
expect(back.defaultPrevented).toBe(false);
1759+
expect(hook.result.current.isDragging).toBe(false);
1760+
});
1761+
1762+
it('gives a list at its end to its scroll on a downward first move, and claims no later pull-up', () => {
1763+
const {hook} = setup({snapHeights: () => [200]});
1764+
const el = makeScroller({
1765+
scrollTop: 600,
1766+
clientHeight: 200,
1767+
scrollHeight: 800,
1768+
});
1769+
act(() => hook.result.current.sheetRef(el));
1770+
act(() => hook.result.current.bodyProps.ref(el));
1771+
// Settle at the lower detent, where the sheet claims a pull up at the
1772+
// end.
1773+
down(hook, 0, 0, el);
1774+
move(hook, 180, 700, el);
1775+
up(hook, 180, 1100, el);
1776+
expect(hook.result.current.settledOffset).toBe(200);
1777+
let back!: Event;
1778+
act(() => {
1779+
touch(el, 'touchstart', 300);
1780+
touch(el, 'touchmove', 303);
1781+
back = touch(el, 'touchmove', 250);
1782+
});
1783+
expect(back.defaultPrevented).toBe(false);
1784+
});
1785+
16321786
it("keeps a stylus pulling up on the body at the tallest detent on a pen's stretch, capped at 48 px", () => {
16331787
const {hook} = setup({
16341788
snapHeights: () => [200],

‎packages/core/src/BottomSheet/useSheetGestures.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,10 +1783,16 @@ export function useSheetGestures({
17831783
pointerMoveRef.current(asPointer(t, scroller, event));
17841784
return;
17851785
}
1786-
if ((armed.top && delta < 0) || (armed.bottom && delta > 0)) {
1787-
// Scrolling away from the armed edge; hand back to native scroll. The
1788-
// touch stays tracked: this is the swipe that may reach the far edge.
1786+
// A move away from an armed edge is a scroll only when the content has
1787+
// room to scroll that way. The native scroll then takes that edge. The
1788+
// touch stays tracked, because this swipe can reach the far edge.
1789+
// Content that fits has nothing to scroll, so both edges stay armed. A
1790+
// pull that starts under the slop, or drifts the other way first, still
1791+
// starts the drag.
1792+
if (armed.top && delta < 0 && !chainAtBottom(armed.scrollers)) {
17891793
armed.top = false;
1794+
}
1795+
if (armed.bottom && delta > 0 && !chainAtTop(armed.scrollers)) {
17901796
armed.bottom = false;
17911797
}
17921798
// Reaching the end of the content mid-gesture. Arming at `touchstart`

0 commit comments

Comments
 (0)