Repository navigation
Expand file tree
/
Copy pathModal.tsx
More file actions
115 lines (106 loc) · 3.67 KB
/
Copy pathModal.tsx
File metadata and controls
115 lines (106 loc) · 3.67 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
import { createContextAndHook, usePortalRoot, useSafeLayoutEffect } from '@clerk/shared/react';
import React, { useRef } from 'react';
import { descriptors, Flex } from '../customizables';
import { usePopover } from '../hooks';
import { useScrollLock } from '../hooks/useScrollLock';
import type { ThemableCssProp } from '../styledSystem';
import { animations, mqu } from '../styledSystem';
import { withFloatingTree } from './contexts';
import { Popover } from './Popover';
export const [ModalContext, _, useUnsafeModalContext] = createContextAndHook<{ toggle?: () => void }>('ModalContext');
type ModalProps = React.PropsWithChildren<{
id?: string;
handleOpen?: () => void;
handleClose?: () => void;
contentSx?: ThemableCssProp;
containerSx?: ThemableCssProp;
canCloseModal?: boolean;
style?: React.CSSProperties;
portalRoot?: HTMLElement | React.MutableRefObject<HTMLElement | null>;
initialFocusRef?: number | React.MutableRefObject<HTMLElement | null>;
}>;
export const Modal = withFloatingTree((props: ModalProps) => {
const { disableScrollLock, enableScrollLock } = useScrollLock();
const { handleClose, handleOpen, contentSx, containerSx, canCloseModal, id, style, portalRoot, initialFocusRef } =
props;
const portalRootFromContext = usePortalRoot();
const overlayRef = useRef<HTMLDivElement>(null);
const { floating, isOpen, context, nodeId, toggle } = usePopover({
defaultOpen: true,
autoUpdate: false,
outsidePress: e => e.target === overlayRef.current,
canCloseModal,
});
React.useEffect(() => {
if (!isOpen) {
handleClose?.();
} else {
handleOpen?.();
}
}, [isOpen]);
const modalCtx = React.useMemo(() => ({ value: canCloseModal === false ? {} : { toggle } }), [toggle, canCloseModal]);
useSafeLayoutEffect(() => {
enableScrollLock();
return () => {
disableScrollLock();
};
}, []);
const effectivePortalRoot = portalRoot ?? portalRootFromContext?.() ?? undefined;
return (
<Popover
nodeId={nodeId}
context={context}
isOpen={isOpen}
outsideElementsInert
root={effectivePortalRoot}
initialFocus={initialFocusRef}
>
<ModalContext.Provider value={modalCtx}>
<Flex
id={id}
ref={overlayRef}
elementDescriptor={descriptors.modalBackdrop}
style={style}
sx={[
t => ({
animation: `${animations.fadeIn} 150ms ${t.transitionTiming.$common}`,
zIndex: t.zIndices.$modal,
backgroundColor: t.colors.$colorModalBackdrop,
alignItems: 'flex-start',
justifyContent: 'center',
overflow: 'auto',
width: '100vw',
height: ['100vh', '-webkit-fill-available'],
position: 'fixed',
// eslint-disable-next-line custom-rules/no-physical-css-properties -- Full-viewport centering (used with right: 0)
left: 0,
top: 0,
}),
containerSx,
]}
>
<Flex
elementDescriptor={descriptors.modalContent}
ref={floating}
aria-modal='true'
role='dialog'
sx={[
t => ({
position: 'relative',
outline: 0,
animation: `${animations.modalSlideAndFade} 180ms ${t.transitionTiming.$easeOut}`,
margin: `${t.space.$16} 0`,
[mqu.sm]: {
margin: `${t.space.$10} 0`,
},
}),
contentSx,
]}
>
{props.children}
</Flex>
</Flex>
</ModalContext.Provider>
</Popover>
);
});