Render a portal and restore focus
A portal moves DOM placement, not React ownership
createPortal takes renderable children, an existing DOM container and optionally a key. It returns a renderable node, not a second root. Use portals for overlays or content that must be physically placed outside a clipping or layout container. Context and React event propagation still follow the component tree. A different target DOM node recreates the portal content; use a stable target. All examples in this lesson are client-only and use document.body, which already exists when the app mounts.
import React,{createContext,useContext} from 'react';import {createPortal} from 'react-dom';const Room=createContext('Unknown');function Legend(){return <aside aria-label="Gallery legend">Current room: {useContext(Room)}</aside>;}export default function App(){return <Room.Provider value="Textile gallery"><main><h1>Exhibition</h1>{createPortal(<Legend/>,document.body)}</main></Room.Provider>;}The legend appears under body, outside the main element in the DOM, but still displays Textile gallery because it belongs to the Room provider in React. Inspect both trees in DevTools. CSS inheritance and stacking depend on the actual DOM location, so a portal is not a universal fix for layering or accessibility.
Understand event propagation across the portal
import React,{useState} from 'react';import {createPortal} from 'react-dom';export default function App(){const [count,setCount]=useState(0);return <section onClick={()=>setCount(n=>n+1)}><h2>Parent received {count} clicks</h2>{createPortal(<aside aria-label="Portal controls"><button type="button">Bubble to parent</button><button type="button" onClick={e=>e.stopPropagation()}>Keep local</button></aside>,document.body)}</section>;}The first button increments the parent count even though it is not a DOM descendant of section. The second does not. stopPropagation stops propagation for that event; it does not cancel browser default actions. Prefer a component structure that makes event ownership clear; do not stop every event globally.
Use native modal behavior and restore focus
import React,{useRef,useId} from 'react';import {createPortal} from 'react-dom';export default function App(){const dialog=useRef(null);const trigger=useRef(null);const title=useId();return <><button ref={trigger} type="button" onClick={()=>dialog.current.showModal()}>Gallery details</button>{createPortal(<dialog ref={dialog} aria-labelledby={title} onClose={()=>trigger.current?.focus()}><h2 id={title}>Textile gallery</h2><p>Quiet viewing room.</p><button type="button" onClick={()=>dialog.current.close()}>Close</button></dialog>,document.body)}</>;}The native dialog is always mounted but initially closed. showModal places it in the browser top layer, makes the rest of the document inert and moves focus into the dialog. The close button and Escape should close it, and the onClose handler returns focus to the still-mounted trigger. The browser supplies modal behavior; the portal alone does not. Test keyboard navigation, Escape, focus restoration, accessible name and screen-reader behavior. Do not add an open attribute to this modal or create an imitation modal by only setting aria-modal. For dynamic trigger removal, choose a logical surviving focus target. In SSR, do not read document during server rendering; use an appropriate client-only integration and test hydration. Practice: inspect the actual DOM placement, test the event buttons and document focus before and after closing.