Skip to content
Sveltier
Mode
API

useTheme

Returns the nearest ThemeProvider. Every field is reactive.

Signature

Signature
import { useTheme, type ThemeInstance } from 'sveltier';

function useTheme(): ThemeInstance;

Call it during component initialisation, inside a ThemeProvider. Outside one it throws [sveltier] useTheme() must be called inside a <ThemeProvider>.

Returns

NameTypeDescription
choice'system' | stringThe current choice.
namestringThe theme in effect. With "system" it follows the OS; on the server it is the light theme.
currentThemeDefinitionThe definition of the theme in effect.
isDarkbooleanWhether the theme in effect is dark.
themesReadonly<Record<string, ThemeDefinition>>All registered themes.
light / darkstringThe pair used by "system" and toggle().
set(choice)(choice: 'system' | string) => voidSelects a theme or "system". The root provider remembers it. Throws for unknown names.
toggle()() => voidSwitches between the light and dark theme.

Example

Theme toggle
<script>
  import { useTheme } from 'sveltier';
  const theme = useTheme();
</script>

<button type="button" onclick={() => theme.toggle()}>
  {theme.isDark ? 'Light' : 'Dark'}
</button>