4.6 KiB
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.
<ThemeScript>(an inline script, the first child of<body>) setsdata-theme="light|dark"andcolor-schemeon<html>before the first paint.- Order: the visitor's choice from
localStorage(keyipal-theme, only when "Allow theme toggle" is on) → Site settings → Default theme → withSystem, the device setting (prefers-color-scheme). localStorage, not a cookie: it is an interface preference, it does not track and needs no consent.- A change in one tab carries over to the others (the
storageevent).
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 } (themeConfigFrom(settings) from the root entry).
Wiring
// src/app/(frontend)/[locale]/layout.tsx
const shell = await getSiteShell(locale)
return (
<html lang={locale} suppressHydrationWarning>
<body>
<SiteProviders shell={shell} theme> {/* theme → <ThemeScript> at the start */}
<Header themeLabel={shell.theme.allowToggle ? shell.theme.toggleLabel : null} … />
…
Without SiteProviders: import { ThemeScript } from '@intecion/ipal-kit/rsc' and <ThemeScript {...shell.theme} />
as the first child of <body>. suppressHydrationWarning on <html> is needed — the attribute differs
from the server HTML on purpose.
// switcher ('use client' or in an RSC — it is a client component itself)
import { ThemeSwitcher } from '@intecion/ipal-kit/client'
<ThemeSwitcher
label={themeLabel}
classNames={{
root: 'inline-flex rounded-full border p-0.5',
light: 'rounded-full p-2 bg-white dark:bg-transparent',
dark: 'rounded-full p-2 dark:bg-surface-raised',
}}
// renderIcon={(t) => (t === 'light' ? <SunIcon /> : <MoonIcon />)} // default lucide Sun / Moon
/>
<button role="switch" aria-checked> (checked = dark). Which button looks active is decided by CSS
from the attribute on <html> (dark:), not React state — nothing jumps after hydration.
Hooks for styling: data-ipal="theme-switch", data-ipal-theme-icon="light|dark".
Custom switcher: useTheme() → { theme, setTheme, toggle } (theme = null on the server);
applyTheme(theme, { persist, transitionMs }), readDocumentTheme(), the constants THEME_ATTRIBUTE,
THEME_STORAGE_KEY, THEME_SWITCHING_ATTRIBUTE, isTheme from /client. From the root entry also
isThemeSetting and themeScript({ allowToggle, defaultTheme }) (the script text only).
CSS (Tailwind 4)
@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
/* smooth color transition only at the moment of switching (applyTheme: data-theme-switching, 300 ms) */
[data-theme-switching] *, [data-theme-switching] *::before, [data-theme-switching] *::after {
transition: background-color 0.3s, border-color 0.3s, color 0.3s, fill 0.3s !important;
}
Dark theme tokens (surface, fg…) stay in the project's styles.css.
Turnstile
BuilderForm / RenderForm → turnstileTheme defaults to 'page': the widget takes the page theme
(data-theme); with no page theme — 'auto' (device theme), as before 1.11.1. A theme change
redraws the widget (the token must be obtained again). A fixed theme: turnstileTheme="light".
Turn off, override, extend
- Turn off: without
SiteProviders theme/<ThemeScript>no theme is set (base CSS);allowThemeToggleoff = the visitor's choice is ignored. The Theme tab in Site settings is currently always added. - Override:
ThemeScripttakesallowToggle/defaultThemedirectly (it does not have to read from the admin panel);ThemeSwitcher—classNames,label,renderIcon;applyTheme(..., { persist, transitionMs }). - Extend: your own switcher on
useTheme();themeScript()as script text for your own tag;themeConfigFrom(settings)to read the settings. Your own fields in Site settings (e.g. extra tokens) —ipalKit({ siteSettingsFields }), they go to the Custom tab.