modal-dialog
Skill0 starsChanged 29 days agoMITView on GitHub
Use when a dialog opens and Tab goes behind it, when focus is lost or jumps to the top of the page after closing, or when Escape does not close the dialog.
In this repository33 other files it gives its agents.
- AGENTS.mdAGENTS.md
- accessible-authenticationSkill
- accessible-formsSkill
- accessible-nameSkill
- accordion-disclosureSkill
- audit-componentSkill
- carouselSkill
- charts-and-datavizSkill
- combobox-autocompleteSkill
- data-tableSkill
- email-html-accessibilitySkill
- fix-color-contrastSkill
- forced-colors-and-dark-modeSkill
- headings-and-landmarksSkill
- keyboard-focusSkill
- live-regionsSkill
- media-captionsSkill
- menu-buttonSkill
- motion-and-animationSkill
- page-title-and-languageSkill
- pagination-and-searchSkill
- pointer-and-gesturesSkill
- predictable-contextSkill
- progress-and-loadingSkill
- screen-reader-test-planSkill
- semantic-htmlSkill
- skip-linksSkill
- svg-and-iconsSkill
- tabsSkill
- tooltip-popoverSkill
- triage-automated-resultsSkill
- wcag22-new-criteriaSkill
- zoom-reflow-text-spacingSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 70
Keep the background unreachable, not merely dimmed: Prefer the inert attribute over aria-hidden="true": inert removes a subtree from the …
- Distinctive: line 83
Test with the keyboard only, in this order — each step fails a specific criterion:
≈ “Common” is an estimate: at least 1 in 5 of the 372,429 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Modal dialog
Criteria: SC 2.1.1 Keyboard, SC 2.1.2 No Keyboard Trap, SC 2.4.3 Focus Order, SC 4.1.2 Name, Role, Value
A modal dialog is the one component where containing focus is correct, and the
platform gives it to you. <dialog> with showModal() contains focus, closes on
Escape, and makes the rest of the page inert. The mistake that matters most is
a hand-built role="dialog" overlay: focus escapes behind it, Escape stops
working, and the page underneath stays reachable.
When to use
- "The dialog opens but Tab goes behind it."
- "After I close it, focus jumps back to the top of the page."
- "Escape does not close it — I have to hunt for the X."
- Reviewing any
role="dialog"oraria-modal="true"implementation.
Procedure
Reach for the native element first.
<dialog>is a modal dialog, andshowModal()is what makes it modal.show()does not.<!-- Fails SC 4.1.2 — a role with no accessible name, and no modal behaviour --> <div class="modal" role="dialog" aria-modal="true"> <h2>Delete file?</h2> <button>Delete</button> </div> <!-- Passes — the platform supplies containment, Escape, and inertness --> <dialog id="confirm" aria-labelledby="confirm-title"> <h2 id="confirm-title">Delete file?</h2> <form method="dialog"> <button value="cancel">Cancel</button> <button value="confirm">Delete</button> </form> </dialog>- Give it a name.
role="dialog"has no implicit name, so SC 4.1.2 requiresaria-labelledbypointing at the visible title (preferred) or anaria-label. A dialog whose title is rendered in a<div>with no id is unnamed. Move focus in on open, and return it to the opener on close.
showModal()handles the first part by focusing the first focusable descendant or an element carryingautofocus. Wire the second part yourself.const dialog = document.getElementById('confirm'); const opener = document.activeElement; // capture before opening dialog.addEventListener('close', () => opener?.focus()); // not document.body dialog.showModal();- Let
Escapeclose it.showModal()handlesEscapeunless acancelhandler callspreventDefault(). If you intercept it, you must still provide another keyboard route out, or you have built a keyboard trap (SC 2.1.2). - Cycle Tab within the modal. This containment is deliberate and is not a
violation of SC 2.1.2 for a modal dialog, because
Escapereleases it. A non-modal dialog, popover, menu, or tooltip must release focus on Tab.<dialog>handles the cycle in modal mode. - Keep the background unreachable, not merely dimmed. Prefer the
inertattribute overaria-hidden="true":inertremoves a subtree from the tab order and the accessibility tree together, whilearia-hiddenalone leaves controls focusable but invisible to assistive technology. - If you cannot use
<dialog>— say an old runtime withoutshowModal()— you take on all of it:role="dialog",aria-modal="true", a name, anEscapehandler, the Tab cycle, moving focus in, restoring focus on close, andinerton the background. Write each of those down and test each one; a partial reimplementation is the norm, not the exception.
Verify
Test with the keyboard only, in this order — each step fails a specific criterion:
| Do this | Passing looks like | Criterion |
| Open the dialog | focus lands on the dialog or its first meaningful control, and the title is announced | 2.4.3, 4.1.2 |
| Press Tab repeatedly to the end | focus cycles inside the dialog, then wraps to the start; it never appears behind the overlay | 2.1.1, 2.4.3 |
| Operate every control with Enter and Space | each one activates | 2.1.1 |
| Press Escape | the dialog closes and focus is back on the button that opened it | 2.1.2, 2.4.3 |
| Reopen and close twice | focus returns to the same opener each time | 2.4.3 |
Then assert the name in code. Automated rules catch a missing name; whether focus moved to the right control is a judgement call and must be walked with the keyboard.
const dialog = document.querySelector('dialog[open]');
// The name lives in the accessibility tree, so confirm the reference resolves.
const labelledby = dialog?.getAttribute('aria-labelledby');
console.assert(labelledby && document.getElementById(labelledby),
'SC 4.1.2: dialog has no accessible name');Failure modes
aria-modal="true"on adivwith no trap and noEscape. The screen reader stops reading the background, so the user believes they are contained, while Tab walks straight out and there is no way back. It is worse than an unlabelled dialog.aria-hidden="true"on a container that still holds the focus. Focus can remain inside a hidden subtree: the focused control vanishes from the accessibility tree while still receiving keystrokes.- Returning focus to
document.body. The keyboard user restarts Tab from the top of a long page. Restore focus to the opener, and if the opener is gone, to the nearest surviving control. show()instead ofshowModal(). The dialog renders centred and looks modal, but the trap, theEscapekey, and the inert background are all absent.Escapeswallowed by an input handler. A keydown listener that callsstopPropagation(), or acancelhandler callingpreventDefault()without an alternative, strands keyboard users inside the dialog.- Focusing the close button instead of the title or the first task control. It is technically inside the dialog, so it passes a casual review, but it hides the dialog's context from screen reader users.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

