Skip to content

Commit f8fa243

Browse files
committed
refactor(Item): swipe actions run on the interactive-transition driver
The row's moving parts are one interactive transition (motion/interactive.ts): the root's translation drives; the window, the track and the entries are map layers of it, and the full swipe's look (the outermost entry and the others) is a state layer of the drag's confirmation. Every settle, release and tracking spring plays through the driver's `to`, on the browser's commit as before; a finger followed one for one under reduced motion is `place`. The swipe's decisions are the driver's second pair of rules (swipeRelease.ts): swipeTrackRule (the full swipe confirms on the finger's travel and the row runs ahead to the confirmation offset) and swipeReleaseRule (resolveRelease on the finger's travel and the drag's confirmation, as the row's physical translation, on the settle spring). swipeCurves goes: the driver keeps every part a function of the root's curve. A slide-out's fire is still timed from its start on screen. The release storyboards recorded before this change hold within 0.5 px and 0.005 opacity, with the same fire times, the late-started slide-out included.
1 parent fb7d372 commit f8fa243

4 files changed

Lines changed: 320 additions & 167 deletions

File tree

‎packages/core/src/Item/swipeRelease.test.ts‎

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ import {
1616
toSpring,
1717
} from '../motion/spring';
1818
import {swipeActions} from '../motion/uikit';
19-
import {confirmedOffset, confirmPoint, resolveRelease} from './swipeRelease';
19+
import {
20+
confirmedOffset,
21+
confirmPoint,
22+
resolveRelease,
23+
SWIPE_TRACK_START,
24+
swipeReleaseRule,
25+
swipeTrackRule,
26+
} from './swipeRelease';
2027

2128
describe('resolveRelease', () => {
2229
// The UIKit lab's releases (batch 10a: plain table, 402 pt rows, two
@@ -237,3 +244,99 @@ describe('the swipe spring', () => {
237244
}
238245
});
239246
});
247+
248+
describe('swipeTrackRule (the interactive-transition driver\u2019s track rule)', () => {
249+
it('confirms on the finger\u2019s travel, runs ahead to the confirmation offset, and remembers it confirmed', () => {
250+
const rowWidth = 375;
251+
const openWidth = 144;
252+
const confirmAt = confirmPoint(openWidth, rowWidth);
253+
for (const sign of [1, -1] as const) {
254+
for (const canConfirm of [true, false]) {
255+
const rule = swipeTrackRule({sign, confirmAt, canConfirm, rowWidth});
256+
let track = SWIPE_TRACK_START;
257+
for (const swipe of [
258+
0,
259+
50,
260+
144,
261+
200,
262+
confirmAt - 1,
263+
confirmAt,
264+
330,
265+
300,
266+
120,
267+
]) {
268+
const step = rule(sign * swipe, track);
269+
const isConfirmed = canConfirm && swipe >= confirmAt;
270+
expect(step.state.isConfirmed).toBe(isConfirmed);
271+
expect(step.state.wasConfirmed).toBe(
272+
track.wasConfirmed || isConfirmed,
273+
);
274+
expect(step.position).toBe(
275+
sign *
276+
(isConfirmed
277+
? Math.max(swipe, confirmedOffset(rowWidth))
278+
: swipe),
279+
);
280+
expect(step.follow).toBe(swipeActions.tracking);
281+
track = step.state;
282+
}
283+
// Confirmed once, then backed out: it remembers.
284+
expect(track.wasConfirmed).toBe(canConfirm);
285+
}
286+
}
287+
});
288+
});
289+
290+
describe('swipeReleaseRule (the interactive-transition driver\u2019s release rule)', () => {
291+
it('lands where resolveRelease says, as the row\u2019s physical translation, on the settle spring', () => {
292+
const openWidth = 144;
293+
const outTravel = 395;
294+
for (const sign of [1, -1] as const) {
295+
for (const behavior of ['reveal', 'commit'] as const) {
296+
for (const [isConfirmed, wasConfirmed] of [
297+
[false, false],
298+
[false, true],
299+
[true, true],
300+
]) {
301+
for (const travel of [0, 30, 71, 72, 140, 260]) {
302+
for (const velocity of [-600, -48, -20, 0, 20, 48, 600]) {
303+
const landing = resolveRelease({
304+
behavior,
305+
isConfirmed,
306+
openWidth,
307+
travel,
308+
velocity,
309+
wasConfirmed,
310+
});
311+
const outcome = swipeReleaseRule(
312+
{
313+
position: sign * travel,
314+
finger: sign * travel,
315+
velocity: sign * velocity,
316+
},
317+
{
318+
behavior,
319+
openWidth,
320+
outTravel,
321+
sign,
322+
track: {isConfirmed, wasConfirmed},
323+
},
324+
);
325+
expect(outcome.outcome).toBe(landing);
326+
expect(outcome.target).toBe(
327+
sign *
328+
(landing === 'perform'
329+
? outTravel
330+
: landing === 'open'
331+
? openWidth
332+
: 0),
333+
);
334+
expect(outcome.motion).toBe(swipeActions.settle);
335+
expect(outcome.exits).toBeUndefined();
336+
}
337+
}
338+
}
339+
}
340+
}
341+
});
342+
});

‎packages/core/src/Item/swipeRelease.ts‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* @input The UIKit constants table (motion/uikit/swipeActions.ts)
66
* @output Exports resolveRelease (where a swipe-action release lands: closed,
77
* open, or the outermost entry fires), confirmPoint (where a full swipe
8-
* confirms) and confirmedOffset
8+
* confirms) and confirmedOffset, and the interactive-transition driver's
9+
* swipe rules (swipeTrackRule, swipeReleaseRule)
910
* @position Pure pieces of Item's swipe actions, beside useSwipeAction;
1011
* tested by swipeRelease.test.ts against the UIKit lab's releases
1112
*
@@ -20,6 +21,11 @@
2021
* toward the side being uncovered, whatever the writing direction.
2122
*/
2223

24+
import type {
25+
ReleaseInput,
26+
ReleaseOutcome,
27+
TrackRule,
28+
} from '../motion/interactive';
2329
import {swipeActions} from '../motion/uikit';
2430

2531
/** What a swipe does (Item's `swipeBehavior`). */
@@ -86,3 +92,92 @@ export function confirmPoint(openWidth: number, rowWidth: number): number {
8692
export function confirmedOffset(rowWidth: number): number {
8793
return rowWidth - swipeActions.confirmInset;
8894
}
95+
96+
/** A swipe's drag state: whether the full swipe is confirmed, and whether it was at some point. */
97+
export interface SwipeTrack {
98+
readonly isConfirmed: boolean;
99+
readonly wasConfirmed: boolean;
100+
}
101+
102+
/** A swipe before the finger has moved. */
103+
export const SWIPE_TRACK_START: SwipeTrack = {
104+
isConfirmed: false,
105+
wasConfirmed: false,
106+
};
107+
108+
/**
109+
* The swipe's track rule, for the interactive-transition driver
110+
* (motion/interactive.ts). The driver is the row's physical translation, so
111+
* `finger` and the position are physical px (`sign` × the logical swipe);
112+
* the rule judges the logical swipe. Past the confirm point, when the
113+
* outermost entry can fire, the full swipe confirms on the finger's travel
114+
* and the row runs ahead to the confirmation offset; backed out, it follows
115+
* the finger again. Either way the row follows on the tracking spring.
116+
*/
117+
export function swipeTrackRule({
118+
sign,
119+
confirmAt,
120+
canConfirm,
121+
rowWidth,
122+
}: {
123+
/** +1 when the side's panel is on the physical left, −1 on the right. */
124+
sign: 1 | -1;
125+
/** The swipe at which the full swipe confirms (`confirmPoint`). */
126+
confirmAt: number;
127+
/** Whether the outermost entry can fire. */
128+
canConfirm: boolean;
129+
rowWidth: number;
130+
}): TrackRule<SwipeTrack> {
131+
return (finger, track) => {
132+
const swipe = Math.max(0, finger * sign);
133+
const isConfirmed = canConfirm && swipe >= confirmAt;
134+
return {
135+
position:
136+
sign *
137+
(isConfirmed ? Math.max(swipe, confirmedOffset(rowWidth)) : swipe),
138+
state: {isConfirmed, wasConfirmed: track.wasConfirmed || isConfirmed},
139+
follow: swipeActions.tracking,
140+
};
141+
};
142+
}
143+
144+
/** What the swipe's release rule knows besides where the gesture let go. */
145+
export interface SwipeReleaseContext {
146+
/** +1 when the side's panel is on the physical left, −1 on the right. */
147+
readonly sign: 1 | -1;
148+
readonly behavior: SwipeReleaseBehavior;
149+
readonly openWidth: number;
150+
/** How far out a row goes when its outermost entry fires (logical px). */
151+
readonly outTravel: number;
152+
}
153+
154+
/**
155+
* The swipe's release rule, for the interactive-transition driver: where a
156+
* release lands (`resolveRelease`, on the finger's travel and the drag's
157+
* confirmation) as the row's physical translation, on the settle spring. A
158+
* full swipe slides the row out past its edge.
159+
*/
160+
export function swipeReleaseRule(
161+
input: ReleaseInput,
162+
context: SwipeReleaseContext & {readonly track: SwipeTrack},
163+
): ReleaseOutcome<SwipeRelease> {
164+
const landing = resolveRelease({
165+
behavior: context.behavior,
166+
isConfirmed: context.track.isConfirmed,
167+
wasConfirmed: context.track.wasConfirmed,
168+
openWidth: context.openWidth,
169+
travel: Math.max(0, input.finger * context.sign),
170+
velocity: input.velocity * context.sign,
171+
});
172+
const travel =
173+
landing === 'perform'
174+
? context.outTravel
175+
: landing === 'open'
176+
? context.openWidth
177+
: 0;
178+
return {
179+
target: context.sign * travel,
180+
motion: swipeActions.settle,
181+
outcome: landing,
182+
};
183+
}

‎packages/core/src/Item/useSwipeAction.test.ts‎

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@
66
* @output The geometry every moving part follows (the window never moves on
77
* screen, the track moves with the row, the entries stay pinned until the
88
* open width and ride the row past it, the full swipe's look) for both
9-
* sides in both directions, and the curves that keep every part a function
10-
* of the root's at every instant
9+
* sides in both directions. The moving parts' curves come from the
10+
* interactive-transition driver, which keeps every part a function of the
11+
* root's at every instant (motion/interactive.test.ts)
1112
* @position Unit tests of packages/core/src/Item/useSwipeAction.ts; the hook
1213
* itself is tested through Item.test.tsx and the interaction spec
1314
*/
1415

1516
import {describe, expect, it} from 'vitest';
16-
import {swipeActions} from '../motion/uikit';
17-
import {
18-
physicalSign,
19-
swipeCurves,
20-
swipeParts,
21-
type SwipeLayout,
22-
} from './useSwipeAction';
17+
import {physicalSign, swipeParts, type SwipeLayout} from './useSwipeAction';
2318

2419
const LAYOUTS: SwipeLayout[] = [
2520
{inlineSign: 1, openWidth: 144, outerWidth: 72, side: 'leading'},
@@ -79,52 +74,3 @@ describe('swipeParts', () => {
7974
},
8075
);
8176
});
82-
83-
describe('swipeCurves', () => {
84-
it.each(LAYOUTS)(
85-
'keeps every part a function of the root at every instant, also on a spring that crosses the open width ($side, inline sign $inlineSign)',
86-
layout => {
87-
const sign = physicalSign(layout.side, layout.inlineSign);
88-
// Closing from a full swipe (300 px) and opening from rest, each
89-
// crossing the 144 px open width mid-flight; one starts moving.
90-
for (const [from, velocity, travel] of [
91-
[300, -400, 0],
92-
[0, 0, 300],
93-
[100, 900, 150],
94-
]) {
95-
const curves = swipeCurves(
96-
layout,
97-
swipeActions.settle,
98-
sign * from,
99-
sign * velocity,
100-
travel,
101-
);
102-
for (let t = 0; t <= curves.root.duration; t += 0.01) {
103-
const x = curves.root.at(t);
104-
const parts = swipeParts(sign * x, layout);
105-
expect(curves.panel.at(t)).toBeCloseTo(parts.panel, 9);
106-
expect(curves.track.at(t)).toBeCloseTo(parts.track, 9);
107-
expect(curves.entries.at(t)).toBeCloseTo(parts.entries, 9);
108-
}
109-
// They start where the row is and end where it lands.
110-
expect(curves.root.at(0)).toBeCloseTo(sign * from, 9);
111-
expect(curves.root.at(curves.root.duration)).toBeCloseTo(
112-
sign * travel,
113-
1,
114-
);
115-
}
116-
},
117-
);
118-
119-
it('gives each part the velocity of its own curve', () => {
120-
const layout = LAYOUTS[1];
121-
const curves = swipeCurves(layout, swipeActions.settle, 0, 0, 300);
122-
for (const t of [0.05, 0.1, 0.3, 0.6]) {
123-
const dt = 1e-5;
124-
for (const curve of [curves.root, curves.panel, curves.entries]) {
125-
const numeric = (curve.at(t + dt) - curve.at(t - dt)) / (2 * dt);
126-
expect(curve.velocity(t)).toBeCloseTo(numeric, 2);
127-
}
128-
}
129-
});
130-
});

0 commit comments

Comments
 (0)