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-script

First time? Installation shows the one-off components.json setup (the @cruiseui registry) and how to turn the theme on.

Example

Corners

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.

tsx
// 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>
  );
}

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

PropTypeDefaultDescription
storageKey?stringCRUISE_THEME_KEYChange 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.