Repository navigation
fix(popover-container): return focus to the trigger when a popover in a shadow root closes - #1558
Conversation
… 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>
There was a problem hiding this comment.
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.
…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>
|
Azure Static Web Apps: Your stage site is ready! Visit it here: https://delightful-beach-055ecb503-1558.westeurope.azurestaticapps.net |
|
|
Azure Static Web Apps: Your stage site is ready! Visit it here: https://delightful-beach-055ecb503-1558.westeurope.azurestaticapps.net |



Description
When a
uui-popover-containersits inside a component's shadow root and closes while focus is inside it (Escape orhidePopover()), 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:
beforetoggle), it remembers the focused element, looking through shadow roots.beforetoggle), it notes whether focus is inside the popover.toggle(closed), if focus was inside and the browser left it on<body>or inside the now-hidden popover, it focuses the remembered element withpreventScroll.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 newtogglehandler 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-tab-group) anduui-responsive-container, as well as the colour picker. All three put their trigger and popover in their own shadow root.mainat 041bcff, 06-11-2026): 24 files renderuui-popover-containerfrom a Lit template, which puts it inside that component's shadow root. 2 tiptap extensions create one withdocument.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
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?
npm run storybookand open Colour Picker > Default in Chrome.main, focus is on<body>, so the next Tab starts again from the top of the page.npx vitest run src/components/popover-container, and for WebKitCI=true npx vitest run src/components/popover-container --project=webkit.hidePopover(), with the popover in the light DOM and in a shadow root (4 tests)mainand pass with the fixmanualpopover; a nested popover behaves like a native<div popover><body>, so the nested-popover test now treats both as "not restored" (a65a5b2)testjob (Chromium, Firefox, WebKit) at a65a5b2main, 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 triggeruui-tab-groupoverflow menu: Enter on More, Tab into the menu, Escape<body>onmainand on the More button with the fixuui-responsive-containeroverflow menu: same steps<body>onmainand on the More button with the fix. Enter on More didn't open the popover in the probe, so I opened it withclick()there and didn't look into whyScreenshots (if appropriate)
Not applicable: the change is about where focus lands, not how anything looks.
Checklist
🤖 Generated with Claude Code