Skip to content

Commit d62a71f

Browse files
committed
chore(core): the sheet's release rule, measured on iOS 26.2
resolveSheetRelease picks where a released BottomSheet goes and on which spring, as UISheetPresentationController does: the stop nearest the release offset plus velocity x 0.114 s, with gone below the screen counting as a stop, on springs.sheetDetent below 1072 pt/s and springs.sheetReleaseFast (or sheetDismissSwipe) from there up. The two sheet-only values live in motion/uikit/sheet.ts with their measurements; the tests replay 47 releases read from UIKit. Pure and internal: no component calls it yet.
1 parent c88087c commit d62a71f

6 files changed

Lines changed: 408 additions & 2 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+
[chore] Internal: the rule a BottomSheet release will follow, measured from UIKit on iOS 26.2, as a pure function. It picks the stop nearest the sheet's offset plus its velocity × 0.114 s, counting gone below the screen as a stop, and picks the 0.8-damped release spring from 1072 pt/s up. Not exported from the package entry point; no component uses it yet.
6+
7+
@vjeux
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file sheetRelease.test.ts
5+
* @input vitest, resolveSheetRelease and the UIKit constants table
6+
* @output Holds the release rule to UIKit's own releases: 47 drags of a
7+
* UISheetPresentationController `[.medium(), .large()]` sheet read from
8+
* UIKit (offset and velocity of the sheet interaction's last drag update
9+
* before the lift, the detent it settled on, the damping of the spring it
10+
* attached); plus the rule's own edges
11+
* @position Testing; pure, no DOM
12+
*/
13+
14+
import {describe, expect, it} from 'vitest';
15+
import {sheet, springs} from '../motion/uikit';
16+
import {resolveSheetRelease} from './sheetRelease';
17+
18+
/**
19+
* Measured on the iOS 26.2 simulator (iPhone 17 Pro, 402 × 874 pt), in
20+
* UIKit's own interaction offsets: the sheet's top at the large detent is at
21+
* 62 pt, at the medium detent 404.32 pt, and gone at 874 pt.
22+
* [case, offset at the lift (pt), velocity (pt/s, + = down), outcome, ζ of
23+
* the release spring (null: a translate release, whose spring was not read)]
24+
*/
25+
const UIKIT_RELEASES: ReadonlyArray<
26+
[string, number, number, 'large' | 'medium' | 'dismissed', number | null]
27+
> = [
28+
['off-300.v0', 120.99, -12.5, 'large', 1],
29+
['off-300.v500', 122.65, -481.4, 'large', 1],
30+
['off-300.v1500', 116.65, -1495.6, 'large', 0.8],
31+
['off-300.v3000', 129.32, -2903.5, 'large', 0.8],
32+
['off-230.v0', 184.32, 0, 'large', 1],
33+
['off-230.v500', 188.65, -472, 'large', 1],
34+
['off-230.v1500', 199.65, -1590, 'large', 0.8],
35+
['off-230.v3000', 225.65, -4037.7, 'large', 0.8],
36+
['off-170.v0', 244.32, 0, 'medium', 1],
37+
['off-170.v500', 250.99, -494.8, 'large', 1],
38+
['off-170.v1500', 282.99, -2323, 'large', 0.8],
39+
['off-170.v3000', 258.65, -3292.1, 'large', 0.8],
40+
['off-120.v0', 294.32, 0, 'medium', 1],
41+
['off-120.v500', 300.65, -466, 'medium', 1],
42+
['off-120.v1500', 296.32, -2209.6, 'large', 0.8],
43+
['off-120.v3000', 356.32, -686.1, 'medium', 1],
44+
['off-60.v0', 354.32, 0, 'medium', 1],
45+
['off-60.v500', 365.65, -493.5, 'medium', 1],
46+
['off-60.v1500', 368.32, -574.9, 'medium', 1],
47+
['off60.v0', 454.32, 0, 'medium', null],
48+
['off60.v500', 446.99, 481.5, 'medium', null],
49+
['off60.v1500', 428.32, 450.6, 'medium', null],
50+
['off120.v0', 514.32, 0, 'medium', null],
51+
['off120.v500', 503.65, 453.3, 'medium', null],
52+
['off120.v1500', 512.32, 1969.9, 'dismissed', null],
53+
['off120.v3000', 476.32, 1139.7, 'dismissed', null],
54+
['off200.v0', 594.32, 0, 'medium', null],
55+
['off200.v500', 583.65, 494.5, 'medium', null],
56+
['off200.v1500', 579.32, 1400, 'dismissed', null],
57+
['off200.v3000', 579.32, 3094.6, 'dismissed', null],
58+
['off300.v0', 694.32, 0, 'dismissed', null],
59+
['off300.v500', 687.65, 478.4, 'dismissed', null],
60+
['off300.v1500', 666.65, 1489.2, 'dismissed', null],
61+
['off300.v3000', 679.32, 3008.2, 'dismissed', null],
62+
['up.end356.v900', 351.65, -926.5, 'medium', 1],
63+
['up.end356.v1000', 352.65, -996.7, 'medium', 1],
64+
['down.end500.v1000', 501.99, 991, 'medium', null],
65+
['down.end500.v1200', 500.65, 1212.4, 'dismissed', null],
66+
['up.long.v400', 277.65, -381.2, 'medium', 1],
67+
['down.end600.v200', 590.32, 210.5, 'medium', null],
68+
['zeta.towardLarge.v720', 160.32, -726.6, 'large', 1],
69+
['zeta.towardLarge.v760', 160.65, -736.9, 'large', 1],
70+
['zeta.towardLarge.v800', 167.65, -746.9, 'large', 1],
71+
['zeta.towardLarge.v840', 161.32, -837.8, 'large', 1],
72+
['zeta.awayFromTarget.v760', 224.32, 801.4, 'medium', 1],
73+
['zeta.awayFromTarget.v840', 224.32, 1147.3, 'medium', 0.8],
74+
['zeta.awayFromTarget.v1000', 208.32, 76, 'large', 1],
75+
];
76+
77+
const LARGE = 62;
78+
const MEDIUM = 404.32;
79+
const GONE = 874;
80+
const MEDIUM_GONE_BOUNDARY = (MEDIUM + GONE) / 2;
81+
82+
/**
83+
* The releases the rule does not reproduce, and why. The first two land on
84+
* opposite sides of the medium / dismissed boundary less than 1 pt from it
85+
* and from each other: one needs a projection time of at least 0.1145 s, the
86+
* other at most 0.1120 s, so no single value separates them (the noise of the
87+
* velocity reading). The third's last drag update read 1140 pt/s for a
88+
* 3000 pt/s injection (its siblings read ~3000): an under-read, not a rule.
89+
*/
90+
const KNOWN_MISSES: Record<string, 'boundary' | 'under-read'> = {
91+
'down.end500.v1200': 'boundary',
92+
'off200.v500': 'boundary',
93+
'off120.v3000': 'under-read',
94+
};
95+
96+
describe('resolveSheetRelease against UIKit', () => {
97+
for (const [name, offset, velocity, outcome, zeta] of UIKIT_RELEASES) {
98+
it(`${name}: ${velocity} pt/s from ${offset} pt → ${outcome}`, () => {
99+
const release = resolveSheetRelease({
100+
detents: [LARGE, MEDIUM],
101+
dismissedOffset: GONE,
102+
position: offset,
103+
velocity,
104+
});
105+
const landed =
106+
release.kind === 'dismiss'
107+
? 'dismissed'
108+
: release.index === 0
109+
? 'large'
110+
: 'medium';
111+
const miss = KNOWN_MISSES[name];
112+
if (miss === 'boundary') {
113+
expect(landed).not.toBe(outcome);
114+
expect(Math.abs(release.projected - MEDIUM_GONE_BOUNDARY)).toBeLessThan(
115+
1,
116+
);
117+
} else if (miss === 'under-read') {
118+
expect(landed).not.toBe(outcome);
119+
} else {
120+
expect(landed).toBe(outcome);
121+
}
122+
if (zeta != null) {
123+
expect(release.spring).toBe(
124+
zeta === 1 ? springs.sheetDetent : springs.sheetReleaseFast,
125+
);
126+
}
127+
});
128+
}
129+
130+
it('reproduces all but the three releases it names', () => {
131+
const misses = UIKIT_RELEASES.filter(([, offset, velocity, outcome]) => {
132+
const release = resolveSheetRelease({
133+
detents: [LARGE, MEDIUM],
134+
dismissedOffset: GONE,
135+
position: offset,
136+
velocity,
137+
});
138+
const landed =
139+
release.kind === 'dismiss'
140+
? 'dismissed'
141+
: release.index === 0
142+
? 'large'
143+
: 'medium';
144+
return landed !== outcome;
145+
}).map(([name]) => name);
146+
expect(misses.sort()).toEqual(Object.keys(KNOWN_MISSES).sort());
147+
});
148+
});
149+
150+
describe('resolveSheetRelease', () => {
151+
it('projects linearly: offset plus the velocity times the projection time', () => {
152+
const release = resolveSheetRelease({
153+
detents: [0, 300],
154+
dismissedOffset: 640,
155+
position: 100,
156+
velocity: 500,
157+
});
158+
expect(release.projected).toBeCloseTo(
159+
100 + 500 * sheet.release.projectionSeconds,
160+
9,
161+
);
162+
});
163+
164+
it('at rest, dismisses only past halfway from the shortest detent to gone', () => {
165+
const at = (position: number) =>
166+
resolveSheetRelease({
167+
detents: [0, 300],
168+
dismissedOffset: 640,
169+
position,
170+
velocity: 0,
171+
}).kind;
172+
expect(at(469)).toBe('detent');
173+
expect(at(471)).toBe('dismiss');
174+
});
175+
176+
it('settles against the motion when the projection says so', () => {
177+
// Just above the shortest detent, moving up slowly: back down to it.
178+
const release = resolveSheetRelease({
179+
detents: [0, 300],
180+
dismissedOffset: 640,
181+
position: 250,
182+
velocity: -200,
183+
});
184+
expect(release).toMatchObject({kind: 'detent', index: 1, offset: 300});
185+
});
186+
187+
it('never dismisses a sheet a drag may not dismiss', () => {
188+
const release = resolveSheetRelease({
189+
detents: [0, 300],
190+
dismissedOffset: null,
191+
position: 600,
192+
velocity: 3000,
193+
});
194+
expect(release).toMatchObject({kind: 'detent', index: 1, offset: 300});
195+
});
196+
197+
it('picks the spring by speed alone, from sheet.release.fastSpeed', () => {
198+
const spring = (velocity: number, position = 150) =>
199+
resolveSheetRelease({
200+
detents: [0, 300],
201+
dismissedOffset: 640,
202+
position,
203+
velocity,
204+
}).spring;
205+
const fast = sheet.release.fastSpeed;
206+
expect(spring(fast - 1)).toBe(springs.sheetDetent);
207+
expect(spring(-(fast - 1))).toBe(springs.sheetDetent);
208+
expect(spring(fast)).toBe(springs.sheetReleaseFast);
209+
expect(spring(-fast)).toBe(springs.sheetReleaseFast);
210+
// Dismissals: the swipe spring when fast, the detent spring when slow.
211+
expect(spring(fast, 600)).toBe(springs.sheetDismissSwipe);
212+
expect(spring(0, 600)).toBe(springs.sheetDetent);
213+
});
214+
215+
it('needs a detent', () => {
216+
expect(() =>
217+
resolveSheetRelease({
218+
detents: [],
219+
dismissedOffset: 640,
220+
position: 0,
221+
velocity: 0,
222+
}),
223+
).toThrow(RangeError);
224+
});
225+
});
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file sheetRelease.ts
5+
* @input The UIKit constants table (`sheet.release`, the sheet springs) and
6+
* the motion foundation's snap-point projection
7+
* @output Exports resolveSheetRelease, which says where a released sheet goes
8+
* and on which spring, and the SheetRelease type
9+
* @position Internal to BottomSheet; pure (no DOM, no React). The rule a
10+
* drag's release follows; no gesture calls it yet.
11+
*
12+
* A released sheet goes where UISheetPresentationController sends it: to the
13+
* position nearest its release offset projected `sheet.release
14+
* .projectionSeconds` ahead at the release velocity, with the dismissed
15+
* position (the sheet fully below the screen) counting as one of the
16+
* positions. A slow release projects onto itself, so it lands on whatever is
17+
* nearest; at rest the sheet dismisses once it is more than halfway from its
18+
* shortest detent to gone. Nothing restricts the direction: a sheet released
19+
* just above a detent while moving up, but slowly, falls back to it.
20+
*
21+
* The sheet gets there on UIKit's release spring, picked by the release speed
22+
* alone (`sheet.release.fastSpeed`), and starting at the finger's velocity.
23+
*
24+
* Offsets are px down from the tallest detent (0 = fully open), velocities
25+
* px/s with positive meaning downward.
26+
*
27+
* SYNC: When modified, update /packages/core/src/BottomSheet/sheetRelease.test.ts
28+
*/
29+
30+
import {selectSnapPoint} from '../motion/projection';
31+
import type {SpringDescription} from '../motion/spring';
32+
import {sheet, springs} from '../motion/uikit';
33+
34+
export interface SheetRelease {
35+
/** A detent to settle on, or a dismissal. */
36+
kind: 'detent' | 'dismiss';
37+
/** Index into the detents passed in; -1 for a dismissal. */
38+
index: number;
39+
/** Where the sheet goes: the detent's offset, or the dismissed offset. */
40+
offset: number;
41+
/** The projected rest offset the choice was made from. */
42+
projected: number;
43+
/** The spring that takes it there, handed the release velocity. */
44+
spring: SpringDescription;
45+
}
46+
47+
/** The deceleration rate whose projection reaches `seconds` of the velocity. */
48+
function rateForProjection(seconds: number): number {
49+
// WWDC18's projection coasts (v / 1000) · r / (1 − r): r / (1 − r) = 1000·τ.
50+
const ms = seconds * 1000;
51+
return ms / (1 + ms);
52+
}
53+
54+
const PROJECTION_RATE = rateForProjection(sheet.release.projectionSeconds);
55+
56+
/**
57+
* Where a sheet released at `position` moving at `velocity` settles, and on
58+
* which spring.
59+
*
60+
* @example
61+
* ```
62+
* // Detents at 0 (open) and 300 px; gone at 640 px. Released at 420 px
63+
* // moving down at 1200 px/s: projected 552 px, nearer gone than 300.
64+
* resolveSheetRelease({
65+
* detents: [0, 300],
66+
* dismissedOffset: 640,
67+
* position: 420,
68+
* velocity: 1200,
69+
* }); // {kind: 'dismiss', offset: 640, spring: springs.sheetDismissSwipe, …}
70+
* ```
71+
*/
72+
export function resolveSheetRelease({
73+
detents,
74+
dismissedOffset,
75+
position,
76+
velocity,
77+
}: {
78+
/** Detent offsets, ascending (the tallest first). */
79+
detents: ReadonlyArray<number>;
80+
/** The offset at which the sheet is fully off screen; null when a drag may not dismiss it. */
81+
dismissedOffset: number | null;
82+
position: number;
83+
velocity: number;
84+
}): SheetRelease {
85+
if (detents.length === 0) {
86+
throw new RangeError('resolveSheetRelease needs at least one detent');
87+
}
88+
const positions =
89+
dismissedOffset == null ? [...detents] : [...detents, dismissedOffset];
90+
const choice = selectSnapPoint(positions, {
91+
position,
92+
velocity,
93+
rate: PROJECTION_RATE,
94+
});
95+
const isFast = Math.abs(velocity) >= sheet.release.fastSpeed;
96+
if (dismissedOffset != null && choice.index === detents.length) {
97+
return {
98+
kind: 'dismiss',
99+
index: -1,
100+
offset: dismissedOffset,
101+
projected: choice.projected,
102+
spring: isFast ? springs.sheetDismissSwipe : springs.sheetDetent,
103+
};
104+
}
105+
return {
106+
kind: 'detent',
107+
index: choice.index,
108+
offset: choice.value,
109+
projected: choice.projected,
110+
spring: isFast ? springs.sheetReleaseFast : springs.sheetDetent,
111+
};
112+
}

‎packages/core/src/motion/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import it by path.
1717
| `uikit/timing.ts` | Non-spring clocks and curves: press highlight, Core Animation's named curves, keyboard, Reduced Motion cross-fade. |
1818
| `uikit/navigation.ts` | UINavigationController: push velocity, parallax, dimming, transition clock, interactive pop finish rule. |
1919
| `uikit/gestures.ts` | Gesture recognizer thresholds, the velocity filter and the haptic stand-ins, as typed entries the gesture system reads. |
20+
| `uikit/sheet.ts` | UISheetPresentationController: the release projection and the speed that picks the release spring. |
2021
| `MEASUREMENTS.md` | The UIKit reference captures each unconfirmed value still needs. |
2122
| `spring.ts` | Closed-form damped spring, every Apple parameterization, settling duration, a spring chasing a moving target. |
2223
| `decay.ts` | UIScrollView's momentum coast. |

‎packages/core/src/motion/uikit/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* @file index.ts
55
* @input The domain files of the UIKit constants table
6-
* @output Re-exports `springs`, `scroll`, `timing`, `navigation` and the
7-
* gesture thresholds (`UIKIT_GESTURES` and the haptic tables)
6+
* @output Re-exports `springs`, `scroll`, `timing`, `navigation`, `sheet`
7+
* and the gesture thresholds (`UIKIT_GESTURES` and the haptic tables)
88
* @position Entry of the UIKit constants table (motion/uikit). Internal to
99
* core: not exported from the package entry point
1010
*
@@ -30,5 +30,6 @@ export {
3030
} from './gestures';
3131
export {navigation} from './navigation';
3232
export {scroll} from './scroll';
33+
export {sheet} from './sheet';
3334
export {springs} from './springs';
3435
export {timing} from './timing';

0 commit comments

Comments
 (0)