-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBottomSheet.tsx
More file actions
572 lines (534 loc) · 17.4 KB
/
Copy pathBottomSheet.tsx
File metadata and controls
572 lines (534 loc) · 17.4 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Copyright (c) Meta Platforms, Inc. and affiliates.
'use client';
/**
* @file BottomSheet.tsx
* @input Uses React, StyleX, core hooks/utils, BottomSheetPanel, BottomSheetSwitcherContext
* @output Exports BottomSheet component and BottomSheetProps
* @position Public BottomSheet router plus private standalone/switcher hosts
*
* BottomSheet selects one of two focused hosts. A standalone host owns its
* native dialog lifecycle; a switcher item participates in the parent's shared
* dialog and transition state machine. Both render the same BottomSheetPanel,
* which owns sheet presentation, gestures, mobile-keyboard accommodation, and
* motion completion.
*
* SYNC: When modified, update these files to stay in sync:
* - /packages/core/src/BottomSheet/BottomSheetPanel.tsx
* - /packages/core/src/BottomSheet/BottomSheet.doc.mjs
* - /packages/core/src/BottomSheet/BottomSheet.test.tsx
* - /packages/core/src/BottomSheet/BottomSheetSwitcher.tsx
* - /packages/core/src/BottomSheet/BottomSheetSwitcher.test.tsx
* - /apps/storybook/stories/BottomSheet.stories.tsx
* - /packages/cli/assets/templates/blocks/components/BottomSheet/BottomSheetShowcase.tsx
*/
import {
use,
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
type ReactNode,
} from 'react';
import * as stylex from '@stylexjs/stylex';
import type {BaseProps} from '../BaseProps';
import type {DialogPurpose} from '../Dialog';
import {colorVars, durationVars, easeVars} from '../theme/tokens.stylex';
import {isImeKeyEvent, useDevWarning, useScrollLock} from '../hooks';
import {
BottomSheetPanel,
type BottomSheetPanelMotion,
type BottomSheetPanelState,
} from './BottomSheetPanel';
import {
BottomSheetSwitcherContext,
type BottomSheetSwitcherContextValue,
type BottomSheetSwitcherPhase,
} from './BottomSheetSwitcherContext';
export type {BottomSheetHeight, BottomSheetSnapPoint} from './BottomSheetPanel';
import type {BottomSheetHeight, BottomSheetSnapPoint} from './BottomSheetPanel';
const styles = stylex.create({
dialog: {
position: 'fixed',
inset: 0,
width: '100dvw',
height: '100dvh',
maxWidth: 'none',
maxHeight: 'none',
margin: 0,
padding: 0,
border: 'none',
backgroundColor: 'transparent',
overflow: 'visible',
display: 'none',
outline: 'none',
},
dialogOpen: {
display: 'block',
},
dialogNonModal: {
pointerEvents: 'none',
zIndex: 1000,
width: '100%',
height: '100%',
},
scrim: {
'::backdrop': {
backgroundColor: colorVars['--color-overlay'],
opacity: {
default: 'var(--_sheet-scrim-opacity, 1)',
'@starting-style': 0,
},
transitionProperty: 'opacity, display',
transitionDuration: durationVars['--duration-medium'],
transitionTimingFunction: easeVars['--ease-standard'],
transitionBehavior: 'allow-discrete',
'@media (prefers-reduced-motion: reduce)': {
transitionDuration: '0.01s',
},
},
},
positioner: {
position: 'absolute',
insetInline: 0,
insetBlockEnd: 0,
display: 'flex',
justifyContent: 'center',
pointerEvents: 'none',
},
positionerHidden: {
display: 'none',
},
positionerTop: {
zIndex: 1,
},
});
interface BottomSheetSharedProps extends BaseProps<HTMLDivElement> {
/** Ref forwarded to the visual sheet panel <div>. */
ref?: React.Ref<HTMLDivElement>;
/** Accessible label for the sheet. */
label: string;
/** Sheet content, rendered below the grab handle in a scrollable area. */
children: ReactNode;
/** Height budget or custom CSS length. Only fully expanded Tall is keyboard-aware. @default 'capped' */
height?: BottomSheetHeight | number | string;
/**
* Extra heights the sheet can rest at when dragged; its own height is always
* the tallest stop, and omitting this gives a sheet that only opens and
* closes. Each stop is the sheet's visible height: a number is a viewport
* fraction (`0.5` is half the screen), `'50%'` the same in CSS, `'320px'` an
* absolute length. A stop of a quarter of the sheet or less is a peek — it
* slides away instead of reflowing, and thins the scrim.
*/
snapPoints?: ReadonlyArray<BottomSheetSnapPoint>;
/**
* Configures implicit dismissal behavior, matching Dialog.
* - required: Blocks swipe, scrim click, and Escape
* - form: Blocks swipe and scrim click, allows Escape
* - info: Allows swipe, scrim click, and Escape
* @default 'info'
*/
purpose?: DialogPurpose;
}
interface StandaloneBottomSheetProps extends BottomSheetSharedProps {
isOpen: boolean;
onOpenChange: (isOpen: boolean) => void;
hasScrim?: boolean;
sheetId?: never;
}
interface SwitcherBottomSheetProps extends BottomSheetSharedProps {
sheetId: string;
isOpen?: never;
onOpenChange?: never;
hasScrim?: never;
}
export type BottomSheetProps =
StandaloneBottomSheetProps | SwitcherBottomSheetProps;
function panelStateForSwitcherPhase(
phase: BottomSheetSwitcherPhase,
alignmentOffset: number,
): BottomSheetPanelState {
switch (phase) {
case 'active':
return {kind: 'open', entering: false};
case 'entering':
return {kind: 'open', entering: true};
case 'covered':
case 'aligning':
case 'fading':
return {kind: 'retained', motion: phase, alignmentOffset};
case 'exiting':
return {kind: 'exiting'};
case 'hidden':
return {kind: 'hidden'};
}
}
// preventScroll on both: presenting a sheet must not scroll the page to reveal
// what it just focused. For an autofocused field that reveal is the mobile
// keyboard's, and it moves the document under a fixed sheet; useMobileKeyboard
// brings the field into view within the sheet instead.
function focusPanel(panel: HTMLElement | null, isModal: boolean): void {
const activeElement = document.activeElement;
if (activeElement != null && panel?.contains(activeElement)) {
return;
}
const autofocus = panel?.querySelector<HTMLElement>('[data-autofocus]');
if (autofocus != null) {
autofocus.focus({preventScroll: true});
} else if (isModal) {
panel?.focus({preventScroll: true});
}
}
function StandaloneBottomSheet({
ref,
isOpen,
onOpenChange,
label,
children,
height = 'capped',
snapPoints,
hasScrim = true,
purpose = 'info',
xstyle,
...props
}: StandaloneBottomSheetProps) {
const dialogRef = useRef<HTMLDialogElement | null>(null);
const panelRef = useRef<HTMLDivElement | null>(null);
const triggerRef = useRef<HTMLElement | null>(null);
const [isPresented, setIsPresented] = useState(isOpen);
const shouldPresent = isOpen || isPresented;
const panelState: BottomSheetPanelState = isOpen
? {kind: 'open', entering: false}
: isPresented
? {kind: 'exiting'}
: {kind: 'hidden'};
const dismissOnEscape = useCallback(() => {
if (purpose !== 'required') {
onOpenChange(false);
}
}, [onOpenChange, purpose]);
const dismissOnLightInteraction = useCallback(() => {
if (purpose === 'info') {
onOpenChange(false);
}
}, [onOpenChange, purpose]);
const handlePanelElementChange = useCallback(
(element: HTMLDivElement | null) => {
panelRef.current = element;
},
[],
);
const handleScrimOpacity = useCallback((opacity: number) => {
dialogRef.current?.style.setProperty(
'--_sheet-scrim-opacity',
String(opacity),
);
}, []);
useEffect(() => {
const dialog = dialogRef.current;
if (dialog == null || !isOpen) {
return;
}
// The controlled prop opens an already-mounted dialog; presentation state
// must latch here so a later close can keep it mounted through its exit.
// eslint-disable-next-line @eslint-react/set-state-in-effect -- latches controlled open state for exit animation
setIsPresented(true);
dialog.style.setProperty('--_sheet-scrim-opacity', '1');
const wasOpen = dialog.open;
if (!wasOpen) {
if (hasScrim) {
triggerRef.current = document.activeElement as HTMLElement | null;
dialog.showModal();
} else {
dialog.show();
}
focusPanel(panelRef.current, hasScrim);
}
}, [hasScrim, isOpen]);
useEffect(() => {
if (!isOpen && isPresented && hasScrim) {
handleScrimOpacity(0);
}
}, [handleScrimOpacity, hasScrim, isOpen, isPresented]);
const handleMotionComplete = useCallback(
(motion: BottomSheetPanelMotion) => {
if (motion !== 'exiting' || isOpen) {
return;
}
const dialog = dialogRef.current;
if (dialog?.open) {
dialog.close();
}
setIsPresented(false);
triggerRef.current?.focus();
triggerRef.current = null;
},
[isOpen],
);
useScrollLock(shouldPresent && hasScrim);
useDevWarning(
'BottomSheet',
'requires a non-empty `label` for an accessible name; the open sheet ' +
'has no built-in heading to derive one from.',
isOpen && !label,
);
const handleCancel = useCallback(
(event: React.SyntheticEvent<HTMLDialogElement>) => {
// No IME guard here: `cancel` is a plain Event carrying no composition
// state, and handleKeyDown claims a composing Escape before the browser
// can raise the close request that would arrive here.
event.preventDefault();
dismissOnEscape();
},
[dismissOnEscape],
);
const handleKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLDialogElement>) => {
if (event.key !== 'Escape') {
return;
}
// Claim the key before reading it: an unclaimed Escape lets the browser
// raise its own close request, which lands on handleCancel and dismisses
// on the same keypress.
event.preventDefault();
// An IME fires this keydown to cancel an in-progress composition, ahead
// of compositionend. It is a composition cancel, not a dismissal command
// — see utils/ime; Dialog and BottomSheetSwitcher guard the same way.
if (isImeKeyEvent(event.nativeEvent)) {
return;
}
dismissOnEscape();
},
[dismissOnEscape],
);
const handleClick = useCallback(
(event: React.MouseEvent<HTMLDialogElement>) => {
if (hasScrim && event.target === event.currentTarget) {
dismissOnLightInteraction();
}
},
[dismissOnLightInteraction, hasScrim],
);
return (
<dialog
{...stylex.props(
styles.dialog,
shouldPresent && styles.dialogOpen,
hasScrim && styles.scrim,
!hasScrim && styles.dialogNonModal,
)}
ref={dialogRef}
aria-label={label}
aria-hidden={!isOpen && isPresented ? 'true' : undefined}
aria-modal={hasScrim && isOpen ? 'true' : undefined}
role={purpose === 'required' ? 'alertdialog' : undefined}
inert={!isOpen && isPresented ? true : undefined}
onCancel={handleCancel}
onClick={handleClick}
onKeyDown={handleKeyDown}>
<div {...stylex.props(styles.positioner)}>
<BottomSheetPanel
{...props}
ref={ref}
state={panelState}
height={height}
snapPoints={snapPoints}
isSwipeDismissAllowed={purpose === 'info'}
isPageScrollLocked={shouldPresent && hasScrim}
xstyle={xstyle}
onDismiss={dismissOnLightInteraction}
onScrimOpacity={handleScrimOpacity}
onElementChange={handlePanelElementChange}
onMotionComplete={handleMotionComplete}>
{children}
</BottomSheetPanel>
</div>
</dialog>
);
}
interface SwitcherBottomSheetItemProps extends SwitcherBottomSheetProps {
switcher: BottomSheetSwitcherContextValue;
}
function SwitcherBottomSheetItem({
switcher,
ref,
sheetId,
label,
children,
height = 'capped',
snapPoints,
purpose = 'info',
xstyle,
...props
}: SwitcherBottomSheetItemProps) {
const {
activeSheet,
hasScrim,
onActiveSheetChange,
getSheetPhase,
getSheetAlignmentOffset,
registerSheetElement,
registerSheetLabel,
registerSheetPurpose,
onSheetEnterStart,
onSheetTransitionComplete,
onSheetScrimOpacityChange,
} = switcher;
const hasValidSheetId = typeof sheetId === 'string' && sheetId.length > 0;
const phase = hasValidSheetId ? getSheetPhase(sheetId) : 'hidden';
const alignmentOffset = hasValidSheetId
? getSheetAlignmentOffset(sheetId)
: 0;
const panelState = panelStateForSwitcherPhase(phase, alignmentOffset);
const isInteractive = phase === 'active' || phase === 'entering';
const isInactive =
phase === 'covered' ||
phase === 'aligning' ||
phase === 'fading' ||
phase === 'exiting';
const isPresented = phase !== 'hidden';
const isTopSheet = phase === 'active' || phase === 'entering';
const panelRef = useRef<HTMLDivElement | null>(null);
const previousPhaseRef = useRef(phase);
const previousPhase = previousPhaseRef.current;
const hasPresentedRef = useRef(false);
useLayoutEffect(() => {
previousPhaseRef.current = phase;
}, [phase]);
const dismissOnSwipe = useCallback(() => {
if (purpose === 'info' && hasValidSheetId && activeSheet === sheetId) {
onActiveSheetChange(null);
}
}, [activeSheet, hasValidSheetId, onActiveSheetChange, purpose, sheetId]);
const handlePanelElementChange = useCallback(
(element: HTMLDivElement | null) => {
panelRef.current = element;
if (hasValidSheetId) {
registerSheetElement(sheetId, element);
}
},
[hasValidSheetId, registerSheetElement, sheetId],
);
const handleMotionStart = useCallback(
(motion: BottomSheetPanelMotion) => {
if (motion === 'entering' && hasValidSheetId) {
onSheetEnterStart(sheetId);
}
},
[hasValidSheetId, onSheetEnterStart, sheetId],
);
const handleMotionComplete = useCallback(
(motion: BottomSheetPanelMotion) => {
if (hasValidSheetId) {
onSheetTransitionComplete({sheetId, phase: motion});
}
},
[hasValidSheetId, onSheetTransitionComplete, sheetId],
);
const handleScrimOpacity = useCallback(
(opacity: number) => {
if (hasValidSheetId) {
onSheetScrimOpacityChange(sheetId, opacity);
}
},
[hasValidSheetId, onSheetScrimOpacityChange, sheetId],
);
useLayoutEffect(() => {
if (!hasValidSheetId) {
return;
}
registerSheetLabel(sheetId, label);
return () => registerSheetLabel(sheetId, null);
}, [hasValidSheetId, label, registerSheetLabel, sheetId]);
useLayoutEffect(() => {
if (!hasValidSheetId) {
return;
}
registerSheetPurpose(sheetId, purpose);
return () => registerSheetPurpose(sheetId, null);
}, [hasValidSheetId, purpose, registerSheetPurpose, sheetId]);
useEffect(() => {
if (isInteractive) {
const wasInteractive =
previousPhase === 'active' || previousPhase === 'entering';
if (!hasPresentedRef.current || !wasInteractive) {
focusPanel(panelRef.current, hasScrim);
}
hasPresentedRef.current = true;
} else if (phase === 'hidden') {
hasPresentedRef.current = false;
}
}, [hasScrim, isInteractive, phase, previousPhase]);
useDevWarning(
'BottomSheet',
'requires a non-empty `label` for an accessible name; the open sheet ' +
'has no built-in heading to derive one from.',
isInteractive && !label,
);
return (
<div
{...stylex.props(
styles.positioner,
!isPresented && styles.positionerHidden,
isTopSheet && styles.positionerTop,
)}
hidden={!isPresented}
aria-hidden={isInactive ? 'true' : undefined}
inert={isInactive ? true : undefined}>
<BottomSheetPanel
{...props}
ref={ref}
state={panelState}
height={height}
snapPoints={snapPoints}
isSwipeDismissAllowed={purpose === 'info'}
isPageScrollLocked={hasScrim}
xstyle={xstyle}
onDismiss={dismissOnSwipe}
onScrimOpacity={handleScrimOpacity}
onElementChange={handlePanelElementChange}
onMotionStart={handleMotionStart}
onMotionComplete={handleMotionComplete}>
{/* A switcher item consumes its parent controller. Its content starts a
fresh ownership scope so a nested BottomSheet is standalone unless
it establishes a nested BottomSheetSwitcher of its own. */}
<BottomSheetSwitcherContext value={null}>
{children}
</BottomSheetSwitcherContext>
</BottomSheetPanel>
</div>
);
}
/**
* A mobile touch sheet that either owns a native dialog or participates in a
* BottomSheetSwitcher shared dialog when given a sheetId inside that context.
*/
export function BottomSheet(props: BottomSheetProps) {
const switcher = use(BottomSheetSwitcherContext);
const runtimeSheetId = (props as {sheetId?: string}).sheetId;
const hasValidSheetId =
typeof runtimeSheetId === 'string' && runtimeSheetId.length > 0;
useDevWarning(
'BottomSheet',
'requires a non-empty `sheetId` when nested in ' +
'BottomSheetSwitcher; standalone `isOpen` / `onOpenChange` props are ' +
'ignored there.',
switcher != null && !hasValidSheetId,
);
useDevWarning(
'BottomSheet',
'`sheetId` only works inside BottomSheetSwitcher. Use `isOpen` and ' +
'`onOpenChange` for a standalone sheet.',
switcher == null && runtimeSheetId != null,
);
if (switcher != null) {
return (
<SwitcherBottomSheetItem
{...(props as SwitcherBottomSheetProps)}
switcher={switcher}
/>
);
}
return <StandaloneBottomSheet {...(props as StandaloneBottomSheetProps)} />;
}
BottomSheet.displayName = 'BottomSheet';