Skip to content

Commit 8480daa

Browse files
committed
fix(core): a fast throw onto a BottomSheet's tallest detent stays within its reserve
Released just below fully open at 2500-3000 px/s, the 80 % damped release spring carried the sheet 50-66 px above its tallest detent, past the 48 px the sheet extends below the screen, so the page showed under it for a moment (astracat-bot on #8109). The hand-off to that spring is now capped so its overshoot stays within the reserve (limitOvershoot).
1 parent b318f75 commit 8480daa

4 files changed

Lines changed: 117 additions & 3 deletions

File tree

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414
import {describe, expect, it} from 'vitest';
1515
import {sheet, springs} from '../motion/uikit';
16-
import {resolveDefaultRelease, resolveSheetRelease} from './sheetRelease';
16+
import {solveSpring, toSpring} from '../motion/spring';
17+
import {
18+
limitOvershoot,
19+
resolveDefaultRelease,
20+
resolveSheetRelease,
21+
} from './sheetRelease';
1722

1823
/**
1924
* Measured on the iOS 26.2 simulator (iPhone 17 Pro, 402 × 874 pt), in
@@ -419,3 +424,35 @@ describe("resolveDefaultRelease (under a mouse: today's rule)", () => {
419424
).toMatchObject({kind: 'detent', offset: 300});
420425
});
421426
});
427+
428+
describe('limitOvershoot', () => {
429+
/** How far the spring goes past `to`, px. */
430+
const overshootOf = (from: number, to: number, velocity: number) => {
431+
const curve = solveSpring(toSpring(springs.sheetReleaseFast), {
432+
from,
433+
to,
434+
velocity,
435+
});
436+
let worst = 0;
437+
for (let t = 0; t <= 1.5; t += 1 / 480) {
438+
worst = Math.max(worst, to - curve.value(t));
439+
}
440+
return worst;
441+
};
442+
443+
it('keeps a hand-off whose overshoot fits', () => {
444+
expect(limitOvershoot(springs.sheetReleaseFast, 5, 0, -1000, 48)).toBe(
445+
-1000,
446+
);
447+
});
448+
449+
it('caps a throw that would overshoot past the reserve', () => {
450+
// Released 5 px below fully open at 3000 px/s: the 80 % damped spring
451+
// would carry the sheet about 60 px above it, past the 48 px reserve.
452+
expect(overshootOf(5, 0, -3000)).toBeGreaterThan(55);
453+
const capped = limitOvershoot(springs.sheetReleaseFast, 5, 0, -3000, 48);
454+
expect(capped).toBeGreaterThan(-3000);
455+
expect(capped).toBeLessThan(-1500);
456+
expect(overshootOf(5, 0, capped)).toBeLessThanOrEqual(48.5);
457+
});
458+
});

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
*/
4040

4141
import {selectSnapPoint} from '../motion/projection';
42-
import type {SpringDescription} from '../motion/spring';
42+
import {solveSpring, toSpring, type SpringDescription} from '../motion/spring';
4343
import {sheet} from '../motion/uikit/sheet';
4444
import {springs} from '../motion/uikit/springs';
4545
import {resolveSettleOffset} from './snapOffsets';
@@ -249,3 +249,43 @@ export function resolveDefaultRelease({
249249
spring: springs.sheetDetent,
250250
};
251251
}
252+
253+
/**
254+
* The release velocity, scaled down if needed so `spring`, released at
255+
* `from` moving at `velocity` toward `to`, overshoots past `to` by at most
256+
* `limit` px. A sheet thrown up onto its tallest detent overshoots above it
257+
* on the 80 % damped spring; the sheet extends `limit` px below the screen,
258+
* so past that the page would show under it.
259+
*/
260+
export function limitOvershoot(
261+
spring: SpringDescription,
262+
from: number,
263+
to: number,
264+
velocity: number,
265+
limit: number,
266+
): number {
267+
const direction = Math.sign(to - from) || -Math.sign(velocity) || 1;
268+
const overshoot = (v: number) => {
269+
const curve = solveSpring(toSpring(spring), {from, to, velocity: v});
270+
const end = Math.min(curve.settleTime(0.1), 3);
271+
let worst = 0;
272+
for (let t = 0; t <= end; t += 1 / 240) {
273+
worst = Math.max(worst, (curve.value(t) - to) * direction);
274+
}
275+
return worst;
276+
};
277+
if (overshoot(velocity) <= limit) {
278+
return velocity;
279+
}
280+
let low = 0;
281+
let high = 1;
282+
for (let i = 0; i < 24; i += 1) {
283+
const mid = (low + high) / 2;
284+
if (overshoot(velocity * mid) <= limit) {
285+
low = mid;
286+
} else {
287+
high = mid;
288+
}
289+
}
290+
return velocity * low;
291+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,6 +2188,28 @@ describe('useSheetGestures: the release on the compositor', () => {
21882188
await expect(exit?.finished).resolves.toBe(true);
21892189
});
21902190

2191+
it('keeps a fast throw onto the tallest detent within the reserve below the screen', () => {
2192+
const {hook} = setup({
2193+
snapHeights: () => [200],
2194+
offscreenBlockEndInset: 48,
2195+
});
2196+
const t = makeTarget();
2197+
const played = recordAnimations(t);
2198+
down(hook, 0, 0, t);
2199+
stream(hook, t, {from: 0, to: 150, t0: 0, t1: 600});
2200+
// Thrown back up at 3000 px/s, released 5 px below fully open.
2201+
stream(hook, t, {from: 150, to: 5, t0: 600, t1: 648});
2202+
act(() =>
2203+
hook.result.current.handleProps.onPointerUp(pointerEvent(5, 650, t)),
2204+
);
2205+
expect(hook.result.current.settledOffset).toBe(0);
2206+
const release = played.at(-1) as Played;
2207+
// The sheet extends 48 px below the screen: it never rises past that.
2208+
for (let ms = 0; ms <= 600; ms += 4) {
2209+
expect(travelAt(release, ms)).toBeGreaterThanOrEqual(-48.5);
2210+
}
2211+
});
2212+
21912213
it('settles a drag dismissal as unfinished when the sheet unmounts mid-exit', async () => {
21922214
const snapHeights = () => [200];
21932215
// A host that closes when asked, as the sheet's own host does.

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ import {
117117
} from './releaseMotion';
118118
import {
119119
DEFAULT_DISMISS_OVERSHOOT_RATIO,
120+
limitOvershoot,
120121
resolveDefaultRelease,
121122
resolveSheetRelease,
122123
} from './sheetRelease';
@@ -998,6 +999,20 @@ export function useSheetGestures({
998999
// travel (design:reduced-motion). A finger started it, so it fades
9991000
// whatever the primary pointer (DR2).
10001001
const touchRelease = dragTouchRef.current;
1002+
// Thrown up onto the tallest detent, the 80 % damped spring overshoots
1003+
// above it; the sheet extends only its reserve below the screen, so
1004+
// the hand-off is capped to keep the page from showing under it.
1005+
const handed = velocity ?? animator.getVelocity('y');
1006+
const v =
1007+
motion.kind === 'uikit' && offset === 0
1008+
? limitOvershoot(
1009+
spring,
1010+
animator.get('y'),
1011+
to,
1012+
handed,
1013+
offscreenBlockEndInsetRef.current,
1014+
)
1015+
: handed;
10011016
const handle =
10021017
motion.kind === 'theme'
10031018
? animator.play(
@@ -1006,7 +1021,7 @@ export function useSheetGestures({
10061021
)
10071022
: animator.animate(
10081023
{y: to},
1009-
{spring, velocity: velocity == null ? undefined : {y: velocity}},
1024+
{spring, velocity: {y: v}},
10101025
{crossfadeAs, touchRelease},
10111026
);
10121027
travelHandleRef.current = handle;

0 commit comments

Comments
 (0)