Skip to content

[combobox] Split the store into state and context - #5581

Merged
michaldudak merged 8 commits into
mui:masterfrom
michaldudak:claude/combobox-store-split-02fec1
Aug 27, 2026
Merged

michaldudak merged 8 commits into
mui:masterfrom
michaldudak:claude/combobox-store-split-02fec1

Conversation

@michaldudak

@michaldudak michaldudak commented Aug 27, 2026 •

Copy link
Copy Markdown
Member

Splits the Combobox store's single 62-member State bag into 41 reactive fields plus 21 non-reactive ones, and stops hand-maintaining the synchronization effect's dependency array.

Before, nothing in the type said which members were reactive, which were refs, and which were NOOP until the root's first render — across 83 store.state.X reads in 18 files. The synchronization effect wrote 29 keys behind a 29-entry dependency array that had to be updated by hand for every new field, making silent staleness the default failure mode.

What changed

  • 13 RefObjects and 8 command callbacks move from State into ReactStore's context slot. None of them is observed by a selector, no ref is ever written through the store, and the commands are already useStableCallback results.
  • The commands are assigned with store.useContextCallback instead of a first-render store.update.
  • The dependency array is derived from the synchronized values rather than written out by hand.
  • The selectors are registered on the store, so parts read state with store.useState('key') instead of threading the selectors object through all 119 call sites. This follows the ToastStore precedent for the third generic parameter and the call style already used by Drawer and Dialog.

What deliberately did not change

store.useSyncedValues is not adopted yet, even though it is the obvious destination. inputOwnsFormValue has a second writer in ComboboxInput.setInputElement, which writes it from a ref callback with a different formula, so the root has to derive it at effect time and publish it in the same store.update. useSyncedValues derives its dependencies from the values it is given, so using it here would require a second write and would publish an intermediate snapshot to subscribers. It becomes a one-line change once that dual writer has a single owner.

useOnFirstRender is also retained. It is not a pass-through: five parts read the prop bags with useStore during render, and a layout effect commits only after all children have rendered, so seeding them later drops role and aria-* from the server output.

Behavior

No public API, DOM, or behavior change is intended, including the pre-existing inputOwnsFormValue staleness, which is preserved rather than fixed. The removed dependency array held exactly the 28 synchronized values plus the store, so the derived array triggers on an identical set of changes.

A characterization suite lands first, in combobox/store.test.tsx, passing against the unmodified implementation before anything moves. It pins the transaction shape (one store.update, no torn inline/ownership snapshot in either split direction), command availability from a descendant ref callback during the first commit, form-value ownership under StrictMode and across hydration. Each ownership assertion is verified by mutating its writer.

Performance

Store.update copies the whole state on every write ({...this.state, ...changes}), so its cost is linear in the number of keys. Taking State from 62 members to 41 measures at roughly 435 ns less per store update (~17 ns per key, linear across 20-80 keys, no threshold effect; microbenchmarked on a MacBook Pro M3 Pro, Node v26.7.0). Store updates fire on every keystroke, highlight move, and open/close, so it is a real saving — measurable, though well below perceptible.

Follow-ups

  • Give inputOwnsFormValue a single owner, then collapse the synchronization effect into store.useSyncedValues.
  • Seven commands are now stabilized twice, since useContextCallback re-wraps an already-stable callback. Left alone because the locals are consumed inside AriaCombobox itself, including in dependency arrays.

🤖 Generated with Claude Code

michaldudak and others added 7 commits August 27, 2026 10:28
Pin the current synchronization behavior before the state/context split:
the single-transaction shape of the sync effect, absence of torn
inline/ownership state on the useStore path, command availability from a
descendant ref callback during the first commit, StrictMode ownership
stability, and ARIA across hydration.

Co-Authored-By: Claude Opus 5 <[email protected]>
Swap the plain Store for ReactStore so the store gains a non-reactive
context slot and the React helpers. No members move yet and every read,
write and useStore call site is unchanged.

Co-Authored-By: Claude Opus 5 <[email protected]>
The 13 RefObjects are created once and never written through the store,
and no selector observes them, so they do not belong in reactive state.
They move to ReactStore's context slot, which also takes them out of the
object rebuilt on every state update.

Note that the two cast-based test fixtures in combobox/utils are not
type-checked against the store, so their fake stores were migrated by
hand.

Co-Authored-By: Claude Opus 5 <[email protected]>
The 8 command callbacks are already stable and no selector observes
them, so they move to the store context and are assigned with
useContextCallback instead of a first-render store.update.

useOnFirstRender stays for the prop bags: the parts read those with
useStore during render, and a layout effect commits only after all
children have rendered, so seeding them later would drop ARIA
attributes from the server output.

Co-Authored-By: Claude Opus 5 <[email protected]>
The hand-maintained 29-entry dependency array had to be updated by hand
for every new synchronized field, which made silent staleness the
default failure mode. Derive it from the synchronized values instead.

The update stays a single atomic store.update: inputOwnsFormValue has a
second writer in ComboboxInput and must be derived at effect time, so
store.useSyncedValues cannot be used until that dual writer is resolved.
Splitting it out would publish an intermediate snapshot.

The trigger set is unchanged: the removed array held exactly the 28
synchronized values plus the store.

Co-Authored-By: Claude Opus 5 <[email protected]>
Review found the characterization suite green against a mutation that
drops the form name from both the popup input and the hidden input, so a
form would submit nothing at all and every test still passed. Ownership
is a two-sided invariant and was only asserted on the negative side.

Both configurations are now covered: the hidden input owns the name when
the input is inside the popup, and the visible input owns it otherwise,
across hydration. Each assertion is verified by mutating the
corresponding writer.

Also renames the useStore test to what it actually pins. It cannot
detect a split transaction - React batches the listener sweeps, so the
torn pair is never rendered - and only the direct store.subscribe test
guards transaction shape.

Co-Authored-By: Claude Opus 5 <[email protected]>
Role queries never match the type="hidden" inputs that multiple mode
renders, so the helper could not see every control carrying the form
name. Query by attribute instead, which is exact across all three shapes
a named control takes.

Also drops the non-strict renderer and the comments describing it: they
justified a render-count assertion that no longer exists.

Co-Authored-By: Claude Opus 5 <[email protected]>
@pkg-pr-new

pkg-pr-new Bot commented Aug 27, 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@5581
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5581
    

commit: 5cefa9b

@code-infra-dashboard

code-infra-dashboard Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+928B(+0.20%) 🔺+19B(+0.01%)

Details of bundle changes

Performance

Total duration: 1,083.09 ms -73.16 ms(-6.3%) | Renders: 76 (+0) | Paint: 1,743.08 ms -142.93 ms(-7.6%)

Test Duration Renders
Checkbox mount (500 instances) 99.65 ms 🔺+44.64 ms(+81.1%) 1 (+0)
Tooltip mount (300 contained roots) 47.14 ms ▼-18.12 ms(-27.8%) 1 (+0)
Select open (500 options) 41.42 ms ▼-14.04 ms(-25.3%) 14 (+0)

12 tests within noise — details

Metric alarms

Test Metric Change
Checkbox mount (500 instances) bench:paint 🔺 +51.47 ms

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

@netlify

netlify Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 5cefa9b
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a90200e59625b00086cdb9a
😎 Deploy Preview https://deploy-preview-5581--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.

Register the selectors on the store so parts can call
store.useState('key') instead of threading the selectors object through
every call site. Converts all 119 useStore(store, selectors.x) reads and
drops the imports they needed.

Follows the ToastStore precedent for the third generic parameter, and
the store.useState call style already used by drawer and dialog.

Co-Authored-By: Claude Opus 5 <[email protected]>
@michaldudak michaldudak added component: combobox Changes related to the combobox component. internal Behind-the-scenes enhancement. Formerly called “core”. labels Aug 27, 2026
@michaldudak
michaldudak marked this pull request as ready for review August 27, 2026 11:34

@flaviendelangle flaviendelangle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The change make a lot of sense to me
Let me know if you want help to clean other components if we have others

Here is the AI review:

All checks pass: full combobox suite (940 tests), autocomplete suite (77 tests), the new store characterization suite (5 tests), repo typecheck, and eslint are all green. Here is my review.


PR review

This is a large but almost entirely mechanical refactor of the Combobox store: reactive State shrinks from 62 to 41 members, with 13 refs and 8 command callbacks moving into ReactStore's non-reactive context slot, the sync effect's dependency array derived instead of hand-written, and 119 useStore(store, selectors.x) reads rewritten as store.useState('x'). I traced the risk-bearing parts — no selector observes any moved member, no store.state.<movedKey> reference survives the move, command-availability timing during first commit is preserved, and the derived dependency array covers exactly the same 28 synchronized values plus the store. Nothing is merge-blocking; I found no correctness, test, or docs defects.

Bugs (0)

No findings.

Notes from verification:

  • Command timing preserved. useContextCallback assigns into context during the root's render body (AriaCombobox.tsx:1414-1421), before any child renders, exactly as the old first-render store.update inside useOnFirstRender did. The NOOP-seeded window closes before any descendant event handler or ref callback can invoke a command, which the "earliest command probe" test exercises.
  • Derived deps are equivalent. The removed 29-entry array and [store, ...Object.values(syncedValues)] resolve to the same trigger set; syncedValues is a static-key object literal, so Object.values is positionally stable across renders. The pre-existing inputInsidePopup staleness in the inputOwnsFormValue derivation is read but intentionally not in deps — unchanged from before.
  • No reactive readers of moved members. The selectors object references only state.* fields that remain reactive; commands and refs are never selected, so moving them out of State cannot drop a subscription.

Tests (0)

No findings.

The characterization suite (store.test.tsx) was landed against the unmodified implementation first and strengthened mid-PR (patches 6–7) after a self-review caught a two-sided ownership invariant asserted only on the negative side; each ownership assertion is now verified by mutating its writer. Coverage is appropriate for a mechanism-preserving refactor.

Simplifications (0)

No findings.

The PR already documents the two obvious follow-ups (single-owner inputOwnsFormValue → adopt store.useSyncedValues; the seven double-stabilized commands where useContextCallback re-wraps an already-stable callback). Both are correct as-is and correctly deferred.

Docs (0)

No findings.

Verdict

Approve — a clean, well-characterized refactor with no behavior change; full combobox/autocomplete suites, the new store tests, typecheck, and lint all pass.


🤖 Review generated with Claude Code

@michaldudak

Copy link
Copy Markdown
Member Author

IIRC, also Select requires such a treatment. You can take care of it if you'd like.

@michaldudak
michaldudak merged commit 7c5199a into mui:master Aug 27, 2026
8 of 9 checks passed
@michaldudak
michaldudak deleted the claude/combobox-store-split-02fec1 branch August 27, 2026 12:41
@flaviendelangle

Copy link
Copy Markdown
Member

OK 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: combobox Changes related to the combobox component. internal Behind-the-scenes enhancement. Formerly called “core”.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants