question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Web components with ShadowDOM, unable to focus on elements in slot (LightDOM)

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
kee-othcommented, Feb 24, 2022

@stefcameron Thanks for much for the work on this! I’ll give this a try shortly 👍

1reaction
stefcameroncommented, Feb 24, 2022

@andresriveratoro Nice, glad to hear you were successful with it!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found