-
-
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
Hash for styles of my Svelte component is doubled #4374
Comments
I noticed this as well, and I think this is only since one of the lastest updates. Just checked, its like this since 3.16.6 |
@tanhauhau would probably be the best person to confirm this, but this may be intended, to ensure specificity isn't messed up by the scoping class. |
But if this is intended then it cause the problem: when I generate css as a separate file my styles do not apply at all to component html. |
I noticed same issue. I hope, it will be fixed soon. Fixed CSS edge case (#4146) it's rare. But css it's bigger in all projects with ^3.16.6. |
yes it is intended. see this example: <style>
.a { color: red }
.a .b .c .d .e .f .g { color: green }
.a .b .c .d { color: green }
.b .c .e { color: red }
</style> to make sure each css selector declaration is scoped within the component, we add hash to the first and the last selector: .a.svelte-hash { color: red }
.a.svelte-hash .b .c .d .e .f .g.svelte-hash { color: green }
.a.svelte-hash .b .c .d.svelte-hash { color: green }
.b.svelte-hash .c .e.svelte-hash { color: red } to make sure that the class however if you noticed in the example, the specificity changed slightly, +1 and +2 in the class specificity. this is okay in this example, but in the example #1277: <div>
<span>red</span>
<span class='foo'>green</span>
</div>
<style>
div span {
color: red;
}
.foo {
color: green;
}
</style> after adding the svelte-hash class, the specificity order changed: div.svelte-hash span.svelte-hash {
color: red;
}
.foo.svelte-hash {
color: green;
} to restore the specificity order, we have to make sure that each declarations have the equal number of svelte hash class added: div.svelte-hash span.svelte-hash {
color: red;
}
.foo.svelte-hash.svelte-hash {
color: green;
} which causes some selectors to have double svelte hash class. |
@tanhauhau This is not smart, in many many cases with or without double/triple/... hash, specificity order is not changed. See first example (#4374 (comment)). With preprocessing you can check if it's needed or not. Right now css files are bigger in all projects. |
Yea i agree with you, it's not quite smart. |
My main problem with this is, that it is hard to overwrite specific things from a parent component. What i don't understand, when the component has one root element with a class, why does this root element needs a higher specificity? <div class="Child">
...
</div> So, in the parent I have to use .Parent :global(.Child.Child.Child) {
my: styles;
}
// or make everything !important
.Parent :global(.Child) {
my: styles !important;
} |
Strangely, we have to add a class, so that a parent component can generate enough specificity. At least, if you want to overwrite only tags. Before this commit, it was not possible to overwrite styles for the <th> element: "div :global(table th)" did not work, because of sveltejs/svelte#4374 Now it is possible by targeting "div :global(table.et th)".
It also looks weird in dev tools and increases css bundle size by duplicating classes. This intended behavior is highly questionable. |
EDIT: Replaced my comment with a new ticket #4795 Since in my case, the duplication of hashes doesn't apply consistently and introduces a bug. |
I spent far too much time revalidating my Webpack config and testing plugins, only to eventually end up here.
Currently I'm having to use Just say no to |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Can I ask a probably very stupid question? Why does svelte not use I mean, it could be built to be "transparent" on targetting the items without adding any specificity at all. It could be an option for users to choose (if old ie support is a must). This way the double class would become totally moot. Funny fact, I am not actually using svelte. I just read an article about these issues and wondered if this has been suggested as a possible solution. Here, I made a quick demo. https://codepen.io/Arkkimaagi/details/qBjxXwa |
A more relevant demonstration here solving the issue @tanhauhau described above : |
You might be referring to my article? 😁 Using This would fix these specificity issues, but I also thought of a few downsides:
For those reasons, if Svelte did implement CSS scoping this way, it should definitely be an opt-in flag as opposed to a new default. |
Yep 😁 I agree, definitely opt-in and not an only option. The browser support and breaking change being the biggest issues. |
Just ran into this issue again on another project. This functionality is essentially Svelte adding it's own Svelte gets so many things right, it's disappointing that this issue isn't getting the proper attention it deserves. |
For anyone that doesn't need the duplicate classes, here's a gist with a Webpack plugin to strip out the duplicates. It's not optimized, may have some bugs, but at the moment, it's better than nothing. |
Proper solution would be to use svelte-preprocess-cssmodules and forget about non-class selectors. |
@non25 I looked through that module's options, and it appears to mutate the original class name and append a hash. In my particular cases, the repos utilize BEM and I'm quite familiar with the cascade - so that wouldn't solve the issue for me (hence the plugin). |
I have just spent several hours with this issue.
Together, they took me several hours to just understand what is going on. I think if something does not work transparently, it should not be added to the core. I went over the changes in #4146 and I think it can be |
Browser support for |
Svelte 5 uses |
When I put my component to REPL I have doubled styles hash and browser could not apply styles when I write them in separate css file.
component code:
CSS output:
.wpk-player.svelte-4i1jb7.svelte-4i1jb7{padding:10px 15px}.wpk-button.svelte-4i1jb7 svg.svelte-4i1jb7{fill-opacity:0.8}
Also this component reproduce the same issue:
CSS outut:
.red.svelte-wvyici.svelte-wvyici{color:red}.red.svelte-wvyici div.svelte-wvyici{color:green}
The text was updated successfully, but these errors were encountered: