Skip to content

fix(popover-container): return focus to the trigger when a popover in a shadow root closes - #1558

Merged
nielslyngsoe merged 4 commits into
mainfrom
v2/fix/popover-container-focus-restore
Nov 19, 2026
Merged

nielslyngsoe merged 4 commits into
mainfrom
v2/fix/popover-container-focus-restore

Conversation

@iOvergaard

@iOvergaard iOvergaard commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

When a uui-popover-container sits inside a component's shadow root and closes while focus is inside it (Escape or hidePopover()), Chromium leaves focus on <body> instead of returning it to the element that opened it. Keyboard and screen reader users lose their place. WebKit returns focus as expected, and so does Chromium when the popover is in the light DOM.

The container now restores focus itself when the browser didn't:

  • When it opens (beforetoggle), it remembers the focused element, looking through shadow roots.
  • When it starts to close (beforetoggle), it notes whether focus is inside the popover.
  • On toggle (closed), if focus was inside and the browser left it on <body> or inside the now-hidden popover, it focuses the remembered element with preventScroll.

Where the browser already returned focus, this does nothing. It keeps the spec's limits: auto popovers only, only the first popover in a stack, and no restore after a click outside (light dismiss).

One known gap: if script opens an unrelated popover while focus is inside this one, and the new popover doesn't take focus, this still returns focus to this popover's trigger. The spec says not to.

Cause

The HTML spec restores focus when the focused element is a shadow-including inclusive descendant of the popover (hide popover algorithm, step 20.2). Chromium tests contains(document.AdjustedFocusedElement()) instead (html_element.cc#L2872). AdjustedFocusedElement() retargets focus to the outermost shadow host (tree_scope.cc#L706), and that host is never inside a popover that lives in a shadow root, so the restore is skipped.

WebKit (HTMLElement.cpp#L1425) and Gecko (nsGenericHTMLElement.cpp#L3867) use the shadow-including check. A plain <div popover> in a shadow root fails the same way in Chromium, so UUI code isn't the cause. I didn't find an existing Chromium issue for this. Once Chromium fixes it, the new toggle handler does nothing there.

Relation to #1555

#1555 fixes the colour picker on its own and doesn't depend on this PR. The two don't overlap. #1555 also returns focus to the colour picker's trigger in WebKit when the popover was opened by a click: a click doesn't focus the trigger there, so the browser has nothing to return to. This PR only restores what had focus when the popover opened, which is the spec's behaviour. From reading both, where both apply (Chromium, opened from the keyboard) they move focus to the same trigger, and whichever runs second finds focus already there. I haven't tested the two branches together.

Other affected places

  • UUI: the tab group's overflow menu (uui-tab-group) and uui-responsive-container, as well as the colour picker. All three put their trigger and popover in their own shadow root.
  • CMS backoffice (main at 041bcff, 06-11-2026): 24 files render uui-popover-container from a Lit template, which puts it inside that component's shadow root. 2 tiptap extensions create one with document.createElement; I didn't check where those are attached. Any of them is affected where focus moves into the popover. Not tested in the backoffice.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (minor updates related to the tooling or maintenance of the repository, does not impact compiled assets)

Motivation and context

Found while fixing the colour picker's keyboard behaviour for #1554 in #1555. Tests there showed that Chromium doesn't return focus to the trigger even when the browser itself closes the popover on Escape, so this is existing behaviour rather than something #1555 introduced. It affects every popover container that lives in a shadow root, not just the colour picker.

How to test?

  1. npm run storybook and open Colour Picker > Default in Chrome.
  2. Tab to the trigger, press Enter, Tab into the picker (for example to the text input), then press Escape.
  3. Focus should be back on the trigger. On main, focus is on <body>, so the next Tab starts again from the top of the page.
  4. Run npx vitest run src/components/popover-container, and for WebKit CI=true npx vitest run src/components/popover-container --project=webkit.
Scenario Result
Vitest, Chromium: focus returns to the trigger on Escape and on hidePopover(), with the popover in the light DOM and in a shadow root (4 tests) Verified. The two shadow-root tests fail on main and pass with the fix
Vitest, Chromium: focus stays put when it was outside the popover at close; no restore after a click outside; no restore for a manual popover; a nested popover behaves like a native <div popover> Verified. Each test fails when the part of the fix it guards is removed
Vitest, WebKit, popover-container tests Verified, 19/19 locally (macOS). On CI (Linux), WebKit can leave focus on the hidden element inside a closed nested popover for a moment before moving it to <body>, so the nested-popover test now treats both as "not restored" (a65a5b2)
Vitest, full suite, Chromium Verified, 1107/1107 locally
CI test job (Chromium, Firefox, WebKit) at a65a5b2 Verified, 261/261 test files
Real headed Google Chrome 157.0.8103.79 against Storybook, with real key presses: Popover Container > Overview (light DOM), Colour Picker > Default, Popover Container > Inside Shadow DOM Scroll Container (with a button added inside the popover) Verified. On main, the light-DOM story returned focus and the two shadow-DOM stories left it on <body>. With the fix, all three return focus to the trigger
uui-tab-group overflow menu: Enter on More, Tab into the menu, Escape Verified in a throwaway Vitest probe (Chromium, not committed). Focus lands on <body> on main and on the More button with the fix
uui-responsive-container overflow menu: same steps Partly verified in the same probe. Focus lands on <body> on main and on the More button with the fix. Enter on More didn't open the popover in the probe, so I opened it with click() there and didn't look into why
Firefox Verified on CI only: Vitest couldn't connect to Firefox locally. Gecko's source uses the spec's check, so the fix should do nothing there
CMS backoffice Not tested
Together with #1555 Not tested
Screen readers Not tested

Screenshots (if appropriate)

Not applicable: the change is about where focus lands, not how anything looks.

Checklist

  • If my change requires a change to the documentation, I have updated the documentation in this pull request. (No documentation change needed.)
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.

🤖 Generated with Claude Code

… a shadow root closes

Chromium skips the HTML spec's focus restore when the popover sits inside a
shadow root: its hide step tests `contains(document.AdjustedFocusedElement())`,
which retargets focus to the outermost shadow host, where the spec asks for a
shadow-including descendant check. Focus then falls to <body> after Escape or
hidePopover() (colour picker, any uui-popover-container inside a component).
WebKit and Gecko follow the spec.

The container now remembers what was focused when it opened and, on the
`toggle` event, restores it only when focus was inside at close and the browser
left it on <body> or inside the hidden popover. It keeps the spec's limits: auto
popovers only, the first popover in a stack only, and no restore after light
dismiss.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 09:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@iOvergaard iOvergaard added the bug Something isn't working label Nov 19, 2026
…t restored

WebKit on Linux CI can leave focus on the hidden element inside a closed
nested popover for a while before moving it to <body>. Both mean nothing
restored focus, so the nested-popover test now compares that outcome
instead of the exact element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Azure Static Web Apps: Your stage site is ready! Visit it here: https://delightful-beach-055ecb503-1558.westeurope.azurestaticapps.net

@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

Azure Static Web Apps: Your stage site is ready! Visit it here: https://delightful-beach-055ecb503-1558.westeurope.azurestaticapps.net

@nielslyngsoe
nielslyngsoe merged commit 0a1e3e2 into main Nov 19, 2026
11 of 12 checks passed
@nielslyngsoe
nielslyngsoe deleted the v2/fix/popover-container-focus-restore branch November 19, 2026 15:25
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.

3 participants