Comments on: What I Like About Writing Styles with Svelte https://css-tricks.com/what-i-like-about-writing-styles-with-svelte/ Tips, Tricks, and Techniques on using Cascading Style Sheets. Tue, 05 Jul 2022 18:37:05 +0000 hourly 1 https://wordpress.org/?v=6.1.1 By: Dallin Smith https://css-tricks.com/what-i-like-about-writing-styles-with-svelte/#comment-1796311 Tue, 05 Jul 2022 18:37:05 +0000 https://css-tricks.com/?p=297067#comment-1796311 I find it funny that this is what you liked about Svelte because this was actually what made me finally give up on Svelte (that and debugging with it was a nightmare).

Yes, everything you mentioned is great – but you’ll notice almost all of it has to do with either styling from within your component or extra boilerplate and verbosity to allow you to style things from outside the component. While you usually need some internal CSS, only styling from within your component is NOT how the real world works. What’s the first thing you do when you start a new web project? Throw a reset.css file in there.

I want a headless UI component system, and that was a nightmare with Svelte for me. They purposefully made it very hard to actually style your components in the normal, natural way of applying styles to the component tag itself without a ton of boilerplate. For example:

The class solution you presented to pass a class into the component was not dependable for me. Svelte started randomly overwriting classes I passed in when it assigned its scoping class behind the scenes. I could not find a way to add a class to a component from outside the component that was dependable in a production environment. Not being able to add a class to an element is incredibly limiting.
You can use :global to style elements, but it’s truly global, meaning it leaks outside of your file to everything on your page – not just the elements and components in the file you’re working in. So you have to wrap everything in a div and scope it to that div everywhere in your code. Additionally, when you use a lot of components, your style sheet just looks like one big long list of :globals() and is difficult to visually parse.
Perhaps worst of all, in Svelte, the component tag you use does not actually represent a real element in the document, similar to a template tag in html. That makes sense with a template tag because you can easily target the elements inside, but not with an enclosed component where you ALSO can’t easily target the things inside. The two ideas are diametrically opposed in my mind.

This caused all sorts of problems and unnatural ways I had to do things:

I couldn’t target the tag name of an element in my CSS – in the same document the tag was in (without wrapping it in yet another :global selector).
I couldn’t do something like put an id on the component and target items in the component’s slot with it. I would have to either wrap another needless div around the component and scope the slotted items with that, or I would have to a.) add a custom property in the component called id, b.) export it, c.) assign an id to my component, d.) in my css, even though I’m targeting the children inside a tag with an id assigned all in this document, I would still have to wrap it in a :global selector.
I couldn’t even position the component itself on my page without adding extra boilerplate inside the component or wrapping it in yet another extra needless div – and in both cases still having to use another :global selector.

The result is, I ended up finding myself wrapping all my components in extra divs, which was just ugly and messy and bloated my html. And they’re aware of this too because not only do they recommend you do this as the solution, but they actually do this behind the scenes themselves with syntactic sugar features such as css variable properties. This can create even more problems because they’re inserting invisible divs into your html that you actually CAN target with your CSS.

I just found accessing anything inside a component to style it or programmatically alter it was just a mess of extra boilerplate I had to set up. I finally realized I was increasing time by using Svelte instead of saving it.

Having said that, I’m sure I did not learn every trick or approach to doing this and there might be better ways, but for me, Svelte provided the opposite of an intuitive, approachable and user-friendly styling API.

]]>
By: Michael https://css-tricks.com/what-i-like-about-writing-styles-with-svelte/#comment-1753957 Thu, 30 Jan 2020 03:56:11 +0000 https://css-tricks.com/?p=297067#comment-1753957 In reply to badunius.

You can separate things.

]]>
By: Giorgos https://css-tricks.com/what-i-like-about-writing-styles-with-svelte/#comment-1753861 Sat, 25 Jan 2020 12:55:57 +0000 https://css-tricks.com/?p=297067#comment-1753861 I don’t think “Unused styles are flagged”

I think it they are actually removed by default https://github.com/sveltejs/svelte/issues/697 but than not sure how one can actually instruct Svelte to not remove specific styles ?

]]>
By: badunius https://css-tricks.com/what-i-like-about-writing-styles-with-svelte/#comment-1753707 Wed, 15 Jan 2020 12:15:28 +0000 https://css-tricks.com/?p=297067#comment-1753707 So, you can’t separate style from component? Because the first part of this article kinda implies you can. I don’t like single file components. I like to reuse my CSS and I like SASS a lot. And making a huge pile of everything but the kitchen sink is definitely not a “taking one step further”. Unless it’s a step into the abyss.

]]>
By: Bill https://css-tricks.com/what-i-like-about-writing-styles-with-svelte/#comment-1752385 Mon, 04 Nov 2019 14:17:02 +0000 https://css-tricks.com/?p=297067#comment-1752385 In reply to Bill.

That’s what I’m using today, but then I can’t use the sweet component styling for App :P It’s not that bad, but it would be nice with a better solution.

]]>
By: Dylan https://css-tricks.com/what-i-like-about-writing-styles-with-svelte/#comment-1752379 Mon, 04 Nov 2019 03:52:41 +0000 https://css-tricks.com/?p=297067#comment-1752379 In reply to Bill.

In App.svelte:

<style lang="scss" global>
  @import "./styles/global.scss";
</style>
]]>
By: Bill https://css-tricks.com/what-i-like-about-writing-styles-with-svelte/#comment-1752223 Fri, 25 Oct 2019 19:47:04 +0000 https://css-tricks.com/?p=297067#comment-1752223 Nice post! Have you used external sass-files? I’m having a pain in the rear trying to import a single global.scss file, even though preprocessing sass in the style tags is working.

Cheers!

]]>
By: Phil Holden https://css-tricks.com/what-i-like-about-writing-styles-with-svelte/#comment-1752215 Fri, 25 Oct 2019 14:48:36 +0000 https://css-tricks.com/?p=297067#comment-1752215 Been playing with Svelte CSS and context based theme using CSS Properties (CSS Vars). This is incredibly powerful. Should be much more performant than approach used normally used with Styled Components and Emotion. Specially with new CSS.registerProperty() which allows for CSS vars to not inherit making them more performant as they will only affect one element.

https://codesandbox.io/s/svelte-theme-components-crt54?fontsize=14&module=%2FButton.svelte

]]>
By: Johan https://css-tricks.com/what-i-like-about-writing-styles-with-svelte/#comment-1752202 Thu, 24 Oct 2019 22:09:25 +0000 https://css-tricks.com/?p=297067#comment-1752202 Happy to see a Svelte article on CSS Tricks! Your BEM to Svelte explanation says it all. Why have we been writing BEM for all these years when a compiler could do it for us?

Although maybe should not promote writing code where we style base elements directly. Even in scoped code this could easily lead to problems when expanding upon the code. A better idea in my opinion is to use simple class names like in Bootstrap. In Svelte, you don’t have the CSS clashes that Bootstrap has due to its lack of namespacing.

]]>