Skip to content

Commit 525eee0

Browse files
committed
feat(lab): useRefreshControl, the refresh control's behavior on the gesture system
RefreshPullRecognizer is a downward pan that never claims its touch: it begins once a pull at the top has nothing under it to scroll (scrollEdge), or when a scroll runs out of content (scrollHandoff), and keeps reading the finger after the browser takes the touch, so pushing back scrolls natively in the same gesture. useRefreshControl drives the pull with UIKit's mapping (one translate write per move, no React render), triggers with the finger down, settles onto the inset on the scroll view's bounce, ends with UIKit's 0.3 s ease-in-out and spins the indicator in stepped keyframes, all on the compositor after the lift. Near the top the scroll owner's overscroll-behavior-y is none (the page's through claimRootScrollStyle), so the browser draws no rubber band and Safari's own reload stays away while a control is mounted; the pull stands down while a scroll lock holds the page. Text selection is off while a finger pulls (iOS Safari would read a slow pull over text as a selection). Each refresh start is announced once. iOS only: the pull, the inset and the indicator follow the iOS idiom (useTouchIdiom). Elsewhere the top edge stays the browser's (Chrome keeps its own pull to refresh on the document) and a programmatic refresh only reports busy and announces itself. Motion goes through the theme: the settle and the trigger turn pass their table springs to the animator, and the end and the retracting indicator read theirs with resolveMotion. gestures/experimental exports the page's scroll-style owner from useScrollLock (claimRootScrollStyle, isScrollLockActive, subscribeScrollLock).
1 parent dfe8ba5 commit 525eee0

12 files changed

Lines changed: 2008 additions & 2 deletions
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] The locale catalog gains `@astryx.refreshControl.refreshing` ("Refreshing"), announced when a pull to refresh starts in the lab RefreshControl.
6+
7+
@vjeux
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+
[experimental] `@astryxdesign/core/gestures/experimental` exports the page's scroll-style owner: `claimRootScrollStyle`, `isScrollLockActive` and `subscribeScrollLock`, so a lab component that takes over the page's top edge (a pull to refresh) composes with scroll locks.
6+
7+
@vjeux

‎packages/core/locales/en.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,10 @@
11711171
"defaultMessage": "Loading",
11721172
"description": "Screen-reader-only default name for a standalone Spinner's role=\"status\" element, used when the consumer supplies neither `aria-label` nor a visible string label; also announced once when a busy input or selector starts loading, in place of its decorative Spinner. Present-progressive form. Kept separate from `button.loading` and `typeahead.loading` — translations may diverge."
11731173
},
1174+
"@astryx.refreshControl.refreshing": {
1175+
"defaultMessage": "Refreshing",
1176+
"description": "Announced to screen readers when a pull to refresh (or a programmatic refresh) starts."
1177+
},
11741178
"@astryx.chatComposerDrawer.expand": {
11751179
"defaultMessage": "Expand {label}",
11761180
"description": "Screen-reader-only label on the ChatComposerDrawer toggle when the drawer is collapsed. `{label}` is the drawer's visible name — example: `Expand Attachments`. Pairs with `collapse`."

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ and cancels the touch's moves for its whole length if so. A second finger is
334334
never cancelled, so pinch-zoom keeps working. With no hold, the listener is
335335
gone and every touch scrolls as without the system.
336336

337+
A gesture that takes over an edge of the page itself (a pull to refresh on
338+
the document) changes the page's `overscroll-behavior-y` (the root's and
339+
the body's) through `claimRootScrollStyle` (`../hooks/rootScrollStyles.ts`),
340+
never by writing the style: each property keeps a claim stack, so a scroll
341+
lock and a pull compose. `isScrollLockActive` and `subscribeScrollLock` tell
342+
such a gesture to stand down while a lock holds the page.
343+
337344
### Multiple fingers
338345

339346
A second finger landing anywhere (often on a plain row no recognizer is on)

‎packages/core/src/gestures/experimental.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const NAMES = [
3434
'useTouchPresentation',
3535
'readTouchPresentation',
3636
'playHaptic',
37+
'claimRootScrollStyle',
38+
'isScrollLockActive',
39+
'subscribeScrollLock',
3740
] as const;
3841

3942
describe('@astryxdesign/core/gestures/experimental', () => {

‎packages/core/src/gestures/experimental.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,10 @@ export type {TouchPresentation} from '../utils/touchPresentation';
8989

9090
export {playHaptic} from './haptics';
9191
export type {HapticFeedbackKind} from '../motion/uikit';
92+
93+
export {
94+
claimRootScrollStyle,
95+
isScrollLockActive,
96+
subscribeScrollLock,
97+
} from '../hooks/rootScrollStyles';
98+
export type {RootScrollStyle} from '../hooks/rootScrollStyles';

‎packages/core/src/hooks/rootScrollStyles.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616
* page's top-edge bounce off) claims the root's styles here, so no caller
1717
* overwrites a value another one still needs.
1818
*
19+
* Lab components reach claimRootScrollStyle, isScrollLockActive and
20+
* subscribeScrollLock through `@astryxdesign/core/gestures/experimental`.
21+
*
1922
* SYNC: When modified, update:
2023
* - /packages/core/src/hooks/useScrollLock.ts
24+
* - /packages/core/src/gestures/experimental.ts (exports it)
2125
*/
2226

23-
/** The root scroll styles a caller can hold. */
27+
/**
28+
* The root scroll styles a caller can hold.
29+
* @experimental
30+
*/
2431
export interface RootScrollStyle {
2532
/** `hidden` or `clip` stops wheel, keyboard and scrollbar scrolling of the page. */
2633
overflow?: 'hidden' | 'clip';
@@ -163,6 +170,7 @@ function applyTopClaim(property: RootScrollProperty): void {
163170
* // later
164171
* release();
165172
* ```
173+
* @experimental
166174
*/
167175
export function claimRootScrollStyle(claim: RootScrollStyle): () => void {
168176
if (typeof document === 'undefined') {
@@ -213,7 +221,10 @@ export function claimRootScrollStyle(claim: RootScrollStyle): () => void {
213221
let lockCount = 0;
214222
const lockListeners = new Set<(isActive: boolean) => void>();
215223

216-
/** Whether a scroll lock (useScrollLock) currently holds the page. */
224+
/**
225+
* Whether a scroll lock (useScrollLock) currently holds the page.
226+
* @experimental
227+
*/
217228
export function isScrollLockActive(): boolean {
218229
return lockCount > 0;
219230
}
@@ -222,6 +233,7 @@ export function isScrollLockActive(): boolean {
222233
* Calls `listener` with the new state each time the page becomes locked or
223234
* free; a second lock taken while one holds is not a change. Returns the
224235
* unsubscribe function.
236+
* @experimental
225237
*/
226238
export function subscribeScrollLock(
227239
listener: (isActive: boolean) => void,
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file RefreshPullRecognizer.ts
5+
* @input Touches from the gesture arena
6+
* (@astryxdesign/core/gestures/experimental)
7+
* @output Exports RefreshPullRecognizer: the downward pan that reads a pull
8+
* to refresh without taking the touch from the browser
9+
* @position The gesture of RefreshControl, on the foundation's
10+
* PanGestureRecognizer
11+
*
12+
* UIKit's refresh pull is the scroll view's own drag, not a second gesture:
13+
* pulling past the top reveals the control, and pushing back keeps scrolling
14+
* the same content in the same gesture. So this pan never claims its touch.
15+
* The browser keeps the finger for native scrolling, and the pan reads the
16+
* finger to draw the overscroll (the scroll owner's `overscroll-behavior-y`
17+
* is `none` near the top, so the browser itself has nothing to draw there):
18+
*
19+
* - it begins once the finger pulls down past the pan's hysteresis while
20+
* nothing under it can scroll that way (`scrollEdge`), or, when the drag
21+
* began as a scroll, once the content runs out (`scrollHandoff`);
22+
* - when the browser takes the touch to scroll, a pull already under way
23+
* keeps reading it instead of being cancelled;
24+
* - one finger: a second finger is the browser's (pinch-zoom).
25+
*
26+
* `onTouchDown` reports a first finger landing, so a settle in flight can
27+
* stop where it is (UIScrollView brakes on touch-down).
28+
*
29+
* SYNC: When modified, update:
30+
* - /packages/lab/src/RefreshControl/useRefreshControl.ts
31+
*/
32+
33+
import {
34+
PanGestureRecognizer,
35+
type GestureEventContext,
36+
type GestureTouch,
37+
type PanGestureRecognizerOptions,
38+
} from '@astryxdesign/core/gestures/experimental';
39+
40+
export class RefreshPullRecognizer extends PanGestureRecognizer {
41+
/** Called when the first finger of a gesture lands, with its timestamp. */
42+
onTouchDown: ((time: number) => void) | null = null;
43+
44+
constructor(options: PanGestureRecognizerOptions = {}) {
45+
super({
46+
name: 'RefreshPull',
47+
direction: 'down',
48+
scrollEdge: true,
49+
scrollHandoff: true,
50+
maximumNumberOfTouches: 1,
51+
...options,
52+
});
53+
}
54+
55+
/** Native scrolling keeps the finger: the pull only reads it. */
56+
override get claimsTouches(): boolean {
57+
return false;
58+
}
59+
60+
protected override touchesBegan(
61+
touches: ReadonlyArray<GestureTouch>,
62+
event: GestureEventContext,
63+
): void {
64+
if (this.numberOfTouches === touches.length) {
65+
this.onTouchDown?.(event.time);
66+
}
67+
super.touchesBegan(touches, event);
68+
}
69+
70+
protected override touchesTakenByBrowser(
71+
touches: ReadonlyArray<GestureTouch>,
72+
event: GestureEventContext,
73+
): boolean {
74+
if (this.state === 'began' || this.state === 'changed') {
75+
return true;
76+
}
77+
return super.touchesTakenByBrowser(touches, event);
78+
}
79+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file refreshIndicator.ts
5+
* @output The names the refresh control's touch layer writes and its
6+
* indicator's styles read
7+
* @position Internal to RefreshControl, shared by refreshTouchController.ts and
8+
* the indicator (RefreshControl.tsx, the drawing)
9+
*/
10+
11+
/** CSS custom property the indicator reads: how much of it is revealed (0–1). */
12+
export const REFRESH_REVEAL_VAR = '--astryx-refresh-reveal';
13+
/** CSS custom property the spokes read: the alpha step while spinning. */
14+
export const REFRESH_SPIN_STEP_VAR = '--astryx-refresh-spin-step';
15+
/** Attribute on the indicator while it spins. */
16+
export const REFRESH_SPINNING_ATTRIBUTE = 'data-refresh-spinning';

0 commit comments

Comments
 (0)