@@ -25,12 +25,10 @@ import type { Props } from '../../types'
25
25
import { env } from '../../utils/env'
26
26
import { forwardRefWithAs , useRender , type HasDisplayName , type RefProp } from '../../utils/render'
27
27
28
- function usePortalTarget ( ref : MutableRefObject < HTMLElement | null > ) : HTMLElement | null {
28
+ function usePortalTarget ( ownerDocument : Document | null ) : HTMLElement | null {
29
29
let forceInRoot = usePortalRoot ( )
30
30
let groupTarget = useContext ( PortalGroupContext )
31
31
32
- let ownerDocument = useOwnerDocument ( ref )
33
-
34
32
let [ target , setTarget ] = useState ( ( ) => {
35
33
// Group context is used, but still null
36
34
if ( ! forceInRoot && groupTarget !== null ) return groupTarget . current ?? null
@@ -77,22 +75,24 @@ export type PortalProps<TTag extends ElementType = typeof DEFAULT_PORTAL_TAG> =
77
75
PortalPropsWeControl ,
78
76
{
79
77
enabled ?: boolean
78
+ ownerDocument ?: Document | null
80
79
}
81
80
>
82
81
83
82
let InternalPortalFn = forwardRefWithAs ( function InternalPortalFn <
84
83
TTag extends ElementType = typeof DEFAULT_PORTAL_TAG ,
85
84
> ( props : PortalProps < TTag > , ref : Ref < HTMLElement > ) {
86
- let theirProps = props
85
+ let { ownerDocument : incomingOwnerDocument = null , ... theirProps } = props
87
86
let internalPortalRootRef = useRef < HTMLElement | null > ( null )
88
87
let portalRef = useSyncRefs (
89
88
optionalRef < ( typeof internalPortalRootRef ) [ 'current' ] > ( ( ref ) => {
90
89
internalPortalRootRef . current = ref
91
90
} ) ,
92
91
ref
93
92
)
94
- let ownerDocument = useOwnerDocument ( internalPortalRootRef )
95
- let target = usePortalTarget ( internalPortalRootRef )
93
+ let defaultOwnerDocument = useOwnerDocument ( internalPortalRootRef )
94
+ let ownerDocument = incomingOwnerDocument ?? defaultOwnerDocument
95
+ let target = usePortalTarget ( ownerDocument )
96
96
let [ element ] = useState < HTMLDivElement | null > ( ( ) =>
97
97
env . isServer ? null : ownerDocument ?. createElement ( 'div' ) ?? null
98
98
)
@@ -154,12 +154,12 @@ function PortalFn<TTag extends ElementType = typeof DEFAULT_PORTAL_TAG>(
154
154
) {
155
155
let portalRef = useSyncRefs ( ref )
156
156
157
- let { enabled = true , ...theirProps } = props
157
+ let { enabled = true , ownerDocument , ...theirProps } = props
158
158
159
159
let render = useRender ( )
160
160
161
161
return enabled ? (
162
- < InternalPortalFn { ...theirProps } ref = { portalRef } />
162
+ < InternalPortalFn { ...theirProps } ownerDocument = { ownerDocument } ref = { portalRef } />
163
163
) : (
164
164
render ( {
165
165
ourProps : { ref : portalRef } ,
0 commit comments