Composable modal dialog
Design a compound modal API with robust focus management.Description
Requirements
Modal exposes trigger, content, title, and close parts that compose through the exported modal API.
The modal supports both internal open state and caller-controlled open state changes.
Open modal content is rendered in a portal outside the trigger's DOM subtree.
Tab and Shift+Tab cycle among focusable controls inside an open modal.
Escape and the close control each close the active modal.
Every close path restores focus to the trigger that opened the modal.
Modal content has dialog semantics, is modal, and is labelled by the rendered modal title.
Focus restoration
Open the modal, then press Escape
The modal closes and focus returns to its trigger
Constraints
Render content in a portalTrap focus without a third-party dialog packageRestore focus after every close path
Hints
Hint 1
Use a narrowly scoped shared value so compound parts can access open state, identifiers, and close behavior.
Hint 2
Capture the trigger before opening, move focus into the portaled dialog, and intercept Tab at the focusable boundaries.
Hint 3
Send Escape, the close control, and controlled-state changes through one close path that restores captured focus.