docs(10_Wiki): Topic_Business/General/Graphic/Programming을 Topics/ 하위로 이동
최상위 10_Wiki/Topic_*였던 4개 카테고리 폴더를 10_Wiki/Topics/Topic_* 로 재배치. 콘텐츠 변경 없음(순수 폴더 이동) — Topics/ 하위 나머지 폴더는 이미 지난 커밋에서 전부 정리된 상태(잔존 항목은 에이전트 운영 상태 및 사용자가 보존을 요청한 업데이트0615/무제 3.canvas 뿐).
This commit is contained in:
@@ -0,0 +1,292 @@
|
||||
---
|
||||
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) 추가 |
|
||||
Reference in New Issue
Block a user