Skip to content

DLG-006 Move focus into a modal dialog when it opens and back to its opener when it closes

Level
Rule
Verdict
Requirement
Status
proposed, version 1.0.1

Rule

When 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.

Examples

Live examples built with Justified's components. Try them with the mouse, the keyboard or a screen reader.

Do: Focus goes into the dialog and back to its button

Expenses

  • Train ticket, 42.00
  • Lunch, 11.50

Press Add expense: focus is in Amount at once, and the line under the example says so. Close the dialog with Cancel, with Escape, with a click on the dimmed page or by adding an expense: focus is on Add expense again each time, and the next Tab goes on from there.

Don't: Focus stays behind the dialog and is lost on closing

Expenses

  • Train ticket, 42.00
  • Lunch, 11.50

A hand-made dialog that never moves focus. Press Add expense and look at the line under the example: focus is still on the button behind the dialog, and Escape does nothing from there. Tab until you reach the dialog and press Cancel: focus is on nothing afterwards, and you have lost your place.

Do: The button is gone, so focus goes to the next claim

Waiting for approval

  • Claim 1041, Anna Berg
  • Claim 1042, Omar Haddad
  • Claim 1043, Lena Voss

Press Send back on a claim and then Send back claim. The claim leaves the list and so does its button, so focus goes to the claim that took its place, or to the one before it if it was last. Send all three back: focus ends on the heading Waiting for approval, above “No claims are waiting.” That choice of places is ours. Cancel and Escape return to the pressed button as usual.

Don't: The button is gone and focus is lost

Waiting for approval

  • Claim 1041, Anna Berg
  • Claim 1042, Omar Haddad
  • Claim 1043, Lena Voss

The same dialog, but the product writes no fallback. Press Send back and then Send back claim: the button is gone, the system has nowhere to return to, and the line under the example says focus is on nothing. A keyboard or screen reader user must find the list again, and in Chrome the next Tab starts from the top of the page.

Why

A keyboard or screen reader user is wherever focus is. If a dialog opens and focus stays on the button behind it, they are still on the page: they may not know that anything opened, and they must hunt for the dialog with Tab, if they can reach it at all. If the dialog closes and focus drops to the start of the page, they have lost their place and must travel back to it key by key. WCAG 2.2, 2.4.3 Focus Order requires that focus moves in an order that keeps meaning and use intact. The criterion does not mention dialogs. This rule rests on how the Working Group reads it for them: it lists as a failure a dialog that the keyboard cannot get to at once when it opens, or that leaves focus somewhere senseless when it closes (see F85). That failure passes a dialog in two ways when it opens: focus is put in it, or it is simply the next stop in the tab order after its opener. It allows both for any dialog, modal or not. For a modal dialog this rule takes the first way. That is our reading of the criterion, not something the failure says: the Working Group's own modal example and technique move focus in, and the opener cannot keep focus once the page behind is out of use. LAY-002 describes the second way for panels that are not modal. On closing, the Working Group's modal example also accepts the control right after the opener, while this rule follows the failure's test and asks for the opener whenever it still exists. Staying inside while the dialog is open is DLG-007.

Do

  • On the page Expenses, the person presses Add expense. The dialog Add expense opens, with Amount, Category, Cancel and Add expense, and focus is inside it at once. Here it lands in Amount. When the dialog closes, focus is on the page's Add expense button again, and the next Tab goes on from there.
  • Return focus the same way whichever route closed the dialog: Cancel, the close icon, Escape, a press on the backdrop if that closes it, or the main action once it has succeeded.
  • A manager presses Send back on a claim under Waiting for approval. The dialog Send back claim has the field Reason, then Cancel and Send back claim. Once the claim is sent back it leaves the list, and the button that opened the dialog goes with it. Focus goes to the claim that took its place in the list, or to the one before it if it was the last.
  • If that was the only claim, focus goes to the heading Waiting for approval, which can take focus from script (tabindex="-1") and now sits above the text "No claims are waiting." That text is the empty state STA-002a describes.
  • The dialog Change category is opened from an item in a claim's More actions menu. The menu has closed by the time the dialog does, so focus goes to the More actions button of that claim.
  • Which place is "nearby" when the opener is gone is for the product to decide, case by case. The order above, the neighbouring item first and then the heading of the list, is our own choice.
  • A list or date picker opened inside the dialog, such as the one under Category, returns focus to its own control inside the dialog when it closes.
  • This rule does not say which element in the dialog gets focus first, and neither does the criterion: the dialog itself, its heading or a control inside it are all acceptable. DLG-004 decides it for a destructive confirmation, where focus starts on Cancel.
  • A dialog the product opens itself, such as the sign-out warning in STA-004, has no opener. Focus still moves into it, and when it closes it goes back to the element that had focus just before, or to a logical place if that element is gone. The documented failure is about dialogs opened from a control, so this case is our own choice, made in the spirit of the same criterion. Keep such dialogs rare; they move focus when the person did nothing.
  • When a question replaces a dialog's content, as DLG-002 and FRM-004b describe, no dialog opens or closes, but the control that had focus disappears. We treat it as an opening and a closing inside the same layer, which is our own application of this rule. The person presses Escape in Send back claim after typing in the text area Reason. "Discard this reason?" takes the form's place with Cancel and Discard reason, and focus moves to Cancel, as DLG-004 says. Cancel brings the form back with focus in Reason, where it was.
  • When the one short confirmation that DLG-002 allows over a full-screen dialog closes, focus returns to the control in the full-screen dialog that opened it.
  • The adapter's Dialog starts focus on the element marked data-autofocus, or without one on the first control after the close icon, so a form starts in its first field. AlertDialog starts on Cancel. Those starting points are our own choice.
  • On closing, both put focus back on the element that had it when the dialog opened, also when the dialog was opened without a trigger. If that element is no longer in the page, the adapter does nothing, and focus is then lost until the product sets it. Set the fallback yourself in onCloseAutoFocus.
  • The native HTML dialog element opened with showModal() moves focus in and returns it to the opener if the opener still exists. The fallback is still yours to write.
  • Moving focus in, and not leaving the dialog as the next Tab stop, is our reading of the criterion for modal dialogs; see Why.

Don't

  • Open Add expense and leave focus on the button behind it, so a keyboard user must pass every link and field on the page before Amount, or never gets there.
  • Let focus fall to the start of the page when the dialog closes, so the person tabs from the top to get back to Add expense.
  • Return focus after Cancel but not after Escape, or not after the main action succeeded.
  • Leave focus on nothing after Send back claim removed the claim, on the grounds that the opener no longer exists.
  • Send focus to the first link on the page when the next claim in the list is right there.
  • Move focus into the dialog only after its content has loaded, so the first key presses land on the page behind.

Exceptions

  • Non-modal panels, menus and popovers. This rule covers dialogs and sheets that are modal. For a panel that people work beside, LAY-002 says how the keyboard reaches it and comes back.
  • The closing action leaves the page. When the dialog's main action ends on another page, such as Create report that opens the new report, there is no opener to go back to. Where focus starts on the new page is outside this rule.
  • While sending. What focus does between the press on the main action and the reply is covered by STA-005a. Its own choice is to keep focus on the pressed button.
  • Neighbouring rules. Keeping focus in the dialog while it is open is DLG-007. Escape is DLG-001, the order of the controls inside the dialog is LAY-002, and the visible focus indicator is KBD-001. First focus in a destructive confirmation is DLG-004.

Verdict

Requirement. Only the judges decide; the expert witnesses are shown but do not vote. How verdicts work

Stances on DLG-006
SourceRoleStancePage
WCAG 2.2LawRequires2.4.3 Focus Order
Material DesignJudge SupportsDialogs: accessibility
Accessibility designing: flow
Apple Human Interface GuidelinesJudge No position
Carbon Design SystemJudge SupportsDialogs
Modal: accessibility
GOV.UK Design SystemExpert witness No position
Nielsen Norman GroupExpert witness No position

Each stance links to the page it rests on, read on the date in the source register. This page never repeats what a source says; read the source itself.

History

  • Version 1.0.1, : Points to STA-002a for the empty state.
  • Version 1.0.0, : First version. Takes over focus return from DLG-001.

Last checked against the other rules for conflicts on .

More in Dialogs: 5 rules and 2 guidelines · All rules