Skip to content

fix(nav): Restore focus to previous element after closing command palette#111445

Open
jshchnz wants to merge 1 commit intofeat/cmd-k-togglefrom
fix/cmd-k-restore-focus
Open

fix(nav): Restore focus to previous element after closing command palette#111445
jshchnz wants to merge 1 commit intofeat/cmd-k-togglefrom
fix/cmd-k-restore-focus

Conversation

@jshchnz
Copy link
Copy Markdown
Member

@jshchnz jshchnz commented Mar 24, 2026

Summary

  • When the new command palette closes, focus now returns to the element that was focused before it opened
  • The GlobalModal's focus-trap doesn't reliably handle this because the palette input's autoFocus moves focus into the portal before the trap's activate() runs, causing it to store the wrong "previous" element
  • Saves document.activeElement explicitly when opening the palette and restores it when the modal closes (via any method: Escape, backdrop click, Cmd+K toggle, route change)

Depends on #111441

Test plan

  • Focus on search query builder → Cmd+K → close palette → focus returns to search input
  • Focus on a button → Cmd+K → Escape → focus returns to button
  • Focus on an input → Cmd+K → click backdrop → focus returns to input
  • Open palette with no specific focus → close → no error thrown

…ette

The GlobalModal's focus-trap doesn't reliably return focus because the
command palette input's autoFocus moves focus into the portal before
the trap activates, causing it to store the wrong "previous" element.

Explicitly save document.activeElement when opening the palette and
restore it when the modal closes, regardless of close method.
@jshchnz jshchnz requested a review from a team as a code owner March 24, 2026 19:24
@github-actions github-actions bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Mar 24, 2026
Copy link
Copy Markdown
Member

@JonasBa JonasBa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes because this landed in #111542 and fixed the behavior for all modals

@getsantry
Copy link
Copy Markdown
Contributor

getsantry bot commented Apr 16, 2026

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you add the label WIP, I will leave it alone unless WIP is removed ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@getsantry getsantry bot added the Stale label Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants