@@ -22,18 +22,25 @@ const getInsertBeforeTarget = (bodyPortalSlots: string[], slot?: string) => {
22
22
}
23
23
24
24
export const BodyPortal = ( {
25
- children, className, role, slot, tagName
26
- } : React . PropsWithChildren < { className ?: string ; role ?: string ; slot ?: string ; tagName ?: string } > ) => {
25
+ children, className, ref, role, slot, tagName
26
+ } : React . PropsWithChildren < {
27
+ className ?: string ;
28
+ ref ?: React . MutableRefObject < HTMLElement | null > ;
29
+ role ?: string ;
30
+ slot ?: string ;
31
+ tagName ?: string
32
+ } > ) => {
27
33
const tag = tagName ?. toUpperCase ( ) ?? 'DIV' ;
28
- const ref = React . useRef < HTMLElement > ( document . createElement ( tag ) ) ;
29
- if ( ref . current . tagName !== tag ) {
30
- ref . current = document . createElement ( tag ) ;
34
+ const internalRef = React . useRef < HTMLElement > ( document . createElement ( tag ) ) ;
35
+ if ( internalRef . current . tagName !== tag ) {
36
+ internalRef . current = document . createElement ( tag ) ;
31
37
}
38
+ if ( ref ) { ref . current = internalRef . current ; }
32
39
33
40
const bodyPortalOrderedRefs = React . useContext ( BodyPortalSlotsContext ) ;
34
41
35
42
React . useLayoutEffect ( ( ) => {
36
- const element = ref . current ;
43
+ const element = internalRef . current ;
37
44
38
45
if ( className ) { element . classList . add ( ...className . split ( ' ' ) ) ; }
39
46
@@ -56,5 +63,5 @@ export const BodyPortal = ({
56
63
} ;
57
64
} , [ bodyPortalOrderedRefs , className , role , slot , tag ] ) ;
58
65
59
- return createPortal ( children , ref . current ) ;
66
+ return createPortal ( children , internalRef . current ) ;
60
67
} ;
0 commit comments