Skip to content

Commit 57a240e

Browse files
committed
refactor(motion): one release policy, resolveSnapPoint, on selectSnapPoint
Where a release lands is decided in one place: motion/resolveSnapPoint.ts. A slow release lands on the snap point nearest where it would rest; a flick (flickThreshold or faster) goes at least to the next snap point in its direction; with start, a release moving back toward it cancels to it (spec:AST-094 FR37); with fromIndex and maxSteps it moves at most that many points. selectSnapPoint in projection.ts stays the math primitive, and paging moves to selectPage.ts on top of resolveSnapPoint. Internal: not exported from the package (a test locks that). No behavior change. Split from #8565 so the public drag-to-snap-points API can build on it;
1 parent d07518f commit 57a240e

8 files changed

Lines changed: 461 additions & 72 deletions

File tree

‎.changeset/resolve-snap-point.md‎

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 motion foundation has one release policy, `motion/resolveSnapPoint.ts` (where a release lands: a flick goes at least to the next snap point, a release moving back toward its start cancels to it), on the `selectSnapPoint` primitive; paging moves to `motion/selectPage.ts` on top of it. No behavior change.
6+
7+
@cixzhang

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ so each keeps its measured value; no shipped component reads the roles yet.
2424
| `decay.ts` | UIScrollView's momentum coast. |
2525
| `coast.ts` | A release's momentum curves for `animator.decay`: the coast, the edge spring, a release past an edge. |
2626
| `rubberBand.ts` | UIScrollView's resistance past an edge, and its inverse. |
27-
| `projection.ts` | Release targets: the rest point nearest the projected coast; paging. |
27+
| `projection.ts` | Release targets: the rest point nearest the projected coast. |
28+
| `resolveSnapPoint.ts` | Release policy: the snap point a release lands on (flick, back-out cancel, step limit), on `selectSnapPoint`. Internal. |
29+
| `selectPage.ts` | Paging: at most one page per release, on `resolveSnapPoint`. |
2830
| `bezier.ts` | Cubic-bézier timing functions (Core Animation's named curves). |
2931
| `animator.ts` | Compositor animator: springs and momentum curves compiled to Web Animations, read back analytically. |
3032

‎packages/core/src/motion/projection.test.ts‎

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
* @file projection.test.ts
55
* @input vitest, the projection functions
66
* @output Checks WWDC18's projection verbatim and the release-target rules
7-
* (nearest projected rest, step limits, paging)
7+
* (nearest projected rest, step limits)
88
* @position Testing; validates projection.ts without a browser
99
*/
1010

1111
import {describe, expect, it} from 'vitest';
12-
import {
13-
project,
14-
projectDistance,
15-
selectPage,
16-
selectSnapPoint,
17-
} from './projection';
12+
import {project, projectDistance, selectSnapPoint} from './projection';
1813
import {scroll} from './uikit/scroll';
1914

2015
describe('projectDistance', () => {
@@ -110,30 +105,3 @@ describe('selectSnapPoint', () => {
110105
);
111106
});
112107
});
113-
114-
describe('selectPage', () => {
115-
it('lands on a real page when the current page is past the end', () => {
116-
const result = selectPage({
117-
position: 400,
118-
velocity: 0,
119-
pageSize: 100,
120-
pageCount: 3,
121-
currentPage: 4,
122-
});
123-
expect(result.value).toBeDefined();
124-
expect(result.index).toBeGreaterThanOrEqual(0);
125-
expect(result.index).toBeLessThanOrEqual(2);
126-
});
127-
128-
it('moves at most one page per release', () => {
129-
const page = (position: number, velocity: number, currentPage: number) =>
130-
selectPage({position, velocity, pageSize: 390, pageCount: 6, currentPage})
131-
.index;
132-
expect(page(390, 0, 1)).toBe(1);
133-
expect(page(430, 8000, 1)).toBe(2);
134-
expect(page(350, -8000, 1)).toBe(0);
135-
// A slow drag past half a page settles on the next page.
136-
expect(page(600, 0, 1)).toBe(2);
137-
expect(page(1950, 9000, 5)).toBe(5);
138-
});
139-
});

‎packages/core/src/motion/projection.ts‎

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
* @file projection.ts
55
* @input A release position and velocity, candidate rest points
66
* @output Exports `projectDistance` / `project` (WWDC18's momentum
7-
* projection), `selectSnapPoint` (the release target UIKit-style) and
8-
* `selectPage` (paging: at most one page per release)
7+
* projection) and `selectSnapPoint` (the release target UIKit-style)
98
* @position Pure math; no DOM, no React
109
*
1110
* WWDC18 "Designing Fluid Interfaces" (session 803): a thrown object goes to
@@ -58,7 +57,10 @@ export interface SnapSelection {
5857

5958
/**
6059
* The rest point a release goes to: the one nearest the projected rest
61-
* position. `fromIndex` + `maxSteps` limit how many points away from the
60+
* position. This is the math primitive (projection + nearest); the release
61+
* policy on top of it (a flick advances at least one detent, a release moving
62+
* back toward its start cancels) is `resolveSnapPoint` (resolveSnapPoint.ts), so a
63+
* component's release rule calls that rather than adding its own. `fromIndex` + `maxSteps` limit how many points away from the
6264
* starting one a single release may travel (by index order; pass points
6365
* sorted along the axis). Ties go to the point in the direction of travel.
6466
*
@@ -112,37 +114,3 @@ export function selectSnapPoint(
112114
}
113115
return {index: best, value: points[best], projected};
114116
}
115-
116-
/**
117-
* UIScrollView paging (`isPagingEnabled`): the page a release settles on, at
118-
* most one page from `currentPage`, chosen by projecting the release with
119-
* the paging projection rate (uikit/scroll.ts; a GUESS until measured).
120-
* Pages are `pageSize` apart starting at 0.
121-
*/
122-
export function selectPage({
123-
position,
124-
velocity,
125-
pageSize,
126-
pageCount,
127-
currentPage,
128-
rate = scroll.paging.projectionRate,
129-
}: {
130-
position: number;
131-
velocity: number;
132-
pageSize: number;
133-
pageCount: number;
134-
currentPage: number;
135-
rate?: number;
136-
}): SnapSelection {
137-
const points = Array.from(
138-
{length: Math.max(1, pageCount)},
139-
(_, i) => i * pageSize,
140-
);
141-
return selectSnapPoint(points, {
142-
position,
143-
velocity,
144-
rate,
145-
fromIndex: currentPage,
146-
maxSteps: 1,
147-
});
148-
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import {fileURLToPath} from 'node:url';
6+
import {describe, expect, it} from 'vitest';
7+
import * as core from '../index';
8+
import {resolveSnapPoint} from './resolveSnapPoint';
9+
10+
const here = path.dirname(fileURLToPath(import.meta.url));
11+
12+
const SHEET = {
13+
flickThreshold: 1008,
14+
tolerance: 1,
15+
projection: {seconds: 0.1, minSpeed: 250},
16+
} as const;
17+
18+
describe('resolveSnapPoint', () => {
19+
it('lands a slow release on the snap point nearest where it was let go', () => {
20+
expect(
21+
resolveSnapPoint({
22+
points: [0, 300, 640],
23+
position: 140,
24+
velocity: 0,
25+
flickThreshold: 48,
26+
}),
27+
).toEqual({index: 0, projected: 140});
28+
expect(
29+
resolveSnapPoint({
30+
points: [0, 300, 640],
31+
position: 160,
32+
velocity: 20,
33+
flickThreshold: 48,
34+
}).index,
35+
).toBe(1);
36+
});
37+
38+
it('projects nothing below the projection speed, and judges a faster slow release where it coasts to', () => {
39+
// 240 px/s is below 250: judged at 140, nearer 0.
40+
expect(
41+
resolveSnapPoint({
42+
...SHEET,
43+
points: [0, 300, 640],
44+
position: 140,
45+
velocity: 240,
46+
}),
47+
).toEqual({index: 0, projected: 140});
48+
// 600 px/s projects 0.1 s ahead: 140 + 60 = 200, nearer 300.
49+
const projected = resolveSnapPoint({
50+
...SHEET,
51+
points: [0, 300, 640],
52+
position: 140,
53+
velocity: 600,
54+
});
55+
expect(projected.index).toBe(1);
56+
expect(projected.projected).toBeCloseTo(200, 6);
57+
});
58+
59+
it('takes a flick at least to the next snap point beyond, or to its projection if that is farther', () => {
60+
// At 300 moving 1200 px/s: projected 420 (nearer 300), but a flick goes on.
61+
const flick = resolveSnapPoint({
62+
...SHEET,
63+
points: [0, 300, 640],
64+
position: 300,
65+
velocity: 1200,
66+
});
67+
expect(flick.index).toBe(2);
68+
expect(flick.projected).toBeCloseTo(420, 6);
69+
// Without projection, from just past the first snap point.
70+
expect(
71+
resolveSnapPoint({
72+
points: [0, 300],
73+
position: 2,
74+
velocity: 60,
75+
flickThreshold: 48,
76+
}).index,
77+
).toBe(1);
78+
// Moving down the axis, to the next snap point below.
79+
expect(
80+
resolveSnapPoint({
81+
...SHEET,
82+
points: [0, 300, 640],
83+
position: 620,
84+
velocity: -1100,
85+
}).index,
86+
).toBe(1);
87+
});
88+
89+
it('counts a snap point within the tolerance as reached, so a flick goes on to the one after it', () => {
90+
expect(
91+
resolveSnapPoint({
92+
points: [0, 300, 640],
93+
position: 300.5,
94+
velocity: 2000,
95+
flickThreshold: 1008,
96+
tolerance: 1,
97+
}).index,
98+
).toBe(2);
99+
// Past the last snap point there is nothing beyond: the nearest stays.
100+
expect(
101+
resolveSnapPoint({
102+
points: [0, 300],
103+
position: 310,
104+
velocity: 2000,
105+
flickThreshold: 1008,
106+
tolerance: 1,
107+
}).index,
108+
).toBe(1);
109+
});
110+
111+
it('cancels a release moving back toward where the drag began, whatever its travel or speed', () => {
112+
// Dragged from 0 to 280, released slowly moving back up: back to 0.
113+
expect(
114+
resolveSnapPoint({
115+
...SHEET,
116+
points: [0, 300, 640],
117+
position: 280,
118+
velocity: -10,
119+
start: 0,
120+
}).index,
121+
).toBe(0);
122+
// A flick back toward the start cancels too.
123+
expect(
124+
resolveSnapPoint({
125+
...SHEET,
126+
points: [0, 300, 640],
127+
position: 500,
128+
velocity: -1500,
129+
start: 300,
130+
}).index,
131+
).toBe(1);
132+
// Moving away from the start does not cancel.
133+
expect(
134+
resolveSnapPoint({
135+
...SHEET,
136+
points: [0, 300, 640],
137+
position: 280,
138+
velocity: 10,
139+
start: 0,
140+
}).index,
141+
).toBe(1);
142+
});
143+
144+
it('sends a tie the way a projected release moves, and otherwise to the earlier snap point', () => {
145+
const at = (velocity: number, projection?: typeof SHEET.projection) =>
146+
resolveSnapPoint({
147+
points: [0, 300],
148+
// Released where its projection lands on the midpoint.
149+
position: 150 - velocity * 0.1 * (projection == null ? 0 : 1),
150+
velocity,
151+
flickThreshold: 1008,
152+
projection,
153+
}).index;
154+
expect(at(0)).toBe(0);
155+
expect(at(10)).toBe(0);
156+
expect(at(-10)).toBe(0);
157+
expect(at(400, SHEET.projection)).toBe(1);
158+
expect(at(-400, SHEET.projection)).toBe(0);
159+
});
160+
161+
it('needs at least one snap point', () => {
162+
expect(() =>
163+
resolveSnapPoint({
164+
points: [],
165+
position: 0,
166+
velocity: 0,
167+
flickThreshold: 1,
168+
}),
169+
).toThrow(RangeError);
170+
});
171+
172+
it('stays internal: not exported from the package or any barrel', () => {
173+
expect(core).not.toHaveProperty('resolveSnapPoint');
174+
const pkg = JSON.parse(
175+
fs.readFileSync(path.join(here, '../../package.json'), 'utf8'),
176+
) as {exports: Record<string, unknown>};
177+
expect(
178+
Object.keys(pkg.exports).filter(key => key.includes('motion')),
179+
).toEqual([]);
180+
const barrels = fs
181+
.readdirSync(here)
182+
.filter(name => /^index\.(ts|tsx|js|mjs)$/.test(name));
183+
for (const barrel of barrels) {
184+
expect(fs.readFileSync(path.join(here, barrel), 'utf8')).not.toMatch(
185+
/resolveSnapPoint/,
186+
);
187+
}
188+
});
189+
});

0 commit comments

Comments
 (0)