Articles EmberJS

ember-csz: A CSS-in-JS solution for styling in Ember

In this article we are going to take a look at an Ember.js addon called ember-csz which provides template helpers for csz, a Runtime CSS-Modules with SASS like pre-processing.

This is based on a talk I gave recently at ESNextConf. I just wanted to experiment the same with Ember.js and it came out well. I liked it very much the idea of keeping your styles as part of your component.js just like how the React community uses other awesome CSS-in-JS solutions like Styled Components and Emotion.


csz is a super-tiny framework agnostic CSS-in-JS solution created by Luke Jackson. Luke is a front-end developer from London. He also created some other amazing tooks like perf-link and servor. csz uses stylis to parse styles from tagged template literals and append them to the head of the document at runtime. Loading in stylesheets dynamically – from .css files – is supported out of the box, so you can write your styles in .css files and import them via url without having to worry about flashes of unstyled content.

And csz supports a lot of other awesome features, please checkout the project’s README for more information.

Installing ember-csz

Just like installing any other Ember.js addon, you would install ember-csz with :

Inline Styling

This addon exposes the default helper called 

 which can be used directly in templates if you want to write your styles inline. But keep in mind that the styles are not applied inline with the style attribute but instead csz generates class names dynamically and append them to the head of the document at runtime and apply this class to the class attribute of your elements.

And the dynamically generated class name will look like this:

And the output in your html will be like:

Example of component styling with csz

Let’s begin with an example of styling a component with csz. This is the typical use case of using CSS-in-JS solution with Ember components. The first step is to import csz into your component.js file. Since the csz library is already part of the ember-csz addon, you don’t have to install it separately. Once you install the ember-csz addon, it will automatically available for you to use in your Javascript files in your Ember apps.



Now we will take a look at how to use the styles in our component template. It’s just as simple as applying or using any other property from your component.js. Just assign 

 to your class property of the parent element.

Using the component

The dynamically generated class names from the style tag will be:

Adapting based on props

Next we will see how to adapt the styles of your components based on the properties you pass to them. In this example we change the background and color property of the button based on the 

 property of the component.



Using the button component

Next we will see how to use the MyButton component in our templates. If the @primary property is passed the button component will have palevioletred and white as the background and color attributes respectively. Otherwise it will take the default value of white and palevioletred for background and color.

Animation and Keyframe namespacing

Next we will take a look at how to create animations with csz. csz will automatically namespace your animations with the dynamically generated class names.



Using the animation component

The dynamically generated class names with animation and keyframes namespacing will be like:

As you can see from the above code, csz automatically do vendor-prefixing which is actually supported by Stylis.

Pseudo selectors

You can also use pseudo selectors, child selectors, sibling selectors and so on with csz. It’s primarily because of stylis which is the CSS preprocessor used by csz. Stylis is the CSS preprocessor used by Styled Components and Emotion. It’s a light weight CSS preprocessor with SASS like pre-processing. And there is no compilation or build step required to process the css because everything happens at runtime. This is way better than using plain SASS pre-processing with Ember becuase you need to install ember-cli-sass and node-sass in your projects to do the same.

my-pseudo component.js


Using the MyPseudo Component


Next we will see how we can implement themes using csz in Ember. You define your themes as simple objects with color values and then use the same in your template literals to use the values to evaluate based on the props or something. In this example we setting up a default theme with main color as 

 and if there is a theme property we will take the color from the passed-in theme property, otherwise we will use the default theme color.



Using the ThemeButton component

When you pass the @theme property to the component, it takes the colors from the theme object otherwise it takes the colors from the default theme.

The class names dynamically generated at runtime in the style tag will be:

And the html markup will look something like:

You can also check out all the above code example inside the addons dummy app folder.