-
-
Notifications
You must be signed in to change notification settings - Fork 648
/
Copy pathSheet.tsx
342 lines (312 loc) · 11 KB
/
Sheet.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { useHotkey } from 'app/hotkeys/useHotkey';
import { t } from 'app/i18next-t';
import ItemPickerContainer from 'app/item-picker/ItemPickerContainer';
import { Portal } from 'app/utils/temp-container';
import SingleVendorSheetContainer from 'app/vendors/single-vendor/SingleVendorSheetContainer';
import clsx from 'clsx';
import {
PanInfo,
Spring,
motion,
useAnimation,
useDragControls,
useReducedMotion,
} from 'motion/react';
import React, {
createContext,
use,
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from 'react';
import { AppIcon, disabledIcon } from '../shell/icons';
import ErrorBoundary from './ErrorBoundary';
import { PressTipRoot } from './PressTip';
import styles from './Sheet.m.scss';
import { sheetsOpen } from './sheets-open';
import { useFixOverscrollBehavior } from './useFixOverscrollBehavior';
/**
* Propagates a function for setting a sheet to disabled. This forms a chain as
* sheets are shown, where each sheet is wired to its parent so that each child
* disables and re-enables its parent automatically.
*/
const SheetDisabledContext = createContext<(shown: boolean) => void>(() => {
// No-op
});
/**
* The contents of the header, footer, and body can be regular elements, or a function that
* takes an "onClose" function that can be used to close the sheet. Using onClose to close
* the sheet ensures that it will animate away rather than simply disappearing.
*/
export type SheetContent = React.ReactNode | ((args: { onClose: () => void }) => React.ReactNode);
// The sheet is dismissed if it's flicked at a velocity above dismissVelocity,
// or dragged down more than dismissAmount times the height of the sheet.
const dismissVelocity = 120; // px/ms
const dismissAmount = 0.5;
const spring: Spring = {
type: 'spring',
stiffness: 280,
damping: 20,
mass: 0.2,
} as const;
const reducedMotionTween = { type: 'tween', duration: 0.01 } as const;
const animationVariants = {
close: { y: window.innerHeight },
open: { y: 0 },
} as const;
const dragConstraints = { top: 0, bottom: window.innerHeight } as const;
const stopPropagation = (e: React.SyntheticEvent) => e.stopPropagation();
const handleKeyDown = (e: React.KeyboardEvent) => {
// Allow "esc" to propagate which lets you escape focus on inputs.
if (e.key !== 'Escape') {
e.stopPropagation();
}
};
/**
* Automatically disable the parent sheet while this sheet is shown. You must
* pass `setParentDisabled` to SheetDisabledContext.Provider.
*/
function useDisableParent(
forceDisabled?: boolean,
): [disabled: boolean, setParentDisabled: React.Dispatch<React.SetStateAction<boolean>>] {
const [disabledByChildSheet, setDisabledByChildSheet] = useState(false);
const setParentDisabled = use(SheetDisabledContext);
const effectivelyDisabled = forceDisabled || disabledByChildSheet;
useEffect(() => {
setParentDisabled(true);
return () => setParentDisabled(false);
}, [setParentDisabled]);
return [effectivelyDisabled, setDisabledByChildSheet];
}
/**
* A Sheet is a UI element that comes up from the bottom of the screen,
* and can be dragged downward to dismiss
*/
export default function Sheet({
header,
footer,
children,
sheetClassName,
closeButtonClassName,
headerClassName,
disabled: forceDisabled,
zIndex,
freezeInitialHeight,
allowClickThrough,
onClose,
}: {
/** A static, non-scrollable header shown in line with the close button. */
header?: SheetContent;
/** A static, non-scrollable footer shown at the bottom of the sheet. Good for buttons. */
footer?: SheetContent;
/** Scrollable contents for the sheet. */
children?: SheetContent;
/**
* Disable the sheet (no clicking, dragging, or close-on-esc). The sheet will
* automatically disable itself if another sheet is shown as a child, so no
* need to set this explicitly most of the time - pretty much just if you need
* to communicate that some "global" sheet like the item picker is up.
*/
disabled?: boolean;
// TODO: remove
/** Override the z-index of the sheet. Useful when stacking sheets on top of other sheets or on top of the item popup. */
zIndex?: number;
/** A custom class name to add to the sheet container. */
sheetClassName?: string;
/** A custom class name to add to the sheet close button. */
closeButtonClassName?: string;
/** A custom class name to add to the sheet header. */
headerClassName?: string;
// TODO: remove
/** If set, the sheet will always be whatever height it was when first rendered, even if the contents change size. */
freezeInitialHeight?: boolean;
// TODO: remove by getting a recursive item popup host
/**
* Allow clicks to escape this sheet. This allows for things like the popups
* in the Compare sheet being closed by clicking in the Compare sheet. By
* default we block clicks so that clicks in sheets spawned from within an
* item popup don't close the popup they were spawned from!
*/
allowClickThrough?: boolean;
onClose: () => void;
// TODO: "skinny" sheet option
}) {
const sheet = useRef<HTMLDivElement>(null);
const sheetContents = useRef<HTMLDivElement | null>(null);
const [frozenHeight, setFrozenHeight] = useState<number | undefined>(undefined);
const frozenHeightIntervalRef = useRef<NodeJS.Timeout | undefined>(undefined);
const [disabled, setParentDisabled] = useDisableParent(forceDisabled);
const reducedMotion = Boolean(useReducedMotion());
const animationControls = useAnimation();
const dragControls = useDragControls();
/**
* Triggering close starts the animation. The onClose prop is called by the callback
* passed to the onAnimationComplete motion prop.
*/
const triggerClose = useCallback(
(e?: React.MouseEvent | KeyboardEvent) => {
e?.preventDefault();
// Animate offscreen
animationControls.start('close');
},
[animationControls],
);
// Handle global escape key
useHotkey('esc', t('Hotkey.ClearDialog'), triggerClose);
// We need to call the onClose callback when then close animation is complete so that
// the calling component can unmount the sheet
// TODO (ryan/ben) move to using a container component and AnimatePresence
const handleAnimationComplete = useCallback(
(animationDefinition: 'close' | 'open') => {
if (animationDefinition === 'close') {
onClose();
}
},
[onClose],
);
// Determine when to drag. Drags if the touch falls in the header, or if the contents
// are scrolled all the way to the top.
const dragHandleDown = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
if (
!sheetContents.current!.contains(e.target as Node) ||
sheetContents.current!.scrollTop === 0
) {
dragControls.start(e);
}
},
[dragControls],
);
useFixOverscrollBehavior(sheetContents);
// When drag ends we determine if the sheet should be closed either via the final
// drag velocity or if the sheet has been dragged halfway the down from its height.
const handleDragEnd = useCallback(
(_event: TouchEvent | MouseEvent | PointerEvent, info: PanInfo) => {
if (
info.velocity.y > dismissVelocity ||
(sheet.current && info.offset.y > dismissAmount * sheet.current.clientHeight)
) {
triggerClose();
return;
}
animationControls.start('open');
},
[animationControls, triggerClose],
);
useLayoutEffect(() => {
clearInterval(frozenHeightIntervalRef.current);
if (freezeInitialHeight && sheetContents.current && !frozenHeight) {
if (sheetContents.current.clientHeight > 0) {
setFrozenHeight(sheetContents.current.clientHeight);
} else {
const setHeight = () => {
if (!sheetContents.current || sheetContents.current.clientHeight === 0) {
return false;
}
setFrozenHeight(sheetContents.current.clientHeight);
frozenHeightIntervalRef.current = undefined;
return true;
};
frozenHeightIntervalRef.current = tryRepeatedlyWithLimit(setHeight);
}
}
}, [freezeInitialHeight, frozenHeight]);
useEffect(() => {
animationControls.start('open');
}, [animationControls]);
// Track the total number of sheets that are open (to help prevent reloads while users are doing things)
useEffect(() => {
sheetsOpen.open++;
return () => {
sheetsOpen.open--;
};
}, []);
const sheetBody = (
<motion.div
// motion props
initial="close"
transition={reducedMotion ? reducedMotionTween : spring}
animate={animationControls}
variants={animationVariants}
onAnimationComplete={handleAnimationComplete}
drag="y"
dragControls={dragControls}
dragListener={false}
dragConstraints={dragConstraints}
dragElastic={0}
onDragEnd={handleDragEnd}
// regular props
style={{ zIndex }}
className={clsx(styles.sheet, sheetClassName, { [styles.sheetDisabled]: disabled })}
ref={sheet}
role="dialog"
aria-modal="false"
onKeyDown={handleKeyDown}
onKeyUp={stopPropagation}
onKeyPress={stopPropagation}
onClick={allowClickThrough ? undefined : stopPropagation}
>
<button
type="button"
className={clsx(styles.close, closeButtonClassName, { [styles.noHeader]: !header })}
onClick={triggerClose}
aria-keyshortcuts="esc"
aria-label={t('General.Close')}
>
<AppIcon icon={disabledIcon} />
</button>
<div className={styles.container} onPointerDown={dragHandleDown}>
{Boolean(header) && (
<div className={clsx(styles.header, headerClassName)}>
{typeof header === 'function' ? header({ onClose: triggerClose }) : header}
</div>
)}
<div
className={styles.contents}
style={frozenHeight ? { flexBasis: frozenHeight } : undefined}
ref={sheetContents}
>
<ErrorBoundary name="sheet-contents">
{typeof children === 'function' ? children({ onClose: triggerClose }) : children}
</ErrorBoundary>
</div>
{Boolean(footer) && (
<div className={styles.footer}>
{typeof footer === 'function' ? footer({ onClose: triggerClose }) : footer}
</div>
)}
</div>
<div
className={styles.disabledScreen}
onClick={stopPropagation}
onPointerDown={stopPropagation}
/>
</motion.div>
);
return (
<Portal>
<SheetDisabledContext value={setParentDisabled}>
<PressTipRoot value={sheet}>
<ItemPickerContainer>
<SingleVendorSheetContainer>{sheetBody}</SingleVendorSheetContainer>
</ItemPickerContainer>
</PressTipRoot>
</SheetDisabledContext>
</Portal>
);
}
function tryRepeatedlyWithLimit(callback: () => boolean, timeout = 500, limit = 5_000) {
let totalTime = 0;
return setInterval(() => {
if (totalTime > limit) {
return;
}
const res = callback();
totalTime += timeout;
if (res) {
return;
}
}, timeout);
}