Communitygithub.com

YepAPI/skills

使用 CSS 變數、系統偏好偵測、next-themes 以及持久化儲存來切換主題。

skills 是什麼?

此技能提供一個完整且可重複使用的深色模式實作方案,專為 Next.js 應用程式設計。它依賴 `next-themes` 函式庫,並在 `<ThemeProvider>` 中設定 `attribute="class"` 與 `defaultTheme="system"`,以實現系統層級的偏好偵測。CSS 變數在 `:root`(淺色模式)和 `.dark`(深色模式)中定義,與 Tailwind CSS 的 `darkMode: "class"` 設定無縫整合。此外,它包含了優化使用者體驗的關鍵做法,例如使用 `suppressHydrationWarning` 來避免水合錯誤,以及透過 `useEffect` 在客戶端掛載主題切換按鈕,以防止主題閃爍。最後,`next-themes` 會自動將使用者偏好儲存於 `localStorage`,確保持久化。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/dark-mode

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

深色模式

規則

  • 使用 next-themes — 將應用程式包裝在 <ThemeProvider attribute="class" defaultTheme="system"> 中
  • CSS 變數定義顏色 — 在 :root(淺色)和 .dark(深色)中定義,並以 var(--background) 方式引用
  • 系統偏好偵測:defaultTheme="system" 會遵從 prefers-color-scheme 媒體查詢
  • 主題切換元件:在淺色、深色與系統之間循環 — 使用 next-themes 的 useTheme() hook
  • Tailwind 整合:在 tailwind 配置中啟用 darkMode: "class",並使用 dark: 前綴進行覆寫
  • 水合修復:僅在 useEffect 後掛載主題切換元件,或使用 suppressHydrationWarning 來避免水合錯誤
  • 持久化:next-themes 將偏好儲存在 localStorage 中,並以 class 的形式套用至 <html> 元素
  • 過渡效果:在 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 的腳本在網頁繪製前執行(預設即會這樣做)
  • 僅支援淺色/深色而缺少系統選項 — 應尊重使用者的作業系統偏好

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.

相關技能