Skip to content

Lesson 4 of 9 · 25m

Keyboard Operation and Focus Management

Tab order, focus visibility, modality, route changes, and the focus-management patterns dynamic interfaces get wrong.

The quickest honest accessibility test is still the simplest: put the mouse away. Can you reach every control, understand where you are, operate the control, and escape it? Most broken interfaces fail within ninety seconds.

Focus is application state

A keyboard user’s position in the interface is focus. When your code deletes the focused element, opens a dialog, or replaces a route without moving focus, you have effectively blindfolded the user.

// After opening a dialog:
dialog.showModal();
dialog.querySelector("input, button, [href]")?.focus();

// After closing it:
triggerButton.focus();

Returning focus to the trigger is not a courtesy. It preserves the user’s place in the workflow.

Keep the order boring

The default tab order follows DOM order. Positive tabindex values rearrange it unpredictably:

<!-- Do not do this. -->
<input tabindex="3">
<input tabindex="1">
<input tabindex="2">

Use tabindex="0" when a genuinely necessary custom widget must enter tab order, and tabindex="-1" when something should be programmatically focusable but not tabbable, such as a routed page heading. Positive values are almost always a bug.

Focus must be visible

:focus-visible {
  outline: 3px solid #7dd3fc;
  outline-offset: 2px;
}

outline: none without a replacement is not minimal design; it is removing the cursor. If brand colors make the default ring ugly, design a better ring.

Modals are focus boundaries

A real modal must move focus inside, keep Tab cycling within the dialog, close on Escape, expose a label, and return focus on close. The native <dialog> plus showModal() gets you partway there, but you still own labeling and restoration. For non-modal menus and popovers, Escape should close and focus should return unless the user deliberately moved onward.

Field notes

  • A keyboard trap in a widget is severity-one even if mouse users never notice.
  • Test Shift+Tab, not just Tab; backward navigation exposes different ordering bugs.
  • Focus outlines should have enough contrast against adjacent colors, not merely exist as a one-pixel gray line.
  • Infinite scrolling content behind a footer makes the footer effectively unreachable by keyboard.

Was this lesson helpful?

Sign in to rate