P PasteCode
Prompt

Prompt to Add Dark Mode with Tailwind CSS v4

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.

CursorClaude CodeCodexWindsurf Next.jsTypeScriptTailwind
.md .json Difficulty: Easy Updated Jun 8, 2026

Give this prompt to your agent to add dark mode using Tailwind v4’s CSS-variable-based theming and next-themes — preventing the flash of unstyled content that plagues naive localStorage implementations.

Main Prompt

Main Prompt
You are working in a Next.js App Router project with TypeScript and Tailwind CSS v4.
Task: add dark mode with system preference detection and a manual toggle.
Requirements:
- Install `next-themes`.
- In `src/app/layout.tsx`, wrap the app in `<ThemeProvider attribute="class" defaultTheme="system" enableSystem>`.
The `ThemeProvider` is a Client Component — create a wrapper `src/components/Providers.tsx` if
the root layout must remain a Server Component.
- In `src/styles/global.css`, define CSS custom properties for both themes using the Tailwind v4
`@theme` directive:
- Light: `--color-bg: oklch(100% 0 0)`, `--color-text: oklch(9% 0 0)`.
- Dark (inside `[class="dark"]` selector): override those variables.
Apply them to the body as `background: var(--color-bg)` and `color: var(--color-text)`.
- Create `src/components/ThemeToggle.tsx` (Client Component) using `useTheme` from `next-themes`.
It should cycle through light → dark → system on click and show a sun/moon/monitor icon.
Use Tailwind's built-in `dark:` variant for icon visibility — do NOT use conditional classNames
based on the `theme` string for icon color.
- Do not use `tailwind.config.ts` `darkMode: 'class'` — that is Tailwind v3 API. In v4, the
`dark:` variant is class-based by default.
- Do NOT install lucide-react or any icon library — use SVG inline.
Stop and list all planned file changes before writing any code.

Implementation Notes

  • In Tailwind v4, dark mode via dark: prefix works with the .dark class on html automatically — no config entry is needed.
  • next-themes reads the saved theme from localStorage and sets the class before first paint, which prevents the FOUC.
  • The ThemeProvider must be client-side, but the root layout.tsx can stay server-side by extracting the provider to a Providers.tsx wrapper component marked 'use client'.

Expected File Changes

src/app/layout.tsx (edited — add Providers wrapper)
src/components/Providers.tsx (new — Client Component with ThemeProvider)
src/components/ThemeToggle.tsx (new — Client Component)
src/styles/global.css (edited — CSS custom properties for light/dark)
package.json (edited — add next-themes)

Acceptance Criteria

  • On first load with system set to dark, the page renders dark without a flash.
  • Toggling ThemeToggle cycles through light, dark, and system modes; the preference persists on reload.
  • All existing dark: Tailwind utilities work without any config changes.
  • bun run build exits 0.

Test Commands

Terminal window
bun add next-themes
bun run typecheck
bun run build
bun run dev
# open DevTools, set system dark mode, reload — confirm no white flash
# click ThemeToggle three times and confirm cycle: light → dark → system

Common AI Mistakes

  • Adding darkMode: 'class' to a tailwind.config.ts that doesn’t exist in a v4 project.
  • Wrapping the root Server Component layout.tsx with 'use client' instead of extracting a Providers component.
  • Using theme === 'dark' conditional rendering instead of Tailwind’s dark: variant, causing hydration mismatches.
  • Forgetting enableSystem on ThemeProvider, making system preference detection non-functional.

Fix Prompt

Fix Prompt
There's a white flash on load or a hydration mismatch. Fix in order:
1. Ensure `ThemeProvider` has `attribute="class"` and `enableSystem` props.
2. Make sure `Providers.tsx` (not `layout.tsx`) is marked `'use client'`.
3. Remove any `theme === 'dark'` conditional rendering from `ThemeToggle` — use `dark:` Tailwind
variants instead so the server and client render the same HTML.
Show only the corrected diff.