How to Implement Dark Mode with Claude Code
Learn how to implement dark mode using Claude Code. Includes practical code examples and step-by-step guidance.
Streamlining Dark Mode With Claude Code
Dark mode is a standard feature of modern web apps, but there’s a lot to consider when implementing it: CSS design, state management, persistence, and more. With Claude Code, you can roll out a consistent implementation in a short time.
Designing Themes With CSS Variables
Start by defining theme tokens as CSS variables.
> Design light and dark themes based on CSS variables.
> Use semantic color names and make them integrate cleanly with 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);
}
Implementing the Theme Switcher
Build a hook that supports both system preference and manual override.
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 };
}
Theme Toggle Button
import { useTheme } from "@/hooks/useTheme";
export function ThemeToggle() {
const { theme, setTheme, resolvedTheme } = useTheme();
const options: { value: Theme; label: string; icon: string }[] = [
{ value: "light", label: "Light", icon: "sun" },
{ value: "dark", label: "Dark", icon: "moon" },
{ value: "system", label: "System", 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>
);
}
Preventing FOUC (Flash of Unstyled Content)
A script that prevents the brief flash of the wrong theme during page load.
<!-- Place inside <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 Integration
// 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)",
},
},
},
},
};
For design system integration, see building a design system, and for responsive design, see responsive design.
Summary
With Claude Code, you can complete dark mode implementation in a coherent and short-form way, covering CSS variable design, the theme switcher logic, FOUC prevention, and Tailwind integration. Simply ask “add dark mode” and Claude Code can retrofit it into an existing project.
For details, see the official Claude Code documentation.
Level up your Claude Code workflow
50 battle-tested prompt templates you can copy-paste into Claude Code right now.
Free PDF: Claude Code Cheatsheet in 5 Minutes
Key commands, shortcuts, and prompt examples on a single printable page.
About the Author
Masa
Engineer obsessed with Claude Code. Runs claudecode-lab.com, a 10-language tech media with 2,000+ pages.
Related Posts
Creating Custom Slash Commands in Claude Code — Tailor Your Workflow
Learn how to create custom slash commands in Claude Code. Covers file placement, arguments, and automating frequent tasks with practical code examples.
10 Tips to Triple Your Productivity with Claude Code
Learn about 10 tips to triple your productivity using Claude Code. Practical tips and code examples included.
Canvas/WebGL Optimization with Claude Code
Learn about Canvas/WebGL optimization using Claude Code. Practical tips and code examples included.