Claude Codeでダークモードを実装する方法
Claude Codeを使ってダークモードを効率的に実装。CSS変数、Tailwind CSS、システム設定連動、永続化まで実践コードで解説。
ダークモード実装をClaude Codeで効率化する
ダークモードは今やWebアプリの標準機能ですが、実装にはCSS設計、状態管理、永続化など多くの考慮が必要です。Claude Codeなら一貫した設計で短時間で実装できます。
CSS変数によるテーマ設計
まずはCSS変数でテーマトークンを定義します。
> CSS変数ベースでライト/ダークテーマを設計して。
> セマンティックな色名を使って、Tailwindと統合できるようにして。
/* globals.css */
:root {
--color-bg-primary: #ffffff;
--color-bg-secondary: #f8fafc;
--color-bg-tertiary: #f1f5f9;
--color-text-primary: #0f172a;
--color-text-secondary: #475569;
--color-text-tertiary: #94a3b8;
--color-border: #e2e8f0;
--color-border-hover: #cbd5e1;
--color-accent: #3b82f6;
--color-accent-hover: #2563eb;
--color-surface: #ffffff;
--color-surface-hover: #f8fafc;
--shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
--shadow-md: 0 4px 6px rgba(0, 0, 0, 0.07);
}
[data-theme="dark"] {
--color-bg-primary: #0f172a;
--color-bg-secondary: #1e293b;
--color-bg-tertiary: #334155;
--color-text-primary: #f1f5f9;
--color-text-secondary: #cbd5e1;
--color-text-tertiary: #64748b;
--color-border: #334155;
--color-border-hover: #475569;
--color-accent: #60a5fa;
--color-accent-hover: #93bbfd;
--color-surface: #1e293b;
--color-surface-hover: #334155;
--shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
--shadow-md: 0 4px 6px rgba(0, 0, 0, 0.4);
}
テーマ切り替えの実装
システム設定連動と手動切り替えの両方に対応するフックを作ります。
import { useState, useEffect, useCallback } from "react";
type Theme = "light" | "dark" | "system";
export function useTheme() {
const [theme, setThemeState] = useState<Theme>(() => {
if (typeof window === "undefined") return "system";
return (localStorage.getItem("theme") as Theme) || "system";
});
const [resolvedTheme, setResolvedTheme] = useState<"light" | "dark">("light");
const applyTheme = useCallback((newTheme: Theme) => {
const root = document.documentElement;
let resolved: "light" | "dark";
if (newTheme === "system") {
resolved = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
} else {
resolved = newTheme;
}
root.setAttribute("data-theme", resolved);
setResolvedTheme(resolved);
}, []);
const setTheme = useCallback(
(newTheme: Theme) => {
setThemeState(newTheme);
localStorage.setItem("theme", newTheme);
applyTheme(newTheme);
},
[applyTheme]
);
useEffect(() => {
applyTheme(theme);
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
const handleChange = () => {
if (theme === "system") applyTheme("system");
};
mediaQuery.addEventListener("change", handleChange);
return () => mediaQuery.removeEventListener("change", handleChange);
}, [theme, applyTheme]);
return { theme, setTheme, resolvedTheme };
}
テーマ切り替えボタン
import { useTheme } from "@/hooks/useTheme";
export function ThemeToggle() {
const { theme, setTheme, resolvedTheme } = useTheme();
const options: { value: Theme; label: string; icon: string }[] = [
{ value: "light", label: "ライト", icon: "sun" },
{ value: "dark", label: "ダーク", icon: "moon" },
{ value: "system", label: "システム", icon: "monitor" },
];
return (
<div className="flex items-center gap-1 p-1 rounded-lg bg-[var(--color-bg-tertiary)]">
{options.map((option) => (
<button
key={option.value}
onClick={() => setTheme(option.value)}
className={`px-3 py-1.5 rounded-md text-sm transition-colors ${
theme === option.value
? "bg-[var(--color-surface)] text-[var(--color-text-primary)] shadow-sm"
: "text-[var(--color-text-secondary)] hover:text-[var(--color-text-primary)]"
}`}
aria-label={option.label}
>
{option.label}
</button>
))}
</div>
);
}
FOUC(ちらつき)の防止
ページ読み込み時にテーマが一瞬切り替わるちらつきを防ぐスクリプトです。
<!-- head内に配置 -->
<script>
(function () {
const theme = localStorage.getItem("theme") || "system";
let resolved = theme;
if (theme === "system") {
resolved = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
}
document.documentElement.setAttribute("data-theme", resolved);
})();
</script>
Tailwind CSSとの統合
// tailwind.config.js
module.exports = {
darkMode: ["selector", '[data-theme="dark"]'],
theme: {
extend: {
colors: {
bg: {
primary: "var(--color-bg-primary)",
secondary: "var(--color-bg-secondary)",
tertiary: "var(--color-bg-tertiary)",
},
text: {
primary: "var(--color-text-primary)",
secondary: "var(--color-text-secondary)",
},
surface: {
DEFAULT: "var(--color-surface)",
hover: "var(--color-surface-hover)",
},
},
},
},
};
デザインシステムとの統合はデザインシステム構築を、レスポンシブ対応はレスポンシブデザインをご覧ください。
まとめ
Claude Codeを使えば、CSS変数設計、テーマ切り替えロジック、FOUC防止、Tailwind統合まで、ダークモードの実装を一貫して短時間で完了できます。「ダークモードを追加して」と依頼するだけで、既存プロジェクトにも適切に組み込めます。
詳しくはClaude Code公式ドキュメントを参照してください。
無料PDF: Claude Code はじめてのチートシート
まずは無料PDFで基本コマンドと最初の使い方をまとめて確認してください。登録後はそのままテンプレート集や導入相談にも進めます。
スパムは送りません。登録情報は厳重に管理します。
Claude Codeを仕事で使える形にしませんか?
無料PDFで基礎を固めたあと、すぐ使えるテンプレート集で試し、必要なら業務自動化や導入相談まで進められます。
この記事を書いた人
Masa
現役DX室長|Claude Code でゼロから多言語AI技術メディア運営中。実務直結の自動化、AI開発相談・研修受付中。
関連書籍・参考図書
この記事のテーマに関連する書籍を楽天ブックスで探せます。
※ 当サイトは楽天市場のアフィリエイトプログラムに参加しています。上記リンクから商品をご購入いただくと、運営者に紹介料が支払われる場合があります。
関連記事
Claude Codeで使うCLAUDE.mdテンプレート7選 | 実案件にそのまま貼れる例
個人開発、コンテンツサイト、API、チーム開発、レガシー改修向けに、そのまま使えるCLAUDE.mdテンプレート7本をまとめました。
Claude Code の Approval / Sandbox 設定ガイド | 安全に毎日使うための実践ルール
Claude Code を allow・ask・deny・sandbox でどう分けるかを、動く設定例、Hooks、失敗例付きで実践的に解説します。
Claude Code 完全入門ガイド2026|ゼロから実務で使えるまでの7ステップ
Claude Codeを初めて触る方向けの完全入門ガイド。インストールから実際の開発ワークフローへの組み込みまで、Masa自身が最初につまずいたポイントを踏まえて丁寧に解説。