Repository navigation
fix(FocusScope): don't move focus if DOM mutation occurred before any nodes had focus - #2546
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughFocusScope's mutation handler now skips when no last-focused element exists and only reacts to mutation records that remove nodes; tests updated with a regression for Dialog+SelectTrigger, a focused-element-removed test, and several simplified focus assertions. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Trigger as DialogTrigger
participant Dialog as DialogContent
participant FocusScope as FocusScope
participant MutObs as MutationObserver
User->>Trigger: click
Trigger->>Dialog: mount & render
Dialog->>FocusScope: mount (auto-focus)
FocusScope->>Dialog: focus first tabbable (e.g., input)
MutObs-->>FocusScope: mutation records
alt lastFocused is null
FocusScope-->>FocusScope: ignore mutations
else mutations contain removed nodes and lastFocused exists
FocusScope->>FocusScope: use cached lastFocusedElement
alt lastFocused removed from container
FocusScope->>Dialog: focus FocusScope container
else
FocusScope-->>FocusScope: no action
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
7071b33 to
cd537d9
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/core/src/FocusScope/FocusScope.test.ts (1)
173-182: Consider usingwaitForfor the focus assertion.Given that other focus assertions in this file wrap
expect(...).toHaveFocus()withwaitFor, applying the same pattern here would improve consistency and guard against potential race conditions during dialog mounting and FocusScope initialization.Additionally, line 180's
expect(input).toBeTruthy()is redundant —getByTestIdwill throw if the element is not found.♻️ Suggested improvement
const input = rendered.getByTestId('email-input') - expect(input).toBeTruthy() - expect(input).toHaveFocus() + await waitFor(() => expect(input).toHaveFocus())🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/core/src/FocusScope/FocusScope.test.ts` around lines 173 - 182, In the test "should auto-focus the first tabbable element when SelectTrigger is present", remove the redundant existence assertion for the element returned by rendered.getByTestId('email-input') and wrap the focus assertion in a waitFor to mirror other tests — i.e., after clicking the 'Open' button (trigger) await waitFor(() => expect(rendered.getByTestId('email-input')).toHaveFocus()); this ensures you reference the same 'email-input' test id and the rendered/getByRole flow used in this spec.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/core/src/FocusScope/FocusScope.test.ts`:
- Around line 173-182: In the test "should auto-focus the first tabbable element
when SelectTrigger is present", remove the redundant existence assertion for the
element returned by rendered.getByTestId('email-input') and wrap the focus
assertion in a waitFor to mirror other tests — i.e., after clicking the 'Open'
button (trigger) await waitFor(() =>
expect(rendered.getByTestId('email-input')).toHaveFocus()); this ensures you
reference the same 'email-input' test id and the rendered/getByRole flow used in
this spec.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 5a566b26-f714-472b-9689-d1509db6f664
📒 Files selected for processing (1)
packages/core/src/FocusScope/FocusScope.test.ts
… nodes had focus Fixes unovue#2506. DOM mutation may be triggered by something unrelated to focus, e.g. class changed on an element, so first check if we even have a previously focused element. Also ensure that captured DOM mutations even have removed nodes. Add test to ensure focus is restored on container on node removal.
aa2bb3e to
d818fd5
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/core/src/FocusScope/FocusScope.test.ts (1)
174-182: Expand this regression to include the reopen path from issue#2506.This currently verifies autofocus only on first open. The reported regression is on subsequent open, so this test can still pass while the original bug reappears.
Suggested test extension
it('should auto-focus the first tabbable element when SelectTrigger is present', async () => { const rendered = render(DialogWithSelect) const trigger = rendered.getByRole('button', { name: 'Open' }) await userEvent.click(trigger) const input = rendered.getByTestId('email-input') expect(input).toHaveFocus() + + // Reproduce `#2506` path: close and reopen + await userEvent.keyboard('{Escape}') + await userEvent.click(trigger) + expect(input).toHaveFocus() })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/core/src/FocusScope/FocusScope.test.ts` around lines 174 - 182, Update the test "should auto-focus the first tabbable element when SelectTrigger is present" (DialogWithSelect) to exercise the reopen path: after opening via the Open button and asserting the email-input has focus, close the dialog (e.g., using the dialog's close control or pressing Escape), then re-open it with the same trigger (rendered.getByRole('button', { name: 'Open' })) and assert again that the element with test id 'email-input' receives focus; ensure you reuse the same rendered/query helpers (rendered.getByTestId/getByRole) so the second-open focus check verifies the regression on subsequent opens.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/core/src/FocusScope/FocusScope.test.ts`:
- Around line 174-182: Update the test "should auto-focus the first tabbable
element when SelectTrigger is present" (DialogWithSelect) to exercise the reopen
path: after opening via the Open button and asserting the email-input has focus,
close the dialog (e.g., using the dialog's close control or pressing Escape),
then re-open it with the same trigger (rendered.getByRole('button', { name:
'Open' })) and assert again that the element with test id 'email-input' receives
focus; ensure you reuse the same rendered/query helpers
(rendered.getByTestId/getByRole) so the second-open focus check verifies the
regression on subsequent opens.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 3667f573-73ee-4a41-8b28-c84f39ff491a
📒 Files selected for processing (2)
packages/core/src/FocusScope/FocusScope.test.tspackages/core/src/FocusScope/FocusScope.vue
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/core/src/FocusScope/FocusScope.vue
d818fd5 to
d926701
Compare
DOM mutation may be triggered by something unrelated to focus, e.g. class changed on an element, so first check if we even have a previously focused element. Also ensure that captured DOM mutations even have removed nodes before restoring focus.
Add test to ensure focus is restored to container on node removal.
🔗 Linked issue
resolves #2506
❓ Type of change
📚 Description
Did some debugging locally with 1Password extension enabled and noticed that MutationObserver callback might be called even before first element is focused and
lastFocusedElementRefis updated. For example,iconifysets class on SVGs. In this caselastFocusedElementRefis stillnullandcontainer.containscheck returns false and focus is move to container.Additionally updated FocusScope tests as these didn't properly await
waitForresults and thus didn't even verify expected results.📸 Screenshots (if appropriate)
Here's with 1Password extension enabled and mentioned fix. First tabbable element focused correctly each time the dialog is opened.
Screen.Recording.2026-03-25.at.9.56.04.PM.mov
📝 Checklist
Summary by CodeRabbit
Bug Fixes
Tests