Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

How use Sass with Web Components?

Hello guys! I think i have an interesting question, a simply one

How can i adapt my current Sass workflow for develop Web Components?

Should be something like this ?

check the link to see the structure easier: http://codepen.io/TibicenasDesign/pen/WvqRXV

|- reset.scss (ie: normalize) |- utilities __|- variables.scss _|- mixins.scss _|- function.scss _|- placeholders.scss |- components _|- my-element _| |- my-element.html _| |- my-element.scss _|- another-element _| |- another-element.html __| |- another-element.scss

I haven't found anything in my searchs, so i'm triying to figure out by my own.

In all the tutorials i have seen, the author use inline css ( <style> .... </style> tag ) in the component.html but that sucks, i want to keep using Sass and my variables for performance and structure my styles.

Then i thought we could import the css, like we actually do in the index.html ( <link rel.... style.css or application.css)

But this also means a css import for each .html components, maybe not the best for performance and browser request to the server for each .css files

So as you can see, i'm "a little lost"

Hope someone want light the way :)

PD: Sorry if i wrote something wrong, english its not my native language ;)

7 Answers

Joshua Briley
PLUS
Joshua Briley
Courses Plus Student 24,645 Points

Great question. Have you considered using something like Gulp or Grunt to inject your compiled Sass into your component templates? I suspect a similar functionality that is used in Addy Osmani's "Critical CSS" Grunt task may be a good place to start. There is another Github Gist that discusses a similar concept for Jekyll. I suspect the process wouldn't be much different for Web Components.

There is a Gulp Basics here on Treehouse that may helpful. I'm definitely interested in where this conversation leads.

Oh thats nice, maybe something like the {% asset critical.css %} of Addy Osmani would be the key...

I'm using Gulp right now, and the idea of embed the css resulting of compile the .scss file, within our <style> tag located in our component.html looks awesome but at the same time scary for me.

All the time trying to avoid the inline css ( ok email exception ;) ) and now, is gonna be this the right way ? A inflection point in the good practice of web development.

Hugo use something similar with React http://hugogiraudel.com/2015/06/18/styling-react-components-in-sass/

Web component are still a baby, and the community need more time to try and try. but i hope some Polymer developer (or similar) publish some article with a new sass/web-component approach, not only focus in the js, as i saw until now.

lets see how evolve this conversation, and if curious like us got something clear enough :)

Joshua Briley
Joshua Briley
Courses Plus Student 24,645 Points

Hugo writes some pretty amazing stuff, doesn't he? My company is waiting for our clients to drop support for <IE10... But, we see web components in our future.

Colin Marshall
Colin Marshall
32,861 Points

I've never used web components before, so I just want to make sure I'm understanding you correctly. If you use a web component on a page, it doesn't have access to the CSS styles and classes that the main page has linked?

Hi Colin Marshall , web components are like .html partials that you import within your main .html file (probably called index.html)

That web components are not affected by the style.css that you apply to your index.html file They have his own styles in each partial. The keyword here is encapsulation. Check about the Shadow DOM: https://www.polymer-project.org/0.5/platform/shadow-dom.html

My question is about how to create a useful workflow with web components and sass, and the best way to add the style into the partials ( @import, inline, other unknown way ) for achieve the best performance in the development process AND && in the web on production.

This is a very interesting question. Remember that Sass is a precompiler and anything live is compiled into normal css. So if you want a variable like $color to be used in any component, it has to be at the development phase. I would not be very portable without specific instructions to the downstream user or strict naming conventions followed in future projects.

That said, I think of this issue as one similar to jQuery. When you use jQuery, you have to install the specific CSS to make it work right. I would think this is the same. You have to install the component CSS with the component. You could do this by having an .scss file that you include in your master .scss that is compiled. Then have a variable at the beginning of the component .scss file that you could set to the equivalent variable of the master project if they are not the same. As far as automating that task, I am not sure how you would do it without knowing the names in advance.

I also edited to comments to answers.

thanks for your reply Ted Sumner
"would not be very portable without specific instructions to the downstream user or strict naming conventions followed in future projects." -Right, i'm gonna create stric naming conventions and instruction, so i gonna know the names in advance.

The point is that we don't gonna have a master .scss. Or i think that because until now i only see inline css in each component.html.

Thats why i thought in what way would be better. @import scss partials for each component.html or a wat to compile the sass code into css and embedded finally inline into each partials.

I am not sure why you would not want a master .scss file. Having one server call for all the styling of a page increases performance from what I understand. Unless it is inline, which is not considered a good practice from what I can tell, you will end up with server calls for each module.

That said, you may be able to do it through PHP. If you component is written with PHP, I think you can access files and inject the content into your code that is sent to the end user. You could have a CSS file for your component that is imported at the server level and delivered as part of the html for the user. If that is the case, then your component would come with an css file that is not linked as a normal css file.

Hi Ted Sumner i want a master .scss file (like always! :) )but as i see until now, with polymer for example, everybody is using inline css in order to make reusable components.

And other point is that the components didnt use the style.css or application.css because they run within the shadowDOM https://www.polymer-project.org/0.5/platform/shadow-dom.html them css rules are independent.

This is what i understood, maybe i'm wrong.

The whole idea behind web components is reusability. Normally inline CSS is a bad idea from a development point of view, however when it comes to web components it is used to limit scope. For example, if I create a web component that contains a list ul li etc and I want to style it in a particular way by default, using inline styles within the component allows me to do that without having to worry that another developer that has created their own list styles for their site which will mess up the appearance of the list in the component I have created, when they use it in their site. My styles wont leak out of the component and the parent site styles wont leak in. That said, the inline styles of my component can be overridden if a developer decides to do so.