11import { useEffect } from 'react'
2+ import { createPortal } from 'react-dom'
23
34export default function Modal ( { children } : { children : React . ReactNode } ) {
45 const overlayStyle = {
56 top : '0' ,
67 left : '0' ,
7- zIndex : 21 ,
8+ zIndex : 80 ,
89 opacity : 0.5 ,
910 width : '100%' ,
1011 height : '100%' ,
11- position : 'absolute ' as 'absolute ' ,
12+ position : 'fixed ' as 'fixed ' ,
1213 backgroundColor : 'var(--ion-background-color)' ,
1314 }
1415
1516 const containerStyle = {
1617 top : '0' ,
1718 left : '0' ,
18- zIndex : 22 , // Higher than overlay
19+ zIndex : 90 ,
1920 width : '100%' ,
2021 height : '100%' ,
2122 display : 'flex' ,
2223 alignItems : 'center' ,
2324 justifyContent : 'center' ,
24- position : 'absolute ' as 'absolute ' ,
25- pointerEvents : 'none' as 'none' , // Allow clicks to pass through to children
25+ position : 'fixed ' as 'fixed ' ,
26+ pointerEvents : 'none' as 'none' ,
2627 }
2728
2829 const innerStyle = {
2930 opacity : 1 ,
3031 padding : '1rem' ,
3132 maxWidth : 'min(22rem, 90%)' ,
33+ maxHeight : 'calc(100vh - 4rem)' ,
34+ position : 'relative' as 'relative' ,
35+ display : 'flex' as 'flex' ,
36+ flexDirection : 'column' as 'column' ,
37+ overflowY : 'auto' as 'auto' ,
3238 borderRadius : '0.5rem' ,
3339 border : '1px solid var(--dark10)' ,
3440 backgroundColor : 'var(--ion-background-color)' ,
35- pointerEvents : 'auto' as 'auto' , // Enable clicks on the modal content
41+ pointerEvents : 'auto' as 'auto' ,
42+ zIndex : 95 ,
3643 }
3744
3845 useEffect ( ( ) => {
@@ -49,12 +56,15 @@ export default function Modal({ children }: { children: React.ReactNode }) {
4956 }
5057 } , [ ] )
5158
52- return (
59+ return createPortal (
5360 < >
5461 < div style = { overlayStyle } />
5562 < div style = { containerStyle } >
56- < div style = { innerStyle } > { children } </ div >
63+ < div style = { innerStyle } className = 'modal-inner' >
64+ { children }
65+ </ div >
5766 </ div >
58- </ >
67+ </ > ,
68+ document . body
5969 )
6070}
0 commit comments