vanilla-extract is a library for writing CSS in JavaScript. It is designed to be used with React, but can be used with any JavaScript framework or without one.
Vanilla-extract was co-created by Mark Dalgleish who is also the creator of css modules. In fact, it's a bit like css modules, but much more capable and with typescript as a preprocessor. Instead of writing css, you use typescript, which makes it look a bit like yet another css in js library, but without the runtime cost of one.
npm create vite@latest vite-vanilla-extract-react -- --template react-ts
cd vite-vanilla-extract-react
npm i --save-dev @vanilla-extract/vite-plugin
npm i @vanilla-extract/css @vanilla-extract/sprinkles @vanilla-extract/dynamic @vanilla-extract/recipes clsx
And then, just edit vite.config.ts
to use the plugin:
import { defineConfig } from 'vite'
import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vanillaExtractPlugin(), react()]
})
- With its sprinkles, it's also a bit like tailwind, but your own tailwind that lets you define your own utility classes.
- Recipes make it a bit like Stitches, but with a different syntax.
- Clean boundary between runtime and build time - all that is in css.ts is build time. But you can still call css functions from runtime code.
- No need to pass theme via context (theme provider) because it depends on css variables.
- Unlike css variables, we can use nested objects. This is useful for defining a theme.