Skip to content

Instantly share code, notes, and snippets.

@mendeza
Created January 27, 2026 21:07
Show Gist options
  • Select an option

  • Save mendeza/727cf0253752035a33a2c01e4858d553 to your computer and use it in GitHub Desktop.

Select an option

Save mendeza/727cf0253752035a33a2c01e4858d553 to your computer and use it in GitHub Desktop.
UI engineers and A11y SMEs need to tracks focus assiduously. `document.activeElement` is not enough; it cannot descend into an iframe or shadowDOM.
# Chrome devtools 'live expressions' for `focus`
## Regular DOM
```
document.activeElement
```
## Inside a StoryBook iframe DOM
```
document.getElementById("storybook-preview-iframe").contentWindow.document.activeElement
```
## Inside ShadowDOM
```
(function getDeepActiveElement(root){
const activeEl = root.activeElement;
if (!activeEl) return null;
if (activeEl.shadowRoot) return getDeepActiveElement(activeEl.shadowRoot);
return activeEl;
})(document)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment