---
name: test-modal-keyboard-focus
description: "Check a modal dialog's keyboard behaviour from an observed trace: focus moves in on opening, Tab and Shift+Tab stay inside, Escape closes it and focus returns to the control that opened it. Use when testing a modal, pop-up or lightbox for keyboard access or focus trapping."
license: CC-BY-4.0
metadata:
  author: Raq.com
  version: "2026-11-22"
  homepage: https://skills.raq.com/skills/test-modal-keyboard-focus
---

# Test keyboard focus in a modal dialog

Use an observed keyboard trace to check entry focus, Tab containment, Escape and focus restoration when a dialog closes.

## When to use this skill

- Someone has tested a modal dialog with the keyboard and wants to know whether the focus behaviour is right.
- Keyboard users report that focus disappears behind a pop-up, gets lost after closing it or jumps to the top of the page.
- A team is comparing a native dialog element with a custom modal and wants one test to run on both.

## When not to use it

- Non-modal pop-ups, menus, tooltips or disclosure panels. They do not contain focus, so these checks do not apply; use the pattern for that component.
- How a screen reader announces the dialog's role, name and description. That needs a separate screen-reader test.
- Focus order and visible focus across the whole page. Those are WCAG 2.4.3 and 2.4.7 checks for every component.

## What you need

- The opening control, dialog contents in tab order, and an observed trace for open, Tab, Shift+Tab, Escape and the return destination.
- Any documented logical successor if the opener disappears or the workflow continues somewhere new.

## Ask before you start

If any of these are missing or unclear, ask before doing the work.

- Which control opens the dialog, and does it still exist after the dialog closes? This decides where focus should return.
- Which are the first and last tabbable elements inside the dialog? The wrap checks are judged at those boundaries.
- Is there a designed destination for focus when the dialog finishes, such as the next row after deleting one? W3C allows a logical successor when the opener has gone or the workflow moves on.
- Which transitions were actually observed, and in which browser? Anything not observed stays untested.

## Method

1. Open the dialog from its control by keyboard. Check that focus moves to an element inside it: usually the first focusable control, or a heading or static element at the start (made focusable with tabindex="-1") when the content needs reading first. Entry fails if focus stays on the page behind.
2. From the last tabbable element, press Tab: focus should move to the first tabbable element in the dialog. From the first, press Shift+Tab: focus should move to the last. Record the two directions as separate results.
3. If Tab or Shift+Tab reaches anything on the page behind the dialog, that wrap fails. Focus passing through the browser's own toolbar before returning to the dialog is acceptable under WCAG 2.4.3, which W3C notes for modal dialogs; record it as a difference from the APG pattern, not as a leak.
4. Press Escape and check that the dialog closes. If it does not, note whether another keyboard route such as a Close button works: with no keyboard way out at all, WCAG 2.1.2 No Keyboard Trap fails.
5. Check where focus lands after closing: normally the control that opened the dialog. If that control no longer exists, or the design names a logical next step, focus should be on that element. Focus on the page body, the top of the page or nowhere visible is a failure.
6. Mark each transition that was not observed as untested, including return when closing was never observed. Markup such as aria-modal="true" or the open attribute does not show focus behaviour; only the trace does.
7. For each failure give the fix and repeat the same keystrokes to confirm it. Leaks into the page and lost return focus relate to WCAG 2.4.3 Focus Order.

## Checks before you deliver

- Checking only Tab misses reverse traversal leaking into the background.
- Moving focus to the body after a dialog closes can leave a keyboard user stranded even when closing itself works.
- Closing the dialog with a mouse click during testing hides keyboard problems. Close it by keyboard, by Escape and by any Cancel or Close button.
- aria-modal="true" on a dialog that does not actually block the page behind can hide that content from screen-reader users while keyboard users can still reach it. W3C advises marking a dialog modal only when it behaves as one.
- A native dialog opened with showModal() makes the page behind inert and returns focus to the previously focused element when it closes; a custom dialog has to do both in script. Do not assume a custom dialog behaves like the native one.

## How to present the result

Start with a one-line result: which of entry, Tab wrap, Shift+Tab wrap, Escape and return focus failed, and which were not tested. Then give a short table with each check, what was observed and what should happen. Finish with the fix for each failure and the exact keystrokes to repeat when retesting.

## Worked example

**Request:** Review this recorded keyboard trace. Opener Edit address remains available. First tab stop Address, last Save.

**Supplied material:**

```text
Open -> Address (inside)
Tab from Save -> site footer (outside)
Shift+Tab from Address -> Save
Escape -> closed
After close -> page body
```

**Answer:** Forward Tab escapes from Save to the site footer. Make it wrap to Address and restore focus to Edit address after Escape closes the dialog. Retest both transitions. Entry, reverse wrap and Escape itself were observed working.

## Structured output for tests

For an ordinary job, answer in the format above and point back to the supplied evidence. Use this structure only for the test cases published with this skill, or when someone asks for structured JSON.

Return failed and untested as arrays of check names in this order: entry, tab-wrap, reverse-wrap, escape, return. Compare return focus against the opener or explicitly justified successor. If closing was not observed and no separate return observation exists, put return in untested.

## Sources

- [W3C ARIA modal dialog pattern](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/): The Authoring Practices pattern: focus moves into the dialog on opening (a static element at the start may take initial focus when content needs reading), Tab and Shift+Tab wrap inside, Escape closes, and focus returns to the opener unless it no longer exists or the workflow makes another element more logical. It warns against aria-modal on dialogs that do not behave as modal.
- [W3C Understanding Focus Order](https://www.w3.org/WAI/WCAG22/Understanding/focus-order.html): Explains WCAG 2.2 success criterion 2.4.3 (Level A). Its modal dialog example says content outside an open modal is inert and cannot receive focus, that the focus cycle may still include browser controls, and that focus returns to the trigger or the element after it when the dialog closes.
- [HTML Standard: the dialog element](https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element): Defines showModal(), which blocks the rest of the document, records the previously focused element and runs the dialog focusing steps, and the close steps that return focus to that element. The skill uses it to separate native dialog behaviour from what a custom modal must script.

Test cases and an answer checker for this skill: https://skills.raq.com/skills/test-modal-keyboard-focus
