Skip to content

TreeView: reduce hydration and mount cost for large trees - #8684

Merged
mattcosta7 merged 8 commits into
mainfrom
mattcosta7-treeview-hydration-cost
Nov 7, 2026
Merged

mattcosta7 merged 8 commits into
mainfrom
mattcosta7-treeview-hydration-cost

Conversation

@mattcosta7

@mattcosta7 mattcosta7 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes #

TreeView was one of the most expensive components to hydrate per row. The two biggest costs were:

  • Focus management set up through focusZone: about 10–13% of hydration CPU.
  • Shortcut text built for every row, even rows without secondary actions: about 5%.

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:

Fixture Before After Total
840 rows, depth 2 21.3 / 43.3 19.8 / 36.1 0.83×
3,396 rows, depth 5 64.9 / 147.5 62.1 / 118.0 0.80×
840 rows with secondaryActions 41.3 / 106.8 40.9 / 97.4 0.91×

These 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). focusZone went from 9.9% and 12.6% of busy JS to 0.7% and 0.6% for its replacement.

Changelog

New

  • Internal useTreeRovingTabIndex hook, used only by TreeView.

Changed

  • Shortcut text. The secondary action shortcut text and usePlatform are computed once per tree instead of per item.
  • Focus management. TreeView no longer uses useRovingTabIndex/focusZone.
    • Rows render tabIndex={-1}. The row that is current when it mounts renders 0.
    • One querySelector picks the tab stop, and focusin moves it.
    • Navigation keys reuse getNextFocusableElement.
    • Nested controls (for example a button in LeadingAction) leave the tab order on mount, and again when rows are added.
    • Alt/Meta+ArrowLeft/ArrowRight are left to the browser, matching the row's own keydown handler.
    • If the tab stop unmounts, a new one is chosen.
  • SubTree name. SubTree is named with aria-labelledby, pointing at the parent item's label, or with the parent's aria-label. This replaces a mount effect that read textContent and re-rendered every subtree.
  • Behavior differences:
    • SSR. Server-rendered markup has one tab stop (the current row) instead of one per row. With no current row, there is no tab stop until hydration.
    • Click. Clicking a row moves the tab stop to it. Before, a pointer focus on the row text didn't update it.
    • Nested inputs. Navigation keys in nested editable controls are no longer prevented. Before, Backspace in a nested input did nothing.
    • Subtree names. Two cases are fixed. With secondaryActions the name was empty. Visual labels were run into the name ("Directorysrc"). Groups are also named before hydration now.

Removed

  • Nothing public. useRovingTabIndex is unchanged.

Rollout strategy

  • No changes needed in github/github-ui
  • Needs changes in github/github-ui
  • Ships with feature flag(s)
  • Not sure
  • None

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, with VITEST_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 hydrateRoot tab 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 current items 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, WebKit ariaSnapshot). After hydration, all group names match base. In SSR markup every group is now named; before, none were.

Not done here: collapsing LevelIndicatorLines into 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.

mattcosta7 and others added 4 commits November 4, 2026 22:04
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-bot

changeset-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 31dc4ab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

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

@github-actions github-actions Bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

⚠️ Action required

👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Check the integration testing docs for step-by-step instructions. Or, apply the integration-tests: skipped manually label to skip these checks.

@github-actions
github-actions Bot temporarily deployed to storybook-preview-8684 November 5, 2026 04:45 Inactive
@mattcosta7
mattcosta7 marked this pull request as ready for review November 5, 2026 11:51
@mattcosta7
mattcosta7 requested a review from a team as a code owner November 5, 2026 11:51
@mattcosta7
mattcosta7 requested review from tay1orjones and a balanced review from Copilot November 5, 2026 11:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Copilot AI commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

One or more custom setup steps configured for this repository failed during this Copilot code review run:

Setup Playwright

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 copilot-code-review.yml file. Read the docs for details.

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>
@github-actions
github-actions Bot temporarily deployed to storybook-preview-8684 November 5, 2026 13:39 Inactive
@github-actions github-actions Bot added integration-tests: failing Changes in this PR cause breaking changes in gh/gh and removed integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm labels Nov 5, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@tay1orjones tay1orjones left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thought, not a blocker to merge though

setTabStop(getPreferredItem())
}
})
observer.observe(container, {childList: true, subtree: true})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@primer-integration

primer-integration Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Integration test results from github/github-ui PR:

Running  Changes   Running
Passed  CI   Passed
Passed  VRT   Passed
Passed  Projects   Passed

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 6, 2026
@mattcosta7
mattcosta7 added this pull request to the merge queue Nov 6, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 6, 2026
@mattcosta7 mattcosta7 added integration-tests: passing Changes in this PR do NOT cause breaking changes in gh/gh and removed integration-tests: failing Changes in this PR cause breaking changes in gh/gh labels Nov 7, 2026
@mattcosta7
mattcosta7 added this pull request to the merge queue Nov 7, 2026
@mattcosta7 mattcosta7 self-assigned this Nov 7, 2026
@mattcosta7
mattcosta7 removed this pull request from the merge queue due to a manual request Nov 7, 2026
@mattcosta7
mattcosta7 added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit 35ff497 Nov 7, 2026
8 checks passed
@mattcosta7
mattcosta7 deleted the mattcosta7-treeview-hydration-cost branch November 7, 2026 00:10
@primer primer Bot mentioned this pull request Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration-tests: passing Changes in this PR do NOT cause breaking changes in gh/gh

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants