Theme script
Theme script is a neobrutalist React component from Cruise UI. A tiny inline script for the root layout's <head> that applies the saved theme choice (localStorage "cruise-theme": "light", "dark" or "system"; nothing saved counts as system) by setting .nepa-mode on <html> before the first paint, so dark mode persists with no light flash. Install it with npx shadcn@latest add @cruiseui/cruise-theme-script.
Install
Terminal
npx shadcn@latest add @cruiseui/cruise-theme-scriptFirst time? Installation shows the one-off components.json setup (the @cruiseui registry) and how to turn the theme on.
Example
Put it in the root layout's <head>. On every page load it applies the saved choice (or the system setting) before anything is drawn, so a dark-mode visitor never sees a light flash. Pair it with the Theme toggle.
// app/layout.tsx
import { CruiseThemeScript } from "@/components/cruise/theme-script";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
// suppressHydrationWarning: the script sets .nepa-mode before React hydrates.
<html lang="en" suppressHydrationWarning>
<head>
<CruiseThemeScript />
</head>
<body className="cruise">{children}</body>
</html>
);
}import { CruiseCodeBlock } from "@/components/cruise/code-block";
// The script has no visible output, so the example shows where it goes.
const LAYOUT = `// app/layout.tsx
import { CruiseThemeScript } from "@/components/cruise/theme-script";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
// suppressHydrationWarning: the script sets .nepa-mode before React hydrates.
<html lang="en" suppressHydrationWarning>
<head>
<CruiseThemeScript />
</head>
<body className="cruise">{children}</body>
</html>
);
}`;
export default function ThemeScriptExample() {
return (
<div className="flex flex-col gap-4">
<p className="max-w-(--measure)">
Put it in the root layout's <code><head></code>. On every page load it applies the saved choice (or the
system setting) before anything is drawn, so a dark-mode visitor never sees a light flash. Pair it with the Theme
toggle.
</p>
<CruiseCodeBlock code={LAYOUT} language="tsx" />
</div>
);
}
<!-- In <head>, before your stylesheet: applies the saved dark mode choice (or the system setting) before the first paint -->
<script>(function(){var d=document.documentElement,k="cruise-theme",m=window.matchMedia?window.matchMedia("(prefers-color-scheme: dark)"):null;function g(){try{return localStorage.getItem(k)}catch(e){return null}}function a(){var s=g();d.classList.toggle("nepa-mode",s==="dark"||(s!=="light"&&!!m&&m.matches))}a();if(m&&m.addEventListener)m.addEventListener("change",function(){var s=g();if(s!=="dark"&&s!=="light")a()})})();</script>
<!-- The toggle saves "light", "dark" or "system" under the same key -->
<button class="brutal btn btn-secondary" type="button" aria-label="Toggle dark mode" aria-pressed="false"
onclick="var on=document.documentElement.classList.toggle('nepa-mode');this.setAttribute('aria-pressed',on);this.querySelector('span').textContent=on?'Light mode':'Dark mode';try{localStorage.setItem('cruise-theme',on?'dark':'light')}catch(e){}">
<span>Dark mode</span>
</button>Needs theme.css. Interactive parts (menus, tabs, drawers) also need the script in the spec for that component.
When to use it
When to use
Any app with a dark mode toggle: put it in the root layout once.
When not to
Apps that force one mode (set or leave out .nepa-mode yourself).
Props
CruiseThemeScript
| Prop | Type | Default | Description |
|---|---|---|---|
storageKey? | string | CRUISE_THEME_KEY | Change only if CruiseThemeToggle uses the same key. |
nonce? | string | — | Your Content Security Policy nonce, if inline scripts need one. |
Usage
- Next.js app/layout.tsx: `<html lang="en" suppressHydrationWarning><head><CruiseThemeScript /></head><body className="cruise">…`. suppressHydrationWarning is needed because the class changes before React hydrates.
- It reads localStorage inside try/catch, so blocked storage (private mode) never breaks the page. "light" and "dark" are applied as saved; "system" (or nothing saved) follows prefers-color-scheme, and keeps following it live when the device switches.
- Pair it with CruiseThemeToggle (or the Dashboard layout's toggle), which saves the choice under the same key. `storageKey` changes the key on both.
- `nonce` for a Content Security Policy. For plain HTML, `cruiseThemeScript()` returns the source for an inline <script> in <head>.
Accessibility
- Follows the system's dark mode setting until the person picks one.
- No visible output.