Files
2nd/10_Wiki/Topics/Frontend/vanilla-extract.md
T
koriweb d8a80f6272 chore(wiki): dangling 링크 canonical 정규화 (768파일/1200건)
이름만 다른(표기 변형) [[위키링크]]를 대상 문서의 canonical 제목으로 치환해
끊겼던 1,200개 링크를 연결. 제목/파일명 정규화 일치만 적용하고 별칭 매칭은
과병합 위험으로 제외(애매성 가드). 원본은 _link_reconcile_backup/ 에 백업.
도구: Datacollect/scripts/link_reconcile_apply.mjs

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 12:24:15 +09:00

293 lines
9.1 KiB
Markdown

---
id: wiki-2026-0508-vanilla-extract
title: vanilla-extract
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [vanilla-extract, vanilla extract, ve, css.ts]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [css-in-js, zero-runtime, vanilla-extract, typescript, build-time]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: typescript
framework: vanilla-extract
---
# vanilla-extract
## 매 한 줄
> **"매 TypeScript 로 type-safe 한 zero-runtime CSS-in-JS"**. 매 Mark Dalgleish (Seek) 가 2021 년 출시. 매 `.css.ts` 파일을 build time 에 static CSS 로 추출, 매 token / variant / theme 매 모두 typed. 매 Next.js App Router (RSC) 의 사실상 표준, 매 component library 배포 의 zero-runtime guarantee.
## 매 핵심
### 매 핵심 API
- `style({...})`: 매 single CSS class 생성 → unique class name 반환.
- `globalStyle('selector', {...})`: 매 global selector style.
- `recipe({base, variants})`: 매 variant-based component (CVA-like, type-safe).
- `createTheme(contract, values)`: 매 token 의 ThemeProvider 대안.
- `createThemeContract({...})`: 매 token shape 정의 (multi-theme).
- `createVar()`: 매 CSS custom property (runtime dynamic).
- `style([base, override])`: 매 style composition.
### 매 build setup
- **Webpack**: `@vanilla-extract/webpack-plugin`.
- **Vite**: `@vanilla-extract/vite-plugin`.
- **Next.js**: `@vanilla-extract/next-plugin` (App Router 호환).
- **esbuild / Rollup / Parcel**: 매 dedicated plugins.
- 매 결과: `.css.ts``.css` 추출 + `.js` 의 class name string export.
### 매 응용
1. Design system 의 token-based theming.
2. Next.js App Router 의 RSC-compatible styling.
3. Component library 의 zero-runtime publish.
4. CVA-style variants via recipes.
5. CSS variable scoped to component.
## 💻 패턴
### Basic style
```ts
// button.css.ts
import { style } from '@vanilla-extract/css';
export const button = style({
padding: '8px 16px',
borderRadius: 4,
background: 'blue',
color: 'white',
cursor: 'pointer',
selectors: {
'&:hover': { background: 'darkblue' },
'&:disabled': { opacity: 0.5, cursor: 'not-allowed' },
},
});
// button.tsx
import { button } from './button.css';
export const Button = (props) => <button className={button} {...props} />;
```
### Theme contract — multi-theme
```ts
// theme.css.ts
import { createTheme, createThemeContract } from '@vanilla-extract/css';
export const vars = createThemeContract({
color: { brand: null, text: null, bg: null, border: null },
space: { xs: null, sm: null, md: null, lg: null, xl: null },
radius: { sm: null, md: null, lg: null },
font: { sans: null, mono: null },
});
export const lightTheme = createTheme(vars, {
color: { brand: '#0066ff', text: '#111', bg: '#fff', border: '#e0e0e0' },
space: { xs: '4px', sm: '8px', md: '16px', lg: '24px', xl: '32px' },
radius: { sm: '2px', md: '4px', lg: '8px' },
font: { sans: 'Inter, sans-serif', mono: 'JetBrains Mono, monospace' },
});
export const darkTheme = createTheme(vars, {
color: { brand: '#3399ff', text: '#fff', bg: '#0a0a0a', border: '#222' },
space: { xs: '4px', sm: '8px', md: '16px', lg: '24px', xl: '32px' },
radius: { sm: '2px', md: '4px', lg: '8px' },
font: { sans: 'Inter, sans-serif', mono: 'JetBrains Mono, monospace' },
});
// app.tsx
<html className={isDark ? darkTheme : lightTheme}>
```
### Component using vars
```ts
// card.css.ts
import { style } from '@vanilla-extract/css';
import { vars } from './theme.css';
export const card = style({
background: vars.color.bg,
color: vars.color.text,
border: `1px solid ${vars.color.border}`,
padding: vars.space.md,
borderRadius: vars.radius.lg,
fontFamily: vars.font.sans,
});
```
### Recipes — variants (CVA-like)
```ts
// button.css.ts
import { recipe, RecipeVariants } from '@vanilla-extract/recipes';
import { vars } from './theme.css';
export const button = recipe({
base: {
padding: vars.space.sm,
borderRadius: vars.radius.md,
cursor: 'pointer',
border: 'none',
fontFamily: vars.font.sans,
},
variants: {
intent: {
primary: { background: vars.color.brand, color: '#fff' },
secondary: { background: 'transparent', color: vars.color.brand,
border: `1px solid ${vars.color.brand}` },
danger: { background: '#dc2626', color: '#fff' },
},
size: {
sm: { fontSize: 12, padding: vars.space.xs },
md: { fontSize: 14, padding: vars.space.sm },
lg: { fontSize: 18, padding: vars.space.md },
},
fullWidth: {
true: { width: '100%' },
},
},
compoundVariants: [
{ variants: { intent: 'primary', size: 'lg' },
style: { fontWeight: 'bold' } },
],
defaultVariants: { intent: 'primary', size: 'md' },
});
export type ButtonVariants = RecipeVariants<typeof button>;
// type ButtonVariants = { intent?: 'primary' | 'secondary' | 'danger'; size?: ...; fullWidth?: boolean }
// usage
<button className={button({ intent: 'danger', size: 'lg' })}>Delete</button>
```
### Sprinkles — atomic CSS (Tailwind-like)
```ts
// sprinkles.css.ts
import { defineProperties, createSprinkles } from '@vanilla-extract/sprinkles';
const space = defineProperties({
properties: {
padding: { sm: 8, md: 16, lg: 24 },
margin: { sm: 8, md: 16, lg: 24 },
},
shorthands: { p: ['padding'], m: ['margin'] },
});
export const sprinkles = createSprinkles(space);
// usage
<div className={sprinkles({ p: 'md', m: 'sm' })}>...</div>
```
### Global style + reset
```ts
// reset.css.ts
import { globalStyle } from '@vanilla-extract/css';
globalStyle('html, body', {
margin: 0,
padding: 0,
fontFamily: 'system-ui, sans-serif',
});
globalStyle('*', {
boxSizing: 'border-box',
});
// 매 import 'one' 으로 reset 적용
// app.tsx: import './reset.css';
```
### Dynamic value via createVar
```ts
// progress.css.ts
import { createVar, style } from '@vanilla-extract/css';
export const progressVar = createVar();
export const bar = style({
width: progressVar,
background: 'blue',
height: 4,
transition: 'width 0.3s',
});
// progress.tsx
import { assignInlineVars } from '@vanilla-extract/dynamic';
<div
className={bar}
style={assignInlineVars({ [progressVar]: `${percent}%` })}
/>
// 매 runtime dynamic — CSS variable 로 변환.
```
### Style composition
```ts
import { style } from '@vanilla-extract/css';
const base = style({ padding: 8, fontSize: 14 });
const primary = style([base, { background: 'blue', color: 'white' }]);
// 매 primary class = base class + extra rules.
```
### Next.js App Router setup
```ts
// next.config.js
const { createVanillaExtractPlugin } = require('@vanilla-extract/next-plugin');
const withVanillaExtract = createVanillaExtractPlugin();
module.exports = withVanillaExtract({
// ... existing config
});
// app/layout.tsx
import { lightTheme } from './theme.css';
import './reset.css';
export default function RootLayout({ children }) {
return <html className={lightTheme}>{children}</html>;
}
// 매 RSC 호환 — server component 에서 매 className 사용 가능.
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Next.js App Router (RSC) | vanilla-extract (built-in plugin). |
| Type-safe theme tokens | `createThemeContract` + `createTheme`. |
| CVA-style variants | `recipe`. |
| Atomic / Tailwind-like | `sprinkles`. |
| Runtime dynamic value | `createVar` + `assignInlineVars`. |
| Component library 배포 | vanilla-extract (zero runtime). |
**기본값**: `style` (single class), `recipe` (variants), `createThemeContract` (multi-theme), `createVar` (runtime dynamic).
## 🔗 Graph
- 부모: [[CSS_Architecture_and_Styling|CSS-in-JS]] · [[Zero-Runtime CSS-in-JS]]
- 변형: [[Panda CSS]] · [[CSS Modules]]
- 응용: [[Design System]] · [[React Server Components — 경계 의식]]
- Adjacent: [[CVA]] · [[CSS_Architecture_and_Styling|Tailwind CSS]]
## 🤖 LLM 활용
**언제**: Next.js App Router (RSC), type-safe design system, component library 배포, multi-theme (light/dark/brand) 의.
**언제 X**: 매 simple prototype (Tailwind 매 빠름), 매 highly-dynamic per-pixel value (CSS-in-JS runtime 가 더 적합), 매 already emotion / styled-components codebase 의 incremental migration 어려움.
## ❌ 안티패턴
- **Runtime value 직접 사용**: 매 `style({ width: dynamicWidth })``dynamicWidth` 매 build time 에 evaluable 해야. 매 runtime 매 `createVar`.
- **`.css.ts` 의 conditional logic**: 매 `if (env.X)` 매 build-time evaluated — 매 runtime 분기 X.
- **Theme class 의 dynamic apply 미숙**: 매 theme 변경 시 `<html className={isDark ? darkTheme : lightTheme}>` — 매 root 단위.
- **plugin setup 누락**: 매 webpack/vite plugin 없으면 .css.ts 매 그냥 TS file 로 처리 — error.
- **`recipe` 의 base 의 selector**: 매 `recipe` base 안의 `selectors` 매 매 supported 하나 매 syntax 주의.
## 🧪 검증 / 중복
- Verified (vanilla-extract.style 공식 문서, GitHub seek-oss/vanilla-extract).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — vanilla-extract API 전체 (style, recipe, theme contract, sprinkles, createVar) 추가 |