Notes

Focus is part of the design

11 October 2026 2 min read

A dialog that works with a mouse is half a dialog. Where keyboard focus goes when it opens, while it is open and when it closes decides whether anyone else can use it.

  • Accessibility
  • Keyboard
  • Components

Most dialogs are tested with a mouse. They open, they close, they look right. Then someone uses the keyboard and focus walks out of the dialog into the page behind it, or disappears when the dialog closes and starts again at the top of the document.

The page

Tab around, open the dialog with the keyboard, then press Tab until something odd happens. Escape closes it.

Where focus went

  1. Nothing yet.
Live demo. Everything runs in your browser. Now try to break it.
FocusDialog.svelte, 124 lines

Open Edit address with Enter. Press Tab until you leave the dialog. Then untick both options and do it again. The log on the right shows every move.

Three moments

Focus has a lifecycle, and a dialog has to handle each part of it.

When it opens, focus moves into the dialog. Usually to the first field, or to the dialog itself when the content is long text. If focus stays on the button behind the overlay, a screen reader user hears nothing happen.

While it is open, focus stays inside. Tab at the last element goes to the first, Shift+Tab at the first goes to the last. Otherwise a keyboard user ends up on a link they cannot see, under a grey overlay.

When it closes, focus goes back to what opened it. This is the one that gets forgotten. Without it, the browser drops focus onto the body, and the next Tab starts from the top of the page. On a long page that is a punishment.

The platform can do most of it

The native <dialog> element opened with showModal() traps focus, makes the rest of the page inert, closes on Escape and restores focus to the previous element. If you can use it, use it, and delete your own code.

const dialog = document.querySelector('dialog');
dialog.showModal(); // focus moves in, page behind becomes inert
dialog.close();     // focus returns to the opener

The demo above does it by hand on purpose, because many design systems still carry a custom version, and that is where the bugs are.

Where the manual version goes wrong

The trap is wrong when the dialog contains something that is focusable but not in your selector: a custom select, a contenteditable, an element with tabindex="0". Maintaining a list of focusable selectors is a losing game, which is another argument for the native element.

Restoring focus is wrong when the opener no longer exists. A "Delete" button inside a list item removes the item, and with it the element that should get focus back. Decide in advance where focus goes: the next item, the previous one, or the list heading.

Make it visible

Everything above depends on seeing where focus is. Do not remove the outline without replacing it. Use :focus-visible, so it shows for keyboard users and not on every mouse click, and give it enough contrast against both the background and the element itself. Two pixels of a colour that is not the brand colour is a reasonable start.

:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
}

A ten-second test

Unplug the mouse. Open every dialog, menu and drawer in your product with Enter. Tab through, close with Escape, and look at where you land. If you cannot tell where you are at any point, neither can your users, and they cannot see the screen you are looking at.

Related

How bright was it?

Five lamps and an optional line. Feedback, a bug, an idea, anything. I read every one and sum them up once a month.

Pick a lamp

Stored without name, address or cookie, and deleted after a year. Details in the privacy notice.