No Need to Trap Focus on a Dialog Element

I was developing a `Modal` component using the `

` element’s `showModal` method. During testing, I noticed that I could tab out of the `` (in modal mode) and onto the address bar.

This surprised me, as accessibility guidelines for modals typically recommend trapping focus within the modal. This seemed incorrect to me.

Further research revealed that trapping focus within the `

` (even in modal mode) is no longer necessary. Thus, focus-trapping is outdated advice when using ``.

Instead of reading the entire GitHub Issue discussion, here are some key points from notable contributors.

Scott O’Hara provides context on the focus-trapping advice history:

WCAG does not normatively require focus to be trapped within a dialog. The WCAG spec does not mention focus behavior requirements in a dialog.

The 2.4.3 focus order understanding document discusses limiting focus behavior within a dialog in the context of a scripted custom dialog, written before `inert` or `

` were widely available.

The APG demonstrates ARIA usage. Without native HTML features like `

` or `inert`, trapping focus within a custom dialog is easier than achieving the `` element’s behavior.

Both the APG modal dialog and the WCAG understanding document were written before the `inert` attribute or the `

` element were widely supported. Instructing developers to trap focus in the dialog prevented the need to ensure all focusable elements outside the modal dialog had `tabindex=-1`.

Léonie Watson explains why it’s acceptable for a screen-reader user to move focus to the address bar:

In the page context, users can tab out of the bottom, navigate browser chrome, use keyboard commands to move to the address bar or open a menu, close the tab, etc. This provides choices for escaping the context.

It seems logical for the same options to be available in a dialog context.

Matatk shared the W3C’s Accessible Platform Architectures (APA) Working Group’s conclusion that the `

`’s `showModal` method doesn’t need to trap focus:

Several APA meetings concluded that the native dialog element’s current behavior should remain, allowing tabbing from the dialog to browser functionalities.

This benefits keyboard users, enabling actions like opening a new tab or changing browser settings. The dialog element provides a natural escape mechanism, such as moving to the address bar, in situations like kiosks where standard keyboard shortcuts are unavailable.

It appears we don’t need to worry about focus trapping when using the Dialog API’s `showModal` method correctly!

Hope this news makes component building easier for you. 😉

Similar Posts