From 2cc204b8548937cb05ea035ba459a14c889a3710 Mon Sep 17 00:00:00 2001 From: Cindy Zhang Date: Mon, 26 Oct 2026 10:41:29 -0700 Subject: [PATCH 1/3] refactor(motion): one release policy picks the detent a release lands on resolveDetent (motion/resolveDetent.ts, internal) is the motion foundation's release policy on selectSnapPoint: a slow release lands on the detent nearest where it was let go or projects to, a flick at least on the next detent in its direction, a release moving back toward its start cancels to it, and an optional step limit keeps it within N detents of where it started. - BottomSheet's resolveSheetRelease calls it with the sheet's UIKit thresholds instead of its own projection, flick and cancel code (moved from here unchanged; its release tests are the oracle). - selectPage moves to motion/selectPage.ts and calls it (one page per release, the paging rate as seconds of projection, no flick). - The interactive driver's header points release rules at it. Not exported from the package or any barrel; a test locks that. Co-authored-by: Christopher Chedeau --- .changeset/release-detent-policy.md | 7 + packages/core/src/BottomSheet/sheetRelease.ts | 74 ++----- packages/core/src/motion/README.md | 4 +- packages/core/src/motion/interactive.ts | 3 + packages/core/src/motion/projection.test.ts | 36 +--- packages/core/src/motion/projection.ts | 42 +--- .../core/src/motion/resolveDetent.test.ts | 184 ++++++++++++++++++ packages/core/src/motion/resolveDetent.ts | 172 ++++++++++++++++ packages/core/src/motion/selectPage.test.ts | 31 +++ packages/core/src/motion/selectPage.ts | 55 ++++++ 10 files changed, 479 insertions(+), 129 deletions(-) create mode 100644 .changeset/release-detent-policy.md create mode 100644 packages/core/src/motion/resolveDetent.test.ts create mode 100644 packages/core/src/motion/resolveDetent.ts create mode 100644 packages/core/src/motion/selectPage.test.ts create mode 100644 packages/core/src/motion/selectPage.ts diff --git a/.changeset/release-detent-policy.md b/.changeset/release-detent-policy.md new file mode 100644 index 0000000000000..ed576dfb23880 --- /dev/null +++ b/.changeset/release-detent-policy.md @@ -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/resolveDetent.ts), with the same thresholds as before. No behavior change. + +@cixzhang diff --git a/packages/core/src/BottomSheet/sheetRelease.ts b/packages/core/src/BottomSheet/sheetRelease.ts index 036673a8c8f40..524001c7839be 100644 --- a/packages/core/src/BottomSheet/sheetRelease.ts +++ b/packages/core/src/BottomSheet/sheetRelease.ts @@ -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 (resolveDetent) * @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 @@ -42,7 +42,7 @@ * - /packages/core/src/BottomSheet/useSheetGestures.ts */ -import {selectSnapPoint} from '../motion/projection'; +import {resolveDetent} from '../motion/resolveDetent'; import {solveSpring, toSpring, type SpringDescription} from '../motion/spring'; import type {ReleaseInput, ReleaseOutcome} from '../motion/interactive'; import {sheet} from '../motion/uikit/sheet'; @@ -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, - 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. @@ -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/resolveDetent.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} = resolveDetent({ + detents: positions, position, - velocity: projectedVelocity, - rate: PROJECTION_RATE, + velocity, + flickSpeed: 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; @@ -175,7 +135,7 @@ export function resolveSheetRelease({ kind: 'dismiss', index: -1, offset: dismissedOffset, - projected: choice.projected, + projected, spring: isFast ? SHEET_DISMISS_SWIPE_SPRING : SHEET_DETENT_SPRING, }; } @@ -183,7 +143,7 @@ export function resolveSheetRelease({ kind: 'detent', index, offset: positions[index], - projected: choice.projected, + projected, spring: isFast ? SHEET_RELEASE_FAST_SPRING : SHEET_DETENT_SPRING, }; } diff --git a/packages/core/src/motion/README.md b/packages/core/src/motion/README.md index faab042308eef..104d75ea31f67 100644 --- a/packages/core/src/motion/README.md +++ b/packages/core/src/motion/README.md @@ -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. | +| `resolveDetent.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 `resolveDetent`. | | `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. | diff --git a/packages/core/src/motion/interactive.ts b/packages/core/src/motion/interactive.ts index 849bcf1c2084b..d66fea2e5f663 100644 --- a/packages/core/src/motion/interactive.ts +++ b/packages/core/src/motion/interactive.ts @@ -29,6 +29,9 @@ * 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. + * A release rule that picks one of a set of rest positions picks it with + * `resolveDetent` (resolveDetent.ts), the motion foundation's release + * policy, rather than with its own nearest/flick logic. * 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. diff --git a/packages/core/src/motion/projection.test.ts b/packages/core/src/motion/projection.test.ts index 21bdae9f96c31..c15cc8106f017 100644 --- a/packages/core/src/motion/projection.test.ts +++ b/packages/core/src/motion/projection.test.ts @@ -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', () => { @@ -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); - }); -}); diff --git a/packages/core/src/motion/projection.ts b/packages/core/src/motion/projection.ts index 8f0d0c8b920c0..72510da2df5e3 100644 --- a/packages/core/src/motion/projection.ts +++ b/packages/core/src/motion/projection.ts @@ -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 @@ -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 `resolveDetent` (resolveDetent.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. * @@ -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, - }); -} diff --git a/packages/core/src/motion/resolveDetent.test.ts b/packages/core/src/motion/resolveDetent.test.ts new file mode 100644 index 0000000000000..fad314752cad5 --- /dev/null +++ b/packages/core/src/motion/resolveDetent.test.ts @@ -0,0 +1,184 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +import fs from 'node:fs'; +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import {describe, expect, it} from 'vitest'; +import * as core from '../index'; +import {resolveDetent} from './resolveDetent'; + +const here = path.dirname(fileURLToPath(import.meta.url)); + +const SHEET = { + flickSpeed: 1008, + tolerance: 1, + projection: {seconds: 0.1, minSpeed: 250}, +} as const; + +describe('resolveDetent', () => { + it('lands a slow release on the detent nearest where it was let go', () => { + expect( + resolveDetent({ + detents: [0, 300, 640], + position: 140, + velocity: 0, + flickSpeed: 48, + }), + ).toEqual({index: 0, projected: 140}); + expect( + resolveDetent({ + detents: [0, 300, 640], + position: 160, + velocity: 20, + flickSpeed: 48, + }).index, + ).toBe(1); + }); + + it('projects nothing below the projection speed, and judges a faster slow release where it coasts to', () => { + // 240 px/s is below 250: judged at 140, nearer 0. + expect( + resolveDetent({ + ...SHEET, + detents: [0, 300, 640], + position: 140, + velocity: 240, + }), + ).toEqual({index: 0, projected: 140}); + // 600 px/s projects 0.1 s ahead: 140 + 60 = 200, nearer 300. + const projected = resolveDetent({ + ...SHEET, + detents: [0, 300, 640], + position: 140, + velocity: 600, + }); + expect(projected.index).toBe(1); + expect(projected.projected).toBeCloseTo(200, 6); + }); + + it('takes a flick at least to the next detent beyond, or to its projection if that is farther', () => { + // At 300 moving 1200 px/s: projected 420 (nearer 300), but a flick goes on. + const flick = resolveDetent({ + ...SHEET, + detents: [0, 300, 640], + position: 300, + velocity: 1200, + }); + expect(flick.index).toBe(2); + expect(flick.projected).toBeCloseTo(420, 6); + // Without projection, from just past the first detent. + expect( + resolveDetent({ + detents: [0, 300], + position: 2, + velocity: 60, + flickSpeed: 48, + }).index, + ).toBe(1); + // Moving down the axis, to the next detent below. + expect( + resolveDetent({ + ...SHEET, + detents: [0, 300, 640], + position: 620, + velocity: -1100, + }).index, + ).toBe(1); + }); + + it('counts a detent within the tolerance as reached, so a flick goes on to the one after it', () => { + expect( + resolveDetent({ + detents: [0, 300, 640], + position: 300.5, + velocity: 2000, + flickSpeed: 1008, + tolerance: 1, + }).index, + ).toBe(2); + // Past the last detent there is nothing beyond: the nearest stays. + expect( + resolveDetent({ + detents: [0, 300], + position: 310, + velocity: 2000, + flickSpeed: 1008, + tolerance: 1, + }).index, + ).toBe(1); + }); + + it('cancels a release moving back toward where the drag began, whatever its travel or speed', () => { + // Dragged from 0 to 280, released slowly moving back up: back to 0. + expect( + resolveDetent({ + ...SHEET, + detents: [0, 300, 640], + position: 280, + velocity: -10, + start: 0, + }).index, + ).toBe(0); + // A flick back toward the start cancels too. + expect( + resolveDetent({ + ...SHEET, + detents: [0, 300, 640], + position: 500, + velocity: -1500, + start: 300, + }).index, + ).toBe(1); + // Moving away from the start does not cancel. + expect( + resolveDetent({ + ...SHEET, + detents: [0, 300, 640], + position: 280, + velocity: 10, + start: 0, + }).index, + ).toBe(1); + }); + + it('sends a tie the way a projected release moves, and otherwise to the earlier detent', () => { + const at = (velocity: number, projection?: typeof SHEET.projection) => + resolveDetent({ + detents: [0, 300], + // Released where its projection lands on the midpoint. + position: 150 - velocity * 0.1 * (projection == null ? 0 : 1), + velocity, + flickSpeed: 1008, + projection, + }).index; + expect(at(0)).toBe(0); + expect(at(10)).toBe(0); + expect(at(-10)).toBe(0); + expect(at(400, SHEET.projection)).toBe(1); + expect(at(-400, SHEET.projection)).toBe(0); + }); + + it('needs at least one detent', () => { + expect(() => + resolveDetent({detents: [], position: 0, velocity: 0, flickSpeed: 1}), + ).toThrow(RangeError); + }); + + it('stays internal: not exported from the package or any barrel', () => { + expect(core).not.toHaveProperty('resolveDetent'); + const pkg = JSON.parse( + fs.readFileSync(path.join(here, '../../package.json'), 'utf8'), + ) as {exports: Record}; + expect( + Object.keys(pkg.exports).filter(key => key.includes('motion')), + ).toEqual([]); + const barrels = fs + .readdirSync(here) + .filter(name => /^index\.(ts|tsx|js|mjs)$/.test(name)); + for (const barrel of barrels) { + expect(fs.readFileSync(path.join(here, barrel), 'utf8')).not.toMatch( + /resolveDetent/, + ); + } + }); +}); diff --git a/packages/core/src/motion/resolveDetent.ts b/packages/core/src/motion/resolveDetent.ts new file mode 100644 index 0000000000000..b7445b2569d79 --- /dev/null +++ b/packages/core/src/motion/resolveDetent.ts @@ -0,0 +1,172 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +/** + * @file resolveDetent.ts + * @input A release (position and velocity on one axis), the detents it can + * land on, and the release policy's thresholds + * @output Exports resolveDetent: the index of the detent a release lands on, + * and the projected rest position it was judged from + * @position The release policy of the motion foundation, on + * `selectSnapPoint` (projection.ts), which stays the math primitive + * (projection + nearest). Pure: no DOM, no React. Internal to core: not + * exported from the package or any barrel (resolveDetent.test.ts locks + * that). A component's release rule calls it with its own detents and + * thresholds; this is the one "where does a release land" helper, so a + * second one should not be added beside it. + * + * - A slow release lands on the detent nearest where it would come to rest: + * its position, or, with `projection`, its position plus the distance it + * would coast at its velocity (projection.ts). Below + * `projection.minSpeed` nothing is projected, so it lands on the detent + * nearest where it was let go. A tie goes the way a projected release + * moves, and otherwise to the earlier detent (`selectSnapPoint`). + * - A flick (`flickSpeed` or faster) lands at least on the next detent + * beyond its position in the direction it moves, or on the nearest + * detent to its projection if that is farther. A detent within + * `tolerance` of the position counts as reached, so the flick goes on to + * the one after it. + * - With `start` (where the drag began), a release moving back toward it + * cancels to the detent nearest `start`, whatever its travel so far or its + * speed. + * - With `fromIndex` and `maxSteps`, a release lands at most `maxSteps` + * detents away from the one at `fromIndex` (a pager's one page per + * release). + * + * Detents are ascending positions on the axis; velocities are in the same + * units per second, positive toward larger positions. + * + * SYNC: When modified, update resolveDetent.test.ts. + */ + +import {selectSnapPoint} from './projection'; + +/** Where a release lands. */ +export interface DetentRelease { + /** Index into the detents passed in. */ + readonly index: number; + /** The rest position the choice was judged from (the position without a projection). */ + readonly projected: number; +} + +/** A release's projection: how far ahead it is judged, and from what speed. */ +export interface DetentProjection { + /** Seconds of the release velocity the projection reaches. */ + readonly seconds: number; + /** Below this speed (units/s), nothing is projected (default 0). */ + readonly minSpeed?: number; +} + +/** 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); +} + +/** The index of the detent nearest `position` (the first on a tie). */ +function nearestIndex( + detents: ReadonlyArray, + position: number, +): number { + let best = 0; + for (let i = 1; i < detents.length; i += 1) { + if (Math.abs(detents[i] - position) < Math.abs(detents[best] - position)) { + best = i; + } + } + return best; +} + +/** + * The detent a release lands on. + * + * @example + * ``` + * // Detents at 0, 300 and 640 px. Released at 300 px moving toward 640 at + * // 1200 px/s, a flick: on to 640, although its projection (420 px) is + * // nearer 300. + * resolveDetent({ + * detents: [0, 300, 640], + * position: 300, + * velocity: 1200, + * flickSpeed: 1008, + * tolerance: 1, + * projection: {seconds: 0.1, minSpeed: 250}, + * start: 0, + * }); // {index: 2, projected: 420} + * ``` + */ +export function resolveDetent({ + detents, + position, + velocity, + flickSpeed, + tolerance = 0, + projection, + start, + fromIndex, + maxSteps, +}: { + /** Detent positions, ascending. */ + detents: ReadonlyArray; + position: number; + velocity: number; + /** From this speed (units/s) on, a release is a flick. */ + flickSpeed: number; + /** A detent this close to the position counts as reached by a flick. */ + tolerance?: number; + /** Judge a slow release where it would coast to; absent, where it was let go. */ + projection?: DetentProjection; + /** Where the drag began: a release moving back toward it cancels to it. */ + start?: number; + /** The detent the release starts from, for `maxSteps`. */ + fromIndex?: number; + /** At most this many detents away from `fromIndex`. */ + maxSteps?: number; +}): DetentRelease { + if (detents.length === 0) { + throw new RangeError('resolveDetent needs at least one detent'); + } + const projectedVelocity = + projection == null || Math.abs(velocity) < (projection.minSpeed ?? 0) + ? 0 + : velocity; + const choice = selectSnapPoint(detents, { + position, + velocity: projectedVelocity, + rate: + projection == null ? undefined : rateForProjection(projection.seconds), + fromIndex, + maxSteps, + }); + let index = choice.index; + if (Math.abs(velocity) >= flickSpeed) { + // A flick goes at least to the next detent beyond where it is. + let next = -1; + if (velocity > 0) { + next = detents.findIndex(d => d > position + tolerance); + } else { + for (let i = detents.length - 1; i >= 0 && next === -1; i -= 1) { + if (detents[i] < position - tolerance) { + next = i; + } + } + } + if (next !== -1 && (velocity > 0 ? next > index : next < index)) { + index = next; + } + } + if ( + start != null && + velocity !== 0 && + Math.sign(velocity) === Math.sign(start - position) + ) { + index = nearestIndex(detents, start); + } + if (fromIndex != null && maxSteps != null) { + // A stale index past either end counts from the nearest real detent. + const from = Math.min(Math.max(fromIndex, 0), detents.length - 1); + index = Math.min(Math.max(index, from - maxSteps), from + maxSteps); + } + return {index, projected: choice.projected}; +} diff --git a/packages/core/src/motion/selectPage.test.ts b/packages/core/src/motion/selectPage.test.ts new file mode 100644 index 0000000000000..39d8785d2d453 --- /dev/null +++ b/packages/core/src/motion/selectPage.test.ts @@ -0,0 +1,31 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +import {describe, expect, it} from 'vitest'; +import {selectPage} from './selectPage'; + +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); + }); +}); diff --git a/packages/core/src/motion/selectPage.ts b/packages/core/src/motion/selectPage.ts new file mode 100644 index 0000000000000..c0813ad2be7fd --- /dev/null +++ b/packages/core/src/motion/selectPage.ts @@ -0,0 +1,55 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +/** + * @file selectPage.ts + * @input A release (position, velocity), the page size and count, the + * current page + * @output Exports `selectPage`: the page a paging release settles on + * @position UIScrollView paging (`isPagingEnabled`) on the release policy + * (resolveDetent.ts). Pure: no DOM, no React + * + * SYNC: When modified, update selectPage.test.ts. + */ + +import type {SnapSelection} from './projection'; +import {resolveDetent} from './resolveDetent'; +import {scroll} from './uikit/scroll'; + +/** + * 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, + ); + // The release policy (resolveDetent.ts), with the paging rate as the + // seconds of velocity it projects, no flick and one page per release. + const {index, projected} = resolveDetent({ + detents: points, + position, + velocity, + flickSpeed: Infinity, + projection: {seconds: rate / (1 - rate) / 1000}, + fromIndex: currentPage, + maxSteps: 1, + }); + return {index, value: points[index], projected}; +} From a8f1133930b8ec4f4dafd9bb5bf07f35e770f747 Mon Sep 17 00:00:00 2001 From: Cindy Zhang Date: Mon, 26 Oct 2026 12:04:33 -0700 Subject: [PATCH 2/3] docs(motion): the driver's resolveDetent pointer covers the finger path The interactive driver's header said every release rule picks its rest position with resolveDetent, but the sheet's mouse path still settles with resolveSettleOffset. It now says a finger path's release rule does, and a mouse path may keep its own rule. --- packages/core/src/motion/interactive.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/core/src/motion/interactive.ts b/packages/core/src/motion/interactive.ts index d66fea2e5f663..623a18b4da108 100644 --- a/packages/core/src/motion/interactive.ts +++ b/packages/core/src/motion/interactive.ts @@ -29,9 +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. - * A release rule that picks one of a set of rest positions picks it with - * `resolveDetent` (resolveDetent.ts), the motion foundation's release - * policy, rather than with its own nearest/flick logic. + * Under a finger, a release rule that picks one of a set of rest + * positions picks it with `resolveDetent` (resolveDetent.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. From c17b31146d784fedcb2ae8e8af4bea252761dd50 Mon Sep 17 00:00:00 2001 From: Cindy Zhang Date: Mon, 26 Oct 2026 12:28:57 -0700 Subject: [PATCH 3/3] refactor(motion): resolveDetent is resolveSnapPoint, with flickThreshold Platform glossary names, aligned to the merged records: the release policy is resolveSnapPoint (motion/resolveSnapPoint.ts) over the selectSnapPoint primitive, its inputs are snap points, and the flick input is flickThreshold. The back-out cancel stays an input, not an option: a release moving back toward a given start always cancels to it (spec:AST-094 FR37). No behavior change; the release tests pass unchanged. --- .changeset/release-detent-policy.md | 2 +- packages/core/src/BottomSheet/sheetRelease.ts | 12 +-- packages/core/src/motion/README.md | 4 +- packages/core/src/motion/interactive.ts | 2 +- packages/core/src/motion/projection.ts | 2 +- ...etent.test.ts => resolveSnapPoint.test.ts} | 97 +++++++++--------- .../{resolveDetent.ts => resolveSnapPoint.ts} | 99 +++++++++---------- packages/core/src/motion/selectPage.ts | 12 +-- 8 files changed, 116 insertions(+), 114 deletions(-) rename packages/core/src/motion/{resolveDetent.test.ts => resolveSnapPoint.test.ts} (66%) rename packages/core/src/motion/{resolveDetent.ts => resolveSnapPoint.ts} (58%) diff --git a/.changeset/release-detent-policy.md b/.changeset/release-detent-policy.md index ed576dfb23880..5df9a65a13ee8 100644 --- a/.changeset/release-detent-policy.md +++ b/.changeset/release-detent-policy.md @@ -2,6 +2,6 @@ '@astryxdesign/core': patch --- -[chore] Internal: BottomSheet's release under a finger and the paging helper pick their landing with one shared release policy (motion/resolveDetent.ts), with the same thresholds as before. No behavior change. +[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 diff --git a/packages/core/src/BottomSheet/sheetRelease.ts b/packages/core/src/BottomSheet/sheetRelease.ts index 524001c7839be..e8f244796f862 100644 --- a/packages/core/src/BottomSheet/sheetRelease.ts +++ b/packages/core/src/BottomSheet/sheetRelease.ts @@ -3,7 +3,7 @@ /** * @file sheetRelease.ts * @input The UIKit constants table (`sheet.release`, the sheet springs) and - * the motion foundation's release policy (resolveDetent) + * 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 @@ -42,7 +42,7 @@ * - /packages/core/src/BottomSheet/useSheetGestures.ts */ -import {resolveDetent} from '../motion/resolveDetent'; +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'; @@ -110,14 +110,14 @@ export function resolveSheetRelease({ } const positions = dismissedOffset == null ? [...detents] : [...detents, dismissedOffset]; - // The release policy (motion/resolveDetent.ts): nearest where it projects, + // 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} = resolveDetent({ - detents: positions, + const {index, projected} = resolveSnapPoint({ + points: positions, position, velocity, - flickSpeed: sheet.release.fastSpeed, + flickThreshold: sheet.release.fastSpeed, tolerance: sheet.release.detentTolerance, projection: { seconds: sheet.release.projectionSeconds, diff --git a/packages/core/src/motion/README.md b/packages/core/src/motion/README.md index 104d75ea31f67..874624caf7f2d 100644 --- a/packages/core/src/motion/README.md +++ b/packages/core/src/motion/README.md @@ -28,8 +28,8 @@ so each keeps its measured value; no shipped component reads the roles yet. | `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. | -| `resolveDetent.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 `resolveDetent`. | +| `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. | diff --git a/packages/core/src/motion/interactive.ts b/packages/core/src/motion/interactive.ts index 623a18b4da108..9d8ebf936d13d 100644 --- a/packages/core/src/motion/interactive.ts +++ b/packages/core/src/motion/interactive.ts @@ -30,7 +30,7 @@ * 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 `resolveDetent` (resolveDetent.ts), the motion + * 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`). diff --git a/packages/core/src/motion/projection.ts b/packages/core/src/motion/projection.ts index 72510da2df5e3..f3ff310d2f829 100644 --- a/packages/core/src/motion/projection.ts +++ b/packages/core/src/motion/projection.ts @@ -59,7 +59,7 @@ export interface SnapSelection { * The rest point a release goes to: the one nearest the projected rest * 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 `resolveDetent` (resolveDetent.ts), so a + * 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. diff --git a/packages/core/src/motion/resolveDetent.test.ts b/packages/core/src/motion/resolveSnapPoint.test.ts similarity index 66% rename from packages/core/src/motion/resolveDetent.test.ts rename to packages/core/src/motion/resolveSnapPoint.test.ts index fad314752cad5..742291cee16d3 100644 --- a/packages/core/src/motion/resolveDetent.test.ts +++ b/packages/core/src/motion/resolveSnapPoint.test.ts @@ -5,32 +5,32 @@ import path from 'node:path'; import {fileURLToPath} from 'node:url'; import {describe, expect, it} from 'vitest'; import * as core from '../index'; -import {resolveDetent} from './resolveDetent'; +import {resolveSnapPoint} from './resolveSnapPoint'; const here = path.dirname(fileURLToPath(import.meta.url)); const SHEET = { - flickSpeed: 1008, + flickThreshold: 1008, tolerance: 1, projection: {seconds: 0.1, minSpeed: 250}, } as const; -describe('resolveDetent', () => { - it('lands a slow release on the detent nearest where it was let go', () => { +describe('resolveSnapPoint', () => { + it('lands a slow release on the snap point nearest where it was let go', () => { expect( - resolveDetent({ - detents: [0, 300, 640], + resolveSnapPoint({ + points: [0, 300, 640], position: 140, velocity: 0, - flickSpeed: 48, + flickThreshold: 48, }), ).toEqual({index: 0, projected: 140}); expect( - resolveDetent({ - detents: [0, 300, 640], + resolveSnapPoint({ + points: [0, 300, 640], position: 160, velocity: 20, - flickSpeed: 48, + flickThreshold: 48, }).index, ).toBe(1); }); @@ -38,17 +38,17 @@ describe('resolveDetent', () => { it('projects nothing below the projection speed, and judges a faster slow release where it coasts to', () => { // 240 px/s is below 250: judged at 140, nearer 0. expect( - resolveDetent({ + resolveSnapPoint({ ...SHEET, - detents: [0, 300, 640], + points: [0, 300, 640], position: 140, velocity: 240, }), ).toEqual({index: 0, projected: 140}); // 600 px/s projects 0.1 s ahead: 140 + 60 = 200, nearer 300. - const projected = resolveDetent({ + const projected = resolveSnapPoint({ ...SHEET, - detents: [0, 300, 640], + points: [0, 300, 640], position: 140, velocity: 600, }); @@ -56,53 +56,53 @@ describe('resolveDetent', () => { expect(projected.projected).toBeCloseTo(200, 6); }); - it('takes a flick at least to the next detent beyond, or to its projection if that is farther', () => { + it('takes a flick at least to the next snap point beyond, or to its projection if that is farther', () => { // At 300 moving 1200 px/s: projected 420 (nearer 300), but a flick goes on. - const flick = resolveDetent({ + const flick = resolveSnapPoint({ ...SHEET, - detents: [0, 300, 640], + points: [0, 300, 640], position: 300, velocity: 1200, }); expect(flick.index).toBe(2); expect(flick.projected).toBeCloseTo(420, 6); - // Without projection, from just past the first detent. + // Without projection, from just past the first snap point. expect( - resolveDetent({ - detents: [0, 300], + resolveSnapPoint({ + points: [0, 300], position: 2, velocity: 60, - flickSpeed: 48, + flickThreshold: 48, }).index, ).toBe(1); - // Moving down the axis, to the next detent below. + // Moving down the axis, to the next snap point below. expect( - resolveDetent({ + resolveSnapPoint({ ...SHEET, - detents: [0, 300, 640], + points: [0, 300, 640], position: 620, velocity: -1100, }).index, ).toBe(1); }); - it('counts a detent within the tolerance as reached, so a flick goes on to the one after it', () => { + it('counts a snap point within the tolerance as reached, so a flick goes on to the one after it', () => { expect( - resolveDetent({ - detents: [0, 300, 640], + resolveSnapPoint({ + points: [0, 300, 640], position: 300.5, velocity: 2000, - flickSpeed: 1008, + flickThreshold: 1008, tolerance: 1, }).index, ).toBe(2); - // Past the last detent there is nothing beyond: the nearest stays. + // Past the last snap point there is nothing beyond: the nearest stays. expect( - resolveDetent({ - detents: [0, 300], + resolveSnapPoint({ + points: [0, 300], position: 310, velocity: 2000, - flickSpeed: 1008, + flickThreshold: 1008, tolerance: 1, }).index, ).toBe(1); @@ -111,9 +111,9 @@ describe('resolveDetent', () => { it('cancels a release moving back toward where the drag began, whatever its travel or speed', () => { // Dragged from 0 to 280, released slowly moving back up: back to 0. expect( - resolveDetent({ + resolveSnapPoint({ ...SHEET, - detents: [0, 300, 640], + points: [0, 300, 640], position: 280, velocity: -10, start: 0, @@ -121,9 +121,9 @@ describe('resolveDetent', () => { ).toBe(0); // A flick back toward the start cancels too. expect( - resolveDetent({ + resolveSnapPoint({ ...SHEET, - detents: [0, 300, 640], + points: [0, 300, 640], position: 500, velocity: -1500, start: 300, @@ -131,9 +131,9 @@ describe('resolveDetent', () => { ).toBe(1); // Moving away from the start does not cancel. expect( - resolveDetent({ + resolveSnapPoint({ ...SHEET, - detents: [0, 300, 640], + points: [0, 300, 640], position: 280, velocity: 10, start: 0, @@ -141,14 +141,14 @@ describe('resolveDetent', () => { ).toBe(1); }); - it('sends a tie the way a projected release moves, and otherwise to the earlier detent', () => { + it('sends a tie the way a projected release moves, and otherwise to the earlier snap point', () => { const at = (velocity: number, projection?: typeof SHEET.projection) => - resolveDetent({ - detents: [0, 300], + resolveSnapPoint({ + points: [0, 300], // Released where its projection lands on the midpoint. position: 150 - velocity * 0.1 * (projection == null ? 0 : 1), velocity, - flickSpeed: 1008, + flickThreshold: 1008, projection, }).index; expect(at(0)).toBe(0); @@ -158,14 +158,19 @@ describe('resolveDetent', () => { expect(at(-400, SHEET.projection)).toBe(0); }); - it('needs at least one detent', () => { + it('needs at least one snap point', () => { expect(() => - resolveDetent({detents: [], position: 0, velocity: 0, flickSpeed: 1}), + resolveSnapPoint({ + points: [], + position: 0, + velocity: 0, + flickThreshold: 1, + }), ).toThrow(RangeError); }); it('stays internal: not exported from the package or any barrel', () => { - expect(core).not.toHaveProperty('resolveDetent'); + expect(core).not.toHaveProperty('resolveSnapPoint'); const pkg = JSON.parse( fs.readFileSync(path.join(here, '../../package.json'), 'utf8'), ) as {exports: Record}; @@ -177,7 +182,7 @@ describe('resolveDetent', () => { .filter(name => /^index\.(ts|tsx|js|mjs)$/.test(name)); for (const barrel of barrels) { expect(fs.readFileSync(path.join(here, barrel), 'utf8')).not.toMatch( - /resolveDetent/, + /resolveSnapPoint/, ); } }); diff --git a/packages/core/src/motion/resolveDetent.ts b/packages/core/src/motion/resolveSnapPoint.ts similarity index 58% rename from packages/core/src/motion/resolveDetent.ts rename to packages/core/src/motion/resolveSnapPoint.ts index b7445b2569d79..b23f5bdbd1dd9 100644 --- a/packages/core/src/motion/resolveDetent.ts +++ b/packages/core/src/motion/resolveSnapPoint.ts @@ -1,55 +1,55 @@ // Copyright (c) Meta Platforms, Inc. and affiliates. /** - * @file resolveDetent.ts - * @input A release (position and velocity on one axis), the detents it can + * @file resolveSnapPoint.ts + * @input A release (position and velocity on one axis), the points it can * land on, and the release policy's thresholds - * @output Exports resolveDetent: the index of the detent a release lands on, + * @output Exports resolveSnapPoint: the index of the snap point a release lands on, * and the projected rest position it was judged from * @position The release policy of the motion foundation, on * `selectSnapPoint` (projection.ts), which stays the math primitive * (projection + nearest). Pure: no DOM, no React. Internal to core: not - * exported from the package or any barrel (resolveDetent.test.ts locks - * that). A component's release rule calls it with its own detents and + * exported from the package or any barrel (resolveSnapPoint.test.ts locks + * that). A component's release rule calls it with its own points and * thresholds; this is the one "where does a release land" helper, so a * second one should not be added beside it. * - * - A slow release lands on the detent nearest where it would come to rest: + * - A slow release lands on the snap point nearest where it would come to rest: * its position, or, with `projection`, its position plus the distance it * would coast at its velocity (projection.ts). Below - * `projection.minSpeed` nothing is projected, so it lands on the detent + * `projection.minSpeed` nothing is projected, so it lands on the snap point * nearest where it was let go. A tie goes the way a projected release - * moves, and otherwise to the earlier detent (`selectSnapPoint`). - * - A flick (`flickSpeed` or faster) lands at least on the next detent + * moves, and otherwise to the earlier snap point (`selectSnapPoint`). + * - A flick (`flickThreshold` or faster) lands at least on the next snap point * beyond its position in the direction it moves, or on the nearest - * detent to its projection if that is farther. A detent within + * snap point to its projection if that is farther. A snap point within * `tolerance` of the position counts as reached, so the flick goes on to * the one after it. * - With `start` (where the drag began), a release moving back toward it - * cancels to the detent nearest `start`, whatever its travel so far or its + * cancels to the snap point nearest `start`, whatever its travel so far or its * speed. * - With `fromIndex` and `maxSteps`, a release lands at most `maxSteps` - * detents away from the one at `fromIndex` (a pager's one page per + * points away from the one at `fromIndex` (a pager's one page per * release). * - * Detents are ascending positions on the axis; velocities are in the same + * Snap points are ascending positions on the axis; velocities are in the same * units per second, positive toward larger positions. * - * SYNC: When modified, update resolveDetent.test.ts. + * SYNC: When modified, update resolveSnapPoint.test.ts. */ import {selectSnapPoint} from './projection'; /** Where a release lands. */ -export interface DetentRelease { - /** Index into the detents passed in. */ +export interface SnapPointRelease { + /** Index into the points passed in. */ readonly index: number; /** The rest position the choice was judged from (the position without a projection). */ readonly projected: number; } /** A release's projection: how far ahead it is judged, and from what speed. */ -export interface DetentProjection { +export interface SnapPointProjection { /** Seconds of the release velocity the projection reaches. */ readonly seconds: number; /** Below this speed (units/s), nothing is projected (default 0). */ @@ -63,14 +63,11 @@ function rateForProjection(seconds: number): number { return ms / (1 + ms); } -/** The index of the detent nearest `position` (the first on a tie). */ -function nearestIndex( - detents: ReadonlyArray, - position: number, -): number { +/** The index of the snap point nearest `position` (the first on a tie). */ +function nearestIndex(points: ReadonlyArray, position: number): number { let best = 0; - for (let i = 1; i < detents.length; i += 1) { - if (Math.abs(detents[i] - position) < Math.abs(detents[best] - position)) { + for (let i = 1; i < points.length; i += 1) { + if (Math.abs(points[i] - position) < Math.abs(points[best] - position)) { best = i; } } @@ -78,60 +75,60 @@ function nearestIndex( } /** - * The detent a release lands on. + * The snap point a release lands on. * * @example * ``` - * // Detents at 0, 300 and 640 px. Released at 300 px moving toward 640 at + * // Snap points at 0, 300 and 640 px. Released at 300 px moving toward 640 at * // 1200 px/s, a flick: on to 640, although its projection (420 px) is * // nearer 300. - * resolveDetent({ - * detents: [0, 300, 640], + * resolveSnapPoint({ + * points: [0, 300, 640], * position: 300, * velocity: 1200, - * flickSpeed: 1008, + * flickThreshold: 1008, * tolerance: 1, * projection: {seconds: 0.1, minSpeed: 250}, * start: 0, * }); // {index: 2, projected: 420} * ``` */ -export function resolveDetent({ - detents, +export function resolveSnapPoint({ + points, position, velocity, - flickSpeed, + flickThreshold, tolerance = 0, projection, start, fromIndex, maxSteps, }: { - /** Detent positions, ascending. */ - detents: ReadonlyArray; + /** Snap point positions, ascending. */ + points: ReadonlyArray; position: number; velocity: number; /** From this speed (units/s) on, a release is a flick. */ - flickSpeed: number; - /** A detent this close to the position counts as reached by a flick. */ + flickThreshold: number; + /** A snap point this close to the position counts as reached by a flick. */ tolerance?: number; /** Judge a slow release where it would coast to; absent, where it was let go. */ - projection?: DetentProjection; + projection?: SnapPointProjection; /** Where the drag began: a release moving back toward it cancels to it. */ start?: number; - /** The detent the release starts from, for `maxSteps`. */ + /** The snap point the release starts from, for `maxSteps`. */ fromIndex?: number; - /** At most this many detents away from `fromIndex`. */ + /** At most this many points away from `fromIndex`. */ maxSteps?: number; -}): DetentRelease { - if (detents.length === 0) { - throw new RangeError('resolveDetent needs at least one detent'); +}): SnapPointRelease { + if (points.length === 0) { + throw new RangeError('resolveSnapPoint needs at least one snap point'); } const projectedVelocity = projection == null || Math.abs(velocity) < (projection.minSpeed ?? 0) ? 0 : velocity; - const choice = selectSnapPoint(detents, { + const choice = selectSnapPoint(points, { position, velocity: projectedVelocity, rate: @@ -140,14 +137,14 @@ export function resolveDetent({ maxSteps, }); let index = choice.index; - if (Math.abs(velocity) >= flickSpeed) { - // A flick goes at least to the next detent beyond where it is. + if (Math.abs(velocity) >= flickThreshold) { + // A flick goes at least to the next snap point beyond where it is. let next = -1; if (velocity > 0) { - next = detents.findIndex(d => d > position + tolerance); + next = points.findIndex(d => d > position + tolerance); } else { - for (let i = detents.length - 1; i >= 0 && next === -1; i -= 1) { - if (detents[i] < position - tolerance) { + for (let i = points.length - 1; i >= 0 && next === -1; i -= 1) { + if (points[i] < position - tolerance) { next = i; } } @@ -161,11 +158,11 @@ export function resolveDetent({ velocity !== 0 && Math.sign(velocity) === Math.sign(start - position) ) { - index = nearestIndex(detents, start); + index = nearestIndex(points, start); } if (fromIndex != null && maxSteps != null) { - // A stale index past either end counts from the nearest real detent. - const from = Math.min(Math.max(fromIndex, 0), detents.length - 1); + // A stale index past either end counts from the nearest real snap point. + const from = Math.min(Math.max(fromIndex, 0), points.length - 1); index = Math.min(Math.max(index, from - maxSteps), from + maxSteps); } return {index, projected: choice.projected}; diff --git a/packages/core/src/motion/selectPage.ts b/packages/core/src/motion/selectPage.ts index c0813ad2be7fd..26f76cb0fee19 100644 --- a/packages/core/src/motion/selectPage.ts +++ b/packages/core/src/motion/selectPage.ts @@ -6,13 +6,13 @@ * current page * @output Exports `selectPage`: the page a paging release settles on * @position UIScrollView paging (`isPagingEnabled`) on the release policy - * (resolveDetent.ts). Pure: no DOM, no React + * (resolveSnapPoint.ts). Pure: no DOM, no React * * SYNC: When modified, update selectPage.test.ts. */ import type {SnapSelection} from './projection'; -import {resolveDetent} from './resolveDetent'; +import {resolveSnapPoint} from './resolveSnapPoint'; import {scroll} from './uikit/scroll'; /** @@ -40,13 +40,13 @@ export function selectPage({ {length: Math.max(1, pageCount)}, (_, i) => i * pageSize, ); - // The release policy (resolveDetent.ts), with the paging rate as the + // The release policy (resolveSnapPoint.ts), with the paging rate as the // seconds of velocity it projects, no flick and one page per release. - const {index, projected} = resolveDetent({ - detents: points, + const {index, projected} = resolveSnapPoint({ + points, position, velocity, - flickSpeed: Infinity, + flickThreshold: Infinity, projection: {seconds: rate / (1 - rate) / 1000}, fromIndex: currentPage, maxSteps: 1,