Stitches is an excellent library if you prefer to be as close as writing CSS with the bonus of having a solid JS API. For this blog, let's create a React application with Stitches to see how the styling library would integrate and influence how we code our interactivities.
We will be creating a straightforward app with the following functionalities.
Show a "Karma Chameleon" in the center.
Three buttons with the labels Light, Dark and Funky
Clicking any buttons would update the background and text color of "Karma Chameleon" to their corresponding theme.
TLDR; a theme toggle application.
For simplicity's sake, let's use create-react-app (CRA) for this one. I won't be changing anything out of the ordinary. I will remove all the unnecessary artifacts like CSS and other setups. After the fact, I should only have the KarmaChameleon.js in the components folder and the index.js that injects the only defined component.
To easily create your app with CRA, just run the command below:
npx create-react-app karma-chameleon
Next stop, let's install Stitches! We will be using the react-based library instead of the framework-agnostic core. I like yarn so I'll use yarn.
yarn add @stitches/react
My approach with any design framework is heavily using design tokens. Tokens define the values of our design system. Luckily, Stitches makes that easy for us. But, before you see that in action, let's define the bread and butter of Stitches, the configuration file.
Let's only define the Light theme, aka the default theme for now. The other values are personal preferences. Feel free to change them depending on your app's design context. Read through the documentation for more information on the different keys I used and any others you can define. I depicted my global CSS preferences as well.
Given the configuration above, we may now implement the initial view of the React application with the default theme! The object returned from the createStitches call contains different APIs that would help style your components. We will gradually touch some of them.
See tokens in action below! For example, we used the $bg token to get the value of the variable bg under the default theme. That's right! Stitches is smart enough to do the corresponding substitution. Not to mention, since it's typed, auto-suggestion is active!
To summarize, we used styled to generate a React component with the following defined styles. We used design tokens in some of the values to use the value specified in our configuration, which will be necessary for our theme overrides and, in general, a good practice for consistency. The globalCss API creates a function we can call to define the app's global CSS. Look at the preview below.
Now, we need to define the other themes. The createTheme API is our hero for this. We will use a state to track the current theme, enabling us to change it through the corresponding button clicks. It follows we need to refactor a little bit to allow value overrides through design tokens. See it in action below!
Time for finishing touches! We want responsiveness in our application. Flashback on the configuration, we defined three breakpoints. Keeping that in mind, we can use the tokens to create media queries, the Stitches way. The @bp2 token is (min-width: 768px) in our configuration.
Having a screen size less than 768px will result to the view below.
Of course, we want the transition of the background and foregrounds to be smooth. We can also add simple animations with the keyframes API of Stitches for those fabulous fade-ins. You may use the css prop to override styles. In this case, I needed to add individual animation delays to produce the effect that I wanted for the buttons.
Plus ultra! The final code is on this repository or check out this website. Feel free to do whatever you want with it.
There is more to explore with Stitches. I've barely scratched the surface. To name a few, you can define utilities in the configuration to extend the available keys on our styles, aka making your own higher order CSS rules, and support SSR.
To summarize, Stitches has the following advantages:
faster benchmarking vs. emotion and styled-components
robust JS API (as you have noticed above)
built with React in mind.
Typed. AUTOCOMPLETE IS EFFIN AWESOME! It also checks type integrity with variants and utilities, which I appreciate.
It's not all unicorns and rainbows, to be honest. I noticed some hit to the dev experience, primarily if you're used to just using CSS. I listed below some of the downsides of using this library.
It's not precisely CSS that can be a good or bad thing.
You use CSS syntax on the values, but camel-case converted CSS strings on keys.
It might be too React centric, as the core library uses class names that could clash with a library's/framework's paradigm.
Non-modern browsers won't run this. (but what styling JS library does? haha)
Custom props are limited to variants, and you can't do prop-based logic without extra overhead, unlike in styled-components where you can access props and do whatever you want.
I love this library. I already used it with this blog and other misc projects. TLDR; React + Stitches = Awesome.