-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathindex.tsx
50 lines (44 loc) · 1.86 KB
/
index.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
import { useEffect, type PropsWithChildren } from 'react';
import { ContentOverlayController } from './content-overlay-controller';
import { useSyncOverlayStore } from './use-sync-overlay-store';
import { createOverlay } from '../../event';
import { createOverlaySafeContext } from '../context';
import { type OverlayStore } from '../store';
export function createOverlayProvider(overlayStore: OverlayStore) {
const overlay = createOverlay(overlayStore);
const { OverlayContextProvider, useCurrentOverlay, useOverlayData } = createOverlaySafeContext();
function OverlayProvider({ children }: PropsWithChildren) {
const overlayState = useSyncOverlayStore(overlayStore);
useEffect(() => {
return () => {
overlayStore.dispatchOverlay({ type: 'REMOVE_ALL' });
};
}, []);
return (
<OverlayContextProvider value={overlayState}>
{children}
{overlayState.overlayOrderList.map((item) => {
const { id: currentOverlayId, isOpen, controller: currentController } = overlayState.overlayData[item];
return (
<ContentOverlayController
key={currentOverlayId}
isOpen={isOpen}
current={overlayState.current}
overlayId={currentOverlayId}
onMounted={() => {
requestAnimationFrame(() => {
overlayStore.dispatchOverlay({ type: 'OPEN', overlayId: currentOverlayId });
});
}}
context={overlayState.overlayData[currentOverlayId].context}
onCloseModal={() => overlay.close(currentOverlayId)}
onExitModal={() => overlay.unmount(currentOverlayId)}
controller={currentController}
/>
);
})}
</OverlayContextProvider>
);
}
return { overlay, OverlayProvider, useCurrentOverlay, useOverlayData };
}