-
-
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
Nesting Svelte Web Components is broken with slots #8377
Comments
Okay, so to get around this, we've had to stop using Svelte's built in Our current workaround has been to create our own web component wrapper for Svelte components which works pretty well. I think it's probably worth removing the built in customElement support, as it's kind of half baked and doesn't support a number of common use cases. In addition, having it in the documentation is misleading, as it implies that it's the best path for using Svelte with Web Components. A better alternative might be to point to some "Official Svelte Web Component Wrapper Example". That way people don't get pushed down the wrong path at the beginning. Svelte can play nice with web components, it's just that the blessed path doesn't seem to. |
@fallaciousreasoning depending on if you're using the light DOM or not, p.s. It might also work pretty well regarding nesting in the shadow DOM as well, it's just fairly new and I haven't tested yet (thus open to feedback)! |
Also, just to clarify (after rereading):
|
p.s.s. @fallaciousreasoning it's insane how similar the code you wrote and Was your solution capable of handling nesting elegantly? Also, is it capable of handling early rendering (like via Basically my solutions these various issues were:
There are some other nuances, but those are the broad strokes. |
Wow, that's quite funny, I wish I'd found Yeah, not knowing slots during initial parsing is a bit of a pain. I was thinking of adding a step to our build process to extract them, but for now I just destroy and recreate the Svelte component. It's not particularly efficient but most of our components are leaf nodes, and for now it seems to work (but it means I have to keep track of all the events which have been added to the component). It was a bit of a shock to me that you can't actually change the slots that render inside a Svelte component, like, it seems quite surprising that this doesn't work (in native Svelte): <MyComponent>
{#if condition}
<span slot="foo">Bar</span>
{/if}
</MyComponent> I've definitely run into quite a few sharp edges trying to use Svelte with Web Components, it's been a bit of a painful experience 😆 |
Similar situation here. Got me thinking, though... there are certainly some optimizations that could be performed by first queueing the Svelte component [re]render in a PoC here: patricknelson/svelte-retag@main...experimental-defer-request-animation-frame. Not ready to do a PR yet since this really breaks unit tests which depend on synchronous rendering right now (plus I'd wanna unit test this particular functionality too, of course). 😅 Nice thing about this |
Oh interesting. Well, thankfully it's at least a work in progress, however it seems (per Simon's post here #8304 (comment)) that's probably going to wait until the architectural dust settles for v5 (so if it lands, it'll likely be 4.x or later). I'm glad when I tested that it wasn't a bug in my code. I'm still new to Svelte (I've studied it for a while but only coded for the past month or two) so I was worried I did something wrong in |
@fallaciousreasoning: Not sure how easy it is for you to give it a shot, but if you're willing: I'm curious to know if it actually saves you some render time. I'll at least setup a draft PR here (fixed the broken unit tests which relied on synchronous rendering). Keep an eye on |
p.s. @fallaciousreasoning just setup a demo for you to check out if you want: https://svelte-retag.vercel.app/ |
Funnily enough me too 😆 I keep thinking I've done something wrong and being surprised when I can repro it in the Svelte repl.
I'd definitely be interested in experimenting with it (I'm sure Looking at the demo, it seems pretty cool! Do you think it would be possible to avoid the |
Sorry I missed this!
Readability and comprehensibility is key, I do agree! I’m not entirely sure, but totally open to a PR if someone can figure it out. Right now it’s primary use case is to assist in segmenting default slot content from the rendering of the component itself, as well as serving as a partition/boundary layer against That said, now that I’ve added context support, I switched to using While I do concede it’s a bit ugly aesthetically, it has proven to be highly functional for now (see demo at https://svelte-retag.vercel.app/). I agree though that the ideal solution wouldn’t require this extra nesting though! It’s at least in my |
Describe the bug
<svelte:options tag="x-parent"/>
<svelte:options tag="x-child"/>
customElement: true
in thecompileOptions
in SvelteWhen rendering an
x-parent
on the page inspecting it's shadow dom reveals that the markup forx-child
has been inlined intox-parent
, which means thatx-child
's slots are exposed viax-parent
and styles forx-child
are not applied.(note: I've set the severity as
blocking all usage
but it only blocks it a specific case: Nesting elements, whencustomElement: true
)Reproduction
Working REPL (without
customElement: true
)https://svelte.dev/repl/fde37dfe1ca64e56960a0cf7b64bc242?version=3.56.0
If you download the REPL and set
customElement: true
inrollup.config.js
and runnpm run dev
nothing will render.Inspecting the output ShadowDOM you can see that the source for
x-child
has been inlined into the template forx-parent
.The expected result here would be that
<x-parent>
contains an<x-child>
Logs
No response
System Info
Severity
blocking all usage of svelte (when
customElement: true
)Potential Workaround
Instead of
it might be possible to directly use the web component
<x-child/>
However, this is not a solution for our (admittedly very specific) use case, as we want to be able to optionally compile to Web Components, rather than always. We're building a component Library, so it's useful to be able to consume the elements from Svelte or expose them via Web Components so we can consume them from existing React & Polymer projects.
The text was updated successfully, but these errors were encountered: