Manage focus and keyboard interactions
Manage focus as part of interaction design
Keyboard focus identifies the element that receives keyboard input. Keep focus visible and follow the reading order. Opening a modal is a change of context: focus moves into the dialog; the background is not interactive; Escape or a visible close control closes it; focus normally returns to the opener if it still exists. Use the native dialog with showModal where supported rather than recreating modality with ARIA alone. A dialog with open alone is nonmodal. A dialog needs a meaningful name and an appropriate initial focus target. For long content, a heading with tabIndex=-1 may be a better starting point than a distant button.
import React,{useId,useRef} from 'react';export default function TrailDialog(){ const title=useId(),dialog=useRef(null),opener=useRef(null),close=useRef(null); function open(){dialog.current.showModal();close.current.focus();} return <><button type="button" ref={opener} onClick={open}>Trail safety details</button> <dialog ref={dialog} aria-labelledby={title} onClose={()=>opener.current?.focus()}> <h2 id={title}>Trail safety</h2><p>Bring water and stay on marked paths.</p> <button ref={close} type="button" onClick={()=>dialog.current.close()}>Close details</button> </dialog></>;}The dialog is always mounted but hidden while closed. showModal makes the background inert; focus is explicitly placed on Close details. The native cancel behavior handles Escape unless it is prevented. The close event restores focus to the still-mounted trigger. If the trigger is removed, choose the next logical destination rather than restoring to an unrelated element. This simple dialog has no nested modal or destructive action. Native behavior may include browser chrome in keyboard cycling; test in each supported browser, not just an assumed Tab loop. The standalone HTML fixture checks native focus, Escape and background inertness; it does not execute this React code.
Move focus after a meaningful page change
import React,{useEffect,useRef,useState} from 'react';export default function TrailViews(){ const [page,setPage]=useState('list'); const heading=useRef(null),previous=useRef(page); useEffect(()=>{ if(previous.current!==page){heading.current?.focus();previous.current=page;} },[page]); return <section><nav aria-label="Trail views"><button type="button" onClick={()=>setPage('list')}>All trails</button><button type="button" onClick={()=>setPage('detail')}>Birch details</button></nav> <h2 ref={heading} tabIndex={-1}>{page==='list'?'All trails':'Birch trail'}</h2> <p>{page==='list'?'Choose a trail.':'Distance: 2 km.'}</p></section>;}This is a local view switch, not a router implementation. On a changed view the heading receives focus after commit; the first render does not steal focus. In a routed app, choose when to update document title and focus using the committed route and navigation intent. Avoid resetting focus for every search keystroke or background refresh. Handle back navigation and loading boundaries deliberately; test keyboard and history behavior. Real navigation should use links. Here buttons select a local view.
Prefer a native disclosure over a custom keyboard widget
import React from 'react';export default function TrailNotes(){return <details><summary>Weather and terrain notes</summary><p>The shaded path can be damp after rain.</p><a href="#meeting">Jump to meeting point</a></details>;}A summary toggles its parent details element using native interaction. Mount this example in a page containing an element with id="meeting"; the link target is a required fixture dependency. The collapsed content is not in the usual Tab sequence. Do not add a competing aria-expanded value that can drift from native state. For custom tabs, menus or comboboxes use a complete established pattern, including appropriate arrow key behavior, focus management and selection semantics; a role by itself implements none of this. Test Tab, Shift+Tab, Enter, Space and Escape where the pattern requires them. Use a keyboard-only pass and check zoom, focus visibility and scroll position. Practice: open, dismiss and reopen the modal, then verify the view change does not steal focus on initial load.