Skip to content

Commit ea6de74

Browse files
fix(ui): a dialog's focus trap no longer pulls Tab back when a tooltip closes on blur (#34091)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9Sy653R4fA2ArwDP1QvFqq9)_ Fixes #33980 Clause-②: no Metadata-protocol: none Claim by `domain:ui#1`, card-thread session `session_01ADHkmPwoJquHrWGDjT1P4r`. ## 维护者速读 - 改了什么:给弹窗焦点陷阱(Radix `FocusScope`)的补丁加一段:只有「上一个获得焦点的元素」真的不在弹窗里了,才把焦点拉回弹窗本身。导入向导里 #33960 加的局部绕法(把 tooltip 挪出弹窗)随之撤掉。 - 为什么改:弹窗里一个带内联 tooltip 的按钮,按 Tab 离开时 tooltip 被卸载,陷阱误判为「焦点元素被删」,把焦点拉回弹窗,键盘用户出不去。除导入向导外,实测「管理视图」对话框也中招:Tab 过不了第一行的 Pin 按钮,其余视图、Done、Close 都到不了。修在陷阱上,所有弹窗一次修好。 - 风险与回滚:只改补丁里一个判断条件,焦点元素真被删除时仍拉回弹窗(单测对照钉住)。改动面是所有 Dialog / Sheet / AlertDialog 的焦点处理;回滚即 revert(会同时带回导入向导的问题,因为局部绕法已撤)。 - 先看:`patches/@radix-ui__react-focus-scope.patch`(新增的 handleMutations 两行),`ui/packages/components/src/__tests__/dialog-popover-focus-scope.test.tsx`(新 describe),`ui/e2e/manage-views-keyboard-tab-order.spec.ts`。 - 两分钟验证:`pnpm --dir ui exec playwright test e2e/manage-views-keyboard-tab-order.spec.ts e2e/import-wizard-keyboard-tab-order.spec.ts --project=chromium`,预期 6 passed。 - 需要你做的:无,席位落地。 ## What changes - **`patches/@radix-ui__react-focus-scope.patch`** (the file `pnpm-workspace.yaml`'s `patchedDependencies` applies) gains a second hunk in `dist/index.js` and `dist/index.mjs`: the trapped scope's `MutationObserver` callback returns early when the last focused element is still inside the container. Stock radix focuses the container whenever a node under it is removed while focus sits on `body`; between the blur and the focus of a native Tab focus does sit on `body`, so a node that a blur unmounts (an inline tooltip closing under its trigger) pulled focus back to the container. The callback now acts only on the case it exists for: the focused element, or an ancestor of it, was itself removed. The earlier stack-race hunk is unchanged. - **`ui/patches/@radix-ui__react-focus-scope.patch`** gets the same bytes. It is an unreferenced copy (nothing reads it; `pnpm-workspace.yaml` names the root file), kept identical rather than left to drift; deleting it is not this card. **`ui/patches/README.md`**: the entry now describes both halves, its Tracking/Upstream rows name the second, and Remove-when requires every regression case green with the patch removed. - **`pnpm-lock.yaml`**: the patch hash for `@radix-ui/react-focus-scope@1.1.16` (7 lines, from `pnpm install`). - **`ui/packages/plugin-grid/src/ImportWizard.tsx`**: #33960's local `createPortal` of the Save-current reason tooltip is removed (`git apply -R` of that hunk); the tooltip mounts inline again, and Tab still leaves it (spec below). - **Changesets**: new `.changeset/33980-focus-trap-blur-unmount.md` (`@object-ui/console`, `@objectstack/console`, `@object-ui/plugin-grid`: patch). **DELIBERATE CORRECTION** of the pending `.changeset/33862-import-wizard-save-reason-tab-trap.md`: its last sentence said "The tooltip now renders outside the dialog, so Tab and Shift+Tab walk every control in order…", which this PR makes false; it now reads "The dialog's focus trap no longer reads that removal as focus lost, so Tab and Shift+Tab walk every control in order and the reason is still read out when the wrapper is focused." `check-empty-changeset` is red on that file by design. ### Why A (the focus-scope patch) and not B (portal `TooltipContent`) Measured first, on `origin/main` `fb79d47c`: which inline-tooltip triggers sit inside a modal dialog. Statically, 35 non-test files under `ui/packages` name `TooltipContent`; six of them also render a Dialog / Sheet / AlertDialog (ImportWizard, ManageViewsDialog, MarketplacePackagePage, MarketplaceRowActions, StudioDesignSurface, the Shadcn sidebar). Measured in Chromium with real Tab presses (Vite bundle from `src/`, no stylesheet): | Site | Before (fb79d47) | After | |---|---|---| | Import wizard, Save-current reason (with #33960's portal removed) | trapped (the #33960 spec's ablation leg, and below) | all 8 stops | | Manage Views dialog, each row's Pin / Set-as-default button (`ObjectView` wires both) | trapped: search, drag, `All`, Pin, **dialog**, search… | all 12 stops | | A generic Dialog with a focusable tooltip trigger between two buttons | trapped: a, b, **dialog**, a… | a, b, c, Close | Not measured: `StudioDesignSurface` (a tooltip in an inspector its selection Sheet may hold), tooltips in views a record drawer may host, and `DisabledReasonTrigger` when metadata places a disabled action inside a modal. A covers them whatever they turn out to be. Why "no stylesheet" is the production case: the Console's `index.css` leaves `@plugin 'tailwindcss-animate'` commented out, so `data-[state=closed]:animate-out` has no rule and a closing tooltip unmounts at once, inside the blur→focus gap. With an exit animation the unmount waits for `animationend` and nothing traps (measured on the generic probe: a, b, c, Close) — which is why this hides in some setups. - **A** fixes the trap's heuristic, so every blur-driven unmount under a modal is covered, not only tooltips, in every dialog at once; the repo already patches this exact package and version for another focus race, with a README entry and a removal condition. - **B was not taken**: it closes only the tooltip half (any other node a blur removes still traps); `ui/packages/components/src/ui/tooltip.tsx` is a Shadcn-synced file (ui/AGENTS.md #7), tracked from shadcn's `default` style, which does not portal, so B is a local divergence to declare in `ui/scripts/shadcn-local-patches.mjs`; and it moves every tooltip in the app out of its subtree into `body`, a DOM change for every tooltip to fix a defect that only exists under a focus trap. ## Tests - **Unit, `ui/packages/components/src/__tests__/dialog-popover-focus-scope.test.tsx`** (the shared harness of the existing patch; new top-level `describe`): a trapped scope whose trigger drops a sibling node on blur — after `blur()` focus is not on the container (red on stock); CONTROL — removing the focused element itself still focuses the container (green either way). A scripted `blur()` puts focus on `body` before the observer callback runs, so this unit case can go red in happy-dom, unlike a `user-event` Tab walk. - **New e2e, `ui/e2e/manage-views-keyboard-tab-order.spec.ts`** (Build & E2E UI, chromium; the harness of #33960's spec): the real `ManageViewsDialog` bundled from source, native Tab and Shift+Tab over two editable views (12 stops, past both Pins and Set-as-default) and a CONTROL of two read-only views (no tooltip trigger, 7 stops). - **`ui/e2e/import-wizard-keyboard-tab-order.spec.ts`**: green with the local portal removed. Forced test edit: its header sentence "The reason tooltip is now portalled out of the dialog…" became false and now names the trap patch; no assertion changed. - **Ablation** (fix committed first): the patch's guard line deleted from the installed `dist/index.mjs` (marker `/* ablated */`, guard count 1 → 0, blob `4dfa11db5c8c` → `a97219dd468f`). - unit: the new case red, the control and the 3 existing cases green; - e2e: Manage Views editable case red (`…, manage-views-pin-all, dialog, manage-views-search, …`), import wizard nothing-mapped and tooltip cases red (`…, reason:import-template-save-btn, dialog, …`), the 3 controls green. - Restore proven both times: blob back to `4dfa11db5c8c`, which is the post-image blob the patch itself names for `dist/index.mjs`; guard count 1. (`scripts/ablation-replace.mjs` refused to certify the restore of a path git does not track, so the restore was done by hand and proven by hash.) <details><summary>Local runs (head 279bded)</summary> ``` playwright e2e/manage-views-keyboard-tab-order.spec.ts e2e/import-wizard-keyboard-tab-order.spec.ts --project=chromium head 279bded: 6 passed ablation (guard deleted): 3 failed (manage-views editable; import-wizard nothing-mapped; import-wizard tooltip), 3 passed (controls) vitest packages/components/src/__tests__/dialog-popover-focus-scope.test.tsx head: 5 passed ablation: 1 failed (a node a blur unmounts...), 4 passed vitest packages/components/ packages/plugin-grid/ packages/plugin-view/ (lock VERDICT command-exit 0) Test Files 827 passed | 1 skipped (828); Tests 7755 passed | 24 skipped (7779) type-check:e2e, @object-ui/components type-check, @object-ui/plugin-grid type-check (lock VERDICT command-exit 0) Chromium probes on origin/main fb79d47 (before the patch; Vite bundle from src, no stylesheet): generic Dialog + tooltip trigger: a, b, dialog, a, b same, with an exit-animation rule: a, b, c, Close, a ManageViewsDialog (2 views): search, drag-all, All, pin-all, dialog, search, ... after the patch: generic: a, b, c, Close, a ManageViewsDialog: search, drag-all, All, pin-all, actions-all, drag-mine, Mine, pin-mine, default-mine, actions-mine, done, Close, search ``` </details> ## Acceptance notes - `ui/patches/README.md`'s header still says patches are "declared in the root `package.json`"; they are declared in `pnpm-workspace.yaml`. Doc drift, outside this card's file surface; noted, not fixed. - No upstream radix issue is known for the mutation-handler half; the README says so rather than citing one. ## Verification Local, on head `279bded8` (merge base `fb79d47c`; `origin/main` `63db7240`, none of its 10 commits touches these files): `node scripts/pm/dispatch-gates.mjs --commands` derived 52; `--ran` with the exit codes: 52 derived, 51 run, 1 NOT-MEASURED, 0 UNRUN. - `check-empty-changeset --base origin/main` exits 1 **by design**: the DELIBERATE CORRECTION of `.changeset/33862-import-wizard-save-reason-tab-trap.md` above, for the seat's record. - `check:dual-build-cjs-loads`: NOT MEASURED locally (exit 3, PREREQUISITE NOT MET: it reads every package's `dist/`, and only the closures these tests need were built). It is derived from the lockfile line only; CI's Lint & Repo Gates runs it on a full build. - Also run: `ui` `check:new-line-citations` (0 new); `check-governed-merges --branch HEAD`: NOT governed, 0 of 10 paths, 477 changed lines. <details><summary>Derived gates, each exit code</summary> ``` node scripts/check-adr-0087-registration.mjs --base origin/main :: exit 0 node scripts/check-adr-0087-registration.mjs --self-test :: exit 0 node scripts/check-changeset-no-major.mjs --base origin/main :: exit 0 node scripts/check-closing-keyword-parity.mjs :: exit 0 node scripts/check-closing-keyword-parity.mjs --self-test :: exit 0 node scripts/check-comment-mask-corpus.mjs :: exit 0 node scripts/check-dts-emitted.mjs --self-test :: exit 0 node scripts/check-empty-changeset.mjs --base origin/main :: exit 1 node scripts/check-empty-changeset.mjs --self-test :: exit 0 node scripts/check-osv-exemptions.mjs :: exit 0 node scripts/check-osv-exemptions.mjs --self-test :: exit 0 node scripts/check-sdui-manifest.mjs :: exit 0 node scripts/check-sdui-manifest.mjs --self-test :: exit 0 node scripts/ci/assert-turbo-remote-cache.mjs --self-test :: exit 0 node scripts/ci/plan-test-core-shards.mjs --self-test :: exit 0 node scripts/ci/prune-turbo-cache.mjs --self-test :: exit 0 node scripts/ci/snapshot-next.mjs --self-test :: exit 0 node scripts/osv-base-relative.mjs --self-test :: exit 0 node scripts/pm/check-governed-queue-guard.mjs --self-test :: exit 0 node scripts/pm/release-rehearsal-clone.mjs --self-test :: exit 0 node scripts/release-pending-publish.mjs --self-test :: exit 0 pnpm --dir ui exec vitest run scripts/ :: exit 0 pnpm --filter @objectstack/spec run check:duration-unit-keys :: exit 0 pnpm --filter @objectstack/spec run check:error-code-provenance :: exit 0 pnpm check:changeset-fixed :: exit 0 pnpm check:changeset-gate-self-tests :: exit 0 pnpm check:closed-ceremony-text :: exit 0 pnpm check:cross-package-test-inputs :: exit 0 pnpm check:dispatcher-error-vocabulary :: exit 0 pnpm check:driver-memory-census :: exit 0 pnpm check:dts-closure :: exit 0 pnpm check:dual-build-cjs-loads :: exit 3 pnpm check:engine-double-contract :: exit 0 pnpm check:gitlink-declared :: exit 0 pnpm check:lean-entry-closure :: exit 0 pnpm check:nul-bytes :: exit 0 pnpm check:override-consistency :: exit 0 pnpm check:published-files :: exit 0 pnpm check:query-options-erasure :: exit 0 pnpm check:refd-timer-probe :: exit 0 pnpm check:registry-log-declared :: exit 0 pnpm check:rest-log-spy-declared :: exit 0 pnpm check:skill-identifier-liveness :: exit 0 pnpm check:sourcemap-no-sources-content :: exit 0 pnpm check:test-source-alias :: exit 0 pnpm check:tier-file-adoption :: exit 0 pnpm check:type-check-coverage :: exit 0 pnpm check:type-check-debt :: exit 0 pnpm check:undeclared-dep-imports :: exit 0 pnpm check:vendor-export-contract-resolve :: exit 0 pnpm check:watch-hint-literal :: exit 0 pnpm check:where-matcher :: exit 0 ``` </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01ADHkmPwoJquHrWGDjT1P4r)_ --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>
1 parent b4eea88 commit ea6de74

10 files changed

Lines changed: 438 additions & 39 deletions

‎.changeset/33862-import-wizard-save-reason-tab-trap.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ fix(plugin-grid): Tab leaves the import wizard's "Save current" reason again
77
Clause-②: no
88
Metadata-protocol: none
99

10-
In the import wizard's mapping step with no column mapped, "Save current" is disabled and a focusable wrapper around it gives the reason in a tooltip. Pressing Tab on that wrapper sent focus to the dialog itself, and the next Tab started over at the template select: a keyboard user could not reach the column-mapping selects, Next, Cancel, Back or Close, and could leave only with Escape. The tooltip's content was mounted inside the dialog and unmounted as the wrapper lost focus; the dialog's focus trap read that removal as the focused element disappearing and pulled focus to the dialog. The tooltip now renders outside the dialog, so Tab and Shift+Tab walk every control in order and the reason is still read out when the wrapper is focused.
10+
In the import wizard's mapping step with no column mapped, "Save current" is disabled and a focusable wrapper around it gives the reason in a tooltip. Pressing Tab on that wrapper sent focus to the dialog itself, and the next Tab started over at the template select: a keyboard user could not reach the column-mapping selects, Next, Cancel, Back or Close, and could leave only with Escape. The tooltip's content was mounted inside the dialog and unmounted as the wrapper lost focus; the dialog's focus trap read that removal as the focused element disappearing and pulled focus to the dialog. The dialog's focus trap no longer reads that removal as focus lost, so Tab and Shift+Tab walk every control in order and the reason is still read out when the wrapper is focused.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@object-ui/console': patch
3+
'@objectstack/console': patch
4+
'@object-ui/plugin-grid': patch
5+
---
6+
7+
fix(console): Tab no longer gets stuck in a dialog on a control whose tooltip closes as it loses focus
8+
9+
Clause-②: no
10+
Metadata-protocol: none
11+
12+
Inside a dialog, sheet or alert dialog, pressing Tab on a control that shows a tooltip while focused sent focus back to the dialog itself, and the next Tab started over at the first control. In the Manage Views dialog a keyboard user could not get past the first view's Pin button: the other views, their Set-as-default and menu buttons, and Done were unreachable. The tooltip closes as the control loses focus, and the dialog's focus trap read the closing tooltip as the focused control disappearing. The trap now pulls focus back only when the control that had focus really is gone, so Tab and Shift+Tab move on from every such control, in every dialog. The import wizard's own workaround for the same problem, which rendered its "Save current" tooltip outside the dialog, is removed: the tooltip renders inline again and Tab still moves on.

‎patches/@radix-ui__react-focus-scope.patch‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
diff --git a/dist/index.js b/dist/index.js
2-
index e8906db3b1e87bfc568b75d72b7ed3d8d1d3c09a..7a05e0e4cffba466301ba1fb67067c0861af0a4d 100644
2+
index e8906db3b1e87bfc568b75d72b7ed3d8d1d3c09a..551c30c1ca1bc1297ffc756c3673602990cddb26 100644
33
--- a/dist/index.js
44
+++ b/dist/index.js
5-
@@ -110,6 +110,10 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
5+
@@ -89,6 +89,8 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
6+
}, handleMutations2 = function(mutations) {
7+
const focusedElement = document.activeElement;
8+
if (focusedElement !== document.body) return;
9+
+ const lastFocusedElement = lastFocusedElementRef.current;
10+
+ if (lastFocusedElement && container.contains(lastFocusedElement)) return;
11+
for (const mutation of mutations) {
12+
if (mutation.removedNodes.length > 0) focus(container);
13+
}
14+
@@ -110,6 +112,10 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
615
}, [trapped, container, focusScope.paused]);
716
React.useEffect(() => {
817
if (container) {
@@ -13,7 +22,7 @@ index e8906db3b1e87bfc568b75d72b7ed3d8d1d3c09a..7a05e0e4cffba466301ba1fb67067c08
1322
focusScopesStack.add(focusScope);
1423
const previouslyFocusedElement = document.activeElement;
1524
const hasFocusedCandidate = container.contains(previouslyFocusedElement);
16-
@@ -126,7 +130,8 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
25+
@@ -126,7 +132,8 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
1726
}
1827
return () => {
1928
container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
@@ -24,10 +33,19 @@ index e8906db3b1e87bfc568b75d72b7ed3d8d1d3c09a..7a05e0e4cffba466301ba1fb67067c08
2433
container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
2534
container.dispatchEvent(unmountEvent);
2635
diff --git a/dist/index.mjs b/dist/index.mjs
27-
index 030ff1a4d2383c9a0a1ba850aa4591890f028784..3dd39a90db9886bc495014a6e4136773e3548448 100644
36+
index 030ff1a4d2383c9a0a1ba850aa4591890f028784..4dfa11db5c8c6e68f38fcf96025822b825b9c036 100644
2837
--- a/dist/index.mjs
2938
+++ b/dist/index.mjs
30-
@@ -75,6 +75,10 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
39+
@@ -54,6 +54,8 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
40+
}, handleMutations2 = function(mutations) {
41+
const focusedElement = document.activeElement;
42+
if (focusedElement !== document.body) return;
43+
+ const lastFocusedElement = lastFocusedElementRef.current;
44+
+ if (lastFocusedElement && container.contains(lastFocusedElement)) return;
45+
for (const mutation of mutations) {
46+
if (mutation.removedNodes.length > 0) focus(container);
47+
}
48+
@@ -75,6 +77,10 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
3149
}, [trapped, container, focusScope.paused]);
3250
React.useEffect(() => {
3351
if (container) {
@@ -38,7 +56,7 @@ index 030ff1a4d2383c9a0a1ba850aa4591890f028784..3dd39a90db9886bc495014a6e4136773
3856
focusScopesStack.add(focusScope);
3957
const previouslyFocusedElement = document.activeElement;
4058
const hasFocusedCandidate = container.contains(previouslyFocusedElement);
41-
@@ -91,7 +95,8 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
59+
@@ -91,7 +97,8 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
4260
}
4361
return () => {
4462
container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);

‎pnpm-lock.yaml‎

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ui/e2e/import-wizard-keyboard-tab-order.spec.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import { uiRoot } from '../scripts/ui-root.mjs';
2121
* was removed while focus is on body" as "the focused element was removed",
2222
* so it focuses the dialog container and the browser abandons the move. The
2323
* next Tab lands on the first stop again: the cycle was template select,
24-
* reason span, dialog container. The reason tooltip is now portalled out of
25-
* the dialog, so its unmount happens outside the trap.
24+
* reason span, dialog container. The trap now refocuses its container only
25+
* when the last focused element is no longer inside it
26+
* (`patches/@radix-ui__react-focus-scope.patch` at the repository root), so
27+
* the tooltip may unmount inline, under the trap, with Tab still moving on.
2628
*
2729
* ## Why a Playwright spec and not a vitest test
2830
*

0 commit comments

Comments
 (0)