Skip to content

Commit b318f75

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 72f266a commit b318f75

5 files changed

Lines changed: 129 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: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ test('a slow drag follows the finger and settles on the nearest detent', async (
270270
const script = slowDragTo(handle, dy);
271271
const lastMove =
272272
toTouchEvents(script).filter(e => e.type === 'touchMove').length - 1;
273-
const tops: number[] = [];
273+
const tracked: {top: number; finger: number}[] = [];
274274
let moves = 0;
275275
let restingTop = NaN;
276276
const started = Date.now();
@@ -279,20 +279,25 @@ test('a slow drag follows the finger and settles on the nearest detent', async (
279279
if (step.type !== 'touchMove') {
280280
return;
281281
}
282-
// Read right away (moves are paced at 60 Hz), and once more after the
283-
// last move has been presented: the finger rests 150 ms before lifting.
284-
tops.push(await sheetTop(sheet));
282+
// A frame after each move, so the page has handled it (Chromium hands
283+
// touch moves over on the next animation frame), and once more after
284+
// the last move: the finger rests 150 ms before lifting.
285+
await frames(page, 1);
286+
tracked.push({
287+
top: await sheetTop(sheet),
288+
finger: step.changed[0].y - handle.y,
289+
});
285290
if (moves++ === lastMove) {
286291
await frames(page, 1);
287292
restingTop = await sheetTop(sheet);
288293
}
289294
},
290295
});
291-
// The sheet tracked the finger: monotonic, and 1:1 once out of the
292-
// starting detent's 40 px magnet.
293-
expect(tops.every((top, i) => i === 0 || top >= tops[i - 1] - 0.5)).toBe(
294-
true,
295-
);
296+
// 1:1 with the finger at every move, no pull toward a detent (within the
297+
// pixel WebKit rounds touch positions to).
298+
for (const {top, finger} of tracked) {
299+
expect(Math.abs(top - tallTop - finger)).toBeLessThanOrEqual(1);
300+
}
296301
expect(restingTop).toBeCloseTo(tallTop + dy, 0);
297302
await waitForRest(sheet);
298303
expect(await sheetTop(sheet)).toBeCloseTo(halfTop, 0);
@@ -302,6 +307,78 @@ test('a slow drag follows the finger and settles on the nearest detent', async (
302307
);
303308
});
304309

310+
test('pulled up past the tallest detent, the sheet holds', async ({page}) => {
311+
const {driver, sheet, tallTop, handle} = await openSheet(page);
312+
const tops: number[] = [];
313+
await driver.play(
314+
drag(handle, {x: handle.x, y: handle.y - 120}, {durationMs: 400, hz: 60}),
315+
{
316+
onEvent: async step => {
317+
if (step.type === 'touchMove') {
318+
tops.push(await sheetTop(sheet));
319+
}
320+
},
321+
},
322+
);
323+
// UIKit's sheet doesn't stretch past its largest detent (iOS 26.2): no
324+
// move lifts it, and the lift leaves nothing to settle.
325+
expect(tops.length).toBeGreaterThan(10);
326+
for (const top of tops) {
327+
expect(top).toBeCloseTo(tallTop, 0);
328+
}
329+
await frames(page, 2);
330+
expect(await sheet.evaluate(el => el.getAnimations().length)).toBe(0);
331+
expect(await sheetTop(sheet)).toBeCloseTo(tallTop, 0);
332+
});
333+
334+
test('on Android too, the drag tracks the finger 1:1 and holds at the tallest detent', async ({
335+
page,
336+
}) => {
337+
// Tracking is universal (F4's Android audit): no shim, Chromium's own idiom.
338+
const {driver, sheet, tallTop, halfTop, handle} = await openSheet(page, {
339+
idiom: 'android',
340+
});
341+
const tracked: {top: number; finger: number}[] = [];
342+
await driver.play(
343+
slowDragTo(handle, Math.round((halfTop - tallTop) * 0.66)),
344+
{
345+
onEvent: async step => {
346+
if (step.type === 'touchMove') {
347+
await frames(page, 1);
348+
tracked.push({
349+
top: await sheetTop(sheet),
350+
finger: step.changed[0].y - handle.y,
351+
});
352+
}
353+
},
354+
},
355+
);
356+
expect(tracked.length).toBeGreaterThan(10);
357+
for (const {top, finger} of tracked) {
358+
expect(Math.abs(top - tallTop - finger)).toBeLessThanOrEqual(1);
359+
}
360+
await waitForRest(sheet);
361+
const restTop = await sheetTop(sheet);
362+
const upHandle = {x: handle.x, y: restTop + 12};
363+
const tops: number[] = [];
364+
// From half, up past the tallest detent: it follows to tall, then holds.
365+
await driver.play(
366+
drag(
367+
upHandle,
368+
{x: upHandle.x, y: tallTop - 108},
369+
{durationMs: 500, hz: 60},
370+
),
371+
{
372+
onEvent: async step => {
373+
if (step.type === 'touchMove') {
374+
tops.push(await sheetTop(sheet));
375+
}
376+
},
377+
},
378+
);
379+
expect(Math.min(...tops)).toBeGreaterThanOrEqual(tallTop - 0.5);
380+
});
381+
305382
test('a downward flick lands on the next detent, and from there dismisses', async ({
306383
page,
307384
}) => {

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -778,18 +778,17 @@ describe('useSheetGestures', () => {
778778
expect(values[values.length - 1]).toBeCloseTo(0.3);
779779
});
780780

781-
it('magnetically settles the live drag onto a nearby detent', () => {
782-
// Detents for a 400px sheet with snaps 300 and 200 -> visible-height snaps
783-
// become offsets [0, 100, 200]. Approaching the middle detent (offset 100)
784-
// from just below (raw ~90) — inside MAGNET_RANGE and BELOW the shortest
785-
// detent so the between-detents magnet applies — should ease toward 100.
781+
it('tracks the finger 1:1 between detents and into the dismiss zone', () => {
782+
// UIKit's sheet follows the finger exactly (interaction offset = detent
783+
// offset + translation, no magnet); the release picks the detent. Offsets
784+
// for a 400px sheet with snaps 300 and 200: [0, 100, 200], gone at 400.
786785
const {hook} = setup({snapHeights: () => [200, 300]});
787786
const t = makeTarget();
788787
down(hook, 0, 0, t);
789-
move(hook, 90, 100, t); // raw offset 90, 10px from the 100 detent
790-
const off = liveOffset(hook);
791-
expect(off).toBeGreaterThan(90); // pulled up toward 100
792-
expect(off).toBeLessThanOrEqual(100);
788+
for (const y of [10, 60, 90, 99, 100, 101, 140, 199, 230, 320]) {
789+
move(hook, y, y * 2, t);
790+
expect(liveOffset(hook)).toBe(y);
791+
}
793792
});
794793

795794
it('translates the surface live during a drag', () => {
@@ -1013,16 +1012,18 @@ describe('useSheetGestures', () => {
10131012
expect(hook.result.current.isDragging).toBe(false);
10141013
});
10151014

1016-
it('rubber-bands an upward drag past the fully-open position', () => {
1015+
it('holds at the fully-open position when dragged further up', () => {
1016+
// UIKit's sheet stops dead at its largest detent (measured on iOS 26.2:
1017+
// no stretch from a grabber or a content touch, or from medium past it).
10171018
const {hook} = setup({snapHeights: () => [200]});
10181019
const t = makeTarget();
10191020
down(hook, 100, 0, t);
1020-
move(hook, 20, 100, t); // drag up 80px past the top of a rested-at-top sheet
1021-
// Overscroll is allowed but damped: a resisted fraction of the raw -80,
1022-
// and negative (above the top), not the raw distance and not clamped to 0.
1023-
const off = liveOffset(hook);
1024-
expect(off).toBeLessThan(0);
1025-
expect(off).toBeGreaterThan(-80); // resisted, so smaller in magnitude
1021+
move(hook, 60, 50, t);
1022+
expect(liveOffset(hook)).toBe(0);
1023+
move(hook, 20, 100, t); // 80px past the top of a rested-at-top sheet
1024+
expect(liveOffset(hook)).toBe(0);
1025+
move(hook, 130, 150, t); // back down: the finger's travel from the press, clamped
1026+
expect(liveOffset(hook)).toBe(30);
10261027
});
10271028

10281029
it('dismisses a single-height sheet on a slow drag past the floor', () => {
@@ -2059,21 +2060,22 @@ describe('useSheetGestures: the release on the compositor', () => {
20592060
const {hook} = setup({snapHeights: () => [200]});
20602061
const t = makeTarget();
20612062
const played = recordAnimations(t);
2062-
down(hook, 200, 0, t);
2063-
// Rest at 0; a fast upward pull past the top settles back at 0 on the
2064-
// fast spring (|v| ≥ sheet.release.fastSpeed).
2063+
down(hook, 0, 0, t);
2064+
// A fast throw down (|v| ≥ sheet.release.fastSpeed) from the top: a
2065+
// flick, to the next detent down (200); 58 px + 1308 px/s × 0.1 s
2066+
// projects to 189, nearest that detent too.
20652067
const speed = sheet.release.fastSpeed + 300;
2066-
stream(hook, t, {from: 200, to: 200 - speed * 0.05, t0: 0, t1: 50});
2068+
const to = speed * 0.044;
2069+
stream(hook, t, {from: 0, to, t0: 0, t1: 44});
20672070
act(() =>
2068-
hook.result.current.handleProps.onPointerUp(
2069-
pointerEvent(200 - speed * 0.05, 51, t),
2070-
),
2071+
hook.result.current.handleProps.onPointerUp(pointerEvent(to, 45, t)),
20712072
);
2073+
expect(hook.result.current.settledOffset).toBe(200);
20722074
const from = travelAt(played.at(-1) as Played, 0);
20732075
const curve = solveSpring(toSpring(springs.sheetReleaseFast), {
20742076
from,
2075-
to: 0,
2076-
velocity: -speed,
2077+
to: 200,
2078+
velocity: speed,
20772079
});
20782080
for (const ms of [16, 50, 100, 200]) {
20792081
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
@@ -155,9 +155,6 @@ function restingDismissOffset(
155155
// its last stretch, not between its last two samples (one slow sample before
156156
// the lift must not cancel a throw), and none after a pause before the lift.
157157
const VELOCITY_WINDOW_MS = 100;
158-
// Within this many px of a detent, the live drag is magnetically eased toward
159-
// it so it "clicks" into place instead of hovering just off the mark.
160-
const MAGNET_RANGE = 40;
161158
// The touch path drives the sheet through the same machinery the pointer path
162159
// uses, by handing it a pointer-shaped object built from a `Touch`. On iOS
163160
// Safari that object is indistinguishable from the real thing: WebKit raises
@@ -171,13 +168,6 @@ function isSyntheticTouch(event: ReactPointerEvent): boolean {
171168
return (event as SyntheticTouchPointer).syntheticTouch === true;
172169
}
173170

174-
// Rubber-band factor for dragging up past fully-open, capped at OVERSCROLL_MAX
175-
// (the sheet reserves that much bottom padding for the lift to reveal).
176-
const OVERSCROLL_RESISTANCE = 0.35;
177-
// SYNC: must match OVERSCROLL_PADDING in BottomSheetPanel.tsx (the reserved
178-
// bottom padding the lift reveals). Kept as a local const rather than a shared
179-
// import so it can be used inside stylex.create there.
180-
const OVERSCROLL_MAX = 48;
181171
// Travel past the point where a scrolling gesture ran out of content before it
182172
// hands the sheet the rest of the pull. Small enough to feel continuous with
183173
// the scroll, large enough that a swipe merely coming to rest on the last pixel
@@ -198,28 +188,6 @@ const CONTENT_END_HANDOFF_SLOP = 4;
198188
// well under what a real pull covers in its first frames.
199189
const DRAG_PROMOTION_SLOP = 8;
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,
@@ -582,7 +550,7 @@ export function useSheetGestures({
582550
setSettlingLayoutOffset(null);
583551
// Released on the detent, so there is no travel left to animate — but
584552
// the layout split may still differ from the one the drag rendered
585-
// with (magnetize() lands a slow drag exactly on a detent). Swapping
553+
// with (a drag that ends exactly on a detent). Swapping
586554
// height for transform is only invisible while transitions are off:
587555
// with them live, the composited transform would animate the whole
588556
// swap while the layout height jumped, throwing the sheet the wrong
@@ -1482,18 +1450,11 @@ export function useSheetGestures({
14821450
const {offsets, peekOffset} = resolveDetents(state.height);
14831451

14841452
const raw = state.baseOffset + delta;
1485-
const maxDetentOffset = offsets[offsets.length - 1];
1486-
let next: number;
1487-
if (raw < 0) {
1488-
// Up past fully-open: damped + capped rubber-band; springs back on release.
1489-
next = Math.max(-OVERSCROLL_MAX, raw * OVERSCROLL_RESISTANCE);
1490-
} else if (raw > maxDetentOffset) {
1491-
// In the dismiss zone: no magnet, so it doesn't fight a drag-to-close.
1492-
next = raw;
1493-
} else {
1494-
// Between detents: magnetically ease toward a nearby one.
1495-
next = magnetize(raw, offsets);
1496-
}
1453+
// 1:1 with the finger between detents and into the dismiss zone, as
1454+
// UIKit's sheet tracks (the release, not the drag, picks the detent).
1455+
// Up past fully open it holds: UIKit's sheet doesn't stretch beyond its
1456+
// largest detent.
1457+
const next = Math.max(0, raw);
14971458
// Dragging above the base restores the full layout height below the
14981459
// viewport; otherwise keep whatever layout the base detent settled with
14991460
// (0 at a peek, so a peek drag stays transform-only).

0 commit comments

Comments
 (0)