Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 38 ยท Keyboard and Focus

Managing Focus in Dialogs and Widgets

Learn how to move focus into dialogs and back out, trap it only when needed, build a roving tabindex toolbar, and keep focus sensible when content changes dynamically.

Static pages need little focus management, because the browser does the right thing. Interactive pages are different: when a dialog opens, a view changes or an item is deleted, the browser does not know where the user's attention should go. If you do nothing, focus stays on a button that may have vanished, or on a page behind a modal, and keyboard and screen reader users lose their place.

This lesson covers three jobs: handling dialogs, building widgets with a roving tabindex, and moving focus after dynamic changes. The rule behind all of them is simple: focus should always go where the user's attention goes, and should return when they are done.

Dialogs

A modal dialog should do four things:

  1. When opened, move focus inside it, usually to the first control or the dialog heading.
  2. While open, make the rest of the page unreachable: no Tab to the background, and hidden from AT.
  3. Escape closes it.
  4. When closed, return focus to the control that opened it.

The native <dialog> element does most of this when opened with showModal(): it moves focus inside, makes the rest of the page inert, closes on Escape, and returns focus to the previously focused element. That is why you should prefer it to hand-built overlays.

A native modal dialog

Open the dialog, press Tab several times (focus stays inside), press Escape, and observe that focus returns to the "Rename file" button.

If you build a dialog by hand, use role="dialog" (or alertdialog), aria-modal="true", a name via aria-labelledby, make the background inert, handle Escape, and restore focus yourself.

Focus in a dialog

Move in Focus the first control
Contain Background is inert
Escape Closes the dialog
Return Back to the opener

Use native dialog when you can.

1 / 2

Roving tabindex

A toolbar with eight buttons would cost a keyboard user eight Tab presses to get past. The ARIA pattern for such composite widgets (toolbars, tab lists, radio groups, menus, listboxes) is a single tab stop with arrow keys moving inside. The technique is the roving tabindex: the current item has tabindex="0", all the others -1, and arrow keys move the 0 and call focus().

A toolbar with roving tabindex

The toolbar is one stop in the tab order. Items toggle with aria-pressed, a state announced as "pressed" or "not pressed". Follow the ARIA Authoring Practices for each pattern, because key conventions differ between toolbars, tabs and menus.

Moving focus after dynamic changes

Change Where focus should go
A dialog opens Inside it, then back to the opener on close
An item is deleted The next item, or the list heading if it was the last
A new route or view loads in a single-page app The new h1 (give it tabindex="-1")
Form submission fails An error summary at the top, or the first invalid field
Content expands inline Usually nowhere: leave focus on the button
A toast or status message appears Nowhere: announce it with a live region

Do not steal focus without a good reason. Moving focus on a timer or on page load is disorienting, and moving it when the user did not act violates WCAG 3.2.1 spirit.

Focus after deleting an item

After a deletion, focus goes to the delete button now in the same position, or to the heading when the list is empty, so a keyboard user can carry on without starting again from the top.

Focus traps: when and how

A trap is wrong in general (WCAG 2.1.2) but correct inside a modal, because the user can always close it with Escape. If you build one, trap Tab and Shift+Tab inside, and always provide a keyboard exit. With inert on the background and a native <dialog>, you rarely need to write the trap yourself.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What should happen to focus when a modal dialog closes?

    Returning focus lets the user continue from where they were.
  2. 2. What is a roving tabindex?

    A composite widget becomes a single tab stop, with arrows moving the 0 between items.
  3. 3. Which element gives you a modal with Escape handling and background inertness?

    Native dialog with showModal handles focus, Escape, backdrop and inert background for you.
  4. 4. After the last list item is deleted, where might focus go?

    Moving focus to a nearby stable element keeps the user oriented when their focused element disappears.

Summary

  • Focus should follow the user's attention and return when they finish.
  • Native <dialog> with showModal() handles most dialog focus work.
  • Composite widgets use a roving tabindex: one tab stop, arrows inside.
  • After deletions, route changes and errors, move focus somewhere sensible, and announce non-focus updates with live regions.

Next: Color Contrast Ratios