--- title: "Laravel Inertia Dark Mode: Fix the White Flash" description: "Add light, dark and system dark mode to a Laravel Inertia app and stop the white flash on page load. Works with Tailwind CSS v4 in Vue, React and Svelte." pageClass: blog-page date: 2026-10-29 author: annu-gupta category: frontend tags: [Dark mode, Inertia] --- # Laravel Inertia Dark Mode: Light, Dark and System Themes Without the White Flash Dark mode sounds like a five-minute job. Add a `dark` class, sprinkle some `dark:` classes around, done. Then you reload the page and see it flash white for a moment before it turns dark. Your users notice that, and it makes the whole app feel a little cheap. In this post we'll set up dark mode in a Laravel and Inertia app the way we do it in our SaaS starter kit: three modes (light, dark and system), no flash when the page loads, and a toggle that works the same in Vue, React and Svelte. ## Give users three choices People expect three options, not two: | Mode | What it does | | --- | --- | | Light | Always light | | Dark | Always dark | | System | Follows the computer or phone, and switches when it changes | Make "System" the default. Most people never open your settings page, so following their device is the friendliest choice. ## Tell Tailwind to use a class By default, Tailwind's `dark:` styles follow the device setting and nothing else. To let users override it, switch Tailwind CSS v4 to a class: ```css /* resources/css/app.css */ @import 'tailwindcss'; @custom-variant dark (&:is(.dark *)); ``` Now `dark:bg-zinc-900` applies whenever the `` tag has a `dark` class, and you decide when that happens. If you use shadcn components, put your colours in CSS variables under `:root` and `.dark`, and the components switch along with them. ## Why the white flash happens An Inertia page is drawn by JavaScript. If only your Vue, React or Svelte code adds the `dark` class, the browser first shows the page with the normal light styles. Then your JavaScript loads, runs and switches to dark. That short gap is the flash. To get rid of it, you have to decide the theme before the browser draws anything. That means doing it on the server, plus a tiny script in the page head that runs before your app's JavaScript. ## Save the choice in a cookie Laravel can't read `localStorage`, but it can read cookies. So save the user's choice in an `appearance` cookie and share it with your main Blade view using a small middleware: ```php // app/Http/Middleware/HandleAppearance.php final class HandleAppearance { public function handle(Request $request, Closure $next): Response { View::share('appearance', $request->cookie('appearance') ?? 'system'); return $next($request); } } ``` Add it to the web middleware group. Your frontend writes this cookie directly, so tell Laravel not to encrypt it, otherwise Laravel can't read it back: ```php // bootstrap/app.php ->withMiddleware(function (Middleware $middleware) { $middleware->encryptCookies(except: ['appearance']); $middleware->web(append: [ HandleAppearance::class, ]); }) ``` ## Set the theme before the page is drawn In `resources/views/app.blade.php`, add the `dark` class on the server when the user picked dark. For "system", only the browser knows the device setting, so add a few lines of JavaScript. The important part: put this script **above** your `@vite` line, so it runs before your app loads. ```blade ($appearance ?? 'system') === 'dark'])> @vite(['resources/js/app.ts']) ``` The small `