Importing CSS as React/Preact components with webpack and unicorns

Sasha Koss
Jul 11, 2017 · 3 min read
Image for post
Image for post

🦄 Keep It Simple, Stupid!

I’m building an app that is supposed to be embedded on 3rd party websites and for such kind of applications the build size is very important.

I was amused by styled-components and had wanted to give it a try for a long time, but I found that it requires a 12kB runtime, which is 4x more than size of the framework I use! Any other CSS-in-JS solution also has an unavoidable overhead, so I decided to stay lean and use plain CSS.

I’ve opted to use CSS Modules, which came with create-preact-app. The approach solves many classic CSS problems but has practically no footprint. With rich PostCSS ecosystem, you can write modern CSS, with variables, nested selectors, extend and many more great features, so it’s a pleasure to use it nowadays.

🦄 Don’t Repeat Yourself?

After a few days I’ve found that I am repeating myself writing the same glue code again and again:

Image for post
Image for post

I’ve realized that I had been writing such components for years! At the first glance, those have a value. They map props to class names, and also wrap children in divs. But at the end, they all look the same.

Also, I recall a lot of dummy components with the only purpose to wrap children within a div with a particular className.

The problem is that it’s not easy to refactor those, so the best you can do is to let them be.

🦄 Convention Over Configuration!

Ruby on Rails taught me that convention over configuration efficiently ditches tons of glue code. So I thought if I can come up with a convention similar to BEM which would allow to define boolean and enum props so I can stop writing such code and automate its generation instead!

After a few iterations I’ve come up with a short list of rules:

  1. .Button is a CSS class which represents a component (other examples are .ListItem , .UI , etc.) and can be used as <Button>Content</Button> .
  2. .Button-disabled generates a component with a boolean prop (e.g. .Button-fullWidth, .Window-inactive ) and can be used as <Button disabled /> .
  3. .Button-color-red represents a component with an enum prop (e.g. .Spacing-direction-column, .Window-mode-alert ) and can be used as <Button color='red' /> .

As it turns out, boolean and enum props cover most of the props use-cases for styling purposes. Having just 2 types inspired me to simplify my code and replace many components with plain styles.

Image for post
Image for post

Nyan CSS

I’ve open-sourced the runtime that parses CSS Modules output and builds React or Preact components and the webpack loader that allows importing components right from CSS files, so grab while it’s 🔥: https://github.com/nyancss/nyancss

I don’t want to claim that this is a superior CSS solution in any sense. There are too many fantastic CSS-in-JS libraries that push the boundaries of what’s possible with CSS, although I believe that Nyan CSS may be relevant in some cases and it’s up to the engineers to make a choice.

If you want to give it a try, see Nyan CSS docs.

If you like it, star the repo and share the post and also follow me on Twitter.

Thank you for reading!

DailyJS

JavaScript news and opinion.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store