Skip to content

fix(core): hide outside content with inert to avoid aria-hidden focus warning - #699

Closed
daveycodez wants to merge 1 commit into
kobaltedev:mainfrom
daveycodez:fix/aria-hide-outside-inert
Closed

daveycodez wants to merge 1 commit into
kobaltedev:mainfrom
daveycodez:fix/aria-hide-outside-inert

Conversation

@daveycodez

Copy link
Copy Markdown
Contributor

Summary

Fixes #541 — the console warning Chrome logs the first time any modal overlay opens:

Blocked aria-hidden on an element because its descendant retained focus. … Consider using the inert attribute instead, which will also prevent focus.

Reproduces on the official DropdownMenu docs (and, per the thread, on Dialog, Select, Popover — anything that uses createHideOutside).

Root cause

On open, ariaHideOutside synchronously walks from <body> and sets aria-hidden="true" on everything outside the portalled content, including the ancestor of the trigger. But focus is moved into the overlay a tick later (deferred autofocus / focus scope). So for one frame the still-focused trigger sits under an aria-hidden ancestor, and Chrome blocks it (and logs the warning). It only surfaces once per session because Chrome dedupes the warning against the unchanged ancestor node.

Fix

Prefer the inert attribute over aria-hidden when the browser supports it — exactly what the warning itself recommends, and what @react-aria/overlays' ariaHideOutside (the file this one is based on) now does. inert removes the subtree from the focus order in addition to hiding it from assistive tech, so the browser doesn't block it. Falls back to aria-hidden when inert is unsupported.

The change is confined to create-hide-outside.ts: a getHidden/setHidden pair replaces the direct aria-hidden reads/writes.

Verification

  • Existing ariaHideOutside unit tests: all pass unchanged. jsdom doesn't implement inert, so supportsInert is false under test and the code takes the aria-hidden path — the current assertions still hold. (Real browsers get inert.)
  • Browser-tested against the DropdownMenu docs example (headless Chromium) — before/after this change:
    • ✅ 0 "Blocked aria-hidden…" warnings on first open (was 1).
    • ✅ On open, #app receives inert (not aria-hidden); focus moves into the menu content.
    • ✅ Focus restore works — pressing Escape closes the menu and returns focus to the trigger.
    • ✅ Outside-click dismissal works — clicking outside still closes the menu; inert is fully removed on close.
    • ✅ No new console errors.

Notes

React Aria gates this behind a shouldUseInert option and opts in from its modal overlays. Here I've defaulted it on, since ariaHideOutside/createHideOutside is only ever used in modal overlay contexts where making the outside inert is the correct, more-robust behavior (and it's verified above not to regress focus restore or outside-dismiss). Happy to instead thread an option through CreateHideOutsideProps and opt in per-component if you'd prefer that shape.

… warning

ariaHideOutside set aria-hidden on everything outside a modal overlay,
including the ancestor of the still-focused trigger during the tick before
focus moves into the overlay. Chrome blocks that and logs "Blocked aria-hidden
on an element because its descendant retained focus" on first open (see kobaltedev#541).

Prefer the `inert` attribute (which also removes the subtree from the focus
order, so the browser doesn't block it) when supported, falling back to
aria-hidden otherwise — mirroring @react-aria/overlays' ariaHideOutside that
this file is based on.
@netlify

netlify Bot commented Jul 20, 2026

Copy link
Copy Markdown

👷 Deploy request for kobalte pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit a4d2c94

@daveycodez

Copy link
Copy Markdown
Contributor Author

Closing this — it needs more testing before it's safe to land, and I found a real regression while trying it in a downstream app.

Switching ariaHideOutside to inert fixes the console warning, but inert force-blurs the currently focused trigger when it's applied to the trigger's ancestor. That blur/refocus disrupts Chromium's :focus-visible heuristic, so when focus then lands in the overlay (or is restored to the trigger on close) it gets treated as keyboard focus — and native :focus-visible styles light up even though the overlay was opened with the mouse. With plain aria-hidden this never happens, because nothing gets blurred.

This is very likely why @react-aria/overlays gates inert behind an opt-in shouldUseInert flag rather than defaulting it on — and React Aria additionally keys its focus rings off its own data-focus-visible attribute instead of native :focus-visible, so it sidesteps the interaction entirely. Kobalte (and its consumers) rely on native :focus-visible, so defaulting inert on would regress focus-ring behavior across the board.

A correct fix probably needs to either:

  • gate inert behind an opt-in option (matching React Aria), and/or
  • coordinate with the focus-move so the modality isn't misread,

and be verified against native :focus-visible in a real component before shipping. I didn't want to leave a PR open that looks ready-to-merge when it isn't. The root-cause analysis in #541 still stands; I'll revisit with a more complete approach (or reopen) once it's properly tested.

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.

Blocked aria-hidden on an element because its descendant retained focus.

1 participant