Overview
TheDrawerPortal component renders its children in a portal at the end of the document body (or a custom container). This helps avoid z-index conflicts in complex layouts with nested stacking contexts.
Note: You typically don’t use this component directly. Instead, use the portal prop on the Drawer component, which handles portal rendering automatically.
Props
Custom portal container element or CSS selector. If not provided, creates a new div and appends it to
document.body.Usage via Drawer Component
Recommended approach:Custom Portal Container
Direct Usage (Advanced)
While not recommended, you can useDrawerPortal directly:
How It Works
- On mount: Creates a portal container div with
data-drawer-portalattribute - Appends to body: The portal container is added to the end of
document.body - Renders children: Drawer content is rendered inside the portal
- On unmount: Cleans up the portal container
Portal Styling
The portal container automatically receives:When to Use Portals
Use portal rendering when:- Complex z-index layouts: You have nested stacking contexts that interfere with drawer visibility
- Third-party components: External libraries create their own stacking contexts
- Overflow hidden: Parent containers have
overflow: hiddenthat clips the drawer - Fixed positioning conflicts: Multiple fixed elements create layering issues
- Modals inside scrollable areas: Drawers inside containers with
overflow-y: auto
When to Avoid Portals
Skip portals when:- Simple layouts: Your app doesn’t have complex z-index issues
- Performance: Portals have a slight overhead (usually negligible)
- SSR concerns: Portals only work client-side (handled automatically)
Examples
Drawer Inside Modal
Multiple Drawers with Portals
Nested Drawers
Browser Compatibility
Portals work in all modern browsers:- Chrome/Edge (all versions)
- Firefox (all versions)
- Safari (all versions)
- Opera (all versions)
SSR Considerations
Portals only work client-side because they depend ondocument.body. The component handles this automatically:
- Waits for
onMountbefore creating portal - Uses
mountedstate to conditionally render - No hydration mismatches
Accessibility
Portals don’t affect accessibility:- Screen readers navigate the DOM tree normally
- Tab order remains logical (focus management handled by
DrawerContent) - ARIA attributes work as expected
Performance
Portal overhead is minimal:- Small one-time cost during mount
- No runtime performance impact
- DOM structure slightly more complex (negligible)
Related Components
- Drawer - Main wrapper with
portalprop - DrawerContent - Content container
- DrawerOverlay - Background overlay