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

Toggle

The ThemeToggleService is the simplest utility for implementing a theme switch (Light/Dark) in your application.

Inject the service and use the toggle() method to switch between themes.

import { Component, inject } from "@angular/core";
import { ThemeToggleService } from "ngx-theme-stack";
@Component({
selector: "app-theme-toggle",
template: `
<button (click)="theme.toggle()">
{{ theme.isDark() ? "🌙" : "☀️" }}
</button>
`,
})
export class ThemeToggle {
protected readonly theme = inject(ThemeToggleService);
}

The ThemeToggleService provides everything needed to manage a switch:

SignalTypeDescription
selectedThemeSignal<NgTheme>The theme chosen by the user (can be 'system').
resolvedThemeSignal<NgTheme>The actual theme applied (never 'system').
isDarkSignal<boolean>true if the resolved theme is dark.
isLightSignal<boolean>true if the resolved theme is light.
isSystemSignal<boolean>true if the user has chosen the system preference.
isHydratedSignal<boolean>true after client-side hydration.
  • toggle(): Switches between 'dark' and 'light'.

For more details, see the ThemeToggleService API Reference.