Skip to content
Sveltier
Mode
API

ThemeProvider

Registers themes, writes their stylesheet, remembers the user's choice and scopes a theme to its subtree.

Import

Import
import { ThemeProvider, type ThemeProviderProps } from 'sveltier';

Props

NameTypeDefaultDescription
theme'system' | string'system'The theme to show: a registered theme name, or "system" to follow prefers-color-scheme. Bindable. Nested providers inherit their parent’s choice when unset.
themesRecord<string, ThemeDefinition>–Required at the root, where it registers the themes, for example monolith or { ...monolith, ...maison }. There is no default, so an app ships only the packs it registers.
lightstringfirst light themeRoot only. Theme used by "system" when the OS prefers light, and by toggle().
darkstringfirst dark themeRoot only. Theme used by "system" when the OS prefers dark, and by toggle().
storageKeystring | false'sv-theme'Root only. localStorage key that remembers the choice and restores it before first paint. false turns persistence off.
noncestring–Root only. CSP nonce added to the generated <style> and <script>.
withBackgroundbooleanfalseNested only. Paints the scope with the theme’s background colour.
refHTMLDivElement | nullnullNested only. Bindable reference to the wrapper element.
childrenSnippet–The themed content.
...restHTMLAttributes<HTMLDivElement>–Nested only. Spread on the wrapper element; class is merged.

Root and nested

The first ThemeProvider in the tree is the root. It renders no element, puts the theme stylesheet (and the restore script) in <head> and marks <html data-sv-theme>. Providers below it render a wrapper element with their own data-sv-theme and inherit the root's themes.

Root
<!-- src/routes/+layout.svelte -->
<script>
  import { UIProvider } from 'sveltier';
  import { monolith } from 'sveltier/themes/monolith';
  let { children } = $props();
</script>

<!-- Monolith, light or dark following the OS -->
<UIProvider themes={monolith}>
  {@render children()}
</UIProvider>
Nested
<UIProvider themes={monolith}>
  <p>The page follows the user's choice.</p>

  <ThemeProvider theme="monolith-dark" withBackground class="card">
    <p>This card is always dark.</p>
  </ThemeProvider>
</UIProvider>

Context

Every provider exposes itself to its descendants. Read it with useTheme().