Skip to content

Commit d8e44d5

Browse files
committed
fix(portal): fall back to document.body when no PortalProvider exists
Update usePortalRoot hook to return document.body as fallback when no explicit PortalProvider is present, instead of returning null. This ensures portals render correctly in applications that don't wrap content in a PortalProvider. Returns null only in SSR environments where document is undefined.
1 parent d9ca5d8 commit d8e44d5

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

packages/baukasten/src/context/PortalProvider.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,21 @@ export function PortalProvider({ root, children }: PortalProviderProps) {
134134

135135
/**
136136
* Hook to access the portal root element from context
137-
*
138-
* Returns null if no PortalProvider is present, which signals components
139-
* to use their default portal behavior.
140-
*
141-
* @returns The portal root element or null
142-
*
137+
*
138+
* Returns the portal root from PortalProvider if present, otherwise falls back
139+
* to `document.body`. This ensures portals always render correctly, even without
140+
* an explicit PortalProvider wrapper.
141+
*
142+
* For multi-window applications (like Theia), wrap your content in PortalProvider
143+
* with the appropriate root element for that window.
144+
*
145+
* @returns The portal root element (from context or document.body), or null in SSR
146+
*
143147
* @example
144148
* ```tsx
145149
* function MyPortalComponent() {
146150
* const portalRoot = usePortalRoot();
147-
*
151+
*
148152
* return (
149153
* <FloatingPortal root={portalRoot}>
150154
* <div>Portal content</div>
@@ -155,5 +159,7 @@ export function PortalProvider({ root, children }: PortalProviderProps) {
155159
*/
156160
export function usePortalRoot(): HTMLElement | null {
157161
const context = useContext(PortalContext);
158-
return context.root;
162+
// Return the context root if provided, otherwise fall back to document.body
163+
// This ensures portals work even without an explicit PortalProvider
164+
return context.root ?? (typeof document !== 'undefined' ? document.body : null);
159165
}

0 commit comments

Comments
 (0)