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,isValidThemeandfontStatefrom@davidnet-net/svelte-ui.
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 need | Use |
|---|---|
| Change the theme | setTheme |
| Mark the choice in a picker | currentTheme.themeName |
| Pick something per active theme, like an image | currentTheme.themeObject |
| Wait until the icon fonts are there | fontState |
setTheme
type themeNames = "light" | "dark" | "system" | "contrast";
function setTheme(themeName: themeNames): void
function isValidTheme(themeName: any): themeName is themeNamessetTheme(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
theme_cache= You are signed out: the cookie is all there is, and the only place where the choice is kept.| Call | Result |
|---|---|
| isValidTheme("dark") | true |
| isValidTheme("pink") | false |
import { setTheme } from "@davidnet-net/svelte-ui";
setTheme("dark");
setTheme("system");currentTheme
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
| Call | Result |
|---|---|
| currentTheme.themeName | light |
| currentTheme.themeObject === darkTheme | false |
const dark = $derived(currentTheme.themeObject === darkTheme);fontState
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
| Call | Result |
|---|---|
| fontState.iconFontsLoaded | false |
| fontState.interFontLoaded | false |
| fontState.momoTrustDisplayFontLoaded | false |
{#if fontState.iconFontsLoaded}<Icon icon="check" />{/if}