# Light / dark theme (1.11.1) Theme logic from the plugin, the look in the project. The plugin provides: a pre-paint script (no flash of the light theme), an unstyled switcher, the `useTheme` hook, fields in Site settings → Theme, and a Turnstile widget theme that matches the page. ## How it works - Pages are ISR/SSG — one HTML for everyone, so the theme does **not** go into the server HTML. - `` (an inline script, the first child of ``) sets `data-theme="light|dark"` and `color-scheme` on `` before the first paint. - Order: the visitor's choice from `localStorage` (key `theme`, only when "Allow theme toggle" is on) → Site settings → Default theme → with `System`, the device setting (`prefers-color-scheme`). - `localStorage`, not a cookie: it is an interface preference the visitor chose themselves, it does not track and needs no separate consent (CNIL treats such a preference as strictly necessary). The cookie scanner declares it as **necessary**, local storage — an editor can change that on review. - A change in one tab carries over to the others (the `storage` event). ## The storage key (`theme`) | | | |---|---| | Default key | `theme`, values `light` / `dark` — the key and values next-themes uses (`THEME_STORAGE_KEY`) | | Own key | `ipalKit({ theme: { storageKey: 'acme-theme' } })` → `getSiteShell(locale).theme.storageKey` → `` uses it and puts it on ``, so `useTheme` / `` use the same key with no extra prop (`` / `useTheme({ storageKey })` to set it by hand) | | Old key | before this change the key was `ipal-theme` (`LEGACY_THEME_STORAGE_KEY`). When the current key is empty and `ipal-theme` holds `light` / `dark`, `` moves it **before the first paint** (no flash) and removes `ipal-theme`; `useTheme` / `` do the same on pages without the script (`migrateThemeStorage(key)`). A choice already under the current key wins; the old key is removed anyway | | next-themes | a project that also runs next-themes shares the key `theme` with it — one choice for both. To keep them apart, give the plugin its own key (`theme.storageKey`) | | CSP | the inline script runs with the CSP nonce (``, `SiteProviders` passes it); no script hash to update | ## Site settings → Theme | Field | Meaning | |---|---| | `defaultTheme` | `light` · `dark` · `system` (since 1.11.1) | | `allowThemeToggle` | show the switcher; the visitor's choice is remembered | | `themeToggleLabel` | the switcher name for screen readers, e.g. "Tryb ciemny" (localized, since 1.11.1) | `getSiteShell(locale).theme` → `{ defaultTheme, allowToggle, toggleLabel, storageKey? }` (`themeConfigFrom(settings, storageKey?)` from the root entry; `storageKey` only when it is not the default). ## Wiring ```tsx // src/app/(frontend)/[locale]/layout.tsx const shell = await getSiteShell(locale) return ( {/* theme → at the start */}
… ``` Without `SiteProviders`: `import { ThemeScript } from '@intecion/ipal-kit/rsc'` and `` as the first child of ``. `suppressHydrationWarning` on `` is needed — the attribute differs from the server HTML on purpose. ```tsx // switcher ('use client' or in an RSC — it is a client component itself) import { ThemeSwitcher } from '@intecion/ipal-kit/client' (t === 'light' ? : )} // default lucide Sun / Moon /> ``` Both icons render: the default is a two-option toggle with the active one highlighted (the `classNames` above). For one icon that changes with the theme (sun in light, moon in dark), hide the other with the `dark:` variant: ```tsx ``` `