Skip to content

Commit bb5bf37

Browse files
committed
feat(lab): UIRefreshControl's pull, trigger and state machine, as UIKit measures them
refreshPull maps finger travel to the overscroll UIKit shows: one to one up to the 60 pt control height after the pan's hysteresis, then the scroll view's rubber band; the trigger distance grows with the scroll view's height (0.198 x the height, never below 73 pt); a trigger within twice the height steps back onto the finger. Its inverse resumes a pull caught mid-settle, and it gives the reveal, the release target and the end motion. The tests replay UIKit's own frames from a UIRefreshControl on the iOS 26.2 simulator. refreshModel is the control's state machine as a pure reducer: a pull triggers once with the finger down; momentum never triggers; a pull while refreshing triggers again past the inset; releasing settles onto the inset or back to the top; a programmatic begin calls the handler only for the user's request; ending twice is a no-op.
1 parent 384295d commit bb5bf37

4 files changed

Lines changed: 672 additions & 0 deletions

File tree

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file refreshModel.test.ts
5+
* @input vitest, refreshModel
6+
* @output Walks UIRefreshControl's state machine: trigger once with the
7+
* finger down, settle targets, cancel, programmatic begin and end, a pull
8+
* while refreshing, and no-ops
9+
* @position Testing; colocated with refreshModel.ts
10+
*/
11+
12+
import {describe, expect, it} from 'vitest';
13+
import {
14+
INITIAL_REFRESH_STATE,
15+
reduceRefresh,
16+
type RefreshEffect,
17+
type RefreshEvent,
18+
type RefreshState,
19+
} from './refreshModel';
20+
21+
function run(
22+
events: ReadonlyArray<RefreshEvent>,
23+
from: RefreshState = INITIAL_REFRESH_STATE,
24+
): {state: RefreshState; effects: RefreshEffect[]} {
25+
let state = from;
26+
const effects: RefreshEffect[] = [];
27+
for (const event of events) {
28+
const next = reduceRefresh(state, event);
29+
state = next.state;
30+
effects.push(...next.effects);
31+
}
32+
return {state, effects};
33+
}
34+
35+
describe('reduceRefresh', () => {
36+
it('triggers once, with the finger down, when the pull reaches the trigger distance', () => {
37+
const {state, effects} = run([
38+
{type: 'pullStart'},
39+
{type: 'pullMove', overscroll: 40, trigger: 74},
40+
{type: 'pullMove', overscroll: 73.33, trigger: 74},
41+
{type: 'pullMove', overscroll: 74, trigger: 74},
42+
{type: 'pullMove', overscroll: 95, trigger: 74},
43+
]);
44+
expect(effects).toEqual([{type: 'trigger'}]);
45+
expect(state).toEqual({
46+
isRefreshing: true,
47+
isPulling: true,
48+
hasTriggered: true,
49+
});
50+
});
51+
52+
it('settles onto the inset when released while refreshing', () => {
53+
const {state, effects} = run([
54+
{type: 'pullStart'},
55+
{type: 'pullMove', overscroll: 80, trigger: 74},
56+
{type: 'pullEnd'},
57+
]);
58+
expect(effects).toEqual([{type: 'trigger'}, {type: 'settle', to: 'inset'}]);
59+
expect(state.isPulling).toBe(false);
60+
expect(state.isRefreshing).toBe(true);
61+
});
62+
63+
it('settles back to the top when released below the trigger', () => {
64+
const {state, effects} = run([
65+
{type: 'pullStart'},
66+
{type: 'pullMove', overscroll: 50, trigger: 74},
67+
{type: 'pullEnd'},
68+
]);
69+
expect(effects).toEqual([{type: 'settle', to: 'top'}]);
70+
expect(state.isRefreshing).toBe(false);
71+
});
72+
73+
it('keeps refreshing when a pull that triggered is pushed back before the lift', () => {
74+
const {state, effects} = run([
75+
{type: 'pullStart'},
76+
{type: 'pullMove', overscroll: 90, trigger: 74},
77+
{type: 'pullMove', overscroll: 10, trigger: 74},
78+
{type: 'pullEnd'},
79+
]);
80+
expect(effects).toEqual([{type: 'trigger'}, {type: 'settle', to: 'inset'}]);
81+
expect(state.isRefreshing).toBe(true);
82+
});
83+
84+
it('settles a cancelled pull like a release', () => {
85+
expect(
86+
run([
87+
{type: 'pullStart'},
88+
{type: 'pullMove', overscroll: 30, trigger: 74},
89+
{type: 'pullCancel'},
90+
]).effects,
91+
).toEqual([{type: 'settle', to: 'top'}]);
92+
expect(
93+
run([
94+
{type: 'pullStart'},
95+
{type: 'pullMove', overscroll: 99, trigger: 74},
96+
{type: 'pullCancel'},
97+
]).effects,
98+
).toEqual([{type: 'trigger'}, {type: 'settle', to: 'inset'}]);
99+
});
100+
101+
it('never triggers from movement that no finger is pulling (a momentum overshoot)', () => {
102+
expect(
103+
run([{type: 'pullMove', overscroll: 120, trigger: 74}]).effects,
104+
).toEqual([]);
105+
});
106+
107+
it('triggers again on a pull while refreshing, a trigger distance past the inset', () => {
108+
const refreshing = run([
109+
{type: 'pullStart'},
110+
{type: 'pullMove', overscroll: 80, trigger: 74},
111+
{type: 'pullEnd'},
112+
]).state;
113+
const short = run(
114+
[
115+
{type: 'pullStart'},
116+
{type: 'pullMove', overscroll: 130, trigger: 74},
117+
{type: 'pullEnd'},
118+
],
119+
refreshing,
120+
);
121+
expect(short.effects).toEqual([{type: 'settle', to: 'inset'}]);
122+
const far = run(
123+
[
124+
{type: 'pullStart'},
125+
{type: 'pullMove', overscroll: 134, trigger: 74},
126+
{type: 'pullMove', overscroll: 150, trigger: 74},
127+
{type: 'pullEnd'},
128+
],
129+
refreshing,
130+
);
131+
expect(far.effects).toEqual([
132+
{type: 'trigger'},
133+
{type: 'settle', to: 'inset'},
134+
]);
135+
expect(far.state.isRefreshing).toBe(true);
136+
});
137+
138+
it('begins programmatically, calling the handler only for the user’s request', () => {
139+
expect(run([{type: 'begin', source: 'user'}]).effects).toEqual([
140+
{type: 'begin', callHandler: true},
141+
]);
142+
expect(run([{type: 'begin', source: 'program'}]).effects).toEqual([
143+
{type: 'begin', callHandler: false},
144+
]);
145+
// Already refreshing: nothing new starts.
146+
expect(
147+
run([
148+
{type: 'begin', source: 'program'},
149+
{type: 'begin', source: 'user'},
150+
]).effects,
151+
).toEqual([{type: 'begin', callHandler: false}]);
152+
});
153+
154+
it('ends a running refresh once, and ignores an end with nothing running', () => {
155+
expect(run([{type: 'end'}]).effects).toEqual([]);
156+
const {state, effects} = run([
157+
{type: 'begin', source: 'program'},
158+
{type: 'end'},
159+
{type: 'end'},
160+
]);
161+
expect(effects).toEqual([
162+
{type: 'begin', callHandler: false},
163+
{type: 'end'},
164+
]);
165+
expect(state.isRefreshing).toBe(false);
166+
});
167+
168+
it('ends a refresh under the finger without settling; the lift then settles to the top', () => {
169+
const {effects} = run([
170+
{type: 'pullStart'},
171+
{type: 'pullMove', overscroll: 80, trigger: 74},
172+
{type: 'end'},
173+
{type: 'pullEnd'},
174+
]);
175+
expect(effects).toEqual([
176+
{type: 'trigger'},
177+
{type: 'end'},
178+
{type: 'settle', to: 'top'},
179+
]);
180+
});
181+
182+
it('ignores a release with no pull in progress', () => {
183+
expect(run([{type: 'pullEnd'}]).effects).toEqual([]);
184+
});
185+
});
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file refreshModel.ts
5+
* @input Pull events from the refresh pull recognizer, programmatic begin and
6+
* end requests
7+
* @output Exports reduceRefresh (UIRefreshControl's state machine as a pure
8+
* reducer that returns the effects to run), the initial state and types
9+
* @position Pure logic for useRefreshControl; no DOM, no React
10+
*
11+
* The rules, as UIKit runs them (measured where marked in
12+
* motion/uikit/refresh.ts):
13+
* - a pull triggers once, with the finger down, when its overscroll reaches
14+
* the trigger distance; only finger-driven overscroll triggers (a momentum
15+
* overshoot never does); a pull while already refreshing triggers again
16+
* (UIKit fires `valueChanged` again) once it reaches the trigger distance
17+
* beyond the refreshing inset;
18+
* - releasing settles onto the refreshing inset while refreshing, back to the
19+
* top otherwise; a cancelled pull settles the same way;
20+
* - a programmatic begin puts the control in the refreshing state; it calls
21+
* the refresh handler only when it stands for the user's request (the
22+
* non-gesture path), not when the app reports a refresh it started;
23+
* - ending a refresh that is not running does nothing.
24+
*
25+
* SYNC: When modified, update:
26+
* - /packages/lab/src/RefreshControl/refreshModel.test.ts
27+
*/
28+
29+
import {REFRESH_HEIGHT} from './refreshPull';
30+
31+
export interface RefreshState {
32+
/** A refresh is in progress (`UIRefreshControl.isRefreshing`). */
33+
readonly isRefreshing: boolean;
34+
/** A finger is pulling the content. */
35+
readonly isPulling: boolean;
36+
/** The current pull has triggered (its mapping turns one to one). */
37+
readonly hasTriggered: boolean;
38+
}
39+
40+
export type RefreshEvent =
41+
| {readonly type: 'pullStart'}
42+
| {
43+
readonly type: 'pullMove';
44+
readonly overscroll: number;
45+
/** The trigger distance for this scroll owner. */
46+
readonly trigger: number;
47+
}
48+
| {readonly type: 'pullEnd'}
49+
| {readonly type: 'pullCancel'}
50+
| {readonly type: 'begin'; readonly source: 'user' | 'program'}
51+
| {readonly type: 'end'};
52+
53+
export type RefreshEffect =
54+
/** The pull reached the trigger: spin, announce, call the handler. */
55+
| {readonly type: 'trigger'}
56+
/** A refresh started without a pull; `callHandler` for the user's request. */
57+
| {readonly type: 'begin'; readonly callHandler: boolean}
58+
/** The finger let go: settle onto the inset or back to the top. */
59+
| {readonly type: 'settle'; readonly to: 'inset' | 'top'}
60+
/** The refresh ended: remove the inset and hide the indicator. */
61+
| {readonly type: 'end'};
62+
63+
export const INITIAL_REFRESH_STATE: RefreshState = {
64+
isRefreshing: false,
65+
isPulling: false,
66+
hasTriggered: false,
67+
};
68+
69+
export function reduceRefresh(
70+
state: RefreshState,
71+
event: RefreshEvent,
72+
): {state: RefreshState; effects: ReadonlyArray<RefreshEffect>} {
73+
switch (event.type) {
74+
case 'pullStart':
75+
return {
76+
state: {...state, isPulling: true, hasTriggered: false},
77+
effects: [],
78+
};
79+
case 'pullMove': {
80+
// GUESS (M-RC3): a pull while refreshing counts from the inset.
81+
const reach = state.isRefreshing
82+
? event.overscroll - REFRESH_HEIGHT
83+
: event.overscroll;
84+
if (!state.isPulling || state.hasTriggered || reach < event.trigger) {
85+
return {state, effects: []};
86+
}
87+
return {
88+
state: {...state, isRefreshing: true, hasTriggered: true},
89+
effects: [{type: 'trigger'}],
90+
};
91+
}
92+
case 'pullEnd':
93+
case 'pullCancel':
94+
if (!state.isPulling) {
95+
return {state, effects: []};
96+
}
97+
return {
98+
state: {...state, isPulling: false},
99+
effects: [{type: 'settle', to: state.isRefreshing ? 'inset' : 'top'}],
100+
};
101+
case 'begin':
102+
if (state.isRefreshing) {
103+
return {state, effects: []};
104+
}
105+
return {
106+
state: {...state, isRefreshing: true},
107+
effects: [{type: 'begin', callHandler: event.source === 'user'}],
108+
};
109+
case 'end':
110+
if (!state.isRefreshing) {
111+
return {state, effects: []};
112+
}
113+
return {
114+
state: {...state, isRefreshing: false},
115+
effects: [{type: 'end'}],
116+
};
117+
}
118+
}

0 commit comments

Comments
 (0)