f8b21af4be
10_Wiki/Topics 대규모 정리: - 오류 캡처/미완성 stub 문서 227개 제거 - 교차폴더 중복 43클러스터 병합 (63파일 → redirect) - 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건 - 카테고리 MOC 6개 신규 생성 - Graph 섹션 미해결 related-keyword 링크 10,058건 제거 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
200 lines
5.5 KiB
Markdown
200 lines
5.5 KiB
Markdown
---
|
|
id: wiki-2026-0508-dynamic-theming
|
|
title: Dynamic Theming
|
|
category: 10_Wiki/Topics
|
|
status: verified
|
|
canonical_id: self
|
|
aliases: [Theming, Dark Mode, CSS Variables Theming]
|
|
duplicate_of: none
|
|
source_trust_level: A
|
|
confidence_score: 0.9
|
|
verification_status: applied
|
|
tags: [frontend, css, theming, design-system]
|
|
raw_sources: []
|
|
last_reinforced: 2026-05-10
|
|
github_commit: pending
|
|
tech_stack:
|
|
language: TypeScript
|
|
framework: React
|
|
---
|
|
|
|
# Dynamic Theming
|
|
|
|
## 매 한 줄
|
|
> **"매 design token 을 runtime swap 할 수 있는 architecture"**. CSS custom properties (variables) 가 매 modern theming 의 backbone 이며, JS bundle 의 무관 하게 instant theme switching 의 가능. 2026 의 light/dark/high-contrast/brand-variant 의 매 standard.
|
|
|
|
## 매 핵심
|
|
|
|
### 매 3-layer token 구조
|
|
- **Primitive tokens**: raw values (`--blue-500: #3B82F6`).
|
|
- **Semantic tokens**: intent-based (`--color-primary: var(--blue-500)`).
|
|
- **Component tokens**: scope-specific (`--button-bg: var(--color-primary)`).
|
|
|
|
### 매 swap 메커니즘
|
|
- `data-theme="dark"` 속성 의 `<html>` element 의 set.
|
|
- CSS 의 `[data-theme="dark"] { --color-bg: #0a0a0a }` 의 override.
|
|
- 매 zero JS re-render — 매 paint cycle 만 trigger.
|
|
|
|
### 매 응용
|
|
1. Light/dark mode toggle.
|
|
2. Brand white-labeling (multi-tenant SaaS).
|
|
3. Accessibility (high-contrast, reduced-motion variant).
|
|
4. Per-user customization (saved theme preference).
|
|
|
|
## 💻 패턴
|
|
|
|
### Token 정의 (CSS)
|
|
```css
|
|
:root {
|
|
/* Primitive */
|
|
--blue-500: #3B82F6;
|
|
--gray-900: #111827;
|
|
--gray-50: #F9FAFB;
|
|
|
|
/* Semantic — light default */
|
|
--color-bg: var(--gray-50);
|
|
--color-fg: var(--gray-900);
|
|
--color-primary: var(--blue-500);
|
|
}
|
|
|
|
[data-theme="dark"] {
|
|
--color-bg: var(--gray-900);
|
|
--color-fg: var(--gray-50);
|
|
}
|
|
|
|
[data-theme="high-contrast"] {
|
|
--color-bg: #000;
|
|
--color-fg: #fff;
|
|
--color-primary: #ffff00;
|
|
}
|
|
```
|
|
|
|
### Theme provider (React 19)
|
|
```tsx
|
|
import { createContext, use, useEffect, useState } from "react";
|
|
|
|
type Theme = "light" | "dark" | "system";
|
|
const ThemeCtx = createContext<{ theme: Theme; set: (t: Theme) => void }>(null!);
|
|
|
|
export function ThemeProvider({ children }: { children: React.ReactNode }) {
|
|
const [theme, setTheme] = useState<Theme>(
|
|
() => (localStorage.getItem("theme") as Theme) ?? "system"
|
|
);
|
|
|
|
useEffect(() => {
|
|
const resolved =
|
|
theme === "system"
|
|
? matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
|
|
: theme;
|
|
document.documentElement.dataset.theme = resolved;
|
|
localStorage.setItem("theme", theme);
|
|
}, [theme]);
|
|
|
|
return <ThemeCtx value={{ theme, set: setTheme }}>{children}</ThemeCtx>;
|
|
}
|
|
|
|
export const useTheme = () => use(ThemeCtx);
|
|
```
|
|
|
|
### FOUC 방지 (inline script)
|
|
```html
|
|
<!-- <head> 의 first script — render-blocking 의 의도적 -->
|
|
<script>
|
|
(function () {
|
|
const t = localStorage.getItem("theme") || "system";
|
|
const resolved = t === "system"
|
|
? (matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light")
|
|
: t;
|
|
document.documentElement.dataset.theme = resolved;
|
|
})();
|
|
</script>
|
|
```
|
|
|
|
### Tailwind 4 의 통합
|
|
```css
|
|
@import "tailwindcss";
|
|
|
|
@theme {
|
|
--color-bg: var(--bg);
|
|
--color-fg: var(--fg);
|
|
}
|
|
|
|
:root { --bg: #fff; --fg: #111; }
|
|
[data-theme="dark"] { --bg: #0a0a0a; --fg: #f5f5f5; }
|
|
```
|
|
|
|
```tsx
|
|
<div className="bg-bg text-fg">매 theme-aware</div>
|
|
```
|
|
|
|
### System preference 의 listen
|
|
```ts
|
|
const mq = matchMedia("(prefers-color-scheme: dark)");
|
|
mq.addEventListener("change", (e) => {
|
|
if (localStorage.getItem("theme") === "system") {
|
|
document.documentElement.dataset.theme = e.matches ? "dark" : "light";
|
|
}
|
|
});
|
|
```
|
|
|
|
### View Transitions API (smooth swap)
|
|
```ts
|
|
function toggleTheme() {
|
|
if (!document.startViewTransition) {
|
|
flipTheme();
|
|
return;
|
|
}
|
|
document.startViewTransition(() => flipTheme());
|
|
}
|
|
```
|
|
|
|
```css
|
|
::view-transition-old(root),
|
|
::view-transition-new(root) {
|
|
animation-duration: 250ms;
|
|
}
|
|
```
|
|
|
|
### Brand variant (multi-tenant)
|
|
```css
|
|
[data-brand="acme"] { --color-primary: #FF6B35; }
|
|
[data-brand="globex"] { --color-primary: #2EB872; }
|
|
```
|
|
|
|
## 매 결정 기준
|
|
| 상황 | Approach |
|
|
|---|---|
|
|
| Static site / blog | CSS variable + `data-theme` |
|
|
| SaaS multi-tenant | CSS variable + brand attribute layer |
|
|
| RN / Native | Theme context + StyleSheet (no CSS vars) |
|
|
| Tailwind 의 사용 | Tailwind 4 `@theme` + CSS variable |
|
|
| Email template | Inline styles + `prefers-color-scheme` media query |
|
|
|
|
**기본값**: CSS custom properties + `data-theme` attribute + inline FOUC script.
|
|
|
|
## 🔗 Graph
|
|
- 부모: [[CSS_Architecture_and_Styling|CSS Architecture]] · [[Design Tokens]]
|
|
- 변형: [[Tailwind CSS 4]] · [[CSS_Architecture_and_Styling|CSS-in-JS]]
|
|
- 응용: [[Dark Mode]] · [[Accessibility (a11y)]]
|
|
- Adjacent: [[View Transitions API]]
|
|
|
|
## 🤖 LLM 활용
|
|
**언제**: design token system 의 설계, dark mode 구현, multi-brand theming.
|
|
**언제 X**: simple 의 single-color brand 의 — 매 over-engineering.
|
|
|
|
## ❌ 안티패턴
|
|
- **JS-only theme**: setState 의 모든 component re-render — 매 slow 의.
|
|
- **Hard-coded color in component**: token 의 bypass — 매 swap 불가능.
|
|
- **No FOUC script**: hydration 전 wrong theme flash — 매 jarring UX.
|
|
- **Theme 의 localStorage 의만 의존**: SSR 의 server-render mismatch.
|
|
|
|
## 🧪 검증 / 중복
|
|
- Verified (MDN, web.dev, Tailwind CSS docs, Adobe Spectrum 의 token system).
|
|
- 신뢰도 A.
|
|
|
|
## 🕓 Changelog
|
|
| 날짜 | 변경 |
|
|
|---|---|
|
|
| 2026-05-08 | Phase 1 |
|
|
| 2026-05-10 | Manual cleanup — 3-layer token + FOUC + View Transitions 추가 |
|