talira

vanilla-extract

Zero-runtime Stylesheets-in-TypeScript

open sourceMITself-hostablefree

vanilla-extract lets you write styles in TypeScript .css.ts files (locally scoped class names, CSS variables, keyframes, font faces and themes) and turns them into static CSS files at build time, so none of the styling code reaches the browser bundle. Its README calls it CSS Modules in TypeScript with scoped CSS variables: styles are typed through CSSType, themes are type-safe token contracts you can apply several of at once, and styleVariants groups variants by name. Official bundler integrations cover Vite, esbuild, webpack, Next.js, Parcel, Rollup and Gatsby, and companion libraries add Sprinkles (a zero-runtime atomic utility API), Recipes (multi-variant styles) and dynamic runtime theming. It's MIT-licensed and comes from SEEK.

Problems it solves

  • our CSS-in-JS library injects styles at runtime and slows down page rendering
  • I want type errors when a component uses a theme variable that doesn't exist
  • write scoped styles in TypeScript but ship plain static CSS files
  • support several themes on one page at the same time without global CSS variables clashing
  • use TypeScript as our CSS preprocessor instead of Sass or Less
  • build our own atomic utility classes, like Tailwind, from our design tokens with zero runtime
  • define button variants (size, colour, tone) once and look them up by name in components
  • server components can't use runtime CSS-in-JS; we need styles extracted at build time
  • locally scoped class names like CSS Modules but with typed variables and themes

Features

  • styles in .css.ts files compiled to static CSS at build time, with no runtime in the bundle
  • locally scoped class names, CSS variables, @keyframes and @font-face
  • type-safe styles via CSSType
  • theme system with typed token contracts and simultaneous themes
  • selectors, pseudo-classes, media, feature and container queries, and global styles
  • styleVariants to group style variants by name
  • calc utilities for variable-based expressions
  • official integrations for Vite, esbuild, webpack, Next.js, Parcel, Rollup and Gatsby, plus a Jest transform
  • Sprinkles: zero-runtime atomic CSS utilities generated from your tokens
  • Recipes: multi-variant style APIs, and an optional API for dynamic runtime theming

Limitations

  • Doesn't work without a bundler: it needs a bundler integration set up to handle CSS1

Alternatives

The same job

These do what vanilla-extract does.

  1. StyleX The expressive, type-safe, composable, predictable and themeable styling system for ambitious interfaces
  2. styled-components Fast, expressive styling for React. Server components, client components, streaming SSR, React Native, all one API.
  3. Panda CSS Universal, type-safe, CSS-in-JS framework for design systems
  4. Emotion CSS-in-JS library designed for high performance style composition
  5. goober A less than 1KB css-in-js solution

Compare vanilla-extract with the top 3