Importing CSS as React/Preact components with webpack and unicorns
🦄 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:
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:
.Button
is a CSS class which represents a component (other examples are.ListItem
,.UI
, etc.) and can be used as<Button>Content</Button>
..Button-disabled
generates a component with a boolean prop (e.g..Button-fullWidth
,.Window-inactive
) and can be used as<Button disabled />
..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.
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!