Files
2nd/10_Wiki/Topic_Programming/Frontend/다크 모드 및 다중 브랜드 테마 동적 전환 시스템.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

248 lines
7.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: wiki-2026-0508-다크-모드-및-다중-브랜드-테마-동적-전환-시스템
title: 다크 모드 및 다중 브랜드 테마 동적 전환 시스템
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Dark Mode, Theme Switching, Multi-brand Theming]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [frontend, css, theming, dark-mode, design-system]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: TypeScript/CSS
framework: React/Next.js
---
# 다크 모드 및 다중 브랜드 테마 동적 전환 시스템
## 매 한 줄
> **"매 CSS variables × design tokens × runtime swap 의 zero-flash theming"**. 매 다크 모드는 단순 색상 toggle이 아닌, design token system의 multi-channel orchestration. 2026 표준은 `light-dark()` CSS function + `prefers-color-scheme` + system token + brand override 의 4-layer cascade.
## 매 핵심
### 매 4-Layer Token Architecture
1. **Primitive tokens**: `--color-blue-500: oklch(60% 0.2 240)`.
2. **Semantic tokens**: `--color-bg-primary` (theme-aware).
3. **Component tokens**: `--button-bg = --color-action`.
4. **Brand override**: `[data-brand="acme"] { --color-action: ... }`.
### 매 SSR Flash 방지
- Inline blocking script (head) 의 `localStorage` read → `<html data-theme="dark">` 의 set before paint.
- Next.js: `next-themes` library 의 표준.
- Cookie-based 의 SSR-aware (Next 15 RSC).
### 매 응용
1. GitHub 의 `light/light-high-contrast/dark/dark-dimmed/dark-high-contrast` 의 5 modes.
2. Stripe Dashboard 의 brand switcher (Atlas, Climate 등).
3. Linear 의 theme + accent color picker.
4. VS Code 의 theme marketplace (color-theme JSON).
## 💻 패턴
### 1. CSS `light-dark()` (2026 baseline)
```css
:root {
color-scheme: light dark;
--bg: light-dark(white, #0a0a0a);
--fg: light-dark(#0a0a0a, white);
--accent: light-dark(oklch(55% 0.2 250), oklch(70% 0.18 250));
}
[data-theme="light"] { color-scheme: light; }
[data-theme="dark"] { color-scheme: dark; }
```
### 2. Multi-brand token override
```css
:root[data-brand="default"] { --brand-primary: oklch(55% 0.2 250); }
:root[data-brand="acme"] { --brand-primary: oklch(60% 0.22 30); }
:root[data-brand="globex"] { --brand-primary: oklch(58% 0.25 140); }
.btn-primary {
background: var(--brand-primary);
color: light-dark(white, black);
}
```
### 3. No-flash inline script (Next.js layout.tsx)
```tsx
const themeScript = `
(function() {
try {
const saved = localStorage.getItem('theme');
const sys = matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
const theme = saved || sys;
document.documentElement.dataset.theme = theme;
document.documentElement.style.colorScheme = theme;
} catch (e) {}
})();
`;
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="ko" suppressHydrationWarning>
<head>
<script dangerouslySetInnerHTML={{ __html: themeScript }} />
</head>
<body>{children}</body>
</html>
);
}
```
### 4. React Theme Provider (next-themes 스타일)
```tsx
'use client';
import { createContext, useContext, useEffect, useState } from 'react';
type Theme = 'light' | 'dark' | 'system';
const ThemeCtx = createContext<{ theme: Theme; setTheme: (t: Theme) => void }>(null!);
export function ThemeProvider({ children }: { children: React.ReactNode }) {
const [theme, setThemeState] = useState<Theme>('system');
useEffect(() => {
const saved = (localStorage.getItem('theme') as Theme) || 'system';
setThemeState(saved);
}, []);
useEffect(() => {
const resolved = theme === 'system'
? (matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
: theme;
document.documentElement.dataset.theme = resolved;
document.documentElement.style.colorScheme = resolved;
}, [theme]);
const setTheme = (t: Theme) => {
localStorage.setItem('theme', t);
setThemeState(t);
};
return <ThemeCtx.Provider value={{ theme, setTheme }}>{children}</ThemeCtx.Provider>;
}
export const useTheme = () => useContext(ThemeCtx);
```
### 5. Brand context + dynamic token injection
```tsx
'use client';
import { useEffect } from 'react';
export function BrandProvider({ brand, children }: { brand: 'acme' | 'globex'; children: React.ReactNode }) {
useEffect(() => {
document.documentElement.dataset.brand = brand;
}, [brand]);
return <>{children}</>;
}
```
### 6. Tailwind v4 + design tokens
```css
/* app/globals.css */
@import "tailwindcss";
@theme {
--color-bg: light-dark(white, #0a0a0a);
--color-fg: light-dark(#0a0a0a, white);
--color-brand: var(--brand-primary, oklch(55% 0.2 250));
}
```
```tsx
<button className="bg-brand text-bg">Click</button>
```
### 7. Image / asset variants (theme-aware)
```tsx
import Image from 'next/image';
import { useTheme } from '@/lib/theme';
export function Logo() {
const { theme } = useTheme();
return (
<picture>
<source srcSet="/logo-dark.svg" media="(prefers-color-scheme: dark)" />
<img src="/logo-light.svg" alt="Logo" />
</picture>
);
}
```
### 8. View Transitions API (smooth theme swap)
```ts
async function toggleTheme(next: 'light' | 'dark') {
if (!document.startViewTransition) {
document.documentElement.dataset.theme = next;
return;
}
await document.startViewTransition(() => {
document.documentElement.dataset.theme = next;
}).ready;
}
```
```css
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 200ms;
}
```
### 9. SSR cookie-based theme (Next 15 RSC)
```tsx
// app/layout.tsx (server)
import { cookies } from 'next/headers';
export default async function Layout({ children }) {
const theme = (await cookies()).get('theme')?.value ?? 'light';
return <html data-theme={theme}>{children}</html>;
}
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| 단일 브랜드 + dark mode | CSS `light-dark()` + `prefers-color-scheme` |
| 다중 브랜드 SaaS | `data-brand` attribute + token override |
| SSR Next.js | next-themes OR cookie-based RSC |
| Tailwind v4 | `@theme` + CSS vars |
| Smooth transition | View Transitions API |
| Legacy browser | Class-based (`.dark`) + localStorage |
**기본값**: CSS `light-dark()` + `data-theme` + `data-brand` + cookie SSR + next-themes.
## 🔗 Graph
- 부모: [[Design_Systems]] · [[CSS Variables]]
- 변형: [[Design_Tokens]]
- 응용: [[Tailwind_v4]] · [[Styled Components v6]] · [[CSS Modules]]
- Adjacent: [[View_Transitions_API]]
## 🤖 LLM 활용
**언제**: token naming convention 제안, contrast ratio audit, brand palette 생성 (OKLCH 기반).
**언제 X**: 최종 brand color decision (디자이너), accessibility AA/AAA 의 자동 보장은 X.
## ❌ 안티패턴
- **No-flash 미구현**: hydration 후 theme 적용 → FOUC.
- **Hardcoded hex**: design token 의 bypass — multi-brand 의 X.
- **JS-only theming**: CSS-only 의 fallback 없으면 SSR flash.
- **Inverted contrast**: 다크 모드의 단순 색상 invert — semantic hierarchy 의 break.
- **`!important` overuse**: token cascade 의 collapse.
- **`localStorage` SSR access**: hydration mismatch — `useEffect`로 lazy.
## 🧪 검증 / 중복
- Verified (web.dev color-scheme guide, MDN `light-dark()`, next-themes docs).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — dark mode + multi-brand theming full content |