board logo
Posted by: WUICTeam - Tuesday, November 24, 2026 9:02:18 AM
[b]Question[/b] I want to change the theme from my own component (for example a "company theme" button). If I call PrimeNG's preset API directly, the colors come out wrong in dark mode until I switch theme a second time. What is the right way to do it? [b]Answer[/b] Inject [c]ThemeRuntimeService[/c] and call [c]apply()[/c] with a theme option taken from [c]ThemeCatalogService[/c]. Do not call the PrimeNG preset functions yourself. The reason is the order of operations. When PrimeNG installs a preset it computes the light/dark CSS variables based on whether the [c]theme-dark[/c] class is already present. So the class has to be on the document [i]before[/i] the preset is applied. [c]apply()[/c] does it in the right sequence: it toggles [c]theme-dark[/c] on both [c]html[/c] and [c]body[/c], then installs the preset, then updates the primary palette, then sets the accessory classes (high contrast, dense). If you reverse the first two steps, dark stays broken until the next theme change. [code] import { Component, inject } from '@angular/core'; import { ThemeRuntimeService, ThemeCatalogService } from 'wuic-framework-lib'; @Component({ selector: 'app-theme-button', template: '<button (click)="go()">Dark blue</button>' }) export class ThemeButtonComponent { private runtime = inject(ThemeRuntimeService); private catalog = inject(ThemeCatalogService); go() { const option = this.catalog.resolve('aura-blue'); // returns the dark state actually in effect const dark = this.runtime.apply(option, { dark: true }); } } [/code] [b]Things to know[/b] [list] [*][c]apply()[/c] returns the effective dark state: a high-contrast theme or a custom theme with a forced mode imposes its own variant, so do not assume it is the one you asked for. [*]By default the choice is saved in localStorage ([c]wuic-selected-theme[/c] and [c]wuic-theme-mode[/c]). Pass [c]persist: false[/c] for a temporary preview that must not survive a reload. [*][c]resolve()[/c] returns [c]undefined[/c] for an unknown value, and [c]apply(undefined)[/c] does nothing except reset high contrast, so check the result if the value comes from user input. [*]For dark mode alone, [c]setDark(true)[/c] toggles the class without touching the preset. [/list] [url=https://wuic-framework.com/docs/themes]Documentation: PrimeNG + Dark Themes[/url]