Skip to content

Instantly share code, notes, and snippets.

@kreo
Forked from MarsiBarsi/get-native-focused.ts
Created February 17, 2024 01:17
Show Gist options
  • Save kreo/b62b7aed344dee7b57da79ff0ba67356 to your computer and use it in GitHub Desktop.
Save kreo/b62b7aed344dee7b57da79ff0ba67356 to your computer and use it in GitHub Desktop.
Returns current active element, including shadow dom
/**
* Returns current active element, including shadow dom
*
* @return element or null
*/
export function getNativeFocused(documentRef: Document): Element | null {
if (!documentRef.activeElement || !documentRef.activeElement.shadowRoot) {
return documentRef.activeElement;
}
let element = documentRef.activeElement.shadowRoot.activeElement;
while (element && element.shadowRoot) {
element = element.shadowRoot.activeElement;
}
return element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment