Modal Dialogs: The Focus Trap Pattern

A modal dialog is a "focus trap" that overlays the UI, forcing a user to complete a task. It's used for critical confirmations or focused inputs, like a date picker. The main footgun is failing to trap keyboard focus, letting users tab into the inert.
Why it exists
Modals exist to interrupt a user's workflow and demand their full attention for a specific, isolated task. This pattern prevents them from interacting with the rest of the application until the action in the modal is completed or dismissed, ensuring critical steps are not missed.
The mental model
Think of a modal as a temporary, mandatory overlay that hijacks user input. It creates a "focus trap," meaning all keyboard interactions, like Tab and Shift + Tab, are confined within its boundaries. The underlying page is inert until the modal is closed.
How it works
When a modal opens, focus moves to an element inside it. The Tab key cycles forward through its focusable elements, and Shift + Tab cycles backward, looping within the modal instead of escaping to the page behind it. The Escape key must always provide a way to close the dialog. Upon closing, focus should return to the element that originally opened the modal, restoring the user's context. The background content is typically dimmed to visually reinforce its inactive state.
When to use it
Use modals for tasks that require user confirmation before proceeding, especially for destructive actions like deleting data. They are also effective for self-contained sub-flows that need user input, such as a login form or a date picker, where context-switching would be disruptive.
When not to use it
Avoid modals for non-essential information or notifications that don't require immediate action, as they can be frustratingly disruptive. If content is purely informational, consider a less intrusive pattern like a toast notification. A key footgun is setting initial focus on a destructive action; always default to the safest choice, like a "Cancel" button.
One canonical example
A "Delete Item" confirmation dialog. When a user clicks "Delete," a modal appears asking "Are you sure?" with two buttons: "Confirm Delete" and "Cancel." By default, focus is set on "Cancel," the least destructive action. If the user hits Escape or clicks "Cancel," the modal closes and focus returns to the original "Delete" button. Tabbing only cycles between the two buttons inside the modal.
Interview question
According to the card, what is the primary purpose of a modal dialog's "focus trap"?
- a.To prevent users from clicking on elements outside the modal while it is open.
- b.To automatically restore focus to the element that initiated the modal once it is dismissed.
- c.To visually dim the background, signaling that the main content is temporarily unavailable.
- d.To ensure that keyboard navigation (Tab, Shift+Tab) cycles only through elements within the modal.Correct
Why? this is the answer
The card explicitly states that a "focus trap" means "all keyboard interactions, like Tab and Shift + Tab, are confined within its boundaries." While other options describe aspects of modal behavior, the focus trap specifically manages keyboard navigation. Preventing clicks outside the modal is a general modal characteristic, not the specific function of the 'focus trap' pattern.
Just read this? Test yourself on what you have been reading.
Read the original → w3.org
- #design systems
- #accessibility
- #ui patterns
- #frontend
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on design systems — each one lists the topics its interview covers.
See open roles