fix: Restore keyboard focus for key triggers (#2634) (#2639)

- Closes #2634
This commit is contained in:
Dermot Duffy
2026-07-30 20:52:54 -07:00
committed by GitHub
parent a736f3dfaf
commit f24064689a
8 changed files with 192 additions and 7 deletions
+14
View File
@@ -0,0 +1,14 @@
/**
* Determine whether focus currently rests on an element, or on any of its
* descendants (including those inside nested shadow roots).
*/
export const isFocusWithin = (element: Element): boolean => {
const root = element.getRootNode();
// Focus is reported per tree, with a shadow host standing in for whatever is
// focused inside it, so the element's own tree is the one that will name it.
const active =
root instanceof Document || root instanceof ShadowRoot ? root.activeElement : null;
return !!active && element.contains(active);
};