Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/release-detent-policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[chore] Internal: BottomSheet's release under a finger and the paging helper pick their landing with one shared release policy (motion/resolveSnapPoint.ts), with the same thresholds as before. No behavior change.

@cixzhang
74 changes: 17 additions & 57 deletions packages/core/src/BottomSheet/sheetRelease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* @file sheetRelease.ts
* @input The UIKit constants table (`sheet.release`, the sheet springs) and
* the motion foundation's snap-point projection
* the motion foundation's release policy (resolveSnapPoint)
* @output Exports resolveSheetRelease, which says where a released sheet goes
* and on which spring under a finger; resolveDefaultRelease, today's rule
* everywhere else; and the SheetRelease type
Expand Down Expand Up @@ -42,7 +42,7 @@
* - /packages/core/src/BottomSheet/useSheetGestures.ts
*/

import {selectSnapPoint} from '../motion/projection';
import {resolveSnapPoint} from '../motion/resolveSnapPoint';
import {solveSpring, toSpring, type SpringDescription} from '../motion/spring';
import type {ReleaseInput, ReleaseOutcome} from '../motion/interactive';
import {sheet} from '../motion/uikit/sheet';
Expand All @@ -66,29 +66,6 @@ export interface SheetRelease {
spring: SpringDescription;
}

/** The deceleration rate whose projection reaches `seconds` of the velocity. */
function rateForProjection(seconds: number): number {
// WWDC18's projection coasts (v / 1000) · r / (1 − r): r / (1 − r) = 1000·τ.
const ms = seconds * 1000;
return ms / (1 + ms);
}

const PROJECTION_RATE = rateForProjection(sheet.release.projectionSeconds);

/** The index of the position nearest `offset` (the first on a tie). */
function nearestIndex(
positions: ReadonlyArray<number>,
offset: number,
): number {
let best = 0;
for (let i = 1; i < positions.length; i += 1) {
if (Math.abs(positions[i] - offset) < Math.abs(positions[best] - offset)) {
best = i;
}
}
return best;
}

/**
* Where a sheet released at `position` moving at `velocity` settles, and on
* which spring.
Expand Down Expand Up @@ -133,39 +110,22 @@ export function resolveSheetRelease({
}
const positions =
dismissedOffset == null ? [...detents] : [...detents, dismissedOffset];
// Below the slowest projected speed the release is where it was let go.
const projectedVelocity =
Math.abs(velocity) < sheet.release.minProjectionSpeed ? 0 : velocity;
const choice = selectSnapPoint(positions, {
// The release policy (motion/resolveSnapPoint.ts): nearest where it projects,
// a flick on to the next position beyond, and a release moving back
// toward where the drag began cancels to it (spec:AST-094 FR37).
const {index, projected} = resolveSnapPoint({
points: positions,
position,
velocity: projectedVelocity,
rate: PROJECTION_RATE,
velocity,
flickThreshold: sheet.release.fastSpeed,
tolerance: sheet.release.detentTolerance,
projection: {
seconds: sheet.release.projectionSeconds,
minSpeed: sheet.release.minProjectionSpeed,
},
start,
});
const isFast = Math.abs(velocity) >= sheet.release.fastSpeed;
let index = choice.index;
if (isFast) {
// A flick goes at least to the next position beyond where it is.
const tolerance = sheet.release.detentTolerance;
let next = -1;
if (velocity > 0) {
next = positions.findIndex(p => p > position + tolerance);
} else {
for (let i = positions.length - 1; i >= 0 && next === -1; i -= 1) {
if (positions[i] < position - tolerance) {
next = i;
}
}
}
if (next !== -1 && (velocity > 0 ? next > index : next < index)) {
index = next;
}
}
// spec:AST-094 FR37: a release whose velocity points back toward where
// the drag began cancels, whatever the travel so far, to the stop nearest
// that start.
if (velocity !== 0 && Math.sign(velocity) === Math.sign(start - position)) {
index = nearestIndex(positions, start);
}
if (dismissedOffset != null && index === detents.length) {
// GUESS (spec:AST-094 FR2): a slow dismissal plays `SHEET_DETENT_SPRING`.
// None of the measured releases that dismissed had its spring read;
Expand All @@ -175,15 +135,15 @@ export function resolveSheetRelease({
kind: 'dismiss',
index: -1,
offset: dismissedOffset,
projected: choice.projected,
projected,
spring: isFast ? SHEET_DISMISS_SWIPE_SPRING : SHEET_DETENT_SPRING,
};
}
return {
kind: 'detent',
index,
offset: positions[index],
projected: choice.projected,
projected,
spring: isFast ? SHEET_RELEASE_FAST_SPRING : SHEET_DETENT_SPRING,
};
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/motion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ so each keeps its measured value; no shipped component reads the roles yet.
| `decay.ts` | UIScrollView's momentum coast. |
| `coast.ts` | A release's momentum curves for `animator.decay`: the coast, the edge spring, a release past an edge. |
| `rubberBand.ts` | UIScrollView's resistance past an edge, and its inverse. |
| `projection.ts` | Release targets: the rest point nearest the projected coast; paging. |
| `projection.ts` | Release targets: the rest point nearest the projected coast. |
| `resolveSnapPoint.ts` | Release policy: the detent a release lands on (flick, back-out cancel, step limit), on `selectSnapPoint`. Internal. |
| `selectPage.ts` | Paging: at most one page per release, on `resolveSnapPoint`. |
| `bezier.ts` | Cubic-bézier timing functions (Core Animation's named curves). |
| `animator.ts` | Compositor animator: springs, coasts and any closed-form curve compiled to Web Animations, read back analytically. |
| `presentation.ts` | Where each moving element is drawn relative to where the engine hit-tests it, reported by the animator while translate runs; read by the gesture host at touch-down. |
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/motion/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
* those units per second. A rule converts at its own boundary (a pop's
* progress is `position / width`).
* - Rules are data: pure functions, tested against their UIKit measurements.
* Under a finger, a release rule that picks one of a set of rest
* positions picks it with `resolveSnapPoint` (resolveSnapPoint.ts), the motion
* foundation's release policy, rather than with its own nearest/flick
* logic (the sheet's finger path does). A mouse path may keep its own
* rule (the sheet's settles with `resolveSettleOffset`).
* The track rule turns the finger's travel into where the driver goes and
* the drag's own state (a confirmed full swipe); the release rule turns
* where the gesture let go, and that state, into a target and a motion.
Expand Down
36 changes: 2 additions & 34 deletions packages/core/src/motion/projection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
* @file projection.test.ts
* @input vitest, the projection functions
* @output Checks WWDC18's projection verbatim and the release-target rules
* (nearest projected rest, step limits, paging)
* (nearest projected rest, step limits)
* @position Testing; validates projection.ts without a browser
*/

import {describe, expect, it} from 'vitest';
import {
project,
projectDistance,
selectPage,
selectSnapPoint,
} from './projection';
import {project, projectDistance, selectSnapPoint} from './projection';
import {scroll} from './uikit/scroll';

describe('projectDistance', () => {
Expand Down Expand Up @@ -110,30 +105,3 @@ describe('selectSnapPoint', () => {
);
});
});

describe('selectPage', () => {
it('lands on a real page when the current page is past the end', () => {
const result = selectPage({
position: 400,
velocity: 0,
pageSize: 100,
pageCount: 3,
currentPage: 4,
});
expect(result.value).toBeDefined();
expect(result.index).toBeGreaterThanOrEqual(0);
expect(result.index).toBeLessThanOrEqual(2);
});

it('moves at most one page per release', () => {
const page = (position: number, velocity: number, currentPage: number) =>
selectPage({position, velocity, pageSize: 390, pageCount: 6, currentPage})
.index;
expect(page(390, 0, 1)).toBe(1);
expect(page(430, 8000, 1)).toBe(2);
expect(page(350, -8000, 1)).toBe(0);
// A slow drag past half a page settles on the next page.
expect(page(600, 0, 1)).toBe(2);
expect(page(1950, 9000, 5)).toBe(5);
});
});
42 changes: 5 additions & 37 deletions packages/core/src/motion/projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* @file projection.ts
* @input A release position and velocity, candidate rest points
* @output Exports `projectDistance` / `project` (WWDC18's momentum
* projection), `selectSnapPoint` (the release target UIKit-style) and
* `selectPage` (paging: at most one page per release)
* projection) and `selectSnapPoint` (the release target UIKit-style)
* @position Pure math; no DOM, no React
*
* WWDC18 "Designing Fluid Interfaces" (session 803): a thrown object goes to
Expand Down Expand Up @@ -58,7 +57,10 @@ export interface SnapSelection {

/**
* The rest point a release goes to: the one nearest the projected rest
* position. `fromIndex` + `maxSteps` limit how many points away from the
* position. This is the math primitive (projection + nearest); the release
* policy on top of it (a flick advances at least one detent, a release moving
* back toward its start cancels) is `resolveSnapPoint` (resolveSnapPoint.ts), so a
* component's release rule calls that rather than adding its own. `fromIndex` + `maxSteps` limit how many points away from the
* starting one a single release may travel (by index order; pass points
* sorted along the axis). Ties go to the point in the direction of travel.
*
Expand Down Expand Up @@ -112,37 +114,3 @@ export function selectSnapPoint(
}
return {index: best, value: points[best], projected};
}

/**
* UIScrollView paging (`isPagingEnabled`): the page a release settles on, at
* most one page from `currentPage`, chosen by projecting the release with
* the paging projection rate (uikit/scroll.ts; a GUESS until measured).
* Pages are `pageSize` apart starting at 0.
*/
export function selectPage({
position,
velocity,
pageSize,
pageCount,
currentPage,
rate = scroll.paging.projectionRate,
}: {
position: number;
velocity: number;
pageSize: number;
pageCount: number;
currentPage: number;
rate?: number;
}): SnapSelection {
const points = Array.from(
{length: Math.max(1, pageCount)},
(_, i) => i * pageSize,
);
return selectSnapPoint(points, {
position,
velocity,
rate,
fromIndex: currentPage,
maxSteps: 1,
});
}
Loading