Opening a dialog changes the user’s immediate task. A keyboard user needs to know where they have landed, what they can do there, and where they will return after closing it. Initial focus is part of that journey, not a decorative detail.
A native dialog can be modal or non-modal. Calling showModal() opens it as a modal, places it in the top layer, and makes the rest of its document inert. Calling show() opens it non-modally, leaving the rest of the page available for interaction. A modal inside an iframe blocks its own document, not the surrounding page.
Consider a confirmation before discarding an unsaved draft. The safer action is a useful initial focus choice because discarding is difficult to undo:
<button type="button" id="open-confirm">Discard draft</button>
<dialog id="confirm" aria-labelledby="confirm-title">
<h2 id="confirm-title">Discard this draft?</h2>
<p>Your unsaved changes will be lost.</p>
<form method="dialog">
<button value="cancel" autofocus>Keep editing</button>
<button value="discard">Discard draft</button>
</form>
</dialog>
<script>
const dialog = document.querySelector('#confirm');
document.querySelector('#open-confirm').addEventListener('click', () => {
dialog.showModal();
});
dialog.addEventListener('close', () => {
if (dialog.returnValue === 'discard') {
// The application would handle the discard here.
}
dialog.returnValue = '';
});
</script>
In a supported browser, a keyboard user can focus the opening button and activate it. The dialog opens with focus on Keep editing. Tab moves through its controls; activating either button closes the dialog. Escape normally closes this modal too. The browser’s dialog closing steps attempt to restore the previously focused element, which in this flow is normally the opening button.
The example only reports the chosen outcome to its close handler. An application must implement the actual discard separately. Keeping the destructive operation out of the opening action gives the person an opportunity to review the consequence. The visible heading and warning provide context for that decision, while the two buttons give it a clear outcome.
That return is a workflow to test, not a reason to ignore changes to the page. If the opening control is removed after an action, arrange a logical alternative focus target. If a completed action creates a new item, focusing that item may make more sense than returning to the old button. Avoid moving focus again unnecessarily when the native return already serves the user.
Autofocus should be intentional. A short confirmation may start on its least destructive action; a longer dialog may need focus near its beginning so that important context is not skipped. For lengthy content, consider focusing a suitable element near the start rather than placing focus immediately on a button at the bottom. Give the dialog an accessible name, keep an explicit close or cancel control, and do not put a tabindex on the dialog element itself.
Test opening, Tab and Shift+Tab, Escape, closing, and focus return with the browsers and assistive technologies you support. Do not manually remove the open attribute to close a modal: use a dialog closing mechanism so its modal state is cleaned up.

The Campfire
CommentsThanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.
Add a comment