Question presented to candidate:
"Your modal is being clipped by a parent with overflow: hidden. How do portals solve that, and what do they not change?"
What a strong answer should cover:
createPortal(children, domNode)renderschildreninto a different DOM container while keeping them in the same place in the React tree.- That split is the whole feature: DOM position changes, React position does not.
- It solves CSS containment problems —
overflow: hidden,z-indexstacking contexts,transformcreating a new containing block — which no amount of z-index tinkering fixes from inside. - Events still bubble through the React tree, not the DOM tree. A click inside the portal reaches the React parent's
onClickeven though the DOM parent is elsewhere. This surprises people and is usually what you want. - Context, state and props all flow normally, because nothing about the React tree changed.
- Typical uses: modals, dialogs, tooltips, toasts, dropdown menus, anything that must escape its container.
- It is not an isolation mechanism — that is shadow DOM. Styles and events still apply.
- Accessibility is not solved for you: focus trapping,
aria-modal, restoring focus on close, and Escape handling are still your job. Prefer<dialog>or a headless library.
Clarifying questions expected:
- "Is the problem CSS containment, or DOM ordering for accessibility?" — portals fix the first; the second needs more.
- "Does this need focus management?" — almost always yes for modals.
Code / implementation expected: Optional. Showing the event bubbling through the React parent is the non-obvious part worth demonstrating.