You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I make it p + p + p in the example above, it shows css_unused_selector too, because it knows there are only 2 unknown elements. Adding a 3rd one makes it happy once again:
Which means that based on multiple defined svelte:element, Svelte seems to be able to recognize that the selector might be applied, so it has to be present in generated CSS.
Why not use :global style?
Two reasons:
Because of how CSS specificity works, :global(p + p) is not applied when there is a style already defined for p in the component:
<svelte:elementthis={'p'}>Both elements</svelte:element>
<svelte:elementthis={'p'}>are red</svelte:element>
<style>
p {color: red; } :global(p+p) {/* not applied because of CSS specificity */color: green; }
</style>
It would require some additional tweaking to make it work, like adding some classes, which seems messy, clumsy and unnecessary.
For my use case, this style belongs to the specific component. It doesn't make sense to make it :global when it is not used anywhere else.
Possible solution
Svelte's understanding of the dynamicity of svelte:element doesn't seem to apply to #each loops. Possibly it's the unpredictability of the number of rendered elements that causes this problem.
One way to handle it would be to disable css_unused_selector altogether when there is svelte:element in #each loop. Possibilities are endless in such case, both in terms of what kind of elements and how many of them will appear. So to me it would make sense allow basically any CSS combinators with any number of elements.
Workaround until it is fixed
Adding multiple svelte:element (even as unreachable code, like below) does the trick:
To make it work for p + p + p there would have to be 3 of them and so on, so it is obviously not ideal. But at least it's just an additional piece of code that can be easily removed later, when the issue is fixed, without adjusting anything else in the actual code.
There is another thing that I described in the playground where when another component has exactly the same style, it suddenly makes it work in the component with p + p combinator with svelte:element in #each loop. I have already spent much more time than anticipated on describing this though, and maybe it should be placed in another issue anyway, so that's it for now.
Reproduction in Svelte REPL Playground v5.16.1
Describe the problem
When using
#each loop
, CSS+
combinator is not being recognized as used (and thus it's absent in generated CSS):But when multiple
svelte:element
are defined one at a time, it works:If I make it
p + p + p
in the example above, it showscss_unused_selector
too, because it knows there are only 2 unknown elements. Adding a 3rd one makes it happy once again:Which means that based on multiple defined
svelte:element
, Svelte seems to be able to recognize that the selector might be applied, so it has to be present in generated CSS.Why not use
:global
style?Two reasons:
:global(p + p)
is not applied when there is a style already defined forp
in the component:It would require some additional tweaking to make it work, like adding some classes, which seems messy, clumsy and unnecessary.
:global
when it is not used anywhere else.Possible solution
Svelte's understanding of the dynamicity of
svelte:element
doesn't seem to apply to#each
loops. Possibly it's the unpredictability of the number of rendered elements that causes this problem.One way to handle it would be to disable
css_unused_selector
altogether when there issvelte:element
in#each
loop. Possibilities are endless in such case, both in terms of what kind of elements and how many of them will appear. So to me it would make sense allow basically any CSS combinators with any number of elements.Workaround until it is fixed
Adding multiple
svelte:element
(even as unreachable code, like below) does the trick:To make it work for
p + p + p
there would have to be 3 of them and so on, so it is obviously not ideal. But at least it's just an additional piece of code that can be easily removed later, when the issue is fixed, without adjusting anything else in the actual code.There is another thing that I described in the playground where when another component has exactly the same style, it suddenly makes it work in the component with
p + p
combinator withsvelte:element
in#each
loop. I have already spent much more time than anticipated on describing this though, and maybe it should be placed in another issue anyway, so that's it for now.Reproduction
Reproduction in Svelte REPL Playground v5.16.1
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: