Repository navigation
[composite] Fix nested list reorder detection - #5156
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,087.29 ms -68.05 ms(-5.9%) | Renders: 78 (+0)
13 tests within noise — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
c9716f3 to
b348c70
Compare
This comment was marked as outdated.
This comment was marked as outdated.
|
@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 |
|
@atomiks i've addressed the feedback but i've kept the explicit |
1abb1a2 to
2b80075
Compare
|
@jjenzz Base UI Review Skill — Fable is by far the best reviewer though (which I used here), no other model is as good.
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:
Might be worth checking also with Codex to see if it can verify+reproduce that, or do it manually with DevTools |
bcd8e72 to
9f2825c
Compare
9f2825c to
e5db5e0
Compare
|
thanks @atomiks 🙌 |
Fixes #5125
Summary
Why
CompositeListderives 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: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
CompositeListcan 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: TabsIf 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}), theregister/unregistercallbacks do not fire for the item, nor the mutation observer, so the item keeps the index it had before the reorder.If
CompositeListmisses that move, the item may still think it has composite index0, soactiveIndex = 0continues 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.