- docs, AGENT.md, the 16 skills, PROGRAMISTA and README use the preferred names (scripted, same list of forms); past CHANGELOG entries and the proposals in docs/dev are not rewritten - AGENT.md: 'Names: IAPL and ipal-kit' — old name, new name, state in 1.x; what is not renamed; storageNames - iapl-upgrade '→ 1.17.0', PROGRAMISTA §6 item 72, CHANGELOG 1.17.0 (draft): nothing required - env.md, doctor.md (legacy-names, storage-names), stats.md, security.md, project-starter.md, iapl-reference, iapl-setup - skill scripts read ipal-audit-skip and IPAL_IP_HEADER too; audit rule ids stay what they were - reference site: iapl(), iapl-doctor.json, iapl-* skills, storageNames: 'iapl'; INDEX.md regenerated Co-Authored-By: Claude Fable 5.2 <[email protected]>
123 lines
6.9 KiB
Markdown
123 lines
6.9 KiB
Markdown
# 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>`) sets `data-theme="light|dark"` and
|
|
`color-scheme` on `<html>` 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 | `iapl({ theme: { storageKey: 'acme-theme' } })` → `getSiteShell(locale).theme.storageKey` → `<ThemeScript>` uses it and puts it on `<html data-theme-storage>`, so `useTheme` / `<ThemeSwitcher>` use the same key with no extra prop (`<ThemeSwitcher storageKey>` / `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`, `<ThemeScript>` moves it **before the first paint** (no flash) and removes `ipal-theme`; `useTheme` / `<ThemeSwitcher>` 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 (`<ThemeScript 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 (
|
|
<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.
|
|
|
|
```tsx
|
|
// 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
|
|
/>
|
|
```
|
|
|
|
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
|
|
<ThemeSwitcher label={themeLabel} classNames={{ light: 'block dark:hidden', dark: 'hidden dark:block' }} />
|
|
```
|
|
|
|
`<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({ storageKey? })` → `{ theme, setTheme, toggle }` (`theme` = `null` on the server);
|
|
`applyTheme(theme, { persist, storageKey, transitionMs })`, `readDocumentTheme()`, `documentThemeStorageKey()`,
|
|
`migrateThemeStorage(key)`, the constants `THEME_ATTRIBUTE`, `THEME_STORAGE_KEY`, `LEGACY_THEME_STORAGE_KEY`,
|
|
`THEME_STORAGE_ATTRIBUTE`, `THEME_SWITCHING_ATTRIBUTE`, `isTheme` from `/client`. From the root entry also
|
|
`isThemeSetting` and `themeScript({ allowToggle, defaultTheme, storageKey })` (the script text only), and the
|
|
type `ThemeOptions` (`iapl({ theme })`).
|
|
|
|
## CSS (Tailwind 4)
|
|
|
|
With the design-token standard ([design-tokens.md](./design-tokens.md)) this is already in
|
|
`@intecion/ipal-kit/tailwind.css`, and the colour roles switch on `data-theme` by themselves (no `dark:` for colours).
|
|
Without the preset, add it yourself:
|
|
|
|
```css
|
|
@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 values: `src/design/tokens.ts` → `color.dark` (design-tokens.md); without the standard, 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);
|
|
`allowThemeToggle` off = the visitor's choice is ignored. The *Theme* tab in Site settings is
|
|
currently always added.
|
|
- **Override:** `ThemeScript` takes `allowToggle` / `defaultTheme` directly (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) — `iapl({ siteSettingsFields })`, they go to the
|
|
*More* tab.
|