Repository navigation
[combobox] Split the store into state and context - #5581
Conversation
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]>
commit: |
Bundle size
PerformanceTotal duration: 1,083.09 ms -73.16 ms(-6.3%) | Renders: 76 (+0) | Paint: 1,743.08 ms -142.93 ms(-7.6%)
12 tests within noise — details Metric alarms
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!Built without sensitive environment variables
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]>
flaviendelangle
left a comment
There was a problem hiding this comment.
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.
useContextCallbackassigns intocontextduring the root's render body (AriaCombobox.tsx:1414-1421), before any child renders, exactly as the old first-renderstore.updateinsideuseOnFirstRenderdid. 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;syncedValuesis a static-key object literal, soObject.valuesis positionally stable across renders. The pre-existinginputInsidePopupstaleness in theinputOwnsFormValuederivation is read but intentionally not in deps — unchanged from before. - No reactive readers of moved members. The
selectorsobject references onlystate.*fields that remain reactive; commands and refs are never selected, so moving them out ofStatecannot 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
|
IIRC, also Select requires such a treatment. You can take care of it if you'd like. |
|
OK 👌 |
Splits the Combobox store's single 62-member
Statebag 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
NOOPuntil the root's first render — across 83store.state.Xreads 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
RefObjects and 8 command callbacks move fromStateintoReactStore'scontextslot. None of them is observed by a selector, no ref is ever written through the store, and the commands are alreadyuseStableCallbackresults.store.useContextCallbackinstead of a first-renderstore.update.store.useState('key')instead of threading theselectorsobject through all 119 call sites. This follows theToastStoreprecedent for the third generic parameter and the call style already used by Drawer and Dialog.What deliberately did not change
store.useSyncedValuesis not adopted yet, even though it is the obvious destination.inputOwnsFormValuehas a second writer inComboboxInput.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 samestore.update.useSyncedValuesderives 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.useOnFirstRenderis also retained. It is not a pass-through: five parts read the prop bags withuseStoreduring render, and a layout effect commits only after all children have rendered, so seeding them later dropsroleandaria-*from the server output.Behavior
No public API, DOM, or behavior change is intended, including the pre-existing
inputOwnsFormValuestaleness, 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 (onestore.update, no torninline/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.updatecopies the whole state on every write ({...this.state, ...changes}), so its cost is linear in the number of keys. TakingStatefrom 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
inputOwnsFormValuea single owner, then collapse the synchronization effect intostore.useSyncedValues.useContextCallbackre-wraps an already-stable callback. Left alone because the locals are consumed insideAriaComboboxitself, including in dependency arrays.🤖 Generated with Claude Code