1- import { createContext , useContext } from "react" ;
1+ import { createContext , ReactNode , useContext } from "react" ;
22import { ModalItemProvider } from "./item-context" ;
3- import { ModalManager , ModalProviderProps } from "./types" ;
3+ import {
4+ InternalModalInstance ,
5+ ModalInstance ,
6+ ModalManager ,
7+ ModalProviderProps ,
8+ } from "./types" ;
49import { useModalManager } from "./use-modal-manager" ;
510
611const ModalContext = createContext < ModalManager | null > ( null ) ;
@@ -27,26 +32,46 @@ function Modals({
2732 modal,
2833} : Omit < ModalProviderProps , "children" > ) {
2934 const modalManager = useModals ( ) ;
30- const { stack, isLoading } = modalManager ;
35+ const { stack, isLoading, createOpen } = modalManager ;
36+
37+ const renderModal = ( internalModal : InternalModalInstance , isNested : boolean = false ) : ReactNode => {
38+ const open = createOpen ( internalModal . id ) ;
39+
40+ const nestedElement = internalModal . nested
41+ ? renderModal ( internalModal . nested , true )
42+ : null ;
43+
44+ const modalInstance : ModalInstance = {
45+ ...internalModal ,
46+ nested : nestedElement ,
47+ open,
48+ isNested,
49+ } ;
50+
51+ return (
52+ < ModalItemProvider key = { modalInstance . id } modal = { modalInstance } >
53+ { modal ? (
54+ modal ( modalInstance , modalManager )
55+ ) : (
56+ < modalInstance . component
57+ { ...modalInstance . props }
58+ close = { modalInstance . close }
59+ isOpen = { modalInstance . isOpen }
60+ isNested = { isNested }
61+ id = { modalInstance . id }
62+ index = { modalInstance . index }
63+ open = { modalInstance . open }
64+ nested = { nestedElement }
65+ />
66+ ) }
67+ </ ModalItemProvider >
68+ ) ;
69+ } ;
3170
3271 return (
3372 < >
3473 { isLoading && loading && loading ( modalManager ) }
35- { stack . map ( ( modalInstance ) => (
36- < ModalItemProvider key = { modalInstance . id } modal = { modalInstance } >
37- { modal ? (
38- modal ( modalInstance , modalManager )
39- ) : (
40- < modalInstance . component
41- { ...modalInstance . props }
42- close = { modalInstance . close }
43- isOpen = { modalInstance . isOpen }
44- id = { modalInstance . id }
45- index = { modalInstance . index }
46- />
47- ) }
48- </ ModalItemProvider >
49- ) ) }
74+ { stack . map ( ( internalModal ) => renderModal ( internalModal ) ) }
5075 { ( isLoading || stack . length > 0 ) && backdrop && backdrop ( modalManager ) }
5176 </ >
5277 ) ;
0 commit comments