React-Bootstrap Modal focus trapping with multiple active portals
0 reputation · 25 Jun 2020, 14:18 UTC
0 reputation · 25 Jun 2020, 14:18 UTC
The React-Bootstrap Modal component utilizes React Portals to render content outside the main DOM hierarchy, ensuring that z-index layering remains consistent. To maintain accessibility, the component implements focus trapping to prevent keyboard navigation from escaping the modal boundaries while the window is active.
In environments where multiple portals are rendered simultaneously—such as a nested modal or a modal appearing alongside other portal-based overlays—the focus management logic may encounter conflicts regarding which element retains priority for the Tab key sequence.
Assuming the use of React 17 and a compatible version of React-Bootstrap, how does the focus trap behave when a second Modal is triggered while a primary Modal is still open? Is there a documented configuration to explicitly define the focus priority between overlapping portals?
When a second Modal is triggered while a primary Modal is already open, React-Bootstrap employs a stack-based focus management system. The most recently mounted modal takes priority, effectively suppressing the focus trap of the underlying modal. Keyboard navigation (the Tab key) is constrained to the topmost modal's DOM subtree, preventing the user from accidentally navigating into the primary modal or the main page content.
There is no documented configuration prop to explicitly define a custom priority sequence between overlapping portals because the priority is implicitly determined by the mounting order. The behavior follows these standard patterns:
To verify that focus trapping is functioning correctly in your specific environment (React 17+), perform the following manual test:
Tab repeatedly. Focus should cycle only through the elements of the second modal.Focus trapping may fail or behave unpredictably if:
Portal implementation for other overlays that does not communicate with the React-Bootstrap focus manager.ref.current.focus() in a useEffect hook that conflicts with the Modal's internal mounting logic.Diagnostic Detail Needed: Are you using the standard Modal component for both overlays, or is one of the portals managed by a different library or a custom createPortal implementation?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.