Skip to content

Commit 33fd380

Browse files
committed
fix(core): a BottomSheet drag tracks the finger the way an iOS sheet does
1:1 with the finger between detents and into the dismissal travel (the 40 px magnet toward a nearby detent is gone; the release picks the detent), and held at the tallest detent when pulled further up, as UIKit's sheet is on iOS 26.2 (no stretch past the largest detent). The surface still extends 48 px below the viewport, now for the 0.8-damped release's overshoot.
1 parent 6861a79 commit 33fd380

5 files changed

Lines changed: 80 additions & 80 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] A BottomSheet drag follows the finger the way an iOS sheet does. The sheet stays exactly under the finger between detents, instead of being pulled toward a nearby detent in the last 40 px; the release picks where it lands. Pulled up past its tallest detent it holds still rather than stretching, as iOS sheets do.
6+
7+
@vjeux

‎packages/core/src/BottomSheet/BottomSheetPanel.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ const HEIGHT_BUDGETS = {
8080
export type BottomSheetHeight = keyof typeof HEIGHT_BUDGETS;
8181
export type {BottomSheetSnapPoint};
8282

83-
// SYNC: must match OVERSCROLL_MAX in useSheetGestures.ts.
83+
// How far the surface extends below the viewport, so a release that carries
84+
// the sheet past fully open (the 0.8-damped spring overshoots) never shows a
85+
// gap under it.
8486
const OVERSCROLL_PADDING = 48;
8587
const MOBILE_KEYBOARD_BOTTOM_CLEARANCE = 48;
8688
const TRANSITION_BACKSTOP_BUFFER_MS = 50;

‎packages/core/src/BottomSheet/__tests__/BottomSheetDetent.interaction.browser.spec.ts‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ test('a slow drag follows the finger and settles on the nearest detent', async (
254254
const script = slowDragTo(handle, dy);
255255
const lastMove =
256256
toTouchEvents(script).filter(e => e.type === 'touchMove').length - 1;
257-
const tops: number[] = [];
257+
const tracked: {top: number; finger: number}[] = [];
258258
let moves = 0;
259259
let restingTop = NaN;
260260
const started = Date.now();
@@ -263,20 +263,25 @@ test('a slow drag follows the finger and settles on the nearest detent', async (
263263
if (step.type !== 'touchMove') {
264264
return;
265265
}
266-
// Read right away (moves are paced at 60 Hz), and once more after the
267-
// last move has been presented: the finger rests 150 ms before lifting.
268-
tops.push(await sheetTop(sheet));
266+
// A frame after each move, so the page has handled it (Chromium hands
267+
// touch moves over on the next animation frame), and once more after
268+
// the last move: the finger rests 150 ms before lifting.
269+
await frames(page, 1);
270+
tracked.push({
271+
top: await sheetTop(sheet),
272+
finger: step.changed[0].y - handle.y,
273+
});
269274
if (moves++ === lastMove) {
270275
await frames(page, 1);
271276
restingTop = await sheetTop(sheet);
272277
}
273278
},
274279
});
275-
// The sheet tracked the finger: monotonic, and 1:1 once out of the
276-
// starting detent's 40 px magnet.
277-
expect(tops.every((top, i) => i === 0 || top >= tops[i - 1] - 0.5)).toBe(
278-
true,
279-
);
280+
// 1:1 with the finger at every move, no pull toward a detent (within the
281+
// pixel WebKit rounds touch positions to).
282+
for (const {top, finger} of tracked) {
283+
expect(Math.abs(top - tallTop - finger)).toBeLessThanOrEqual(1);
284+
}
280285
expect(restingTop).toBeCloseTo(tallTop + dy, 0);
281286
await waitForRest(sheet);
282287
expect(await sheetTop(sheet)).toBeCloseTo(halfTop, 0);
@@ -286,6 +291,30 @@ test('a slow drag follows the finger and settles on the nearest detent', async (
286291
);
287292
});
288293

294+
test('pulled up past the tallest detent, the sheet holds', async ({page}) => {
295+
const {driver, sheet, tallTop, handle} = await openSheet(page);
296+
const tops: number[] = [];
297+
await driver.play(
298+
drag(handle, {x: handle.x, y: handle.y - 120}, {durationMs: 400, hz: 60}),
299+
{
300+
onEvent: async step => {
301+
if (step.type === 'touchMove') {
302+
tops.push(await sheetTop(sheet));
303+
}
304+
},
305+
},
306+
);
307+
// UIKit's sheet doesn't stretch past its largest detent (iOS 26.2): no
308+
// move lifts it, and the lift leaves nothing to settle.
309+
expect(tops.length).toBeGreaterThan(10);
310+
for (const top of tops) {
311+
expect(top).toBeCloseTo(tallTop, 0);
312+
}
313+
await frames(page, 2);
314+
expect(await sheet.evaluate(el => el.getAnimations().length)).toBe(0);
315+
expect(await sheetTop(sheet)).toBeCloseTo(tallTop, 0);
316+
});
317+
289318
test('a downward flick lands on the next detent, and from there dismisses', async ({
290319
page,
291320
}) => {

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -582,18 +582,17 @@ describe('useSheetGestures', () => {
582582
expect(values[values.length - 1]).toBeCloseTo(0.3);
583583
});
584584

585-
it('magnetically settles the live drag onto a nearby detent', () => {
586-
// Detents for a 400px sheet with snaps 300 and 200 -> visible-height snaps
587-
// become offsets [0, 100, 200]. Approaching the middle detent (offset 100)
588-
// from just below (raw ~90) — inside MAGNET_RANGE and BELOW the shortest
589-
// detent so the between-detents magnet applies — should ease toward 100.
585+
it('tracks the finger 1:1 between detents and into the dismiss zone', () => {
586+
// UIKit's sheet follows the finger exactly (interaction offset = detent
587+
// offset + translation, no magnet); the release picks the detent. Offsets
588+
// for a 400px sheet with snaps 300 and 200: [0, 100, 200], gone at 400.
590589
const {hook} = setup({snapHeights: () => [200, 300]});
591590
const t = makeTarget();
592591
down(hook, 0, 0, t);
593-
move(hook, 90, 100, t); // raw offset 90, 10px from the 100 detent
594-
const off = liveOffset(hook);
595-
expect(off).toBeGreaterThan(90); // pulled up toward 100
596-
expect(off).toBeLessThanOrEqual(100);
592+
for (const y of [10, 60, 90, 99, 100, 101, 140, 199, 230, 320]) {
593+
move(hook, y, y * 2, t);
594+
expect(liveOffset(hook)).toBe(y);
595+
}
597596
});
598597

599598
it('translates the surface live during a drag', () => {
@@ -795,16 +794,18 @@ describe('useSheetGestures', () => {
795794
expect(hook.result.current.isDragging).toBe(false);
796795
});
797796

798-
it('rubber-bands an upward drag past the fully-open position', () => {
797+
it('holds at the fully-open position when dragged further up', () => {
798+
// UIKit's sheet stops dead at its largest detent (measured on iOS 26.2:
799+
// no stretch from a grabber or a content touch, or from medium past it).
799800
const {hook} = setup({snapHeights: () => [200]});
800801
const t = makeTarget();
801802
down(hook, 100, 0, t);
802-
move(hook, 20, 100, t); // drag up 80px past the top of a rested-at-top sheet
803-
// Overscroll is allowed but damped: a resisted fraction of the raw -80,
804-
// and negative (above the top), not the raw distance and not clamped to 0.
805-
const off = liveOffset(hook);
806-
expect(off).toBeLessThan(0);
807-
expect(off).toBeGreaterThan(-80); // resisted, so smaller in magnitude
803+
move(hook, 60, 50, t);
804+
expect(liveOffset(hook)).toBe(0);
805+
move(hook, 20, 100, t); // 80px past the top of a rested-at-top sheet
806+
expect(liveOffset(hook)).toBe(0);
807+
move(hook, 130, 150, t); // back down: the finger's travel from the press, clamped
808+
expect(liveOffset(hook)).toBe(30);
808809
});
809810

810811
it('dismisses a single-height sheet on a slow drag past the floor', () => {
@@ -1837,21 +1838,21 @@ describe('useSheetGestures: the release on the compositor', () => {
18371838
const {hook} = setup({snapHeights: () => [200]});
18381839
const t = makeTarget();
18391840
const played = recordAnimations(t);
1840-
down(hook, 200, 0, t);
1841-
// Rest at 0; a fast upward pull past the top settles back at 0 on the
1842-
// fast spring (|v| ≥ sheet.release.fastSpeed).
1841+
down(hook, 0, 0, t);
1842+
// A fast throw down (|v| ≥ sheet.release.fastSpeed) from the top: 60px
1843+
// + 1372 px/s × 0.114 s projects to 216, nearest the 200 detent.
18431844
const speed = sheet.release.fastSpeed + 300;
1844-
stream(hook, t, {from: 200, to: 200 - speed * 0.05, t0: 0, t1: 50});
1845+
const to = speed * 0.044;
1846+
stream(hook, t, {from: 0, to, t0: 0, t1: 44});
18451847
act(() =>
1846-
hook.result.current.handleProps.onPointerUp(
1847-
pointerEvent(200 - speed * 0.05, 51, t),
1848-
),
1848+
hook.result.current.handleProps.onPointerUp(pointerEvent(to, 45, t)),
18491849
);
1850+
expect(hook.result.current.settledOffset).toBe(200);
18501851
const from = travelAt(played.at(-1) as Played, 0);
18511852
const curve = solveSpring(toSpring(springs.sheetReleaseFast), {
18521853
from,
1853-
to: 0,
1854-
velocity: -speed,
1854+
to: 200,
1855+
velocity: speed,
18551856
});
18561857
for (const ms of [16, 50, 100, 200]) {
18571858
expect(travelAt(played.at(-1) as Played, ms)).toBeCloseTo(

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

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ function restingDismissOffset(
139139
): number {
140140
return maxOffset + shortestDetentHeight / 2;
141141
}
142-
// Within this many px of a detent, the live drag is magnetically eased toward
143-
// it so it "clicks" into place instead of hovering just off the mark.
144-
const MAGNET_RANGE = 40;
145142
// The touch path drives the sheet through the same machinery the pointer path
146143
// uses, by handing it a pointer-shaped object built from a `Touch`. On iOS
147144
// Safari that object is indistinguishable from the real thing: WebKit raises
@@ -155,13 +152,6 @@ function isSyntheticTouch(event: ReactPointerEvent): boolean {
155152
return (event as SyntheticTouchPointer).syntheticTouch === true;
156153
}
157154

158-
// Rubber-band factor for dragging up past fully-open, capped at OVERSCROLL_MAX
159-
// (the sheet reserves that much bottom padding for the lift to reveal).
160-
const OVERSCROLL_RESISTANCE = 0.35;
161-
// SYNC: must match OVERSCROLL_PADDING in BottomSheetPanel.tsx (the reserved
162-
// bottom padding the lift reveals). Kept as a local const rather than a shared
163-
// import so it can be used inside stylex.create there.
164-
const OVERSCROLL_MAX = 48;
165155
// Travel past the point where a scrolling gesture ran out of content before it
166156
// hands the sheet the rest of the pull. Small enough to feel continuous with
167157
// the scroll, large enough that a swipe merely coming to rest on the last pixel
@@ -198,28 +188,6 @@ function hapticTick(): void {
198188
navigator.vibrate(8);
199189
}
200190

201-
// Pull a value toward the nearest of `targets` when within MAGNET_RANGE, easing
202-
// the last stretch so the surface settles crisply onto a detent while dragging.
203-
function magnetize(value: number, targets: number[]): number {
204-
let nearestTarget = targets[0];
205-
let nearestDist = Math.abs(value - nearestTarget);
206-
for (const t of targets) {
207-
const d = Math.abs(value - t);
208-
if (d < nearestDist) {
209-
nearestDist = d;
210-
nearestTarget = t;
211-
}
212-
}
213-
if (nearestDist >= MAGNET_RANGE) {
214-
return value;
215-
}
216-
// Ease-in over the range: pull grows as you approach (t^2), so the click
217-
// feels magnetic near the detent but doesn't fight a deliberate drag-through.
218-
const t = nearestDist / MAGNET_RANGE;
219-
const pull = 1 - t * t;
220-
return value + (nearestTarget - value) * pull;
221-
}
222-
223191
function preservationInsetForOffset(
224192
baseOffset: number,
225193
targetOffset: number,
@@ -589,7 +557,7 @@ export function useSheetGestures({
589557
setSettlingLayoutOffset(null);
590558
// Released on the detent, so there is no travel left to animate — but
591559
// the layout split may still differ from the one the drag rendered
592-
// with (magnetize() lands a slow drag exactly on a detent). Swapping
560+
// with (a drag that ends exactly on a detent). Swapping
593561
// height for transform is only invisible while transitions are off:
594562
// with them live, the composited transform would animate the whole
595563
// swap while the layout height jumped, throwing the sheet the wrong
@@ -1336,18 +1304,11 @@ export function useSheetGestures({
13361304
const {offsets, peekOffset} = resolveDetents(state.height);
13371305

13381306
const raw = state.baseOffset + delta;
1339-
const maxDetentOffset = offsets[offsets.length - 1];
1340-
let next: number;
1341-
if (raw < 0) {
1342-
// Up past fully-open: damped + capped rubber-band; springs back on release.
1343-
next = Math.max(-OVERSCROLL_MAX, raw * OVERSCROLL_RESISTANCE);
1344-
} else if (raw > maxDetentOffset) {
1345-
// In the dismiss zone: no magnet, so it doesn't fight a drag-to-close.
1346-
next = raw;
1347-
} else {
1348-
// Between detents: magnetically ease toward a nearby one.
1349-
next = magnetize(raw, offsets);
1350-
}
1307+
// 1:1 with the finger between detents and into the dismiss zone, as
1308+
// UIKit's sheet tracks (the release, not the drag, picks the detent).
1309+
// Up past fully open it holds: UIKit's sheet doesn't stretch beyond its
1310+
// largest detent.
1311+
const next = Math.max(0, raw);
13511312
// Dragging above the base restores the full layout height below the
13521313
// viewport; otherwise keep whatever layout the base detent settled with
13531314
// (0 at a peek, so a peek drag stays transform-only).

0 commit comments

Comments
 (0)