Files
2nd/10_Wiki/Topics/Domain_Programming/Frontend/Dynamic Theming.md
T
Antigravity Agent c24165b8bc refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게
[공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류.
문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인).

- Topic_Programming → Domain_Programming (내부 구조 보존)
- Topic_Graphic → Domain_Design
- Topic_Business → Domain_Product
- Topic_General → Domain_General
- _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning),
  Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing)
- 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서)
- 빈 폴더 정리 (memory/procedures)
- 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 11:05:56 +09:00

5.5 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-dynamic-theming Dynamic Theming 10_Wiki/Topics verified self
Theming
Dark Mode
CSS Variables Theming
none A 0.9 applied
frontend
css
theming
design-system
2026-05-10 pending
language framework
TypeScript 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)

: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)

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)

<!-- <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 의 통합

@import "tailwindcss";

@theme {
  --color-bg: var(--bg);
  --color-fg: var(--fg);
}

:root         { --bg: #fff; --fg: #111; }
[data-theme="dark"] { --bg: #0a0a0a; --fg: #f5f5f5; }
<div className="bg-bg text-fg"> theme-aware</div>

System preference 의 listen

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)

function toggleTheme() {
  if (!document.startViewTransition) {
    flipTheme();
    return;
  }
  document.startViewTransition(() => flipTheme());
}
::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: 250ms;
}

Brand variant (multi-tenant)

[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

🤖 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 추가