Skip to content

Autocomplete: use blur relatedTarget to control menu visibility#8206

Draft
iansan5653 with Copilot wants to merge 2 commits into
mainfrom
copilot/copy-test-changes-fix
Draft

Autocomplete: use blur relatedTarget to control menu visibility#8206
iansan5653 with Copilot wants to merge 2 commits into
mainfrom
copilot/copy-test-changes-fix

Conversation

Copilot AI commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Split out from #7644 (see thread) so it can ship independently of the wider useProvidedRefOrCreateuseMergedRefs refactor.

AutocompleteInput's blur handler decided whether to close the menu by comparing document.activeElement against the input's ref. That inference can't distinguish "focused, then focus moved away" from "never focused", and it depends on how the input's ref is wired up — so migrating TextInput to merged refs (in #7644) breaks the blur logic.

The handler now reads the blur event's relatedTarget to detect whether focus is moving into the menu, closing the menu only when it isn't. This tracks the actual focus transition rather than post-interaction DOM state, and is independent of the ref setup.

safeSetTimeout(() => {
  const nextFocusedElement = event.relatedTarget as Node | null
  const menuElement = document.getElementById(`${id}-listbox`)
  if (!nextFocusedElement || (nextFocusedElement !== menuElement && !menuElement?.contains(nextFocusedElement))) {
    setShowMenu(false)
    // ...reset input value
  }
}, 0)

Changelog

New

  • Test: menu stays open when focus moves into the menu.

Changed

  • Autocomplete: input blur handler uses the blur event's relatedTarget to decide whether to close the menu.

Removed

  • Reliance on document.activeElement/inputRef for blur-driven menu dismissal.

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Testing & Reviewing

Run npx vitest run packages/react/src/Autocomplete/Autocomplete.test.tsx. The new "keeps the menu open when focus moves into the menu" test exercises the relatedTarget path. Note: browser-mode synthetic focus makes a strict old-vs-new discriminator unreliable (the fragility called out in the source thread), so the test locks in the intended relatedTarget contract rather than reproducing the original failure.

Copilot AI and others added 2 commits July 21, 2026 14:36
Co-authored-by: iansan5653 <2294248+iansan5653@users.noreply.github.com>
Co-authored-by: iansan5653 <2294248+iansan5653@users.noreply.github.com>
@changeset-bot

changeset-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 64f9a95

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants