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.

Support nested custom elements

See original GitHub issue

Is your feature request related to a problem? Please describe. If I have multiple levels of nested components, the components “disappear” when compiled to custom elements. I need to use custom elements for CSS encapsulation in a component that has multiple layers of svelte components.

Describe the solution you’d like

<!--Calendar.svelte-->
<div>
     <ButtonComponent/>
</div>

<!--ButtonComponent.svelte-->
<div class='button'>
     <LinkComponent/>
</div>

when exported should output

<my-calendar>
     <my-button>
          <my-link></my-link>
     </my-button>
</my-calendar>

instead of

<my-calendar>
     <div class='button'>
          <slot></slot>
     </div>
</my-calendar>

If all the nested Svelte components in the tree have the <svelte:option tag="my-component"> set.

Describe alternatives you’ve considered The only alternative I’ve come up with is to export each component individually and then nest them elsewhere. Doing it this way beats the purpose of using Svelte and the conveniences it brings. I have tried creating a custom element and then embedding a Svelte component inside it, but Svelte javascript inlines styles into the main document’s head, and not into the shadow dom of the parent custom component, so styles are not applied to the Svelte component (This is unexpected, and I think this should be a separate issue on its own).

How important is this feature to you? This is a very important feature. I’m working on a Chrome extension that injects a Svelte component into a range of pages, so the shadow dom is needed for CSS encapsulation. My component has other Svelte components such as Buttons etc. nested deeply inside.

This might be related to #2605

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Rich-Harriscommented, Sep 7, 2019

It’s possible that Svelte’s behaviour should change here, but in the meantime what about doing it like this?

<!--Calendar.svelte-->
<svelte:options tag="my-calendar"/>

<script>
  import './ButtonComponent.svelte';
</script>

<div>
  <my-button/>
</div>
2reactions
MonkeyAndrescommented, Oct 21, 2020

I just created a template for using Svelte and web components interchangeably in a codebase. And also comes with Storybook and i18n configured. More info: https://github.com/redradix/svelte-custom-element-template

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using custom elements - Web Components | MDN
This article introduces the use of the Custom Elements API. ... nested batch of elements that together provide a custom page feature.
Read more >
Nested custom element not rendered when parent element is ...
createElement('div', {is: 'entry-page'}) , the nested custom elements do not render as if not defined at all. Even customElements.whenDefined(' ...
Read more >
Custom Element Best Practices - web.dev
When a browser does not support custom elements, the nested content remains available, visible and accessible.
Read more >
Nested Custom Attributes
You can use nested custom attributes to send objects as a new data type for custom attributes. This nested data allows you to...
Read more >
Custom elements - The Modern JavaScript Tutorial
We can create custom HTML elements, described by our class, ... well-supported across the browsers, to show a nicely formatted time.
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 Hashnode Post

No results found