TabTrap

Primitive #8

🔍 Problem it solves

When a modal is open, pressing the Tab key should cycle focus only inside the modal, not escape to the background page. Without trapping, keyboard users can navigate to hidden interactive elements.

✅ Solution

Listens for Tab and Shift+Tab keys, finds the first and last focusable elements inside the container, and wraps focus accordingly. Prevents focus from leaving the modal.

🧩 Used by components

Dialog, Modal, Drawer, Popover, Sheet, AlertDialog

🎮 Interactive demo