Web components with ShadowDOM, unable to focus on elements in slot (LightDOM)
See original GitHub issueI’m using focus-trap
in a web components library but I am unable to make the package work with elements within a ShadowDOM slot
. Is there a way to have a single focus trap contain elements from both the ShadowDOM and elements passed in via the slot
?
Example:
// modal.js
const Modal = (props) => {
const trapFocus = (element) => {
const trap = focusTrap.createFocusTrap(element)
trap.activate()
}
return (
<div ref={trapFocus}>
<a href="#">These items</a>
<a href="#">get trapped</a>
<slot /> // Elements in slot don't get trapped
</div>
);
}
// index.html
<custom-modal>
<button>This goes in the slot and isn't included in the focus trap</button>
</custom-modal>
Thank you!
Issue Analytics
- State:
- Created 2 years ago
- Comments:18 (9 by maintainers)
Top Results From Across the Web
Shadow DOM v1 - Self-Contained Web Components
Elements are allowed to "cross" the shadow DOM boundary when a <slot> invites them in. These elements are called distributed nodes. Conceptually ...
Read more >Focus inside Shadow DOM - Medium
This is important for Web Components. But I'll focus on Shadow DOM, and tricky logic around document.activeElement — starting with a quick ...
Read more >Using shadow DOM - Web Components | MDN
Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree — this shadow DOM tree starts with...
Read more >Working with Shadow DOM - Lit.dev
Lit components use shadow DOM to encapsulate their DOM. Shadow DOM provides a way to add a separate isolated and encapsulated DOM tree...
Read more >slot selector limit in web component - shadow dom
You are only allowed to change CSS rules for the top level elements inside the slot. And you are even limited to what...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@stefcameron Thanks for much for the work on this! I’ll give this a try shortly 👍
@andresriveratoro Nice, glad to hear you were successful with it!