Proposing Next.js vanilla-extract styling setup
Outlined using @vanilla-extract/next-plugin with outputCss: true in next.config.js plus defining strict design tokens and sprinkles for type-safe atomic CSS classes, limiting styles to theme values; planned code includes theme tokens, responsive and typography sprinkles, and example component usage demonstrating atomic class generation.
Below is a minimal, but complete, setup for “zero‑runtime CSS‑in‑JS” with vanilla‑extract in Next.js, configured to:
- Emit real CSS at build time (
outputCss).
- Use atomic classes for utility styles (via
@vanilla-extract/sprinkles).
- Restrict all utility values to the design token theme (TypeScript‑typed).
1) Install dependencies
npm i @vanilla-extract/css @vanilla-extract/sprinkles
npm i -D @vanilla-extract/next-plugin
(Vanilla Extract works with plain Next.js; no SWC/Babel plugin needed when using the Next plugin.)
2) Configure Next.js for vanilla‑extract
next.config.js
/** @type {import('next').NextConfig} */
const { createVanillaExtractPlugin } = require('@vanilla-extract/next-plugin');
const withVanillaExtract = createVanillaExtractPlugin({
// Ensures CSS is written to disk at build time (zero runtime)
outputCss: true,
});
module.exports = withVanillaExtract({
reactStrictMode: true,
// ...your existing Next config
});
outputCss: true writes .vanilla.css files into .next (and when exported, into out/), so you ship static CSS.
3) Define your design tokens (theme)
src/styles/theme.css.ts
import { createGlobalTheme, createThemeContract } from '@vanilla-extract/css';
const tokens = createThemeContract({
color: {
background: null,
surface: null,
textPrimary: null,
textSecondary: null,
brand: null,
},
space: {
none: null,
xs: null,
sm: null,
md: null,
lg: null,
xl: null,
},
radii: {
sm: null,
md: null,
lg: null,
},
font: {
body: null,
heading: null,
},
fontSize: {
sm: null,
md: null,
lg: null,
xl: null,
},
media: {
tablet: null,
desktop: null,
},
});
// Assign concrete values (your design tokens here)
export const vars = createGlobalTheme(':root', tokens, {
color: {
background: '#0B0C10',
surface: '#12141D',
textPrimary: '#E6E7EB',
textSecondary: '#9BA0AE',
brand: '#4F46E5',
},
space: {
none: '0',
xs: '4px',
sm: '8px',
md: '12px',
lg: '16px',
xl: '24px',
},
radii: {
sm: '4px',
md: '8px',
lg: '12px',
},
font: {
body: '"Inter", system-ui, -apple-system, sans-serif',
heading: '"Inter", system-ui, -apple-system, sans-serif',
},
fontSize: {
sm: '12px',
md: '14px',
lg: '16px',
xl: '20px',
},
media: {
tablet: 'screen and (min-width: 768px)',
desktop: 'screen and (min-width: 1024px)',
},
});
The contract + theme gives you type-safe tokens. All references in Sprinkles come from vars, so developers can only use defined values.
4) Create Sprinkles (atomic utilities) bound to tokens
src/styles/sprinkles.css.ts
import { createSprinkles, defineProperties } from '@vanilla-extract/sprinkles';
import { vars } from './theme.css';
const responsiveProperties = defineProperties({
conditions: {
mobile: {},
tablet: { '@media': vars.media.tablet },
desktop: { '@media': vars.media.desktop },
},
defaultCondition: 'mobile',
properties: {
display: ['none', 'block', 'inline-block', 'flex', 'inline-flex', 'grid'],
flexDirection: ['row', 'column'],
alignItems: ['stretch', 'flex-start', 'center', 'flex-end'],
justifyContent: ['flex-start', 'center', 'flex-end', 'space-between'],
gap: vars.space,
padding: vars.space,
paddingTop: vars.space,
paddingRight: vars.space,
paddingBottom: vars.space,
paddingLeft: vars.space,
margin: vars.space,
marginTop: vars.space,
marginRight: vars.space,
marginBottom: vars.space,
marginLeft: vars.space,
borderRadius: vars.radii,
background: vars.color,
color: vars.color,
},
shorthands: {
p: ['padding'],
pt: ['paddingTop'],
pr: ['paddingRight'],
pb: ['paddingBottom'],
pl: ['paddingLeft'],
px: ['paddingLeft', 'paddingRight'],
py: ['paddingTop', 'paddingBottom'],
m: ['margin'],
mt: ['marginTop'],
mr: ['marginRight'],
mb: ['marginBottom'],
ml: ['marginLeft'],
mx: ['marginLeft', 'marginRight'],
my: ['marginTop', 'marginBottom'],
},
});
const typographyProperties = defineProperties({
properties: {
fontFamily: vars.font,
fontSize: vars.fontSize,
fontWeight: ['400', '500', '600', '700'],
lineHeight: ['1', '1.2', '1.4', '1.6'],
textAlign: ['left', 'center', 'right'],
},
});
export const sprinkles = createSprinkles(
responsiveProperties,
typographyProperties
);
export type Sprinkles = Parameters<typeof sprinkles>[0];
Each property/value pair becomes an atomic CSS class. Because every property points to vars, developers are constrained to your design tokens.
5) Example usage in a Next.js component
src/components/Card.tsx
import { sprinkles } from '../styles/sprinkles.css';
import { vars } from '../styles/theme.css';
type CardProps = {
title: string;
children: React.ReactNode;
};
export function Card({ title, children }: CardProps) {
return (
<div
className={sprinkles({
display: 'flex',
flexDirection: 'column',
gap: 'sm',
p: 'md',
background: 'surface',
color: 'textPrimary',
borderRadius: 'md',
})}
style={{
// Example of a one-off style if truly needed:
// boxShadow: '0 10px 40px rgba(0,0,0,0.25)',
}}
>
<h2
className={sprinkles({
fontFamily: 'heading',
fontSize: 'lg',
fontWeight: '600',
color: 'textPrimary',
})}
>
{title}
</h2>
<p
className={sprinkles({
fontFamily: 'body',
fontSize: 'md',
color: 'textSecondary',
lineHeight: '1.4',
})}
>
{children}
</p>
</div>
);
}
The sprinkles call returns a single, hashed class name that composes multiple atomic classes. No runtime style objects are shipped; just class names referencing real CSS.
6) Global base styles (optional)
src/styles/global.css.ts
import { globalStyle } from '@vanilla-extract/css';
import { vars } from './theme.css';
globalStyle('html, body', {
margin: 0,
padding: 0,
background: vars.color.background,
color: vars.color.textPrimary,
fontFamily: vars.font.body,
});
globalStyle('*', {
boxSizing: 'border-box',
});
Import this file once (e.g., in _app.tsx) to apply global resets/styles.
7) Use in _app.tsx
import type { AppProps } from 'next/app';
import '../styles/global.css.ts'; // ensure side-effects get included
export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
}
Notes on “atomic CSS” with vanilla‑extract
@vanilla-extract/sprinkles generates one class per property/value pair (atomic). The sprinkles() call simply concatenates the needed atomic classes.
- Non‑sprinkles styles created with
style() are not atomic by default; they generate a single class per style object. Use sprinkles for utility/atomic patterns, and style() for component‑specific styles when needed.
- Because all sprinkles properties refer to
vars (typed tokens), developers cannot use arbitrary values—only what’s defined in your theme.
That’s it. You now have a Next.js setup with vanilla‑extract producing zero‑runtime, atomic CSS constrained to your design tokens.