Accessible dialog patterns for shadcn
Rules / Dialogs: 5 rules and 2 guidelines
A dialog takes over the screen for a moment. People need to know how to get out of it, where their keyboard focus is, and what happens to the work they leave behind. Radix Dialog and AlertDialog, which shadcn’s dialogs are built on, handle a lot of the mechanics: they move focus in, keep Tab inside, close on Escape and hide the page behind from screen readers. They do not decide what Escape should mean when someone has typed something, whether a second dialog may open on top, which button gets focus in a delete confirmation, or how that button looks. Is shadcn accessible? goes through that split in detail.
Where dialogs go wrong
- Escape that saves, submits or deletes, or that throws away a half-written form at once when Cancel would have asked first. DLG-001
- A “Discard changes?” dialog opened on top of the dialog that holds the changes, so there are two layers of focus and two ways back. DLG-002
- A delete confirmation with OK and Cancel, where OK is the delete. DLG-003
- Focus that starts on the Delete button, so a quick Enter destroys data. DLG-004, a guideline (our choice)
- A destructive button that looks like any other, or one that is red with nothing else to say it is dangerous. DLG-005, a guideline (our choice)
- A dialog opened from a menu item that leaves focus at the top of the page when it closes. DLG-006
- A dialog where Tab wanders out into the page behind it. DLG-007
How the rules fit together
Three rules are about the keyboard. When a modal dialog opens, focus moves into it, and when it closes, focus goes back to where the person was (DLG-006). While it is open, Tab and Shift+Tab stay inside and nothing behind it can be used (DLG-007). Escape does what Cancel does, including any question before typed input is lost (DLG-001).
One rule is about structure: one dialog at a time (DLG-002). A follow-up question replaces the dialog’s content instead of stacking a new layer on top.
Three are about confirming a destructive action. The button names the action and a Cancel is always there (DLG-003). Two guidelines, our choice, complete them: focus starts on Cancel, so Enter alone never destroys anything (DLG-004), and the destructive button has the danger style, while its words still say what it does, so colour is never the only sign (DLG-005).
A dialog that holds a form also follows the form patterns: a Cancel that always works (FRM-003), and a question before unsaved input is thrown away (FRM-004a, and FRM-004b, a guideline). The order of the buttons in the footer is ACT-003a. Whether to ask for a confirmation at all is a separate question, answered under destructive actions.
What WCAG requires and what the judges decide
One rule here is a requirement of WCAG 2.2: moving focus into a modal dialog and back again follows from 2.4.3 Focus Order. The others are decided by the judges. Escape doing what Cancel does and a destructive button that names its action had all three in support. One dialog at a time and keeping Tab inside had both judges who spoke.
Two texts split the judges one to one: initial focus on Cancel (DLG-004) and the danger style on the destructive button (DLG-005). They are guidelines, our choice with our reasons on each page, and a product that chooses otherwise is not wrong.
Rules and guidelines
Each one has its own page with the full rule, Do and Don't cases, exceptions and the verdict. A rule is decided by WCAG 2.2 or by the judges. A guideline is our own choice. How the method works
Required by WCAG 2.2
A WCAG 2.2 Level AA criterion requires the behaviour. Where a rule adds advice of its own, it says so.
DLG-006Move focus into a modal dialog when it opens and back to its opener when it closesWhen a modal dialog opens, keyboard focus moves into it. When it closes, focus returns to the control that opened it, or to a logical place nearby if that control is gone.
Decided by the judges
No WCAG criterion requires these. The judges who spoke support them, all or most of them.
DLG-001Let Escape do what Cancel does in a dialogIn a dialog that has a dismissive action, which is Cancel, Close, a close icon or, where none of those exists, the button that leaves everything as it is, pressing Escape does exactly what that action does, including any check before unsaved input is thrown away, and never confirms, submits or runs the destructive or default action.DLG-002Keep one dialog open at a timeNever open a dialog on top of another dialog. Show any further step, including a question about discarding typed input, by replacing the open dialog's content, and a failed main action as a notice inside it, or close the dialog and move the work to a page. Only a full-screen dialog may have a short confirmation or alert on top.DLG-003Name the action on the destructive button and always offer CancelIn a dialog that asks the person to confirm a destructive action, label the button that performs it with a verb that names that action, never "OK", "Yes" or another word that does not say what will happen, and always include a button labelled Cancel that is enabled and closes the dialog with nothing changed.DLG-007Keep Tab inside a modal dialog and the page behind it out of useWhile a modal dialog is open, Tab and Shift+Tab stay within it, and nothing behind it can be used until it closes.
Guidelines, our choice
The judges are split or too few have spoken, so we choose and give our reasons. A product that chooses otherwise is not wrong.
DLG-004Never make the destructive button the default in a confirmation dialogIn a dialog that asks the person to confirm a destructive action, put initial focus on the Cancel button, never on the destructive button, and never let Enter run the destructive action unless the person has moved focus to that button.DLG-005Give the destructive button the danger style, also when the person asked for the actionIn a dialog that asks the person to confirm a destructive action, give the destructive button the danger style, even when the person has just asked for that action. Its label still names the action, as DLG-003 says, so colour is never the only sign of danger.
All rules · Is shadcn accessible? · Give the rules to an AI agent
Justified by Blackwood & Blake helps you work toward WCAG 2.2 AA. It is not a certification. Read more
Justified is an independent project. It is not affiliated with, sponsored or endorsed by the organisations whose guidance it cites. Read the full statement.
Apple is a trademark of Apple Inc., registered in the U.S. and other countries and regions.