Active Element
<x-focus>
#shadow-root
<input type="text" placeholder="input inside shadow dom">Active Element in Shadow DOM
// ⭐️ if you click <input> inside a shadow root,
// the `focus` event will look like it came from <x-focus>,
// not the <input>❗️
document.activeElement // ⭐️ <x-focus>
// ⭐️ only works with {mode: open}.
document.activeElement.shadowRoot.activeElement // ⭐️ <input> Active Element down Multiple Levels of Shadow DOM
function deepActiveElement() {
// top-level active element
let a = document.activeElement;
// recursively drill into shadow roots to find activeElement
while (a && a.shadowRoot && a.shadowRoot.activeElement) {
a = a.shadowRoot.activeElement;
}
// return the real active element
return a;
}{delegatesFocus: true}
Last updated