Theme engine

Read and change the theme of the app, and see when the fonts are ready.

What it does
Holds the theme the person chose (light, dark or system), and tells when the fonts are loaded.
Exported from the package?
Yes. Import setTheme, currentTheme, isValidTheme and fontState from @davidnet-net/svelte-ui.
Where the theme is kept
setTheme stores the choice in the theme_cache cookie on .davidnet.net, so every Davidnet app reads it, signed in or not. The cookie is set without an expiry date. setTheme does not save to the account: the settings screen of AppShell does. Signed in, AppShell applies the theme of the account when the session is found, so the account wins over the cookie at the start. Signed out, the cookie (or system) is all there is.

Which one do I use?

What I needUse
Change the themesetTheme
Mark the choice in a pickercurrentTheme.themeName
Pick something per active theme, like an imagecurrentTheme.themeObject
Wait until the icon fonts are therefontState

setTheme

typescript
typescript
type themeNames = "light" | "dark" | "system" | "contrast";

function setTheme(themeName: themeNames): void
function isValidTheme(themeName: any): themeName is themeNames

setTheme(name) sets the theme to "light", "dark" or "system" (which follows the system), and stores it in the theme_cache cookie. "contrast" is accepted but not built yet: it logs a warning and uses the dark theme. isValidTheme(value) checks an unknown value, for instance from a cookie or a profile.

When to use

Use it in a theme picker of a settings screen. AppShell already sets the theme from the cookie and the profile on start.

When not to use, and what to use instead

Do not set colors yourself for a theme: use the design tokens, they follow the theme. Do not use setTheme to preview a theme in one component.

Live example

Checking the session…
Result Chosen: light Active theme: light Cookie: theme_cache= You are signed out: the cookie is all there is, and the only place where the choice is kept.
CallResult
isValidTheme("dark")true
isValidTheme("pink")false
typescript
typescript
import { setTheme } from "@davidnet-net/svelte-ui";

setTheme("dark");
setTheme("system");

currentTheme

typescript
typescript
const currentTheme: {
	readonly themeName: themeNames;
	readonly themeObject: typeof lightTheme | typeof darkTheme;
}

currentTheme has themeName (what was chosen, so possibly "system") and themeObject (the theme that is active, which is the light or the dark one). Both are reactive.

When to use

Use themeName to mark the choice in a picker, and themeObject when you need the active theme in code, for instance to pick an image per theme.

When not to use, and what to use instead

For colors in styles use the design tokens: they already follow the theme and need no code.

Live example

CallResult
currentTheme.themeNamelight
currentTheme.themeObject === darkThemefalse
typescript
typescript
const dark = $derived(currentTheme.themeObject === darkTheme);

fontState

typescript
typescript
const fontState: {
	iconFontsLoaded: boolean;
	interFontLoaded: boolean;
	momoTrustDisplayFontLoaded: boolean;
}

fontState tells whether the icon fonts, the Inter font and the display font are loaded (iconFontsLoaded, interFontLoaded, momoTrustDisplayFontLoaded). It is reactive, and set when the fonts have loaded, after a few retries.

When to use

Use it to hide an icon until its font is there, so people do not see the name of the icon as text.

When not to use, and what to use instead

Do not wait for the fonts to show text: show it with the fallback font. Only icons need the check, because the name of an icon would show as text.

Live example

CallResult
fontState.iconFontsLoadedfalse
fontState.interFontLoadedfalse
fontState.momoTrustDisplayFontLoadedfalse
svelte
svelte
{#if fontState.iconFontsLoaded}<Icon icon="check" />{/if}