@@ -8,12 +8,12 @@ import React, {
8
8
useRef ,
9
9
useState ,
10
10
} from 'react'
11
- import { createPortal } from 'react-dom'
12
11
import PropTypes from 'prop-types'
13
12
import classNames from 'classnames'
14
13
import { Transition } from 'react-transition-group'
15
14
16
15
import { CBackdrop } from '../backdrop/CBackdrop'
16
+ import { CConditionalPortal } from '../conditional-portal'
17
17
import { CModalContent } from './CModalContent'
18
18
import { CModalDialog } from './CModalDialog'
19
19
@@ -119,22 +119,22 @@ export const CModal = forwardRef<HTMLDivElement, CModalProps>(
119
119
const [ _visible , setVisible ] = useState ( visible )
120
120
const [ staticBackdrop , setStaticBackdrop ] = useState ( false )
121
121
122
- useEffect ( ( ) => {
123
- setVisible ( visible )
124
- } , [ visible ] )
125
-
126
122
const contextValues = {
127
123
visible : _visible ,
128
124
setVisible,
129
125
}
130
126
131
127
useEffect ( ( ) => {
132
- modalRef . current && modalRef . current . addEventListener ( 'click' , handleClickOutside )
133
- modalRef . current && modalRef . current . addEventListener ( 'keyup' , handleKeyDown )
128
+ setVisible ( visible )
129
+ } , [ visible ] )
130
+
131
+ useEffect ( ( ) => {
132
+ document . addEventListener ( 'click' , handleClickOutside )
133
+ document . addEventListener ( 'keydown' , handleKeyDown )
134
134
135
135
return ( ) => {
136
- modalRef . current && modalRef . current . removeEventListener ( 'click' , handleClickOutside )
137
- modalRef . current && modalRef . current . removeEventListener ( 'keyup ' , handleKeyDown )
136
+ document . removeEventListener ( 'click' , handleClickOutside )
137
+ document . removeEventListener ( 'keydown ' , handleKeyDown )
138
138
}
139
139
} , [ _visible ] )
140
140
@@ -195,49 +195,45 @@ export const CModal = forwardRef<HTMLDivElement, CModalProps>(
195
195
const handleKeyDown = useCallback (
196
196
( event : KeyboardEvent ) => {
197
197
if ( event . key === 'Escape' && keyboard ) {
198
- return handleDismiss ( )
198
+ handleDismiss ( )
199
199
}
200
200
} ,
201
201
[ modalRef , handleDismiss ] ,
202
202
)
203
203
204
- const modal = ( ref ?: React . Ref < HTMLDivElement > , state ?: string ) => {
205
- return (
206
- < CModalContext . Provider value = { contextValues } >
207
- < div
208
- className = { classNames (
209
- 'modal' ,
210
- {
211
- 'modal-static' : staticBackdrop ,
212
- fade : transition ,
213
- } ,
214
- state === 'entering'
215
- ? 'd-block'
216
- : state === 'entered'
217
- ? 'show d-block'
218
- : state === 'exiting'
219
- ? 'd-block'
220
- : '' ,
221
- className ,
222
- ) }
223
- tabIndex = { - 1 }
224
- role = "dialog"
225
- ref = { ref }
226
- >
227
- < CModalDialog
228
- alignment = { alignment }
229
- fullscreen = { fullscreen }
230
- scrollable = { scrollable }
231
- size = { size }
232
- >
233
- < CModalContent { ...rest } ref = { modalContentRef } >
234
- { children }
235
- </ CModalContent >
236
- </ CModalDialog >
237
- </ div >
238
- </ CModalContext . Provider >
239
- )
240
- }
204
+ // const Modal = ({ ref, state }: { ref?: React.Ref<HTMLDivElement>; state?: string }) => (
205
+ // <div
206
+ // className={classNames(
207
+ // 'modal',
208
+ // {
209
+ // 'modal-static': staticBackdrop,
210
+ // fade: transition,
211
+ // },
212
+ // state === 'entering'
213
+ // ? 'd-block'
214
+ // : state === 'entered'
215
+ // ? 'show d-block'
216
+ // : state === 'exiting'
217
+ // ? 'd-block'
218
+ // : '',
219
+ // className,
220
+ // )}
221
+ // tabIndex={-1}
222
+ // role="dialog"
223
+ // ref={ref}
224
+ // >
225
+ // <CModalDialog
226
+ // alignment={alignment}
227
+ // fullscreen={fullscreen}
228
+ // scrollable={scrollable}
229
+ // size={size}
230
+ // >
231
+ // <CModalContent {...rest} ref={modalContentRef}>
232
+ // {children}
233
+ // </CModalContent>
234
+ // </CModalDialog>
235
+ // </div>
236
+ // )
241
237
242
238
return (
243
239
< >
@@ -250,15 +246,49 @@ export const CModal = forwardRef<HTMLDivElement, CModalProps>(
250
246
unmountOnExit = { unmountOnClose }
251
247
timeout = { ! transition ? 0 : duration }
252
248
>
253
- { ( state ) => {
254
- return typeof window !== 'undefined' && portal
255
- ? createPortal ( modal ( forkedRef , state ) , document . body )
256
- : modal ( forkedRef , state )
257
- } }
249
+ { ( state ) => (
250
+ < CConditionalPortal portal = { portal } >
251
+ < CModalContext . Provider value = { contextValues } >
252
+ < div
253
+ className = { classNames (
254
+ 'modal' ,
255
+ {
256
+ 'modal-static' : staticBackdrop ,
257
+ fade : transition ,
258
+ } ,
259
+ state === 'entering'
260
+ ? 'd-block'
261
+ : state === 'entered'
262
+ ? 'show d-block'
263
+ : state === 'exiting'
264
+ ? 'd-block'
265
+ : '' ,
266
+ className ,
267
+ ) }
268
+ tabIndex = { - 1 }
269
+ role = "dialog"
270
+ ref = { forkedRef }
271
+ >
272
+ < CModalDialog
273
+ alignment = { alignment }
274
+ fullscreen = { fullscreen }
275
+ scrollable = { scrollable }
276
+ size = { size }
277
+ >
278
+ < CModalContent { ...rest } ref = { modalContentRef } >
279
+ { children }
280
+ </ CModalContent >
281
+ </ CModalDialog >
282
+ </ div >
283
+ </ CModalContext . Provider >
284
+ </ CConditionalPortal >
285
+ ) }
258
286
</ Transition >
259
- { typeof window !== 'undefined' && portal
260
- ? backdrop && createPortal ( < CBackdrop visible = { _visible } /> , document . body )
261
- : backdrop && < CBackdrop visible = { _visible } /> }
287
+ { backdrop && (
288
+ < CConditionalPortal portal = { portal } >
289
+ < CBackdrop visible = { _visible } />
290
+ </ CConditionalPortal >
291
+ ) }
262
292
</ >
263
293
)
264
294
} ,
0 commit comments