DEV Community

Alex Spinov
Alex Spinov

Posted on

Vanilla Extract Has a Free API You Should Know About

Vanilla Extract is a zero-runtime CSS-in-TypeScript library. Write styles as TypeScript expressions, get type-safe, locally scoped CSS at build time.

Why Vanilla Extract for Type-Safe CSS

A design system team had CSS conflicts from global class names. CSS Modules helped but lacked type safety. Vanilla Extract gives them TypeScript imports for every style — typos are caught at compile time.

Key Features:

  • Zero Runtime — All CSS generated at build time
  • TypeScript — Full type safety and autocomplete
  • Locally Scoped — No class name conflicts
  • Theme Support — Type-safe theme contracts
  • Framework Agnostic — Works with any framework

Quick Start

npm install @vanilla-extract/css
Enter fullscreen mode Exit fullscreen mode
// button.css.ts
import { style } from "@vanilla-extract/css"

export const button = style({
  padding: "12px 24px",
  borderRadius: 8,
  backgroundColor: "#3b82f6",
  color: "white",
  ":hover": { backgroundColor: "#2563eb" }
})
Enter fullscreen mode Exit fullscreen mode
import { button } from "./button.css"

function Button() {
  return <button className={button}>Click me</button>
}
Enter fullscreen mode Exit fullscreen mode

Recipes

import { recipe } from "@vanilla-extract/recipes"

export const button = recipe({
  base: { padding: "12px 24px", borderRadius: 8 },
  variants: {
    color: { primary: { background: "blue" }, danger: { background: "red" } },
    size: { small: { fontSize: 14 }, large: { fontSize: 18 } }
  },
  defaultVariants: { color: "primary", size: "small" }
})
Enter fullscreen mode Exit fullscreen mode

Why Choose Vanilla Extract

  1. Type-safe — CSS errors caught at compile time
  2. Zero runtime — no performance penalty
  3. Scoped — impossible to have class conflicts

Check out Vanilla Extract docs to get started.


Need web solutions? Check out my Apify actors or email spinov001@gmail.com for custom solutions.

Top comments (0)