-
Notifications
You must be signed in to change notification settings - Fork 14
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
Document edge-cases/issues with replacing default Svelte scoping #36
Comments
The Svelte approach is to append every css selectors with a unique class to only affect the elements of the component. The CSS Modules approach is to scope every class names with a unique id/name to only affect the elements of the component. Approach ExampleHere is the svelte component <p>lorem ipsum tut etoyu</p>
<p class="red">lorem ipsum tut etoyu</p>
<style>
p {
font-size: 14px
}
.red {
color: red;
}
</style> After compilation, the component will look like this Svelte scoped styles<p class="svelte-123qwe>lorem ipsum tut etoyu</p>
<p class="red svelte-123qwe">lorem ipsum tut etoyu</p>
<style>
p.svelte-123qwe {
font-size: 14px
}
.red.svelte-123qwe {
color: red;
}
</style>
CSS Modules preprocessor<p>lorem ipsum tut moue</p>
<p class="red-123qwe">lorem ipsum tut moue</p>
<style>
p {
font-size: 14px
}
.red-123qwe {
color: red;
}
</style>
As non class selectors rules are applying globally, it is preferable to base each selector with a class. <p class="text">lorem ipsum tut moue</p>
<p class="text red">lorem ipsum tut moue</p>
<style module>
.text {
font-size: 14px
}
.red {
color: red;
}
</style> Compiling to <p class="text-456rty">lorem ipsum tut moue</p>
<p class="text-456rty red-123qwe">lorem ipsum tut moue</p>
<style>
.text-456rty{
font-size: 14px
}
.red-123qwe {
color: red;
}
</style> Why using CSS Modules over Svelte scoping ?
|
Thank you for the very thorough write up! I think this should def be added to the readme or similar. I’m particularly interested in the scoped strategy, so you get the same behaviour as svelte’s scoping while also being able to pass scoped classnames to child components. If doubling classnames length is the only issue I think that’s a very good trade off |
Actually it won't work. This is something I should add up to the list of pros/cons. Due to the svelte scoping, the <!-- Child Component Button.svelte -->
<button class={$$restProps.class}><slot /></button>
<style>
button {
background: red;
color: white;
}
</style> <!-- Parent Component -->
<script>
import Button from './Button.svelte';
</script>
<div class="wrapper">
<h1>Welcome</h1>
<Button class="btn" />
</div>
<style module="scoped">
.wrapper {
margin: 0 auto;
padding: 16px;
max-width: 400px;
}
.btn {
margin-top: 30px;
}
</style>
<!-- Parent Component -->
<script>
import Button from './Button.svelte';
</script>
<div class="wrapper">
<h1>Welcome</h1>
<p class="space">Lorem ipsum tut ewou tu po</p>
<Button class="space" />
</div>
<style module="scoped">
.wrapper {
margin: 0 auto;
padding: 16px;
max-width: 400px;
}
.space {
margin-top: 30px;
}
</style>
So to summarize
|
@madeleineostoja After adding some new features, I updated the Readme. Thank you for your inputs. |
The README mentions that enabling
useAsDefaultScoping
can cause issues especially when applied to external elements. What are these issues? And more broadly, what potential edge-cases can come up with this preprocessor over Svelte's default scoping strategy? It seems to be a pretty drastic rewrite of core scoping logic, so it'd great to have some kind of breakdown of pros/cons/potential gotchas with this approach. Right now it seems a little too good to be true.The text was updated successfully, but these errors were encountered: