Skip to content
⭐ Loving ngx-theme-stack? Support us with a star on GitHub!

API Reference

ngx-theme-stack provides a powerful and flexible API designed around Angular Signals for maximum reactivity.

The brain of the library. It handles theme persistence, system preference detection, and DOM updates.

Signal / PropTypeDescription
selectedThemeSignal<NgTheme>The theme explicitly selected by the user (can be 'system').
resolvedThemeSignal<NgTheme>The actual theme applied to the DOM (resolves 'system' to 'light' or 'dark'). Never 'system'.
isDarkSignal<boolean>true if the resolved theme is 'dark'. false for custom themes (e.g. 'sunset').
isLightSignal<boolean>true if the resolved theme is 'light'. false for custom themes.
isSystemSignal<boolean>true if the selected theme is 'system'.
isHydratedSignal<boolean>true after client-side hydration. Crucial for avoiding SSR flicker.
availableThemesNgTheme[]The full resolved list of themes (built-ins + custom, post-merge).
  • setTheme(theme: NgTheme): void: Changes the active theme and persists the choice. Throws an NgxThemeStackError if the theme is not in the valid themes list.

These services internally inject CoreThemeService and expose all of its state signals so you don’t have to inject both services.

  • selectedTheme, resolvedTheme, isDark, isLight, isSystem, isHydrated.

Ideal for simple switches.

  • toggle(): Toggles between 'dark' and 'light'.

Ideal for buttons that rotate through all available themes.

  • cycle(): Advances to the next theme in the themes array.
  • availableThemes: Array of themes configured for the cycle.
  • cycleIndex: Signal<number> - Index of the current theme.
  • preceding: Signal<NgTheme> - The previous theme.
  • upcoming: Signal<NgTheme> - The next theme.

Ideal for dropdown menus.

  • select(theme: NgTheme): Sets the active theme.
  • availableThemes: Array of all configured themes.

provideThemeStack(config?: Partial<NgConfig>)

Section titled “provideThemeStack(config?: Partial<NgConfig>)”

Initializes the library. Custom themes passed in config.themes are automatically merged with the default themes (['light', 'dark', 'system' ]).

OptionTypeDefaultDescription
defaultThemeNgTheme'system'Theme used on first visit.
storageKeystring'ngx-theme-stack'Key for localStorage.
modeNgMode'class'How to apply theme to <html>: 'class', 'attribute' or 'both'.
strategyNgStrategy'critters'How theme CSS is delivered: 'critters' inlines vars in <head> (zero requests); 'blocking' loads themes.css as a render-blocking stylesheet (HTTP-cacheable).
themesNgTheme[]['light', 'dark', 'system']Custom themes to add. Merged with built-ins — passing ['sepia'] resolves to ['system', 'light', 'dark', 'sepia'].

A type-safe union. If used with as const in the configuration, it provides exact autocomplete.

  • 'class': Adds the theme name as a class to <html>.
  • 'attribute': Sets data-theme="name" on <html>.
  • 'both': Applies both.
  • 'critters' (default): Inlines all theme CSS variables directly in <head> at build time via Angular’s Critters optimizer. Zero network requests. Works with CSR, SSR, and SSG.
  • 'blocking': Loads themes.css as a render-blocking external stylesheet. One network request (then HTTP-cached). Required for strict CSP environments.