Skip to content

Commit 489c802

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 ce50d22 commit 489c802

10 files changed

Lines changed: 1591 additions & 2 deletions

File tree

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
@@ -276,6 +276,13 @@ scrollers under the finger. This is the sheet over its own list:
276276
touch and each scroller, scroll chaining stopped by `overscroll-behavior`,
277277
and RTL `scrollLeft` ranges.
278278

279+
A gesture that takes over an edge of the page itself (a pull to refresh on
280+
the document) changes the root's `overscroll-behavior-y` through
281+
`claimRootScrollStyle` (`../hooks/rootScrollStyles.ts`), never by writing
282+
the style: each property keeps a claim stack, so a scroll lock and a pull
283+
compose. `isScrollLockActive` and `subscribeScrollLock` tell such a gesture
284+
to stand down while a lock holds the page.
285+
279286
### Multiple fingers
280287

281288
A second finger never breaks a one-finger pan that is already moving: the

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ const NAMES = [
3737
'IOS_SUPPORTS_QUERY',
3838
'IOS_TOUCH_STYLE_KEYS',
3939
'playHaptic',
40+
'claimRootScrollStyle',
41+
'isScrollLockActive',
42+
'subscribeScrollLock',
4043
] as const;
4144

4245
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
@@ -92,3 +92,10 @@ export type {TouchIdiom, TouchPresentation} from '../utils/touchPresentation';
9292

9393
export {playHaptic} from './haptics';
9494
export type {HapticFeedbackKind} from '../motion/uikit';
95+
96+
export {
97+
claimRootScrollStyle,
98+
isScrollLockActive,
99+
subscribeScrollLock,
100+
} from '../hooks/rootScrollStyles';
101+
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
@@ -13,11 +13,18 @@
1313
* page's top-edge bounce off) claims the root's styles here, so no caller
1414
* overwrites a value another one still needs.
1515
*
16+
* Lab components reach claimRootScrollStyle, isScrollLockActive and
17+
* subscribeScrollLock through `@astryxdesign/core/gestures/experimental`.
18+
*
1619
* SYNC: When modified, update:
1720
* - /packages/core/src/hooks/useScrollLock.ts
21+
* - /packages/core/src/gestures/experimental.ts (exports it)
1822
*/
1923

20-
/** The root scroll styles a caller can hold. */
24+
/**
25+
* The root scroll styles a caller can hold.
26+
* @experimental
27+
*/
2128
export interface RootScrollStyle {
2229
/** `hidden` or `clip` stops wheel, keyboard and scrollbar scrolling of the page. */
2330
overflow?: 'hidden' | 'clip';
@@ -72,6 +79,7 @@ function applyTopClaim(property: RootScrollProperty): void {
7279
* // later
7380
* release();
7481
* ```
82+
* @experimental
7583
*/
7684
export function claimRootScrollStyle(claim: RootScrollStyle): () => void {
7785
if (typeof document === 'undefined') {
@@ -113,7 +121,10 @@ export function claimRootScrollStyle(claim: RootScrollStyle): () => void {
113121
let lockCount = 0;
114122
const lockListeners = new Set<(isActive: boolean) => void>();
115123

116-
/** Whether a scroll lock (useScrollLock) currently holds the page. */
124+
/**
125+
* Whether a scroll lock (useScrollLock) currently holds the page.
126+
* @experimental
127+
*/
117128
export function isScrollLockActive(): boolean {
118129
return lockCount > 0;
119130
}
@@ -122,6 +133,7 @@ export function isScrollLockActive(): boolean {
122133
* Calls `listener` with the new state each time the page becomes locked or
123134
* free; a second lock taken while one holds is not a change. Returns the
124135
* unsubscribe function.
136+
* @experimental
125137
*/
126138
export function subscribeScrollLock(
127139
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+
}

0 commit comments

Comments
 (0)