download

How should a modal dialog manage keyboard focus?

Frontend~6 minReviewed 30 Nov 2026Free · No sign-in

Question

You open a confirmation modal from a Delete button. Pressing Tab reaches a link behind the modal. Describe the keyboard behavior you would expect and how you would implement it. The dialog offers Cancel and Delete.

Show hints (2)
  1. "START INSIDE DIALOG"

    Choose a sensible first focus target for an action that is hard to undo.

  2. "LOOK BEYOND OPENING"

    Think about Tab, Shift+Tab, Escape, and where focus goes after close.

Show checked explanation

Use a named <dialog> opened with showModal(). Move focus to Cancel for this destructive action. Keep keyboard navigation within the modal, let Escape cancel, and return focus to the opener or a sensible target when it closes.

Initial focus, blocked background interaction, Escape, and focus return were checked in Chromium.

Walkthrough

Name the dialog with its visible heading using aria-labelledby. Put autofocus on Cancel, so the first key action is safe. showModal() makes the underlying page inert. Check keyboard navigation in both directions. If confirming deletion removes the opener, move focus to a logical control in the updated list.

Common mistakes

  • Using a visual overlay while leaving background links reachable by keyboard.
  • Adding aria-modal to a custom overlay without making the background inert or handling focus.
  • Returning focus to a control that was removed after confirming the action.

Follow-up questions

  • "Can the same dialog use show()?" show() opens a non-modal dialog. For this confirmation use showModal() so the underlying page cannot be interacted with while the modal is open.
  • "Should initial focus always go to the first button?" Choose it for the task. For a destructive confirmation, Cancel is a safe default. For a long dialog, focusing the heading can help people start reading.

What a strong answer covers

Cover the dialog name, safe initial focus, keyboard navigation, Escape, and the return path.

Open in interactive practice deck →

Original OfferVibe question. WAI modal dialog pattern · HTML dialog element.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.