Course contentsShow
← Web Development
Lesson 1092 of 2,98219. Keyboard Navigation and FocusFree lesson

Focus Restoration Patterns

Storing and restoring focus position when navigating between views or returning from overlays.

Focus Restoration Patterns

What you'll learn: How to remember and return keyboard focus to its previous location when closing overlays or navigating between views.


Why Focus Restoration Matters

Imagine you're reading a book, place a bookmark at page 47, read an appendix, then close it—you'd expect to return to page 47, not restart at page 1. The same principle applies to keyboard focus on web pages.

When users open a modal dialog, navigate to a different view, or trigger an overlay, their focus moves away from where they were. When that interaction ends, good accessibility means returning focus to where they left off, not abandoning them at the top of the page or in a random location.


The Basic Pattern

Before opening a modal or changing views:

  1. Store a reference to the currently focused element
  2. Move focus into the new context (like your modal)

After closing or returning:

  1. Retrieve that stored reference
  2. Return focus programmatically to that element

Think of it as saving your place before jumping elsewhere, then teleporting back when done.


When to Use This

  • Modal dialogs: When the modal closes, return focus to the button that opened it
  • Route changes: In single-page apps, restore focus after navigating back
  • Expandable sections: When collapsing content, return to the trigger
  • Temporary overlays: Dropdowns, tooltips, or pop-ups that appear and disappear

Without focus restoration, keyboard users get "lost" on your page, forced to tab through everything again to find their spot—frustrating and disorienting.


Key Takeaway: Focus restoration means remembering where the user's focus was before an interaction and returning it there afterward, creating a seamless, predictable experience for keyboard and screen reader users.