{"skills":[{"name":"Test keyboard focus in a modal dialog","description":"Use an observed keyboard trace to check entry focus, Tab containment, Escape and focus restoration when a dialog closes.","content":"# Test keyboard focus in a modal dialog\n\nUse an observed keyboard trace to check entry focus, Tab containment, Escape and focus restoration when a dialog closes.\n\n## When to use this skill\n\n- Someone has tested a modal dialog with the keyboard and wants to know whether the focus behaviour is right.\n- Keyboard users report that focus disappears behind a pop-up, gets lost after closing it or jumps to the top of the page.\n- A team is comparing a native dialog element with a custom modal and wants one test to run on both.\n\n## When not to use it\n\n- 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.\n- How a screen reader announces the dialog's role, name and description. That needs a separate screen-reader test.\n- Focus order and visible focus across the whole page. Those are WCAG 2.4.3 and 2.4.7 checks for every component.\n\n## What you need\n\n- The opening control, dialog contents in tab order, and an observed trace for open, Tab, Shift+Tab, Escape and the return destination.\n- Any documented logical successor if the opener disappears or the workflow continues somewhere new.\n\n## Ask before you start\n\nIf any of these are missing or unclear, ask before doing the work.\n\n- Which control opens the dialog, and does it still exist after the dialog closes? This decides where focus should return.\n- Which are the first and last tabbable elements inside the dialog? The wrap checks are judged at those boundaries.\n- 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.\n- Which transitions were actually observed, and in which browser? Anything not observed stays untested.\n\n## Method\n\n1. 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.\n2. 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.\n3. 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.\n4. 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.\n5. 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.\n6. 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.\n7. 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.\n\n## Checks before you deliver\n\n- Checking only Tab misses reverse traversal leaking into the background.\n- Moving focus to the body after a dialog closes can leave a keyboard user stranded even when closing itself works.\n- 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.\n- 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.\n- 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.\n\n## How to present the result\n\nStart 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.\n\n## Worked example\n\n**Request:** Review this recorded keyboard trace. Opener Edit address remains available. First tab stop Address, last Save.\n\n**Supplied material:**\n\n```text\nOpen -> Address (inside)\nTab from Save -> site footer (outside)\nShift+Tab from Address -> Save\nEscape -> closed\nAfter close -> page body\n```\n\n**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.\n\n## Structured output for tests\n\nFor 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.\n\nReturn 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.\n\n## Sources\n\n- [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.\n- [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.\n- [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.\n\nTest cases and an answer checker for this skill: https:\/\/skills.raq.com\/skills\/test-modal-keyboard-focus\n","scope":"contextual","trigger_keywords":["Test keyboard focus in a modal dialog","test-modal-keyboard-focus","test modal keyboard focus","Does keyboard focus behave properly when our pop-up dialog opens and closes?","Does keyboard focus behave properly when our pop-up dialog opens and closes","focus in this modal","dialog keyboard focus","modal focus"],"is_private":true,"is_active":true,"scripts":[{"name":"test-modal-keyboard-focus-check","description":"Compare a saved JSON answer with a published test. Pass tests\/case-1\/expected.json (or another case) and the path to your answer.","language":"python","filename":"test-modal-keyboard-focus-check.py","content":"from pathlib import Path\nimport runpy\n\nrunpy.run_path(str(Path(__file__).parent \/ 'tests' \/ 'check.py'), run_name='__main__')\n","supporting_files":[{"path":"SKILL.md","content":"---\nname: test-modal-keyboard-focus\ndescription: \"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.\"\nlicense: CC-BY-4.0\nmetadata:\n  author: Raq.com\n  version: \"2026-11-22\"\n  homepage: https:\/\/skills.raq.com\/skills\/test-modal-keyboard-focus\n---\n\n# Test keyboard focus in a modal dialog\n\nUse an observed keyboard trace to check entry focus, Tab containment, Escape and focus restoration when a dialog closes.\n\n## When to use this skill\n\n- Someone has tested a modal dialog with the keyboard and wants to know whether the focus behaviour is right.\n- Keyboard users report that focus disappears behind a pop-up, gets lost after closing it or jumps to the top of the page.\n- A team is comparing a native dialog element with a custom modal and wants one test to run on both.\n\n## When not to use it\n\n- 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.\n- How a screen reader announces the dialog's role, name and description. That needs a separate screen-reader test.\n- Focus order and visible focus across the whole page. Those are WCAG 2.4.3 and 2.4.7 checks for every component.\n\n## What you need\n\n- The opening control, dialog contents in tab order, and an observed trace for open, Tab, Shift+Tab, Escape and the return destination.\n- Any documented logical successor if the opener disappears or the workflow continues somewhere new.\n\n## Ask before you start\n\nIf any of these are missing or unclear, ask before doing the work.\n\n- Which control opens the dialog, and does it still exist after the dialog closes? This decides where focus should return.\n- Which are the first and last tabbable elements inside the dialog? The wrap checks are judged at those boundaries.\n- 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.\n- Which transitions were actually observed, and in which browser? Anything not observed stays untested.\n\n## Method\n\n1. 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.\n2. 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.\n3. 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.\n4. 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.\n5. 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.\n6. 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.\n7. 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.\n\n## Checks before you deliver\n\n- Checking only Tab misses reverse traversal leaking into the background.\n- Moving focus to the body after a dialog closes can leave a keyboard user stranded even when closing itself works.\n- 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.\n- 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.\n- 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.\n\n## How to present the result\n\nStart 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.\n\n## Worked example\n\n**Request:** Review this recorded keyboard trace. Opener Edit address remains available. First tab stop Address, last Save.\n\n**Supplied material:**\n\n```text\nOpen -> Address (inside)\nTab from Save -> site footer (outside)\nShift+Tab from Address -> Save\nEscape -> closed\nAfter close -> page body\n```\n\n**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.\n\n## Structured output for tests\n\nFor 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.\n\nReturn 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.\n\n## Sources\n\n- [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.\n- [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.\n- [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.\n\nTest cases and an answer checker for this skill: https:\/\/skills.raq.com\/skills\/test-modal-keyboard-focus\n"},{"path":"LICENSE.txt","content":"Original Raq.com instructions and synthetic examples: CC BY 4.0.\nhttps:\/\/creativecommons.org\/licenses\/by\/4.0\/\nAttribution: Raq.com, https:\/\/skills.raq.com\/skills\/test-modal-keyboard-focus\nReferenced third-party materials retain their own terms.\n"},{"path":"examples\/worked-example.md","content":"# Delivery-address editor\n\nReview this recorded keyboard trace. Opener Edit address remains available. First tab stop Address, last Save.\n\n## Supplied material\n\nOpen -> Address (inside)\nTab from Save -> site footer (outside)\nShift+Tab from Address -> Save\nEscape -> closed\nAfter close -> page body\n\n## Worked answer\n\nForward 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.\n\n## Reasoning\n\nForward traversal escapes the dialog and focus is lost after closing. Reverse traversal is independently correct.\n"},{"path":"tests\/check.py","content":"\"\"\"Check a saved agent answer locally with Python 3 (standard library only).\"\"\"\nimport json\nimport math\nimport sys\nfrom pathlib import Path\n\n\ndef compare(expected, actual, path=\"$\", accepted=None):\n    if isinstance(expected, bool) or isinstance(actual, bool):\n        if expected is not actual:\n            yield path\n    elif isinstance(expected, (int, float)) and isinstance(actual, (int, float)):\n        if not math.isclose(expected, actual, rel_tol=0, abs_tol=1e-8):\n            yield path\n    elif isinstance(expected, dict) and isinstance(actual, dict):\n        for key in sorted(set(expected) | set(actual)):\n            if key not in expected or key not in actual:\n                yield f\"{path}.{key}\"\n            elif not any(\n                not list(compare(option, actual[key]))\n                for option in (accepted or {}).get(key, [])\n            ):\n                yield from compare(expected[key], actual[key], f\"{path}.{key}\")\n    elif isinstance(expected, list) and isinstance(actual, list):\n        if len(expected) != len(actual):\n            yield f\"{path} (length)\"\n        for i, (left, right) in enumerate(zip(expected, actual)):\n            yield from compare(left, right, f\"{path}[{i}]\")\n    elif type(expected) != type(actual) or expected != actual:\n        yield path\n\n\ndef main():\n    if len(sys.argv) != 3:\n        raise SystemExit(\"Usage: python3 check.py expected.json answer.json\")\n    try:\n        expected, actual = [json.loads(Path(p).read_text(encoding=\"utf-8\")) for p in sys.argv[1:]]\n        alternatives = Path(sys.argv[1]).with_name(\"accepted.json\")\n        accepted = json.loads(alternatives.read_text(encoding=\"utf-8\")) if alternatives.is_file() else {}\n    except (ValueError, OSError) as error:\n        raise SystemExit(str(error))\n    differences = list(compare(expected, actual, accepted=accepted))\n    if differences:\n        raise SystemExit(\"Different fields: \" + \", \".join(differences))\n    print(\"Pass\")\n\n\nif __name__ == \"__main__\":\n    main()\n"},{"path":"tests\/README.md","content":"Give your agent SKILL.md and one request.txt in a fresh session. Keep the expected answer aside. Save the reply as answer.json, then run this from the tests folder:\n\n    python3 check.py case-1\/expected.json answer.json\n\nEach case also has an explanation. Where accepted.json exists, the checker accepts the alternatives listed there.\n"},{"path":"tests\/case-1\/request.txt","content":"Review this recorded keyboard trace. Opener Edit address remains available. First tab stop Address, last Save.\nReturn only the JSON specified in SKILL.md for this test.\n\nOpen -> Address (inside)\nTab from Save -> site footer (outside)\nShift+Tab from Address -> Save\nEscape -> closed\nAfter close -> page body\n"},{"path":"tests\/case-1\/expected.json","content":"{\n    \"failed\": [\n        \"tab-wrap\",\n        \"return\"\n    ],\n    \"untested\": []\n}\n"},{"path":"tests\/case-1\/explanation.txt","content":"Forward traversal escapes the dialog and focus is lost after closing. Reverse traversal is independently correct.\n"},{"path":"tests\/case-2\/request.txt","content":"Review a delete-row dialog. Opener belongs to removed row. The design specifies the next row selector as the logical successor. First stop Cancel, last Delete.\nReturn only the JSON specified in SKILL.md for this test.\n\nOpen -> dialog heading (inside, programmatically focusable)\nTab from Delete -> Cancel\nReverse traversal was not observed\nEscape -> closed during a separate cancellation run\nAfter confirmed deletion -> next row selector\n"},{"path":"tests\/case-2\/expected.json","content":"{\n    \"failed\": [],\n    \"untested\": [\n        \"reverse-wrap\"\n    ]\n}\n"},{"path":"tests\/case-2\/explanation.txt","content":"Initial focus may land on a heading, and the documented successor is appropriate after deletion. The reverse wrap needs observation.\n"}],"is_active":true,"is_approved":false}]}]}