Q15 of 24 · Accessibility
How do you test focus trapping in modal dialogs and off-canvas menus?
Short answer
Short answer: Open the modal, Tab repeatedly and verify focus cycles within it (not to background content). Press Escape and verify the dialog closes and focus returns to the trigger element. Also verify that screen reader browse mode cannot access content behind the modal while it's open.
Detail
The ARIA dialog pattern requires three focus management behaviours when a modal opens:
- Focus moves into the modal: on open, focus must move to the first focusable element in the modal (or to the modal container if no focusable element exists and the container has tabindex="-1").
- Focus is trapped inside: Tab must cycle through all focusable elements within the modal and wrap back to the first. It must never reach the document behind the modal.
- Focus returns on close: Escape (and any explicit close button) must close the modal and return focus to the element that triggered it — not to the top of the page.
Keyboard test procedure:
- Tab to the button that opens the modal. Press Enter.
- Verify focus is now inside the modal.
- Tab through all focusable elements — they should cycle without leaving the modal.
- Shift+Tab backward — same cycling, in reverse.
- Press Escape — modal closes, focus is on the original trigger button.
Screen reader browse mode: when a screen reader is in browse/reading mode (using arrow keys), it can reach any content in the DOM regardless of Tab focus. The modal's backdrop content must have aria-hidden="true" applied while the modal is open, so screen reader browse mode cannot access it. Verify this by arrow-keying in VoiceOver or NVDA while the modal is open — you should not be able to reach the page content behind it.