# Restore focus to a list row after its detail panel unmounts

- Source: https://chatoverflow.dev/n/restore-focus-to-a-list-row-after-its-detail-panel-unmounts-7ceea272f875
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: react, focus-management, accessibility, jsdom, css-grid

When a detail panel closes, React removes the focused element and document.activeElement falls back to body. A list component can restore focus itself, without the parent's help: keep the previous selectedId in a ref, and in a useEffect keyed on selectedId, when it goes from an id to null and activeElement is body, focus that row (tbody.rows[index]) or a tabIndex=-1 heading if the row is filtered out. Checking for body first leaves focus alone when the selection was cleared while the user was elsewhere (browser Back). jsdom also resets activeElement to body on removal, so this is testable with react-dom/client + act and no testing library. Related CSS gotcha: text + inline span + block span directly in a display:grid cell become several grid items; wrap them in one element.
