Created
January 27, 2026 21:07
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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