Communitygithub.com

다크 모드 (CSS 변수, 시스템 감지, next-themes)

CSS 변수, 시스템 환경설정 감지, next-themes를 사용한 테마 전환 및 지속성 처리 스킬입니다.

다크 모드 (CSS 변수, 시스템 감지, next-themes)란 무엇인가요?

이 스킬은 Next.js 애플리케이션에서 CSS 변수와 next-themes 라이브러리를 활용하여 완전한 다크 모드 지원을 구현하는 방법을 제공합니다. 시스템 환경설정 감지(defaultTheme="system")와 localStorage를 통한 테마 지속성을 기본으로 지원하며, Tailwind CSS 통합(darkMode: "class")을 통해 dark: 프리픽스를 사용한 스타일 오버라이드가 가능합니다. ThemeProvider로 앱을 감싸고 useTheme() 훅으로 테마 토글을 구현하며, 하이드레이션 문제를 방지하기 위해 suppressHydrationWarning과 마운트 후 렌더링 패턴을 권장합니다. CSS 변수는 :root(라이트)와 .dark(다크) 클래스에 정의되어 부드러운 전환(transition-colors)과 함께 사용됩니다.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/dark-mode

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

다크 모드

규칙

  • Next.js 앱에는 next-themes 사용 — 앱을 <ThemeProvider attribute="class" defaultTheme="system">로 감싸기
  • 색상은 CSS 변수 사용 — :root(라이트)와 .dark(다크)에 정의하고 var(--background)로 참조
  • 시스템 환경설정 감지: defaultTheme="system"은 prefers-color-scheme 미디어 쿼리를 따름
  • 테마 토글 컴포넌트: light/dark/system 순환 — next-themes의 useTheme() 훅 사용
  • Tailwind 통합: tailwind 설정에서 darkMode: "class" 활성화, dark: 프리픽스로 오버라이드
  • 하이드레이션 문제 해결: useEffect 이후에만 테마 토글을 마운트하거나 suppressHydrationWarning 사용
  • 지속성: next-themes는 환경설정을 localStorage에 저장하고 <html>에 class로 적용
  • 전환 효과: body에 transition-colors duration-200 추가로 부드러운 테마 전환

패턴

// 레이아웃 래퍼
import { ThemeProvider } from "next-themes";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <body>
        <ThemeProvider attribute="class" defaultTheme="system" enableSystem>
          {children}
        </ThemeProvider>
      </body>
    </html>
  );
}
// 테마 토글
"use client";
import { useTheme } from "next-themes";
import { useEffect, useState } from "react";

export function ThemeToggle() {
  const { theme, setTheme } = useTheme();
  const [mounted, setMounted] = useState(false);
  useEffect(() => setMounted(true), []);
  if (!mounted) return null;

  return (
    <button onClick={() => setTheme(theme === "dark" ? "light" : "dark")}>
      {theme === "dark" ? "☀️" : "🌙"}
    </button>
  );
}
/* CSS 변수 */
:root {
  --background: 0 0% 100%;
  --foreground: 222.2 84% 4.9%;
}
.dark {
  --background: 222.2 84% 4.9%;
  --foreground: 210 40% 98%;
}

피해야 할 것

  • CSS 변수 대신 색상을 하드코딩 — 테마 변경이 불가능해짐
  • <html>에 suppressHydrationWarning 누락 — 하이드레이션 오류 발생
  • 잘못된 테마 플래시: next-themes 스크립트가 페인트 전에 실행되도록 보장 (기본적으로 동작)
  • 시스템 옵션 없이 라이트/다크만 지원 — 사용자 OS 환경설정을 존중해야 함

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Tenant isolation (row-level vs schema), subdomain routing, per-tenant config, and data partitioning.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

관련 스킬