Modal with focus trap

Forms & Events — Intermediate React challenge.

Build a Modal with a real keyboard focus trap — not just auto-focus. Tab and Shift+Tab cycle focus between only the modal's own elements, Escape and backdrop clicks close it, and focus is restored to whatever opened it once it closes. (Focus-tracking behavior is graded manually/visually — the automated tests below cover open/close, overlay, and CSS.)

Requirements

Signature

function Modal({
  open: boolean;
  onClose: () => void;
  triggerRef: React.RefObject<HTMLElement>;
}): JSX.Element | null

Example

<Modal open={open} onClose={() => setOpen(false)} triggerRef={triggerRef} />
// opens -> input auto-focused
// Tab from Close button -> wraps back to input
// Escape or backdrop click -> closes, focus returns to the trigger button

Constraints

🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code
Loading workspace…