useRouteFocus
Moves focus to the new page heading after a client-side route change.
import { useRouteFocus } from "@wunderui/react"Playground
Installation
useRouteFocus comes with WunderUI Core and Pro, in the entry of the package from your plan. See the plans. Then import it:
import { useRouteFocus } from "@wunderui/react"When to use
Use useRouteFocus for
- An app whose pages change without a full page load, with Next.js, React Router or your own router.
- A wizard or a tab-like view where a step replaces the whole content and the user should hear the new title.
Use something else for
- AppLayout for the shell that renders the skip link and the landmark. Use the hook inside it for the heading.
- A live region for a message that appears without a view change, such as a saved toast. Focus on a heading is for new views, not for status messages.
- autoFocus or a plain `focus()` for a field that should be focused in a form or a dialog. The hook is for the heading of a page.
Signature
A single-page app keeps focus on the link that was clicked, so keyboard and screen reader users stay where the old page was. returns a ref for the and focuses it whenever the key you pass changes, the route in most apps. Focus is the announcement: a screen reader reads the heading that takes it.
useRouteFocus<T>(routeKey: unknown): React.RefObject<T | null>Parameters
Returns
A ref object. Put it on the heading of the view; the hook finds the element through it.
Behavior and motion
One effect that depends on the key you pass. It keeps no state, so it never causes a render of its own.
- State
- None. The hook holds a ref for the element and a ref that remembers the first run. The first run does nothing, so opening the app never moves focus.
- Why focus
- A link click that swaps the view leaves focus on the link, and a screen reader says nothing about the new page. Moving focus to the heading makes it read the heading, which is the announcement (WCAG 2.4.3, focus order). Do not add a live region for the same change: the two read at once.
- Making it focusable
- If the element has no , the hook sets . It can take focus from code but is not a Tab stop. A you set yourself is kept.
- Scrolling
- The hook focuses without the browser's own scroll and then scrolls it to the middle of the view, at once, so a sticky navbar never covers it (WCAG 2.4.11). Every scrollable ancestor moves, the page included, so the page can jump to bring the heading to the middle of the window.
- When the heading is missing
- If the ref is empty when the key changes, because the heading renders only after data has loaded, nothing is focused. Render the heading in the first render of the new view.
- Server rendering
- The effect runs only in the browser, and nothing is read during render, so there is no hydration mismatch.
- Cleanup
- There is nothing to clean up: no listeners, no timers. In development with React Strict Mode the effect runs twice on mount; the second run sees the same key and leaves the focus alone, so the heading takes the focus only when the key changes.
- Motion
- The hook does not animate. The scroll is instant () and the focus change is not a visual effect.
- Reduced motion
- Nothing to switch off, since nothing moves smoothly. The heading's own focus style is yours; shows it for keyboard use.
Best practices
- Do
Pass a key that changes only when the view changes, such as the pathname. A key that changes while the user types steals focus from the field.
- Do
Put the ref on the one of the view, and render it in the first render of every view.
- Do
Keep a visible focus style on the heading, or at least do not remove the outline for keyboard focus.
- Don’t
Do not add an region that announces the same page change. Focus is the announcement; two voices talk over each other.
- Don’t
Do not use it for dialogs, toasts or inline updates. A dialog takes focus itself, and a status message belongs in a live region.
- Do
Add the skip link and the landmark with , so keyboard users can also jump past the navigation.
Notes for AI
Give your coding assistant the page or the MCP call instead of screenshots.
get_component("useRouteFocus")import { useRouteFocus } from "@wunderui/react"The full library index for assistants is at /llms.txt.