Repository navigation
fix(core): hide outside content with inert to avoid aria-hidden focus warning - #699
daveycodez wants to merge 1 commit into
Conversation
… 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.
👷 Deploy request for kobalte pending review.Visit the deploys page to approve it
|
|
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 This is very likely why A correct fix probably needs to either:
and be verified against native |
Summary
Fixes #541 — the console warning Chrome logs the first time any modal overlay opens:
Reproduces on the official DropdownMenu docs (and, per the thread, on
Dialog,Select,Popover— anything that usescreateHideOutside).Root cause
On open,
ariaHideOutsidesynchronously walks from<body>and setsaria-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 anaria-hiddenancestor, 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
inertattribute overaria-hiddenwhen 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.inertremoves the subtree from the focus order in addition to hiding it from assistive tech, so the browser doesn't block it. Falls back toaria-hiddenwheninertis unsupported.The change is confined to
create-hide-outside.ts: agetHidden/setHiddenpair replaces the directaria-hiddenreads/writes.Verification
ariaHideOutsideunit tests: all pass unchanged. jsdom doesn't implementinert, sosupportsInertisfalseunder test and the code takes thearia-hiddenpath — the current assertions still hold. (Real browsers getinert.)#appreceivesinert(notaria-hidden); focus moves into the menu content.inertis fully removed on close.Notes
React Aria gates this behind a
shouldUseInertoption and opts in from its modal overlays. Here I've defaulted it on, sinceariaHideOutside/createHideOutsideis 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 throughCreateHideOutsidePropsand opt in per-component if you'd prefer that shape.