|
| 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 | + isAlwaysScrollable = false, |
| 87 | + children, |
| 88 | +}: { |
| 89 | + onRefresh: () => unknown; |
| 90 | + isRefreshing?: boolean; |
| 91 | + text: string; |
| 92 | + count: number; |
| 93 | + /** |
| 94 | + * Makes the content 1 px taller than the container. The pull applies only |
| 95 | + * in a container that overflows, so a short list pulls only with this. |
| 96 | + */ |
| 97 | + isAlwaysScrollable?: boolean; |
| 98 | + children: ReactNode; |
| 99 | +}) { |
| 100 | + const control = useRefreshControl({ |
| 101 | + onRefresh, |
| 102 | + isRefreshing, |
| 103 | + announce: 'Refreshing', |
| 104 | + }); |
| 105 | + return ( |
| 106 | + <div |
| 107 | + data-refresh-scroller="" |
| 108 | + data-refresh-count={count} |
| 109 | + style={{height: '100%', overflowY: 'auto'}}> |
| 110 | + <div |
| 111 | + ref={control.contentRef} |
| 112 | + data-refresh-content="" |
| 113 | + aria-busy={control.isRefreshing || undefined} |
| 114 | + style={{ |
| 115 | + position: 'relative', |
| 116 | + minHeight: isAlwaysScrollable ? 'calc(100% + 1px)' : undefined, |
| 117 | + }}> |
| 118 | + {children} |
| 119 | + <div |
| 120 | + style={{ |
| 121 | + position: 'absolute', |
| 122 | + insetInline: 0, |
| 123 | + bottom: '100%', |
| 124 | + height: '100lvh', |
| 125 | + overflow: 'hidden', |
| 126 | + pointerEvents: 'none', |
| 127 | + }}> |
| 128 | + <div |
| 129 | + ref={control.indicatorRef} |
| 130 | + aria-hidden |
| 131 | + style={{ |
| 132 | + position: 'absolute', |
| 133 | + insetInline: 0, |
| 134 | + bottom: 0, |
| 135 | + height: 60, |
| 136 | + display: 'flex', |
| 137 | + alignItems: 'center', |
| 138 | + justifyContent: 'center', |
| 139 | + }}> |
| 140 | + <span style={revealed}> |
| 141 | + <Spinner size="lg" shade="subtle" label={text} /> |
| 142 | + </span> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + ); |
| 148 | +} |
| 149 | + |
| 150 | +function useInbox(refreshMs: number) { |
| 151 | + const [count, setCount] = useState(0); |
| 152 | + const onRefresh = useCallback(async () => { |
| 153 | + await refreshFor(refreshMs); |
| 154 | + setCount(value => value + 1); |
| 155 | + }, [refreshMs]); |
| 156 | + return {count, onRefresh}; |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * A list in the app's own scroll container. Pull it down past the top on a |
| 161 | + * touch screen, or with a trackpad's two-finger scroll. Past the trigger, the |
| 162 | + * refresh starts with the finger down. |
| 163 | + */ |
| 164 | +export const InAScrollContainer: Story = { |
| 165 | + render: function InAScrollContainerStory({refreshMs, rows}) { |
| 166 | + const {count, onRefresh} = useInbox(refreshMs); |
| 167 | + return ( |
| 168 | + <div style={{height: '100dvh'}}> |
| 169 | + <PullToRefresh |
| 170 | + onRefresh={onRefresh} |
| 171 | + count={count} |
| 172 | + text={`Updated ${count} times`}> |
| 173 | + <Messages rows={rows} count={count} /> |
| 174 | + </PullToRefresh> |
| 175 | + </div> |
| 176 | + ); |
| 177 | + }, |
| 178 | +}; |
| 179 | + |
| 180 | +/** |
| 181 | + * Controlled: the button refreshes too (the pull has no keyboard or |
| 182 | + * screen-reader equivalent), and the app sets `isRefreshing` itself. |
| 183 | + */ |
| 184 | +export const Controlled: Story = { |
| 185 | + render: function ControlledStory({refreshMs, rows}) { |
| 186 | + const [isRefreshing, setIsRefreshing] = useState(false); |
| 187 | + const [count, setCount] = useState(0); |
| 188 | + const refresh = useCallback(() => { |
| 189 | + setIsRefreshing(true); |
| 190 | + void refreshFor(refreshMs).then(() => { |
| 191 | + setCount(value => value + 1); |
| 192 | + setIsRefreshing(false); |
| 193 | + }); |
| 194 | + }, [refreshMs]); |
| 195 | + return ( |
| 196 | + <div style={{height: '100dvh', display: 'flex', flexDirection: 'column'}}> |
| 197 | + <div style={{padding: 8}}> |
| 198 | + <Button label="Refresh" onClick={refresh} isDisabled={isRefreshing} /> |
| 199 | + </div> |
| 200 | + <div style={{flex: 1, minHeight: 0}}> |
| 201 | + <PullToRefresh |
| 202 | + onRefresh={refresh} |
| 203 | + isRefreshing={isRefreshing} |
| 204 | + count={count} |
| 205 | + text={`Updated ${count} times`}> |
| 206 | + <Messages rows={rows} count={count} /> |
| 207 | + </PullToRefresh> |
| 208 | + </div> |
| 209 | + </div> |
| 210 | + ); |
| 211 | + }, |
| 212 | +}; |
| 213 | + |
| 214 | +/** |
| 215 | + * A list shorter than the screen still pulls: the app makes its content 1 px |
| 216 | + * taller than the container, so the container overflows. |
| 217 | + */ |
| 218 | +export const ShortList: Story = { |
| 219 | + args: {rows: 3}, |
| 220 | + render: function ShortListStory({refreshMs, rows}) { |
| 221 | + const {count, onRefresh} = useInbox(refreshMs); |
| 222 | + return ( |
| 223 | + <div style={{height: '100dvh'}}> |
| 224 | + <PullToRefresh |
| 225 | + onRefresh={onRefresh} |
| 226 | + count={count} |
| 227 | + isAlwaysScrollable |
| 228 | + text="Pull to refresh"> |
| 229 | + <Messages rows={rows} count={count} /> |
| 230 | + </PullToRefresh> |
| 231 | + </div> |
| 232 | + ); |
| 233 | + }, |
| 234 | +}; |
0 commit comments