#7681·ariakit

Set data-focus-visible and call onFocusVisible for keyboard focus inside a shadow root

Author: diegohazCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbughas workarounda11yp2

Context

This came up in the pre-commit review of the pull request that fixes #7677. That pull request fixes a different hasFocus caller, in the Combobox autoSelect effect. It does not change Focusable. Related earlier reports: #7657 and #7677 (both about hasFocus inside an open shadow root). #7657 notes that no user-visible failure was confirmed for the other hasFocus callers. This issue tracks one of them that now has a confirmed failure.

Problem

Inside an open shadow root, Focusable (and so Combobox, Button, and other components built on it) never sets data-focus-visible when it receives focus with the keyboard. It also never calls onFocusVisible. The browser matches :focus-visible on the same element.

handleFocusVisible in packages/ariakit-react-components/src/focusable/focusable.tsx starts with:

typescript
if (!hasFocus(element)) return;

hasFocus (packages/ariakit-utils/src/focus.ts) compares getActiveElement(element) with element. getActiveElement reads document.activeElement. While focus is inside an open shadow root, that value is the shadow host, so the check always fails and handleFocusVisible returns before it sets the attribute or calls onFocusVisible.

The other hasFocus call in that file (the autoFocus microtask) has no visible effect. It only calls element.focus() on an element that is already focused, which fires no events. This comes from reading the code, not from a browser test.

Evidence

This is pre-existing on main. I checked it on main at e307f798d8d3f816ac0b7b36a5b3e952a667e0b8 with Chromium and Playwright. After hydration, I pressed Tab in /react/previews/combobox-list-7657/, which renders two comboboxes inside an open shadow root. I added a plain Button with an onFocusVisible counter to the same shadow root for the probe. Results:

Element (in the shadow root, focused with Tab) data-focus-visible matches(":focus-visible") onFocusVisible calls
Combobox "Popover fruit" absent true 0
Combobox "List fruit" absent true not measured
Button absent true 0

Control: a Combobox in the light DOM (/react/previews/combobox-group/) gets data-focus-visible="true".

Cause check: I removed the hasFocus guard in a throwaway worktree. Then data-focus-visible was "true" on all three elements, and onFocusVisible fired once for the Combobox and once for the Button.

I searched open issues for shadow root, Focusable, data-focus-visible, onFocusVisible, and hasFocus. I found no existing report of this behavior.

Why the pull request for #7677 can finish without it

That pull request fixes the Combobox autoSelect effect, which needs no Focusable change. This bug affects only the focus-visible attribute and callback. It exists on main independently of that pull request.

Expected outcome

Keyboard focus inside an open shadow root sets data-focus-visible and calls onFocusVisible, as it does in the light DOM. The fix should use a shadow-aware focus check, for example the getTreeActiveElement helper in composite/utils.ts or a shadow-aware option on hasFocus. Browser tests in an app sandbox should cover Tab focus on a Combobox and a Button inside a shadow root.

Workaround

See the triage comment with a tested :focus-visible workaround and browser evidence.