# Dark Mode Toggle Flash-free dark mode for Next.js 14+ with Tailwind — three-way light/dark/system toggle, localStorage persistence, OS preference sync, and hydration-safe rendering. ## What's included - `ThemeProvider` — context provider; reads localStorage on mount, listens for OS preference changes when mode is `system` - `useTheme()` — returns `{ theme, resolved, setTheme, cycleTheme }` — `theme` is the stored preference, `resolved` is always `'light'` or `'dark'` - `ThemeToggle` — button that cycles light → dark → system; renders a fixed-size placeholder before mount to prevent layout shift - `THEME_SCRIPT` — inline script string to paste into `
` before stylesheets; eliminates flash of wrong theme on first load - `useColorScheme()` — standalone hook returning the OS preference; usable without `ThemeProvider` - `Theme` type — `'light' | 'dark' | 'system'` - `ResolvedTheme` type — `'light' | 'dark'` ## Setup ### 1. Install dependencies No extra packages — only React and Tailwind, which you already have. ### 2. Inject the no-flash script ```tsx // app/layout.tsx import { THEME_SCRIPT } from '@/blocks/darkmode' export default function RootLayout({ children }: { children: React.ReactNode }) { return ( {/* stylesheets go after */} {children} ) } ``` ### 3. Wrap your app ```tsx // app/providers.tsx 'use client' import { ThemeProvider } from '@/blocks/darkmode' export function Providers({ children }: { children: React.ReactNode }) { return