Skip to content

Skip focus restore in the dropdown when no target is found - #2490

Merged
Flo0807 merged 1 commit into
developfrom
fix/dropdown-focus-restore
Nov 8, 2026
Merged

Flo0807 merged 1 commit into
developfrom
fix/dropdown-focus-restore

Conversation

@Flo0807

@Flo0807 Flo0807 commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

Problem

BackpexDropdown.updated() restores focus to the element that had it before a patch. When the patch replaced that element and it had no id, this.focusedBeforeUpdate.id && this.el.querySelector(...) evaluates to "". Optional chaining only guards against null/undefined, so ""?.focus() throws:

TypeError: (intermediate value)(intermediate value)(intermediate value)?.focus is not a function
    at updated (...)

We see this regularly in Sentry from an app using Backpex 0.23.0.

Fix

Only call focus() when a target was found. A unit test in test/js/dropdown_test.js reproduces the error on develop.

@Flo0807 Flo0807 added the bug Something isn't working label Nov 8, 2026
@Flo0807 Flo0807 self-assigned this Nov 8, 2026
@Flo0807
Flo0807 enabled auto-merge November 8, 2026 10:30
When a patch removed the focused element inside a dropdown and that
element had no id, `id && querySelector(...)` evaluated to an empty
string. Optional chaining only guards against null and undefined, so
`""?.focus()` threw "focus is not a function" in the updated() callback.
@Flo0807
Flo0807 force-pushed the fix/dropdown-focus-restore branch from 75f1c94 to aac45a7 Compare November 8, 2026 11:42
@Flo0807
Flo0807 added this pull request to the merge queue Nov 8, 2026
Merged via the queue into develop with commit 0fb5f56 Nov 8, 2026
10 checks passed
@Flo0807
Flo0807 deleted the fix/dropdown-focus-restore branch November 8, 2026 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant