Skip to content

Commit 0584eb5

Browse files
fix(components): scroll the form tab strip in the commit that activates the tab (#34892)
🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01WGufzgwLiKcfujYUug7sPs Fixes #34782 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:表单字段页签条在手机宽度下把当前页签滚入可见区的那段逻辑,从 passive effect 改为 layout effect;对应测试多读一次"页签被激活那一刻"的滚动位置。 - 为什么改:提交失败后跳到出错页签是异步触发的,滚动晚一个任务才执行,期间页签已显示为激活但仍在条外;merge queue 里这条测试因此偶发红(期望 [261],实际 [])。 - 风险与回滚:低。只换了 effect 的时机(提交前、绘制前执行),逻辑本身一字未改;回滚即 revert 本 PR。 - 先看:`ui/packages/components/src/renderers/form/form.tsx` 中 `fieldTabListRef` 那段 `React.useLayoutEffect`;其次 `form-field-tabs.test.tsx` 的 "a failed submit that jumps to an off-screen tab" 用例。 - 两分钟验证:`pnpm --dir ui exec vitest run packages/components/src/renderers/form/__tests__/form-field-tabs.test.tsx`,预期 14 passed。 - 需要你做的:无,席位落地。 ## What changes - `form.tsx`: the phone-width field-tab strip's "scroll the active tab into the strip" effect is a `React.useLayoutEffect` instead of a `React.useEffect`. Body unchanged. The file already uses `React.useLayoutEffect` directly in six other places (no isomorphic wrapper convention in this package), one of them for the same class of flake (a passive `reset()` racing input). - `form-field-tabs.test.tsx`, case "a failed submit that jumps to an off-screen tab scrolls it into view, error dot and all": adds a `MutationObserver` on the Activity trigger's `data-state` that snapshots `writes` in the microtask right after the activating commit, and asserts that snapshot is `[620 - 359]`. The existing `expect(writes).toEqual([620 - 359])` stays exactly as it was. No skip, no retry, no timeout change. - `.changeset/34782-form-tab-strip-scroll-same-commit.md`: `@object-ui/components` patch. ### Cause, measured The failed submit activates the tab from react-hook-form's async validation callback, so `setPickedFieldTab` is a non-discrete (default-lane) update. React 19 commits it in a scheduler task, calls `requestPaint()`, and the scheduler yields before the passive-effect flush, which runs in the NEXT task. The DOM mutation that sets `data-state="active"` fires `waitFor`'s MutationObserver in the microtask right after the commit, `waitFor` resolves, and RTL's `asyncWrapper` then waits one `setTimeout(0)`. Whether that timer or the scheduler's `setImmediate` (the passive effect) runs first depends on whether 1 ms passed in between: on an idle box the effect wins, on a loaded merge-queue shard the timer wins and the case reads `[]`. The choice of root: the scroll belongs in the commit that marks the tab active. That is a product fix too, not only a test fix: as a passive effect the activated tab was painted off-screen for a task. Measuring layout and writing `scrollLeft` before paint is what a layout effect is for. The alternative (have the test `waitFor` the write) would have made the case green while leaving the off-screen frame in the product. ## Tests <details><summary>Race probe (temporary file, deleted before commit; not in this PR)</summary> A probe copied the exact case and ran it N times in one test, optionally stalling the box for S ms a few microtask hops after the activating commit (after `waitFor` arms its trailing timer). It logged the order of: commit observed, effect write, assertion, and the writes 20 ms later. Before (origin/main d947a93, passive effect): ``` PROBE N=200 stall=0ms empty=0 [["effect-write > assert",200]] PROBE N=50 stall=1ms empty=50 [["commit-observed > stall-done > assert > effect-write > late:[261]",50]] PROBE N=50 stall=3ms empty=50 [["commit-observed > stall-done > assert > effect-write > late:[261]",50]] ``` So: green 200/200 on an idle box, red 50/50 with a 1 ms stall, and the write `[261]` does land, just after the assertion. After (e1eb423, layout effect): ``` PROBE N=50 stall=1ms empty=0 [["effect-write > commit-observed > stall-done > assert > late:[261]",50]] PROBE N=50 stall=3ms empty=0 [["effect-write > commit-observed > stall-done > assert > late:[261]",50]] PROBE N=200 stall=0ms empty=0 [["effect-write > assert > late:[261]",200]] ``` The write now precedes the commit being observable at all. </details> <details><summary>Ablation of the fix against the new pin</summary> `node scripts/ablation-replace.mjs` swapped `React.useLayoutEffect(() => {` back to `React.useEffect(() => {` on the strip effect (anchor 1 → 0, blob 4e3a9de → 900932a945ac), then ran the file 5 times: 5/5 red, `Tests 1 failed | 13 passed (14)`, each with `scrolled in the same commit that marks the tab active: expected [] to deeply equal [ 261 ]`. Restored: blob == HEAD (4e3a9de), `git diff HEAD` empty. So the case now fails deterministically on the old effect timing instead of one run in many. </details> <details><summary>Suites at e1eb423</summary> - `pnpm --dir ui exec vitest run --maxWorkers=2 packages/components/src/renderers/form/__tests__/form-field-tabs.test.tsx`: 14 passed (14). Ten more runs with `--sequence.shuffle`: 10/10 at 14 passed (14). - `pnpm --dir ui exec vitest run --maxWorkers=2 packages/components/src/renderers/form/`: Test Files 87 passed (87), Tests 616 passed, 17 skipped (633). - `pnpm --filter @object-ui/components type-check` (`tsc --noEmit && tsc -p tsconfig.test.json`): exit 0. </details> ## Acceptance notes - The "opens on an off-screen defaultFieldTab already scrolled to it", "scrolls the fifth tab into the strip when it is tapped" and CONTROL cases pass unchanged under the layout effect. - No SSR concern was found: the form renderer already runs six other `React.useLayoutEffect` calls directly. ## Verification Gate readings are in the os-dev report comment on this PR. <!-- signed: repo:objectstack#3 · os-support-ai · session_01WGufzgwLiKcfujYUug7sPs --> Co-authored-by: Claude <noreply@anthropic.com>
1 parent ec86867 commit 0584eb5

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/components': patch
3+
---
4+
5+
fix(components): a form's phone-width field-tab strip scrolls a tab activated by a failed submit into view in the same commit
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
Below `sm`, a horizontal `fieldTabs` strip that does not fit scrolls sideways, and the active tab is scrolled into its visible window. That scroll ran in a passive effect. When the activation came from a non-discrete update, such as a failed submit jumping to the first tab holding an invalid field (react-hook-form validates asynchronously), the passive effect ran a task after the commit. For that window the newly active tab was painted while still scrolled out of the strip. The scroll now runs in a layout effect, so it lands in the same commit that marks the tab active, before paint. Tapping a tab and opening on an off-screen `defaultFieldTab` behave as before.

‎ui/packages/components/src/renderers/form/__tests__/form-field-tabs.test.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,25 @@ describe('form renderer — fieldTabs strip at phone width (objectstack#34615)',
268268
it('a failed submit that jumps to an off-screen tab scrolls it into view, error dot and all', async () => {
269269
renderFive();
270270
const writes = layOutStrip(true);
271+
// The jump is a non-discrete update (validation is async), so a scroll left
272+
// to a passive effect lands a task after the commit that activates the tab
273+
// — painted off-screen meanwhile, and racing this test's `waitFor`
274+
// (objectstack#34782). Read the strip at that commit: a mutation observer
275+
// runs in the microtask straight after it, before any later task.
276+
const trigger = tab(/Activity/);
277+
let writesAtActivation: number[] | undefined;
278+
const activation = new MutationObserver(() => {
279+
if (writesAtActivation === undefined && trigger.getAttribute('data-state') === 'active') {
280+
writesAtActivation = [...writes];
281+
}
282+
});
283+
activation.observe(trigger, { attributes: true, attributeFilter: ['data-state'] });
271284
submit();
272285
await waitFor(() => expect(tab(/Activity/)).toHaveAttribute('data-state', 'active'));
286+
activation.disconnect();
273287
expect(tab(/Activity/)).toHaveAttribute('data-error', 'true');
274288
expect(tab(/Activity/).querySelector('.bg-destructive'), 'the error dot rides on the trigger').not.toBeNull();
289+
expect(writesAtActivation, 'scrolled in the same commit that marks the tab active').toEqual([620 - 359]);
275290
expect(writes).toEqual([620 - 359]);
276291
});
277292

‎ui/packages/components/src/renderers/form/form.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2739,8 +2739,16 @@ ComponentRegistry.register('form',
27392739
// offending tab can each select a trigger scrolled out of view. Only the
27402740
// strip's own `scrollLeft` moves, never an outer scroller, and a strip
27412741
// that fits (every desktop strip) is left untouched.
2742+
//
2743+
// A LAYOUT effect, so the scroll lands in the same commit that marks the
2744+
// new trigger active. As a passive effect it ran a task later whenever the
2745+
// activation came from a non-discrete update — the failed-submit jump is
2746+
// one: react-hook-form validates asynchronously — so for that window the
2747+
// active tab was painted, and observable, while still scrolled out of the
2748+
// strip (objectstack#34782). It measures layout and writes `scrollLeft`
2749+
// before paint, which is what a layout effect is for.
27422750
const fieldTabListRef = React.useRef<HTMLDivElement>(null);
2743-
React.useEffect(() => {
2751+
React.useLayoutEffect(() => {
27442752
const list = fieldTabListRef.current;
27452753
if (!list || activeFieldTab === undefined || list.scrollWidth <= list.clientWidth) return;
27462754
const trigger = list.querySelector<HTMLElement>('[role="tab"][data-state="active"]');

0 commit comments

Comments
 (0)