Fix keyboard trap in a custom modal dialog

Tests accessible modal focus management. Strong answers: role="dialog" and aria-modal="true", move focus on open, trap Tab/Shift+Tab cycles inside, Escape to close, return focus to trigger. Red flag: CSS-only fixes or aria-hidden without JS focus control.
What's really being asked
Whether you understand that an accessible modal is a focus management problem, not just a visual overlay problem. Interviewers want to see that you know the W3C Dialog Pattern requirements: the dialog must be perceivable as a modal, focus must be contained, and the user must be able to exit and return to their prior context. They are checking if you can translate ARIA specifications into concrete JavaScript behavior.
The full answer
First, ARIA semantics on the container. The dialog element needs role="dialog", aria-modal="true", and usually aria-labelledby pointing to the title. Second, initial focus placement. On open, move focus into the dialog with JavaScript, typically to the first focusable element. If the content is long or structurally complex, focus a static element like the heading by giving it tabindex="-1". Third, focus trapping. Listen for Tab and Shift+Tab keydown events. If Tab is pressed on the last tabbable element, move focus to the first. If Shift+Tab is pressed on the first, move focus to the last. You can query tabbable elements with selectors for visible links, buttons, inputs, and elements with tabindex="0". Fourth, close behavior. Bind Escape to close the dialog. Fifth, focus restoration. Store the element that triggered the dialog, and on close call .focus() on it. If that element is gone from the DOM, move focus to a logical workflow successor.
The mistakes people make
Relying on aria-hidden="true" on the background without actually moving focus into the modal or trapping it. Using positive tabindex values anywhere. Forgetting to restore focus on close, which strands keyboard users. Suggesting only CSS solutions like pointer-events: none on the backdrop. Failing to handle Shift+Tab wrap, creating a one-way trap. Not using aria-modal so screen readers do not treat the background as inert.
What usually comes next
How do you make the background truly inert for screen readers? The answer is using the inert attribute or aria-hidden on sibling content while keeping the dialog outside that subtree. What if the dialog contains an iframe or web component? You may need a MutationObserver or shadow DOM query to update your list of tabbable elements when content changes. How do you handle nested modals? Track a focus stack so each close restores focus to the opener of that specific layer.
A concrete example
A delete confirmation modal. The trigger is a Delete button in a table row. Open: set role="dialog", aria-modal="true", aria-labelledby="delete-title". Move focus to the Cancel button because deletion is destructive. Trap Tab so it cycles between Cancel and Confirm Delete. On Escape or Cancel, close the modal and return focus to the original Delete button. If the row was deleted and the button no longer exists, move focus to the next row or a table summary.
Interview question
A developer builds a modal dialog using divs and ARIA attributes. Keyboard users can Tab into the background page and lose their place when it closes. Which additional implementation is required?
- a.Set tabindex='0' on the dialog container and rely on aria-modal='true' to make the browser handle focus automatically
- b.Add aria-hidden='true' to the background content and use CSS to dim the backdrop
- c.Programmatically move focus into the dialog on open, trap Tab and Shift+Tab cycles, and restore focus to the trigger on closeCorrect
- d.Use positive tabindex values on all interactive elements inside the dialog to keep them first in tab order
Why? this is the answer
The card emphasizes that an accessible modal is a focus management problem requiring JavaScript to move focus on open, trap Tab and Shift+Tab cycles, and restore focus on close. Option B is a tempting distractor because aria-hidden and CSS alone do not actually control focus or prevent users from tabbing out of the modal.
Just read this? Test yourself on what you have been reading.
Read the original → w3.org
- #accessibility
- #aria
- #focus-management
- #modal
- #javascript
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 accessibility — each one lists the topics its interview covers.
See open roles