@augment-vir/web
Version:
A collection of augments, helpers types, functions, and classes only for web (frontend) JavaScript environments.
28 lines (27 loc) • 817 B
JavaScript
/* node:coverage disable */
/**
* Get the currently active element and walks the shadow dom to find, if any, the truly active
* element nested within the shadow dom.
*
* @category Web : Elements
* @category Package : @augment-vir/web
* @returns The depth that walking stopped at.
* @package [`@augment-vir/web`](https://www.npmjs.com/package/@augment-vir/web)
*/
export function walkActiveElement(callback) {
let depth = 0;
let activeElement = document.activeElement || undefined;
while (activeElement) {
if (callback({
depth,
element: activeElement,
})) {
return depth;
}
activeElement = activeElement.shadowRoot?.activeElement || undefined;
if (activeElement) {
++depth;
}
}
return depth;
}