Skip to content

Articles · Good practices

Modal dialogs: focus, the Escape key and returning to the starting point

A modal dialog opens, but focus stays behind it or never comes back. The four behaviours a modal needs, the native dialog element, and the RGAA criteria.

By

· Published on

The person clicks “Add to basket”. A dialog opens in the middle of the screen to confirm the item was added. Using the keyboard, they press Tab: focus carries on through the page, behind the dialog, on links they can no longer see. With a screen reader, they do not even know that a dialog has opened.

Four expected behaviours

  1. When it opens, focus moves into the dialog, onto its title or its first useful element.
  2. While it is open, Tab and Shift+Tab stay inside the dialog, and the rest of the page is inert.
  3. The Escape key and a visible button close it.
  4. When it closes, focus returns to the element that opened it.

The screen reader also needs to announce a dialog and its accessible name: the dialog role, the modal state and a title linked with aria-labelledby.

The dialog element does most of the work

Opened with the showModal() method, the HTML <dialog> element provides the role, makes the rest of the page inert and closes with Escape. According to MDN, the browser moves focus to the first element in the dialog that can receive it; the autofocus attribute lets you pick another one. The HTML specification says that when a dialog opened with showModal() closes, focus goes back to the element that had it before the dialog opened. Check this in the browsers you support.

<button type="button" id="open">Add to basket</button>
<dialog id="confirmation" aria-labelledby="confirmation-title">
  <h2 id="confirmation-title">Item added to your basket</h2>
  <button type="button" id="close">Continue shopping</button>
</dialog>
<script>
  const dialog = document.getElementById('confirmation');
  document.getElementById('open').addEventListener('click', () => dialog.showModal());
  document.getElementById('close').addEventListener('click', () => dialog.close());
</script>

The RGAA criteria involved

  • Criterion 7.1: « Chaque script est-il, si nécessaire, compatible avec les technologies d’assistance ? » (Is each script, where necessary, compatible with assistive technologies?) This covers the name, role and state changes of the user interface component (test 7.1.1).
  • Criterion 7.3: « Chaque script est-il contrôlable par le clavier et par tout dispositif de pointage (hors cas particuliers) ? » (Can each script be operated with the keyboard and with any pointing device, except in particular cases?) This covers opening and closing with the keyboard.
  • Criterion 12.8: « Dans chaque page web, l’ordre de tabulation est-il cohérent ? » (On each web page, is the tab order consistent?) Its test 12.8.2 covers focus moving into the dialog and back out: « Pour chaque script qui met à jour ou insère un contenu, l’ordre de tabulation reste-t-il cohérent ? » (For each script that updates or inserts content, does the tab order remain consistent?)
  • Criterion 12.9: « Dans chaque page web, la navigation ne doit pas contenir de piège au clavier. Cette règle est-elle respectée ? » (On each web page, navigation must not contain any keyboard trap. Is this rule respected?) This covers a dialog that cannot be left with the keyboard.

The Dialog (Modal) design pattern in the ARIA Authoring Practices wraps Tab and Shift+Tab inside the dialog and closes it with Escape, so the person always has a keyboard way out.

Our advice

Start from the <dialog> element rather than a home-made library. Test the four behaviours with the keyboard alone, then with NVDA and VoiceOver. And ask yourself whether the dialog is needed: a status message in the page is often enough to confirm that an item was added to the basket.

Sources

Leave a Reply

Every comment is reviewed before it is published. Your email address will not be published. Fields marked with an asterisk (*) are required. See the privacy policy.

Read also