Skip to content

[composite] Fix nested list reorder detection - #5156

Merged
jjenzz merged 3 commits into
mui:masterfrom
jjenzz:fix-compositelist-reordering
Jul 6, 2026
Merged

jjenzz merged 3 commits into
mui:masterfrom
jjenzz:fix-compositelist-reordering

Conversation

@jjenzz

@jjenzz jjenzz commented Jul 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #5125

Summary

  • Walks up the DOM tree to find the common ancestor of each adjacent pair of registered items, and observes those boundaries for child-list mutations, covering nested/grouped list changes.
  • Only re-sorts when a mutation batch contains a moved node and the registered items are no longer in their last known document order.

Why

CompositeList derives item indexes from DOM order so keyboard navigation and typeahead can move through the same order the user sees. Before this change, reorder detection watched each registered item's immediate parent:

mutationObserver.observe(node.parentElement, { childList: true });

That works for a flat list where items are direct siblings. If React moves an item from one position to another under the same parent, that parent receives child-list mutations and CompositeList can re-sort the map.

Grouped lists are different. Items are often nested under group wrappers:

List
  Group: API Reference
    Item: Tabs > Tab
    Item: Tabs > Root
  Group: Components
    Item: Tabs

If the item keeps the same key (key={item.id}), React can preserve the component and DOM node which means it doesn't unmount/remount. That is usually desirable, but in this case, if a later query moves the highlighted item's group elsewhere in the list (key={group.id}), the register/unregister callbacks do not fire for the item, nor the mutation observer, so the item keeps the index it had before the reorder.

If CompositeList misses that move, the item may still think it has composite index 0, so activeIndex = 0 continues to highlight it even though another item is now visually first, and this created the strange keyboard navigation ordering.

Observing the common ancestor of each adjacent pair of items fixes this by watching the boundaries that actually own the composite ordering: a reorder that changes item indexes must invert at least one adjacent pair, and the mutation that causes it happens at that pair's common ancestor. For a flat list this collapses to the single item parent; for grouped lists it includes the group wrapper boundaries.

Performance Notes

The observer still only watches child-list mutations (no subtree), and observes one node per adjacent-pair boundary instead of every item's parent, so a flat list collapses to a single observation. The callback also filters out batches that contain no moved node, and verifies the items' relative document order actually changed before re-sorting.

@jjenzz jjenzz added component: composite type: bug It doesn't behave as expected. labels Jul 1, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@5156
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5156
    

commit: a02c3d1

@code-infra-dashboard

code-infra-dashboard Bot commented Jul 1, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+397B(+0.08%) 🔺+150B(+0.10%)

Details of bundle changes

Performance

Total duration: 1,087.29 ms -68.05 ms(-5.9%) | Renders: 78 (+0)

Test Duration Renders
Checkbox mount (500 instances) 74.32 ms 🔺+14.08 ms(+23.4%) 1 (+0)

13 tests within noise — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Jul 1, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit a02c3d1
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a489e9b099ec30008e29f3c
😎 Deploy Preview https://deploy-preview-5156--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@jjenzz
jjenzz force-pushed the fix-compositelist-reordering branch from c9716f3 to b348c70 Compare July 1, 2026 22:01
@jjenzz
jjenzz marked this pull request as ready for review July 1, 2026 22:12
@atomiks

This comment was marked as outdated.

@jjenzz

jjenzz commented Jul 2, 2026 •

Copy link
Copy Markdown
Member Author

@atomiks i love these reviews you do, what prompt/skill are you using to generate them? and i wonder if it is worth setting up some infra to do it automatically on member PRs.

@jjenzz

jjenzz commented Jul 2, 2026 •

Copy link
Copy Markdown
Member Author

@atomiks i've addressed the feedback but i've kept the explicit containerRef prop rather than deriving the container from registered items. Deriving can work, but the component already knows the intended ordering boundary, so passing it explicitly avoids repeated ancestor scans whenever the registered map changes, and seems more predictable. lmk if you have any thoughts there though

@jjenzz
jjenzz force-pushed the fix-compositelist-reordering branch from 1abb1a2 to 2b80075 Compare July 2, 2026 14:31
@atomiks

atomiks commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

@jjenzz Base UI Review Skill — Fable is by far the best reviewer though (which I used here), no other model is as good. /base-ui-review high effort makes the other models get ~close, but /base-ui-review (default medium review effort, cheaper) is still better with Fable I noticed.

i've kept the explicit containerRef prop rather than deriving the container from registered items.

IMO the case for simplicity is worth it here, because the perf seems like it's not an issue. I got Fable to benchmark it:

I benchmarked the derive-vs-explicit question in a real Chrome DOM, mirroring this PR's sortedMap memo and observer setup exactly (realistic fixture: 10-deep app wrappers, popup with arrow siblings, keyed group wrappers). The LCA walk (first.parentElement up until it contains(last)) would run in the same effect as the observer setup, i.e. exactly as often as the compareDocumentPosition sort — so per-map-change cost is the fair comparison:

Scenario sort+rebuild (already per map change) LCA derivation overhead
20 items, 4 groups 2 µs 0.07 µs 3.5%
500 flat 306 µs 0.38 µs 0.12%
500 grouped (50×10) 70 µs 0.43 µs 0.61%
500 grouped, items 10 wrappers deep 86 µs 0.71 µs 0.82%
5000 grouped (500×10) 988 µs 3.7 µs 0.37%

The "repeated ancestor scans" are sub-microsecond — under 1% of work the memo already does on the identical trigger, and ~100–200× cheaper than the per-parent observe() loop the fallback runs per map change today. On predictability: the derived LCA resolved to exactly the list element in every scenario, which is tighter than the popupRef we pass explicitly in Menu/Select (that subtree includes the arrow nodes). Deriving would also let us drop the prop, the 10-file ref threading, the per-render merged refs in CompositeRoot/Tabs/Accordion/ComboboxList, and most of the +351B — the PR becomes CompositeList + tests. One real edge: items split across portals can push the LCA toward body; guarding with "if the walk reaches body, use the per-parent fallback" covers it.

Might be worth checking also with Codex to see if it can verify+reproduce that, or do it manually with DevTools

@jjenzz
jjenzz force-pushed the fix-compositelist-reordering branch 8 times, most recently from bcd8e72 to 9f2825c Compare July 3, 2026 17:28
@jjenzz
jjenzz force-pushed the fix-compositelist-reordering branch from 9f2825c to e5db5e0 Compare July 3, 2026 17:30
@jjenzz

jjenzz commented Jul 6, 2026

Copy link
Copy Markdown
Member Author

thanks @atomiks 🙌

@jjenzz
jjenzz merged commit 2437d81 into mui:master Jul 6, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: composite type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[docs] Command palette arrow keys out of order

2 participants