How do you manage focus and ARIA in custom modals and dropdowns?
IntermediateHTML and accessibility interview questionBy UIRound, updated 21 November 2026
Short answer
For a modal, use the native <dialog> element with showModal() first: it moves focus in, makes the rest of the page inert, closes on Escape and returns focus when it closes. A custom modal must do all of that itself, with role="dialog", aria-modal="true" and an accessible name. A dropdown menu needs a trigger with aria-expanded, arrow key navigation inside, Escape to close, and focus back on the trigger.
The modal contract
Whatever you build, a keyboard or screen reader user should get this:
- On open, focus moves into the dialog.
- Tab and Shift+Tab stay inside it.
- Content behind it cannot be focused, clicked or read.
- Escape closes it.
- On close, focus returns to the element that opened it.
- The dialog has a role and a name, so a screen reader announces "Delete this project?, dialog".
These are the dialog part of the wider checklist in how to make a website accessible.
Start with the native dialog
<button id="delete-btn">Delete project</button>
<dialog id="confirm" aria-labelledby="confirm-title">
<h2 id="confirm-title">Delete this project?</h2>
<p>This cannot be undone.</p>
<form method="dialog">
<button value="cancel" autofocus>Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>const dialog = document.querySelector("#confirm");
document.querySelector("#delete-btn").addEventListener("click", () => dialog.showModal());
dialog.addEventListener("close", () => {
if (dialog.returnValue === "delete") deleteProject();
});showModal() puts the dialog in the top layer above every z-index, makes everything outside it inert, moves focus to the autofocus element (or the first focusable one), closes on Escape, and returns focus to the previously focused element on close. A button inside <form method="dialog"> closes the dialog and sets returnValue. Put autofocus on the least destructive action. show(), without "Modal", opens a non-modal dialog and does none of the focus handling.
The native dialog does not stop Tab from leaving the page for the browser's own controls. That is intended behaviour, not a bug to fix.
Building a custom modal
Sometimes a design system needs its own component. Then you implement the contract yourself:
let lastFocused = null;
function openModal(modal) {
lastFocused = document.activeElement;
document.querySelector("#app").inert = true; // the page behind, a sibling of the modal
modal.hidden = false;
modal.querySelector("[data-autofocus]")?.focus();
modal.addEventListener("keydown", onKeydown);
}
function closeModal(modal) {
modal.removeEventListener("keydown", onKeydown);
modal.hidden = true;
document.querySelector("#app").inert = false;
lastFocused?.focus();
}
function onKeydown(event) {
const modal = event.currentTarget;
if (event.key === "Escape") return closeModal(modal);
if (event.key !== "Tab") return;
const items = [...modal.querySelectorAll("button, [href], input, select, textarea, [tabindex]:not([tabindex='-1'])")]
.filter((el) => !el.disabled && el.offsetParent !== null);
const first = items[0];
const last = items.at(-1);
if (event.shiftKey && document.activeElement === first) {
event.preventDefault();
last.focus();
} else if (!event.shiftKey && document.activeElement === last) {
event.preventDefault();
first.focus();
}
}<div class="modal" role="dialog" aria-modal="true" aria-labelledby="modal-title" hidden>
<h2 id="modal-title">Edit profile</h2>
...
</div>aria-modal="true"tells screen readers that content outside the dialog is unavailable. It does not stop focus or clicks, so setinerton the background as well. The modal must not be inside the element you make inert, which is why modals usually render at the end ofbody, or through a React portal.- With the background inert, Tab already skips it. The wrap-around trap keeps focus cycling inside the dialog instead of escaping to the browser UI.
- Lock page scrolling while it is open, and close it on a backdrop click if the design allows that.
Practise the pieces in Implement an accessible focus trap for a dialog, Make the page inert behind an open drawer and Build a modal dialog in plain JavaScript.
Dropdown menus and menu buttons
First decide what the dropdown is. A list of site navigation links is a disclosure: a <button aria-expanded> that shows a list of ordinary links, with Tab moving between them. role="menu" is for application-style action menus, such as "Rename, Duplicate, Delete", and it changes how screen readers expect the keys to work.
<button id="actions-btn" aria-haspopup="menu" aria-expanded="false" aria-controls="actions-menu">
Actions
</button>
<ul id="actions-menu" role="menu" aria-labelledby="actions-btn" hidden>
<li role="menuitem" tabindex="-1">Rename</li>
<li role="menuitem" tabindex="-1">Duplicate</li>
<li role="menuitem" tabindex="-1">Delete</li>
</ul>The expected behaviour for a menu button:
- Enter, Space or Down Arrow opens the menu and focuses the first item. Up Arrow opens it on the last item.
- Up and Down Arrow move between items, usually wrapping. Home and End jump to the ends.
- Enter activates the focused item, closes the menu and returns focus to the button.
- Escape closes the menu and returns focus to the button.
- Tab closes the menu and moves focus on as normal. A click outside also closes it.
aria-expandedchanges to"true"and back every time, in the same code that shows and hides the list.
Move focus with a roving tabindex (focus the item itself) or keep focus on the container and point aria-activedescendant at the current item. The Popover API gives you light dismiss and Escape for free, but not the arrow keys.
Announcing what happened
Some changes do not move focus, such as "Project deleted" after a dialog closes. Put the message in a live region that already exists in the page: role="status" waits for the screen reader to finish speaking, and role="alert" interrupts it. Use alert only for urgent errors.
aria-label or aria-labelledby
Use aria-labelledby when visible text already names the element, such as a dialog's heading, so the spoken name always matches the screen. Use aria-label when there is no visible text, such as an icon-only close button: <button aria-label="Close">. When both are present, aria-labelledby wins. What is ARIA covers the full naming rules.
Common follow-up questions
Where should focus go when a modal opens?
To the first useful control, usually the least destructive action. For a long dialog that starts with text, focus the heading with tabindex="-1" so a screen reader starts reading at the top.
What if the trigger no longer exists when the modal closes?
For example, the dialog deleted the row that opened it. Move focus to a sensible nearby element, such as the next row or the list heading. Never let focus fall back to body.
How do nested modals work?
Keep a stack. Only the top modal handles Escape, and each close restores focus to the element that opened that modal. Build a modal stack manager practises this.
Mistakes to avoid
Setting
aria-modal="true"and assuming it blocks the backgroundUse
inertorshowModal().Forgetting to return focus to the trigger, which leaves keyboard users at the top of the page
Putting
role="menu"on site navigation, which tells screen reader users to expect arrow keys that do not existLeaving
aria-expandedout of sync with whether the list is visibleAdding a live region at the same moment as its message, so nothing is announced
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Implement an accessible focus trap for a dialogMedium40 min
- Design an accessible modal dialog component for a design systemHard45 min
- Design an accessible dropdown menu and popover componentHard45 min
- Build a modal dialog in plain JavaScript: Escape, backdrop clicks, focus return and scroll lockEasy30 min
- Build a modal stack manager: nested modals, focus restore, scroll lock counting and Escape for the top oneHard60 min
- Make the page
inertbehind an open drawer: restore each element's previous inert stateEasy25 min