Paj Digital Labs
All articles
Web Design & Engineering

Accessible Dialogs: Focus, Escape and the Page Behind the Window

A dialog must manage more than its appearance. Use a maintained primitive, move focus deliberately and keep the background unavailable until the task ends.

Paj Digital Labs 4 min read
Web design workspace with desktop and laptop displays
Editorial illustration · Web Design & Engineering

A dialog opens, but keyboard focus stays on the button behind it. Tab moves through the hidden page, Escape does nothing, and closing the window sends focus to the top of the document. Visually it looks like a dialog. Behaviourally it is an overlay with a trapdoor.

Dialogs interrupt the normal page flow. That interruption creates responsibilities around focus, semantics and recovery. A maintained dialog primitive is usually a better foundation than a custom absolutely positioned div.

Choose whether you need a dialog

Use a dialog for a focused task or decision that belongs in the current context. A full article, complicated settings area or substantial workflow often deserves a page with a URL instead.

Distinguish modal from nonmodal behaviour. A modal prevents interaction with the background; a nonmodal does not. Do not style one as the other and leave keyboard users guessing which world they are in.

Confirmation dialogs should contain the actual consequence. "Are you sure?" without the affected record or action is insufficient, especially for deletion or payments.

Use the platform or a proven primitive

Native dialog with showModal provides important browser behaviour, including modality. Component libraries can provide robust patterns across supported environments. Either choice still needs accessible naming, appropriate content and testing.

This minimal browser example illustrates the native lifecycle, not a complete application component:

html
<button id=class="text-code-string">"open-preferences">Preferences</button>
<dialog id=class="text-code-string">"preferences" aria-labelledby=class="text-code-string">"preferences-title">
  <h2 id=class="text-code-string">"preferences-title">Preferences</h2>
  <form method=class="text-code-string">"dialog">
    <button value=class="text-code-string">"cancel">Close</button>
  </form>
</dialog>
<script>
  const trigger = document.getElementById(class="text-code-string">"open-preferences");
  const dialog = document.getElementById(class="text-code-string">"preferences");
  trigger.addEventListener(class="text-code-string">"click", () => dialog.showModal());
</script>

In a server-rendered framework, browser-only work belongs after hydration or in the framework's supported client lifecycle. Use your existing design-system controls and dialog component instead of introducing a second visual vocabulary.

Put initial focus in the right place

For a short form, the first relevant input may be appropriate. For a long explanation, focus a meaningful heading or container so the beginning is available. For a destructive decision, the least destructive action is often a sensible starting point.

Do not automatically focus a control far down a scrollable dialog and leave the title out of view. Initial focus should help the visitor understand the context before acting.

If a library offers default focus handling, review it for this task rather than overriding it reflexively. Custom behaviour adds another obligation to test.

Keep focus inside a modal

Tab and Shift-Tab should remain within the available dialog controls. Background elements should not receive interaction while the modal is open. A translucent backdrop does not accomplish that by itself.

Nested dialogs, portals and popovers complicate focus management. Prefer the supported composition patterns of the chosen library. Hand-written keydown handlers often miss dynamically added controls, disabled elements or focus moving through shadow DOM.

Make a visible focus indicator available on every control. A correct focus order that nobody can see is still a poor interaction.

Define closing behaviour

Escape usually dismisses a dialog when cancellation is safe. Clicking the backdrop may also dismiss it for lightweight tasks, but should not accidentally discard important work or confirm a destructive action.

When closing, return focus to the opening control if it still exists. If the operation removed that control's row, move focus to a logical successor such as the next row action or the section heading.

A submitted form with an error should keep the dialog open, preserve values and move or announce errors appropriately. Closing on every submit because the button uses a dialog form method is wrong when validation or a remote action can fail.

Manage scrolling and mobile height

Keep the dialog within the visible viewport and make long content scrollable without hiding its close control. Mobile browser chrome and the onscreen keyboard change available space; test actual form entry.

Avoid a fixed height that makes the primary action inaccessible on a small device. Scroll locking should preserve the page position when the dialog closes and should not cause a large layout jump.

If the task becomes a full-screen experience on mobile, its semantics and focus responsibilities still apply. A different presentation does not remove the need for an understandable title and escape route.

Test the complete lifecycle

Open with a keyboard, tab forward and backward, activate the primary action, trigger validation, dismiss with Escape and reopen. Test a screen reader's announced name and description. Use long text and a small viewport.

Verify what happens after success when the initiating item disappears. Confirm that opening and closing repeatedly does not leave the body scroll locked or background controls unavailable.

An accessible dialog makes the interruption predictable. The visitor knows where they are, what the decision affects and how to leave. That is the behavioural polish that a shadow and animation cannot provide.

Paj Digital Labs

The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.

Related reading