11import * as React from 'react' ;
22import { createPortal } from 'react-dom' ;
3+ import canUseDom from 'rc-util/lib/Dom/canUseDom' ;
34import OrderContext from './Context' ;
45import useDom from './useDom' ;
56import useScrollLocker from './useScrollLocker' ;
7+ import { inlineMock } from './mock' ;
68
7- // ZombieJ: Since React 18 strict mode logic change.
8- // We should rewrite for compatible.
9+ export type ContainerType = Element | DocumentFragment ;
10+
11+ export type GetContainer = string | ContainerType | ( ( ) => ContainerType ) ;
912
1013export interface PortalProps {
1114 /** Customize container element. Default will create a div in document.body when `open` */
12- getContainer ?: ( ) => Element | DocumentFragment ;
15+ getContainer ?: GetContainer ;
1316 children ?: React . ReactNode ;
1417 /** Show the portal children */
1518 open ?: boolean ;
1619 /** Lock screen scroll when open */
1720 autoLock ?: boolean ;
1821}
1922
23+ const getPortalContainer = ( getContainer : GetContainer ) => {
24+ if ( ! canUseDom ( ) ) {
25+ return null ;
26+ }
27+
28+ if ( typeof getContainer === 'string' ) {
29+ return document . querySelector ( getContainer ) ;
30+ }
31+ if ( typeof getContainer === 'function' ) {
32+ return getContainer ( ) ;
33+ }
34+ return getContainer ;
35+ } ;
36+
2037export default function Portal ( props : PortalProps ) {
2138 const { open, autoLock, getContainer, children } = props ;
2239
@@ -30,7 +47,7 @@ export default function Portal(props: PortalProps) {
3047 } , [ open ] ) ;
3148
3249 // ======================== Container ========================
33- const customizeContainer = getContainer ?. ( ) ;
50+ const customizeContainer = getPortalContainer ( getContainer ) ;
3451
3552 const [ defaultContainer , queueCreate ] = useDom (
3653 mergedRender && ! customizeContainer ,
@@ -39,13 +56,14 @@ export default function Portal(props: PortalProps) {
3956
4057 // ========================= Render ==========================
4158 // Do not render when nothing need render
42- if ( ! mergedRender ) {
59+ if ( ! mergedRender || ! canUseDom ( ) ) {
4360 return null ;
4461 }
4562
63+ console . log ( inlineMock ( ) ) ;
4664 return (
4765 < OrderContext . Provider value = { queueCreate } >
48- { createPortal ( children , mergedContainer ) }
66+ { inlineMock ( ) ? children : createPortal ( children , mergedContainer ) }
4967 </ OrderContext . Provider >
5068 ) ;
5169}
0 commit comments