Skip to content

Commit 0560e1f

Browse files
committed
refactor getactivelement
1 parent 697a7b1 commit 0560e1f

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

packages/mui-utils/src/getActiveElement/getActiveElement.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* the shadow host element. This function recursively traverses shadow roots to find
66
* the actual focused element.
77
*
8-
* @param root - The document or shadow root to start from. Defaults to document.
8+
* @param root - The document to query for the active element.
99
* @returns The actual focused element, or null if no element has focus.
1010
*
1111
* @example
@@ -17,17 +17,12 @@
1717
* // Starting from a specific document
1818
* const activeElement = getActiveElement(ownerDocument(element));
1919
*/
20-
export default function getActiveElement(root: Document | ShadowRoot = document): Element | null {
21-
const activeEl = root.activeElement;
20+
export default function activeElement(doc: Document): Element | null {
21+
let element = doc.activeElement;
2222

23-
if (!activeEl) {
24-
return null;
23+
while (element?.shadowRoot?.activeElement != null) {
24+
element = element.shadowRoot.activeElement;
2525
}
2626

27-
// If the active element has a shadow root, recursively check inside it
28-
if (activeEl.shadowRoot && activeEl.shadowRoot.activeElement) {
29-
return getActiveElement(activeEl.shadowRoot);
30-
}
31-
32-
return activeEl;
27+
return element;
3328
}

0 commit comments

Comments
 (0)