Skip to content

How to conditionally render slotted content #3843

Answered by justinfagnani
steve2507 asked this question in Q&A
Discussion options

You must be logged in to vote

Slotted elements belong to the outer scope of a host with a shadow root, so their connected-ness isn't controlled by the host, it's controlled by whatever put them there. Like you noted, removing the slot doesn't remove the element.

Since the slot is an implementation detail of the host, this DOM:

<some-dialog>
  <some-other-element></some-other-element>
</some-dialog>

...doesn't change regardless of the presence of slots in <some-dialog>.

This is the same for a native detail element. The content is not detached, it's just not rendered. If you want to know when the content is displayed, you can listen for the toggle event.

I think there are two (maybe 3) main ways to handle this:

  1. Conditi…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@steve2507
Comment options

@steve2507
Comment options

@filimon-danopoulos-stratsys
Answer selected by steve2507
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants