@@ -3,6 +3,7 @@ import { composeEventHandlers } from '@radix-ui/primitive';
33import { useComposedRefs } from '@radix-ui/react-compose-refs' ;
44import { createContext , createContextScope } from '@radix-ui/react-context' ;
55import { useId } from '@radix-ui/react-id' ;
6+ import { useLayoutEffect } from '@radix-ui/react-use-layout-effect' ;
67import { useControllableState } from '@radix-ui/react-use-controllable-state' ;
78import { DismissableLayer } from '@radix-ui/react-dismissable-layer' ;
89import { FocusScope } from '@radix-ui/react-focus-scope' ;
@@ -239,7 +240,7 @@ const DialogOverlayImpl = React.forwardRef<DialogOverlayImplElement, DialogOverl
239240 const isOpenRef = React . useRef ( context . open ) ;
240241 isOpenRef . current = context . open ;
241242
242- React . useLayoutEffect ( ( ) => {
243+ useLayoutEffect ( ( ) => {
243244 return ( ) => {
244245 // Only perform synchronous cleanup for forced unmounts (navigation while dialog is open).
245246 // When the dialog closes normally, `context.open` is already `false` before this
@@ -580,8 +581,15 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
580581 if ( titleId ) {
581582 // Use getRootNode() to support Shadow DOM contexts where document.getElementById
582583 // would fail to find elements rendered inside a shadow root.
584+ // Guard: getRootNode() may return a DocumentFragment or other Node without
585+ // getElementById (e.g. portal into a DocumentFragment). Fall back to
586+ // ownerDocument ?? document in those cases.
583587 const rootNode = contentRef . current ?. getRootNode ( ) ?? document ;
584- const hasTitle = ( rootNode as Document | ShadowRoot ) . getElementById ( titleId ) ;
588+ const searchRoot =
589+ rootNode instanceof Document || rootNode instanceof ShadowRoot
590+ ? rootNode
591+ : ( contentRef . current ?. ownerDocument ?? document ) ;
592+ const hasTitle = searchRoot . getElementById ( titleId ) ;
585593 if ( ! hasTitle ) console . error ( MESSAGE ) ;
586594 }
587595 } , [ MESSAGE , contentRef , titleId ] ) ;
@@ -606,8 +614,15 @@ const DescriptionWarning: React.FC<DescriptionWarningProps> = ({ contentRef, des
606614 if ( descriptionId && describedById ) {
607615 // Use getRootNode() to support Shadow DOM contexts where document.getElementById
608616 // would fail to find elements rendered inside a shadow root.
617+ // Guard: getRootNode() may return a DocumentFragment or other Node without
618+ // getElementById (e.g. portal into a DocumentFragment). Fall back to
619+ // ownerDocument ?? document in those cases.
609620 const rootNode = contentRef . current ?. getRootNode ( ) ?? document ;
610- const hasDescription = ( rootNode as Document | ShadowRoot ) . getElementById ( descriptionId ) ;
621+ const searchRoot =
622+ rootNode instanceof Document || rootNode instanceof ShadowRoot
623+ ? rootNode
624+ : ( contentRef . current ?. ownerDocument ?? document ) ;
625+ const hasDescription = searchRoot . getElementById ( descriptionId ) ;
611626 if ( ! hasDescription ) console . warn ( MESSAGE ) ;
612627 }
613628 } , [ MESSAGE , contentRef , descriptionId ] ) ;
0 commit comments