Repository navigation
TreeView: reduce hydration and mount cost for large trees - #8684
Conversation
Every TreeView.Item called usePlatform and built the keybinding hint string, even though it is only used when secondaryActions is set and is identical for every item. Compute it once in the root and read it from context. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
TreeView used useRovingTabIndex -> useFocusZone -> focusZone, which on
mount walks every element in the tree with a JavaScript TreeWalker
filter, rewrites tabindex on every row and observes the whole subtree.
That was about 10-12% of TreeView hydration CPU.
Items now render tabIndex={-1}, except the item that is current when it
mounts, which renders 0. A new internal useTreeRovingTabIndex hook picks
one tab stop with a querySelector, moves it on focusin and handles the
navigation keys with the existing getNextFocusableElement. Nested
controls are removed from the tab order when focus first enters the tree
(focus entering from outside is redirected to the current item, as
before), and on later additions. If the tab stop unmounts a new one is
chosen.
Behavior changes:
- Server-rendered markup has one tab stop (the current item) instead of
every row.
- Clicking an item moves the tab stop to it.
- Navigation keys in nested editable controls (e.g. Backspace in an
input) are no longer prevented.
The public useRovingTabIndex hook is unchanged.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Every SubTree read its parent's accessible name in an effect after mount (getElementById + textContent) and stored it in state, which re-rendered every SubTree and wrote aria-label once per subtree. SubTree now points aria-labelledby at the element that labels its parent item, or uses the parent's aria-label. This is in the server-rendered markup, so groups are named before hydration. It also fixes two cases where the old name was wrong: with secondaryActions the name was empty, and visual labels were concatenated into the name. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
🦋 Changeset detectedLatest commit: 31dc4ab The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
There was a problem hiding this comment.
🟡 Changes recommended
Tree keyboard handling blocks Alt/Meta-modified browser navigation shortcuts and can leave nested controls tabbable when focus enters before the listener attaches.
2 open issues
What changed in this PR
Reduces TreeView mount and hydration overhead for large trees by replacing general-purpose focus management and removing repeated per-row work.
Changes:
- Adds a TreeView-specific roving tab-index hook.
- Computes shortcut text once per tree and derives subtree names directly from parent labels.
- Adds regression coverage for tab order, hydration, accessible names, and platform-specific shortcuts.
| File | Description |
|---|---|
packages/react/src/TreeView/useTreeRovingTabIndex.ts |
Implements lightweight tab-stop management and keyboard navigation. |
packages/react/src/TreeView/TreeView.tsx |
Integrates the hook, shares shortcut text, and removes subtree label state. |
packages/react/src/TreeView/TreeView.test.tsx |
Adds focus, hydration, naming, and shortcut regression tests. |
.changeset/treeview-hydration-cost.md |
Records the patch release and server-rendering changes. |
Review effort: Balanced
|
One or more custom setup steps configured for this repository failed during this Copilot code review run: Setup steps run before each review. If the review above is missing context, or no review was posted at all, the failing step above may be the cause. See the workflow run for failure details, fix your setup steps configuration, and re-request a review. Note You can configure setup steps for Copilot code review separately from Copilot cloud agent with a |
The nested-control scan ran only on the first focusin. Until then an aria-hidden LeadingAction could contain a tabbable button (axe aria-hidden-focus), and focus already inside the tree when the hook mounted never triggered the scan. Run the scan during setup instead. Also leave Alt/Meta+ArrowLeft/ArrowRight alone, matching the item's keydown handler, so browser shortcuts such as Back aren't blocked. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
tay1orjones
left a comment
There was a problem hiding this comment.
One thought, not a blocker to merge though
| setTabStop(getPreferredItem()) | ||
| } | ||
| }) | ||
| observer.observe(container, {childList: true, subtree: true}) |
There was a problem hiding this comment.
What if a nested element already in the tree becomes focusable later, for example, an anchor gains an href or an element’s tabIndex changes to 0? I think the hook wouldn't remove it from the tab order, so the tree would end up with multiple tab stops.
There was a problem hiding this comment.
Good catch, fixed in 7d613b4. The observer now also watches href, tabindex, contenteditable and controls, and re-checks only the mutated element, so the cost per change stays constant on large trees. A new test covers a nested link that gains href and a nested button whose tabIndex changes from -1 to 0 after mount; it fails without the change.
For context, useFocusZone on main only re-checked elements when hidden or disabled changed, so these two cases also slipped through before this PR.
…come focusable The roving tab index observer only handled added nodes, so an element already in the tree that later gained `href` or a `tabindex` of 0 became an extra tab stop. Observe those attributes and re-check only the mutated element. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Integration test results from github/github-ui PR: |

Closes #
TreeView was one of the most expensive components to hydrate per row. The two biggest costs were:
focusZone: about 10–13% of hydration CPU.The cost matters for deep trees such as the code-view file tree and the PR files tree. This PR removes both costs and the SubTree label re-render.
Measured on a production React 18 bundle, SSR then hydrate, medians of 7 interleaved rounds, Chromium. Each cell is render+commit / total in ms:
secondaryActionsThese figures include the synchronous nested-control scan added in review. WebKit and CPU profile figures were measured before that change: WebKit total was 0.82× on both the 840-row and 3,396-row trees, and in the CPU profile busy JS drops 16% (840 rows) and 22% (3,396 rows).
focusZonewent from 9.9% and 12.6% of busy JS to 0.7% and 0.6% for its replacement.Changelog
New
useTreeRovingTabIndexhook, used only by TreeView.Changed
usePlatformare computed once per tree instead of per item.useRovingTabIndex/focusZone.tabIndex={-1}. The row that iscurrentwhen it mounts renders0.querySelectorpicks the tab stop, andfocusinmoves it.getNextFocusableElement.LeadingAction) leave the tab order on mount, and again when rows are added.SubTreeis named witharia-labelledby, pointing at the parent item's label, or with the parent'saria-label. This replaces a mount effect that readtextContentand re-rendered every subtree.currentrow) instead of one per row. With nocurrentrow, there is no tab stop until hydration.secondaryActionsthe name was empty. Visual labels were run into the name ("Directorysrc"). Groups are also named before hydration now.Removed
useRovingTabIndexis unchanged.Rollout strategy
Not sure because of the tab order and subtree name changes. I recommend running the github-ui integration workflow for tests that assert
tabindex="0"on rows or the old subtree names.Testing & Reviewing
npx vitest run --project "@primer/react (chromium)" packages/react/src/TreeView packages/react/src/KeybindingHint(React 18): passed, withVITEST_FAIL_ON_CONSOLE=true(TreeView: 135 tests after review fixes).TreeView tests on React 19.3.1, set up with
node script/setup-react-19.mts: passed, 132 tests.npm run type-check: passed.npm run lint: passed.npx turbo run build --filter=@primer/react: passed.19 new tests cover:
SSR plus
hydrateRoottab stop.The first item as the tab stop when nothing is current.
The tab stop following keyboard and pointer focus.
Tab and Shift+Tab with a nested control.
Nested controls and late-mounted
currentitems kept out of the tab order.Tab stop reassignment after unmount.
Nested input key handling.
Six SubTree name cases plus SSR.
Platform-specific shortcut text.
Nested controls removed on mount, including when focus is already inside the tree at hydration.
Alt/Meta-modified ArrowLeft/ArrowRight not handled.
On the old implementation, 5 of the 7 original tab-order tests and 4 of the 7 name tests fail. The 3 tests added in review failed before their fix.
Keyboard parity (Chromium and WebKit, 840-row, 3,396-row and secondaryActions fixtures, Playwright). Base and this branch move focus to identical rows across Tab in/out, Shift+Tab, Arrow keys, expand/collapse, Home, End, PageUp, PageDown, typeahead and Backspace. Per-key handler time is unchanged.
Accessibility tree (Chromium
Accessibility.getFullAXTree, WebKitariaSnapshot). After hydration, all group names match base. In SSR markup every group is now named; before, none were.Not done here: collapsing
LevelIndicatorLinesinto a single element. A prototype cut deep-tree DOM by 33% and hydration by 13–18%. It changes VRT, and gradient backgrounds disappear in forced-colors mode, so it needs its own change.