{
  "id": "add-dark-mode-with-tailwind",
  "type": "prompts",
  "category": "prompts",
  "locale": "en",
  "url": "/prompts/add-dark-mode-with-tailwind",
  "title": "Prompt to Add Dark Mode with Tailwind CSS v4",
  "description": "AI agent prompt to add system-aware dark mode to a Next.js app using Tailwind CSS v4 CSS variables and next-themes, with no flash on load.",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "TypeScript",
    "Tailwind"
  ],
  "tags": [
    "tailwind",
    "nextjs",
    "typescript"
  ],
  "difficulty": "easy",
  "updated": "2026-06-08",
  "markdown": "Give this prompt to your agent to add dark mode using Tailwind v4's CSS-variable-based\ntheming and `next-themes` — preventing the flash of unstyled content that plagues naive\n`localStorage` implementations.\n\n## Main Prompt\n\n```txt title=\"Main Prompt\"\nYou are working in a Next.js App Router project with TypeScript and Tailwind CSS v4.\n\nTask: add dark mode with system preference detection and a manual toggle.\n\nRequirements:\n- Install `next-themes`.\n- In `src/app/layout.tsx`, wrap the app in `<ThemeProvider attribute=\"class\" defaultTheme=\"system\" enableSystem>`.\n  The `ThemeProvider` is a Client Component — create a wrapper `src/components/Providers.tsx` if\n  the root layout must remain a Server Component.\n- In `src/styles/global.css`, define CSS custom properties for both themes using the Tailwind v4\n  `@theme` directive:\n    - Light: `--color-bg: oklch(100% 0 0)`, `--color-text: oklch(9% 0 0)`.\n    - Dark (inside `[class=\"dark\"]` selector): override those variables.\n  Apply them to the body as `background: var(--color-bg)` and `color: var(--color-text)`.\n- Create `src/components/ThemeToggle.tsx` (Client Component) using `useTheme` from `next-themes`.\n  It should cycle through light → dark → system on click and show a sun/moon/monitor icon.\n  Use Tailwind's built-in `dark:` variant for icon visibility — do NOT use conditional classNames\n  based on the `theme` string for icon color.\n- Do not use `tailwind.config.ts` `darkMode: 'class'` — that is Tailwind v3 API. In v4, the\n  `dark:` variant is class-based by default.\n- Do NOT install lucide-react or any icon library — use SVG inline.\n\nStop and list all planned file changes before writing any code.\n```\n\n## Implementation Notes\n\n- In Tailwind v4, dark mode via `dark:` prefix works with the `.dark` class on `html` automatically\n  — no config entry is needed.\n- `next-themes` reads the saved theme from `localStorage` and sets the class before first paint,\n  which prevents the FOUC.\n- The `ThemeProvider` must be client-side, but the root `layout.tsx` can stay server-side by\n  extracting the provider to a `Providers.tsx` wrapper component marked `'use client'`.\n\n## Expected File Changes\n\n```txt\nsrc/app/layout.tsx                     (edited — add Providers wrapper)\nsrc/components/Providers.tsx           (new — Client Component with ThemeProvider)\nsrc/components/ThemeToggle.tsx         (new — Client Component)\nsrc/styles/global.css                  (edited — CSS custom properties for light/dark)\npackage.json                           (edited — add next-themes)\n```\n\n## Acceptance Criteria\n\n- On first load with system set to dark, the page renders dark without a flash.\n- Toggling ThemeToggle cycles through light, dark, and system modes; the preference persists on reload.\n- All existing `dark:` Tailwind utilities work without any config changes.\n- `bun run build` exits 0.\n\n## Test Commands\n\n```bash\nbun add next-themes\nbun run typecheck\nbun run build\nbun run dev\n# open DevTools, set system dark mode, reload — confirm no white flash\n# click ThemeToggle three times and confirm cycle: light → dark → system\n```\n\n## Common AI Mistakes\n\n- Adding `darkMode: 'class'` to a `tailwind.config.ts` that doesn't exist in a v4 project.\n- Wrapping the root Server Component `layout.tsx` with `'use client'` instead of extracting a Providers component.\n- Using `theme === 'dark'` conditional rendering instead of Tailwind's `dark:` variant, causing hydration mismatches.\n- Forgetting `enableSystem` on `ThemeProvider`, making system preference detection non-functional.\n\n## Fix Prompt\n\n```txt title=\"Fix Prompt\"\nThere's a white flash on load or a hydration mismatch. Fix in order:\n1. Ensure `ThemeProvider` has `attribute=\"class\"` and `enableSystem` props.\n2. Make sure `Providers.tsx` (not `layout.tsx`) is marked `'use client'`.\n3. Remove any `theme === 'dark'` conditional rendering from `ThemeToggle` — use `dark:` Tailwind\n   variants instead so the server and client render the same HTML.\nShow only the corrected diff.\n```"
}