{
  "id": "add-dark-mode-with-tailwind",
  "type": "prompts",
  "category": "prompts",
  "locale": "es",
  "url": "/es/prompts/add-dark-mode-with-tailwind",
  "title": "Indicación para agregar modo oscuro con Tailwind CSS v4",
  "description": "Indicación para agente de IA para agregar modo oscuro consciente del sistema a una aplicación Next.js usando variables CSS de Tailwind CSS v4 y next-themes, sin destello al cargar.",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "TypeScript",
    "Tailwind"
  ],
  "tags": [
    "tailwind",
    "nextjs",
    "typescript"
  ],
  "difficulty": "easy",
  "updated": "2026-06-08",
  "markdown": "Entrega esta indicación a tu agente para agregar modo oscuro usando el sistema de variables CSS de Tailwind v4 y `next-themes` — evitando el destello de contenido sin estilo que afecta a las implementaciones ingenuas de `localStorage`.\n\n## Indicación principal\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## Notas de implementación\n\n- En Tailwind v4, el modo oscuro mediante el prefijo `dark:` funciona automáticamente con la clase `.dark` en `html` — no se necesita entrada de configuración.\n- `next-themes` lee el tema guardado de `localStorage` y establece la clase antes del primer pintado, lo que previene el FOUC.\n- El `ThemeProvider` debe estar en el lado del cliente, pero el `layout.tsx` raíz puede permanecer del lado del servidor extrayendo el proveedor a un componente envoltorio `Providers.tsx` marcado como `'use client'`.\n\n## Cambios esperados en archivos\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## Criterios de aceptación\n\n- En la primera carga con el sistema configurado en oscuro, la página se renderiza oscura sin destello.\n- Alternar ThemeToggle recorre los modos claro, oscuro y sistema; la preferencia persiste al recargar.\n- Todas las utilidades `dark:` de Tailwind existentes funcionan sin cambios de configuración.\n- `bun run build` termina con código 0.\n\n## Comandos de prueba\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## Errores comunes de IA\n\n- Agregar `darkMode: 'class'` a un `tailwind.config.ts` que no existe en un proyecto v4.\n- Envolver el Server Component raíz `layout.tsx` con `'use client'` en lugar de extraer un componente Providers.\n- Usar renderizado condicional `theme === 'dark'` en lugar de la variante `dark:` de Tailwind, causando discrepancias de hidratación.\n- Olvidar `enableSystem` en `ThemeProvider`, haciendo que la detección de preferencia del sistema no funcione.\n\n## Indicación de corrección\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```"
}