- buildRevalidateHook skips outside Next (no NEXT_RUNTIME) and with context.disableRevalidate - BuilderForm focuses only on a real step change (no scroll to the form on remount) - mediaImageProps: placeholder colour only for JPEG (placeholderColor); hook stores no colour for transparent images - buildLinkField: interface name per shape (CmsLink, CmsLinkPlain, CmsLinkNoLabel) + interfaceName option - Cookie Settings: buttons.save -> buttons.savePreferences (Mongoose reserved name) - ThemeScript, ThemeSwitcher, useTheme; Site Settings defaultTheme 'system' + themeToggleLabel; Turnstile follows page theme - extractHeadings, RenderRichText headingIds, TableOfContents, buildTableOfContentsField - createPageRoute onMissing: 'redirectHome' - Form builder: localized placeholder on text/email/textarea/number, new time field - ipal-audit: no false positive for object keys between JSX elements; /md route not reported as dynamic Co-Authored-By: Claude Opus 5.5 <[email protected]>
61 lines
3.0 KiB
TypeScript
61 lines
3.0 KiB
TypeScript
/**
|
|
* Light / dark theme — shared names and the pre-paint script. Pure: safe on the
|
|
* server and in the browser.
|
|
*
|
|
* The theme never reaches the server HTML (pages are ISR/SSG, one HTML for
|
|
* everyone): <ThemeScript> sets `data-theme` on <html> before the first paint,
|
|
* from the visitor's choice (localStorage — an interface preference, no cookie,
|
|
* no tracking, no consent needed) or Site Settings → Theme → Default theme.
|
|
*/
|
|
|
|
export type Theme = 'dark' | 'light'
|
|
/** Site Settings → Default theme. 'system' follows the device (prefers-color-scheme). */
|
|
export type ThemeSetting = 'system' | Theme
|
|
|
|
/** localStorage key of the visitor's choice. */
|
|
export const THEME_STORAGE_KEY = 'ipal-theme'
|
|
/** Attribute on <html>: `[data-theme=dark]`, Tailwind `@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *))`. */
|
|
export const THEME_ATTRIBUTE = 'data-theme'
|
|
/** Set on <html> for a moment while switching — for a colour transition only then (`[data-theme-switching] * { transition: … }`). */
|
|
export const THEME_SWITCHING_ATTRIBUTE = 'data-theme-switching'
|
|
|
|
export const isTheme = (value: unknown): value is Theme => value === 'light' || value === 'dark'
|
|
export const isThemeSetting = (value: unknown): value is ThemeSetting => value === 'system' || isTheme(value)
|
|
|
|
/** Theme options from Site Settings — what getSiteShell(locale).theme holds. */
|
|
export type ThemeConfig = {
|
|
/** Visitors may switch (and their choice is remembered). */
|
|
allowToggle: boolean
|
|
/** null: no Site Settings — no attribute, the base (light) CSS applies. */
|
|
defaultTheme: null | ThemeSetting
|
|
/** Accessible name of the switch, e.g. "Dark mode" (Site Settings → Theme). */
|
|
toggleLabel: null | string
|
|
}
|
|
|
|
/** Reads Theme options from a Site Settings document. */
|
|
export function themeConfigFrom(settings: unknown): ThemeConfig {
|
|
const s = (settings && typeof settings === 'object' ? settings : {}) as Record<string, unknown>
|
|
const label = typeof s.themeToggleLabel === 'string' && s.themeToggleLabel.trim() ? s.themeToggleLabel.trim() : null
|
|
return {
|
|
allowToggle: s.allowThemeToggle === true,
|
|
defaultTheme: isThemeSetting(s.defaultTheme) ? s.defaultTheme : null,
|
|
toggleLabel: label,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The inline script for <ThemeScript>: stored choice (only when the toggle is
|
|
* allowed) → default theme → for 'system' the device setting. Sets
|
|
* `data-theme` and `color-scheme` (native controls, scrollbars) on <html>.
|
|
*/
|
|
export function themeScript({ allowToggle, defaultTheme }: Pick<ThemeConfig, 'allowToggle' | 'defaultTheme'>): string {
|
|
const stored = allowToggle
|
|
? `try{var s=localStorage.getItem(${JSON.stringify(THEME_STORAGE_KEY)});if(s==='light'||s==='dark')t=s}catch(e){}`
|
|
: ''
|
|
return (
|
|
`(function(){var d=document.documentElement,t=${JSON.stringify(defaultTheme ?? null)};${stored}` +
|
|
`if(t==='system')t=window.matchMedia&&window.matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light';` +
|
|
`if(t==='light'||t==='dark'){d.setAttribute(${JSON.stringify(THEME_ATTRIBUTE)},t);d.style.colorScheme=t}})()`
|
|
)
|
|
}
|