Focus Management

Interactive components must deliberately manage where keyboard focus goes when content opens, closes, changes, or is dynamically inserted. Good focus management keeps keyboard users oriented and prevents focus from becoming lost or trapped unexpectedly.

Focus is the browser's current keyboard interaction target. Users who navigate with Tab, Shift+Tab, or assistive technology depend on a predictable focus order, so interactive UI should preserve a logical relationship between the user's action and the element that receives focus next.

When a component opens, focus often needs to move into the newly available content. A dialog, for example, should generally place focus on an appropriate control inside it rather than leaving focus behind the overlay. When the component closes, focus should normally return to the control that opened it, provided that control still exists and is usable.

Dynamic interfaces require explicit handling because the DOM can change without producing an obvious visual cue for keyboard users. After adding, removing, or replacing content, determine whether focus should remain where it is, move to the new content, or return to another meaningful control. Avoid moving focus merely because the screen changed; unnecessary focus changes can be disorienting.

Focus management is different from simply making elements focusable. Native interactive elements such as buttons and links already participate in keyboard interaction, while custom widgets may require keyboard behavior, focus styling, and sometimes roving `tabindex` or `aria-activedescendant`. Focus should remain visible and should follow the component's intended interaction model.

Modal dialogs require special care: while open, keyboard interaction should remain within the modal according to the chosen dialog pattern, and the underlying page should not accidentally become reachable. After closing, focus should be restored to a sensible element, with fallback behavior if the original trigger has disappeared or become disabled.

Key points

Interview questions

What is focus management, and why is it important in frontend accessibility?

Focus management means intentionally controlling where keyboard focus goes as an interface changes. It matters because visual changes do not necessarily tell keyboard users what became available or what happened. Poor focus handling can leave users behind an overlay, at a removed element, or in an unexpected part of the page. Good focus management keeps interaction predictable and preserves context.

How would you manage focus for a modal dialog?

When the dialog opens, I would move focus to an appropriate focusable element inside it, often the first meaningful control or a deliberately chosen heading or control. While the modal is active, keyboard interaction should follow the modal dialog pattern rather than allowing accidental navigation into the obscured page. When it closes, I would normally restore focus to the element that opened it. If that element no longer exists or cannot receive focus, I would use a sensible fallback based on the resulting UI state.

When should you avoid moving focus programmatically?

I would avoid moving focus merely because content changed visually or because a component rendered. If the user is already interacting with a valid control, unexpectedly moving focus can interrupt their task and make keyboard navigation confusing. Focus should move when there is a clear interaction or accessibility reason, such as entering a newly opened dialog. Otherwise, preserving the current focus position is usually preferable.

What is the difference between roving tabindex and aria-activedescendant?

With roving `tabindex`, the DOM focus moves among the items in a composite widget, while typically only one item has `tabindex="0"` and the others have `tabindex="-1"`. With `aria-activedescendant`, DOM focus remains on a container while an ARIA property identifies the currently active descendant. Both can support widgets such as listboxes or grids, but they have different implementation and event-handling requirements. The choice depends on the widget pattern and which focus model best fits the interaction.

What happens if the element that currently has focus is removed from the DOM?

The application should not assume that focus will automatically land on the intended replacement. Removing the focused element can leave the user without a useful interaction target, depending on the browser and resulting DOM state. Before or as part of the removal, the component should identify an appropriate fallback target when the interaction requires continuity. This is especially important when deleting rows, closing panels, or replacing navigation structures.