Skip to content

Commit ade2f5b

Browse files
committed
chore(lab): the pull-to-refresh story
A Storybook story shows useRefreshControl on a plain scroll container, with Core's Spinner as the app's indicator. The interaction spec opens it, and the story can hold a refresh until a test ends it.
1 parent d164db4 commit ade2f5b

1 file changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
import {useCallback, useState, type ReactNode} from 'react';
4+
import type {Meta, StoryObj} from '@storybook/react';
5+
import {REFRESH_REVEAL_VAR, useRefreshControl} from '@astryxdesign/lab';
6+
import {Button} from '@astryxdesign/core/Button';
7+
import {List, ListItem} from '@astryxdesign/core/List';
8+
import {Spinner} from '@astryxdesign/core/Spinner';
9+
10+
interface StoryArgs {
11+
/**
12+
* How long a refresh takes, in ms. Below 0, it lasts until the page
13+
* dispatches `refreshcontrol-story-end` on `window`, so a test ends it.
14+
*/
15+
refreshMs: number;
16+
/** Rows in the list. */
17+
rows: number;
18+
}
19+
20+
const meta: Meta<StoryArgs> = {
21+
title: 'Lab/Pull to refresh',
22+
parameters: {
23+
layout: 'fullscreen',
24+
},
25+
args: {
26+
refreshMs: 1500,
27+
rows: 40,
28+
},
29+
argTypes: {
30+
refreshMs: {control: {type: 'number', min: 0, step: 100}},
31+
rows: {control: {type: 'number', min: 1, step: 1}},
32+
},
33+
};
34+
35+
export default meta;
36+
37+
type Story = StoryObj<StoryArgs>;
38+
39+
const wait = (ms: number) =>
40+
new Promise<void>(resolve => {
41+
setTimeout(resolve, ms);
42+
});
43+
44+
/** One refresh: `ms`, or until the page ends it when `ms` is below 0. */
45+
function refreshFor(ms: number): Promise<void> {
46+
if (Number(ms) >= 0) {
47+
return wait(ms);
48+
}
49+
return new Promise<void>(resolve => {
50+
window.addEventListener('refreshcontrol-story-end', () => resolve(), {
51+
once: true,
52+
});
53+
});
54+
}
55+
56+
function Messages({rows, count}: {rows: number; count: number}) {
57+
return (
58+
<List header={`Inbox (refreshed ${count} times)`}>
59+
{Array.from({length: rows}, (_, index) => (
60+
<ListItem
61+
key={index}
62+
label={`Message ${index + 1}`}
63+
description={
64+
index === 0 ? `Refresh ${count}` : 'Pull down at the top to refresh'
65+
}
66+
/>
67+
))}
68+
</List>
69+
);
70+
}
71+
72+
/** The app's indicator shows as much of itself as the pull reveals. */
73+
const revealed = {opacity: `var(${REFRESH_REVEAL_VAR}, 0)`};
74+
75+
/**
76+
* What an app writes: its own scroll container, the content the hook pulls,
77+
* and its own indicator, here Core's Spinner. The indicator band sits at the
78+
* bottom of an overflow-hidden box on the content's top edge, after the
79+
* content.
80+
*/
81+
function PullToRefresh({
82+
onRefresh,
83+
isRefreshing,
84+
text,
85+
count,
86+
children,
87+
}: {
88+
onRefresh: () => unknown;
89+
isRefreshing?: boolean;
90+
text: string;
91+
count: number;
92+
children: ReactNode;
93+
}) {
94+
const control = useRefreshControl({
95+
onRefresh,
96+
isRefreshing,
97+
announcement: 'Refreshing',
98+
});
99+
return (
100+
<div
101+
data-refresh-scroller=""
102+
data-refresh-count={count}
103+
style={{height: '100%', overflowY: 'auto'}}>
104+
<div
105+
ref={control.contentRef}
106+
data-refresh-content=""
107+
aria-busy={control.isRefreshing || undefined}
108+
style={{position: 'relative'}}>
109+
{children}
110+
<div
111+
style={{
112+
position: 'absolute',
113+
insetInline: 0,
114+
bottom: '100%',
115+
height: '100lvh',
116+
overflow: 'hidden',
117+
pointerEvents: 'none',
118+
}}>
119+
<div
120+
ref={control.indicatorRef}
121+
aria-hidden
122+
style={{
123+
position: 'absolute',
124+
insetInline: 0,
125+
bottom: 0,
126+
height: 60,
127+
display: 'flex',
128+
alignItems: 'center',
129+
justifyContent: 'center',
130+
}}>
131+
<span style={revealed}>
132+
<Spinner size="lg" shade="subtle" label={text} />
133+
</span>
134+
</div>
135+
</div>
136+
</div>
137+
</div>
138+
);
139+
}
140+
141+
function useInbox(refreshMs: number) {
142+
const [count, setCount] = useState(0);
143+
const onRefresh = useCallback(async () => {
144+
await refreshFor(refreshMs);
145+
setCount(value => value + 1);
146+
}, [refreshMs]);
147+
return {count, onRefresh};
148+
}
149+
150+
/**
151+
* A list in the app's own scroll container. Pull it down past the top on a
152+
* touch screen, or with a trackpad's two-finger scroll. Past the trigger, the
153+
* refresh starts with the finger down.
154+
*/
155+
export const InAScrollContainer: Story = {
156+
render: function InAScrollContainerStory({refreshMs, rows}) {
157+
const {count, onRefresh} = useInbox(refreshMs);
158+
return (
159+
<div style={{height: '100dvh'}}>
160+
<PullToRefresh
161+
onRefresh={onRefresh}
162+
count={count}
163+
text={`Updated ${count} times`}>
164+
<Messages rows={rows} count={count} />
165+
</PullToRefresh>
166+
</div>
167+
);
168+
},
169+
};
170+
171+
/**
172+
* Controlled: the button refreshes too (the pull has no keyboard or
173+
* screen-reader equivalent), and the app sets `isRefreshing` itself.
174+
*/
175+
export const Controlled: Story = {
176+
render: function ControlledStory({refreshMs, rows}) {
177+
const [isRefreshing, setIsRefreshing] = useState(false);
178+
const [count, setCount] = useState(0);
179+
const refresh = useCallback(() => {
180+
setIsRefreshing(true);
181+
void refreshFor(refreshMs).then(() => {
182+
setCount(value => value + 1);
183+
setIsRefreshing(false);
184+
});
185+
}, [refreshMs]);
186+
return (
187+
<div style={{height: '100dvh', display: 'flex', flexDirection: 'column'}}>
188+
<div style={{padding: 8}}>
189+
<Button label="Refresh" onClick={refresh} isDisabled={isRefreshing} />
190+
</div>
191+
<div style={{flex: 1, minHeight: 0}}>
192+
<PullToRefresh
193+
onRefresh={refresh}
194+
isRefreshing={isRefreshing}
195+
count={count}
196+
text={`Updated ${count} times`}>
197+
<Messages rows={rows} count={count} />
198+
</PullToRefresh>
199+
</div>
200+
</div>
201+
);
202+
},
203+
};
204+
205+
/** A list shorter than the screen still pulls. */
206+
export const ShortList: Story = {
207+
args: {rows: 3},
208+
render: function ShortListStory({refreshMs, rows}) {
209+
const {count, onRefresh} = useInbox(refreshMs);
210+
return (
211+
<div style={{height: '100dvh'}}>
212+
<PullToRefresh
213+
onRefresh={onRefresh}
214+
count={count}
215+
text="Pull to refresh">
216+
<Messages rows={rows} count={count} />
217+
</PullToRefresh>
218+
</div>
219+
);
220+
},
221+
};

0 commit comments

Comments
 (0)