-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support nested custom elements #3520
Comments
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> |
Right. Why didn't I think of that? 😅 I like this pattern better than what I was using, and will probably switch to it in for now. That said, I think that the jump from <svelte:options tag="my-calendar"/>
<script>
import ButtonComponent from './ButtonComponent.svelte';
customElements.define("my-botton", ButtonCompontent);
</script>
<div>
<my-button/>
</div> Svelte can help solve this. I'm in support of writing Svelte components as Svelte components and having the compiler take care of making them custom elements under the hood if that's practically implementable. I think having it done under the hood the right kind of "magical". 🙂 |
I just created a template for using Svelte and web components interchangeably in a codebase. And also comes with Storybook and i18n configured. |
Going through the same issue. |
A year of Svelte later, I don't feel the same way about this feature request anymore. I prefer the current behaviour to the requested one. I now avoid custom elements like plague, but if I must, I've learnt I only need a wrapper and don't want the children to also become web components. But there seems to be marginal interest, so I'll leave the issue open for now. Thoughts? |
I think it's undecided if this is something that we'd want to change, and unless a community member appears and makes this change, it's unlikely to get changed anytime soon, since web components aren't really the primary focus. Happy for it to be left open or closed as people see fit. |
@kwangure Could you please ellaborate on this ? |
I use the web component as a parent, and then every other component nested inside is a regular Svelte component. <my-wrapper>
<RegularComponent1></RegularComponent1>
<RegularComponent2></RegularComponent2>
</my-wrapper> I usually only reach for web components when I need to encapsulate CSS. In my example, the |
Yes. I do exactly that. If you're using svelte-kit see comments about setup at sveltejs/kit#773. |
Thanks for the help, I'm quite new to svelte and it's much appreciated ! I've set svelte({
preprocess: sveltePreprocessOpts,
include: WEB_COMPONENT_POSTFIX,
compilerOptions: {
customElement: true,
...svelteCompilerOpts,
}
}),
svelte({
preprocess: sveltePreprocessOpts,
emitCss: false,
exclude: WEB_COMPONENT_POSTFIX,
compilerOptions: {
customElement: false,
...svelteCompilerOpts,
}
}), Is there a way to have shadowDOM |
It seems like you're following the long-winded path I've gone down before.😂 I opened an issue when I first got to where you've gotten. I mention how I work around that here #4039 (comment). |
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
when exported should output
instead of
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
The text was updated successfully, but these errors were encountered: