ngx-theme-stack 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,7 +12,7 @@ A simple and powerful headless theme manager for **Angular**. Built for performa
12
12
  - 🛠️ **Highly Customizable**: Support for custom themes, class prefixes, and configurable storage.
13
13
  - 🧱 **Modern Architecture**: Powered by Angular Signals for maximum reactivity and performance.
14
14
  - 🌍 **SSR Ready**: Safe to use in Server-Side Rendering environments.
15
- - 🚫 **Zero Flicker**: Includes an optimized anti-flash script to prevent theme jumps on load.
15
+ - 🚫 **Zero Flicker**: Includes an optimized anti-flash script and the **Critters Trick** strategy to prevent theme jumps and network requests on load.
16
16
 
17
17
  ## 📦 Installation
18
18
 
@@ -31,12 +31,25 @@ When running `ng add`, you will be presented with two configuration options:
31
31
  - Initial theme: `system`.
32
32
  - Apply mode: `class` (adds the theme class to the `<html>` element).
33
33
  - Available themes: `['light', 'dark', 'system']`.
34
+ - **Strategy**: `critters` (Zero-flash via CSS inlining).
34
35
 
35
36
  2. **Custom Mode**:
36
37
  - Choose which themes to include (e.g., if you have a `blue` or `high-contrast` theme).
37
38
  - Configure the default theme upon app startup.
38
39
  - Change the `localStorage` key where the theme choice is saved.
39
40
  - Decide how to apply themes: via classes (`class`), attributes (`data-theme`), or both.
41
+ - **Pick your strategy**: `critters` for modern SSR/SSG apps or `blocking` for standard CSS loading.
42
+
43
+ ## 🤖 What does `ng add` do for you?
44
+
45
+ To provide a "Zero Config" experience, the installation command automates the following:
46
+
47
+ 1. **`package.json`**: Adds a `"prebuild"` script that executes the synchronization automatically before every build.
48
+ 2. **`angular.json`**:
49
+ - Adds `src/themes.css` to the global styles list.
50
+ - Configures the `inlineCritical` optimization based on your selected strategy.
51
+ 3. **`index.html`**: Injects the marker and the blocking anti-flash script into the `<head>`.
52
+ 4. **`themes.css`**: Creates a base file with selectors ready for you to define your variables.
40
53
 
41
54
  ## 🏗️ Architecture & Extensibility
42
55
 
@@ -64,6 +77,37 @@ For common use cases, we include three services with predefined logic:
64
77
  | **Angular 19** | ✅ Stable |
65
78
  | **Angular 18** | ✅ Stable |
66
79
 
80
+ ## ⚙️ Configuration
81
+
82
+ The best way to configure the library is during installation, but you can also manually adjust the providers in your `app.config.ts`:
83
+
84
+ ```typescript
85
+ import { provideThemeStack } from 'ngx-theme-stack';
86
+
87
+ export const appConfig: ApplicationConfig = {
88
+ providers: [
89
+ provideThemeStack({
90
+ themes: ['light', 'dark', 'sunset'], // Your theme identifiers
91
+ defaultTheme: 'system', // Initial fallback ('system' resolves via matchMedia)
92
+ mode: 'class', // 'class', 'attribute' or 'both'
93
+ strategy: 'critters', // 'critters' (SSR) or 'blocking' (Standard SPA)
94
+ storageKey: 'ngx-theme-stack-theme' // LocalStorage key
95
+ })
96
+ ]
97
+ };
98
+ ```
99
+
100
+ | Option | Type | Default | Description |
101
+ | :--- | :--- | :--- | :--- |
102
+ | `themes` | `string[]` | `['light', 'dark', 'system']` | List of supported theme identifiers. |
103
+ | `defaultTheme` | `string` | `'system'` | Theme used on first visit or when no preference is saved. |
104
+ | `mode` | `NgMode` | `'class'` | How the theme is applied: `class`, `attribute` (`data-theme`), or `both`. |
105
+ | `strategy` | `NgStrategy`| `'critters'` | Anti-flash performance strategy: `critters` (inlined CSS) or `blocking`. |
106
+ | `storageKey` | `string` | `'ngx-theme-stack-theme'` | Key used to persist theme preference in `localStorage`. |
107
+
108
+ > [!IMPORTANT]
109
+ > Whenever you update these settings, run `ng generate ngx-theme-stack:sync` to ensure your `index.html` is updated with the correct anti-flash script.
110
+
67
111
  ## 🛠️ Basic Usage
68
112
 
69
113
  ### CoreThemeService API
@@ -123,24 +167,101 @@ export class ThemeToggleComponent {
123
167
 
124
168
  ## 🎨 Styling
125
169
 
126
- By default, the library adds the theme name as a class or attribute to the `<html>` element. Use this in your global styles:
170
+ The `ng add` command automatically creates a **`src/themes.css`** file in your project. This is where you should define your theme-specific CSS variables.
171
+
172
+ The library targets the `<html>` element. Based on your configured `mode`, you should define your variables like this:
127
173
 
128
174
  ```css
129
- /* Using Classes (Default) */
130
- html.dark {
131
- background-color: #121212;
132
- color: white;
175
+ /* src/themes.css */
176
+
177
+ /* Using Classes (Default Mode) */
178
+ :root,
179
+ .light {
180
+ --bg-color: #ffffff;
181
+ --text-color: #333333;
133
182
  }
134
183
 
135
- html.light {
136
- background-color: #ffffff;
137
- color: #333;
184
+ .dark {
185
+ --bg-color: #121212;
186
+ --text-color: #ffffff;
138
187
  }
139
188
 
140
- /* Using Attributes */
141
- [data-theme='blue'] {
142
- --primary-color: #0000ff;
189
+ .sunset {
190
+ --bg-color: #ff5f6d;
191
+ --text-color: #ffffff;
143
192
  }
193
+
194
+ /* Using Attributes (Attribute Mode) */
195
+ [data-theme='light'] {
196
+ --bg-color: #ffffff;
197
+ --text-color: #333333;
198
+ }
199
+
200
+ [data-theme='dark'] {
201
+ --bg-color: #121212;
202
+ --text-color: #ffffff;
203
+ }
204
+
205
+ [data-theme='sunset'] {
206
+ --bg-color: #ff5f6d;
207
+ --text-color: #ffffff;
208
+ }
209
+ ```
210
+
211
+ ## 🌪️ Tailwind CSS v4 Integration
212
+
213
+ If you are using **Tailwind CSS v4**, you can achieve a much cleaner HTML by mapping your `themes.css` variables to your Tailwind theme. This avoids cluttering your components with `dark:` variants.
214
+
215
+ ### 1. Configure Custom Variants
216
+
217
+ In your main `styles.css`, define how Tailwind should detect your themes:
218
+
219
+ ```css
220
+ /* src/styles.css */
221
+ @import 'tailwindcss';
222
+
223
+ /* If using Class mode */
224
+ @custom-variant dark (&:where(.dark, .dark *));
225
+
226
+ /* If using Attribute mode */
227
+ @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
228
+ ```
229
+
230
+ ### 2. Map Semantic Variables
231
+
232
+ Extend your Tailwind theme using the variables defined in `themes.css`:
233
+
234
+ ```css
235
+ @theme {
236
+ --color-main-bg: var(--bg-color);
237
+ --color-main-text: var(--text-color);
238
+ --color-card-bg: var(--card-bg);
239
+ }
240
+ ```
241
+
242
+ ### 3. Usage in Components
243
+
244
+ Now, instead of writing `<div class="bg-white dark:bg-black">`, you simply write:
245
+
246
+ ```html
247
+ <div class="bg-main-bg text-main-text shadow-xl">
248
+ <!-- This automatically changes colors based on the active theme -->
249
+ </div>
250
+ ```
251
+
252
+ This approach keeps your UI code clean, semantic, and fully synchronized with `ngx-theme-stack`.
253
+
254
+ ## ⚡ Performance Strategies
255
+
256
+ `ngx-theme-stack` offers two ways to handle the initial theme application to prevent that annoying white flash:
257
+
258
+ 1. **Critters (Default)**: Best for SSR/Static sites. It uses hidden markers to trick the Angular builder into inlining all your theme CSS variables directly in the HTML `<head>`. Result: **Zero network requests for CSS variables.**
259
+ 2. **Blocking**: Best for standard SPAs. It loads the `themes.css` file as a traditional blocking resource.
260
+
261
+ The `ng-add` schematic helps you configure the right one automatically. You can always use the **Sync Command** to refresh your `index.html` if you change your configuration:
262
+
263
+ ```bash
264
+ ng generate ngx-theme-stack:sync --project YOUR_PROJECT_NAME
144
265
  ```
145
266
 
146
267
  ## 📄 License
@@ -58,6 +58,7 @@ const DEFAULT_NG_CONFIG = {
58
58
  defaultTheme: 'system',
59
59
  storageKey: 'ngx-theme-stack-theme',
60
60
  mode: 'class',
61
+ strategy: 'critters',
61
62
  themes: [...DEFAULT_THEMES],
62
63
  };
63
64
  // The token uses NgConfig<string> because Angular DI resolves types at runtime
@@ -71,6 +72,14 @@ const NGX_THEME_STACK_CONFIG = new InjectionToken('NGX_THEME_STACK_CONFIG', {
71
72
  *
72
73
  * Custom `themes` are **merged** with the built-in defaults
73
74
  * (`'light'`, `'dark'`, `'system'`), so you never lose the base themes.
75
+ *
76
+ * **Defaults:**
77
+ * - `themes`: `['light', 'dark', 'system']`
78
+ * - `defaultTheme`: `'system'`
79
+ * - `storageKey`: `'ngx-theme-stack-theme'`
80
+ * - `mode`: `'class'`
81
+ * - `strategy`: `'critters'`
82
+
74
83
  *
75
84
  * The type parameter `T` is **inferred automatically** from the `themes` array
76
85
  * when passed as a `const` — no need to specify it manually.
@@ -90,6 +99,13 @@ const NGX_THEME_STACK_CONFIG = new InjectionToken('NGX_THEME_STACK_CONFIG', {
90
99
  * provideThemeStack()
91
100
  *
92
101
  * @example
102
+ * // SSR/SSG Optimization — uses Critters inlining strategy
103
+ * provideThemeStack({
104
+ * strategy: 'critters',
105
+ * mode: 'class',
106
+ * })
107
+ *
108
+ * @example
93
109
  * // Closed union: TypeScript infers 'sepia' | 'ocean' from the array
94
110
  * provideThemeStack({
95
111
  * themes: ['sepia', 'ocean'] as const,
@@ -101,7 +117,7 @@ const NGX_THEME_STACK_CONFIG = new InjectionToken('NGX_THEME_STACK_CONFIG', {
101
117
  * // Custom storage key and mode
102
118
  * provideThemeStack({
103
119
  * storageKey: 'my-app-theme',
104
- * mode: 'class',
120
+ * mode: 'attribute',
105
121
  * })
106
122
  */
107
123
  function provideThemeStack(config = {}) {
@@ -150,7 +166,11 @@ class CoreThemeService {
150
166
  availableThemes = this.#config.themes;
151
167
  /** Internal Set for O(1) existence checks. */
152
168
  #validThemes = new Set(this.availableThemes);
153
- /** The anti-flash class to remove from the host element. */
169
+ /**
170
+ * The anti-flash class to remove from the host element.
171
+ * Internal mechanism to bridge the gap between the blocking script's
172
+ * initial DOM state and Angular's first effect run.
173
+ */
154
174
  #antiFlashClass = null;
155
175
  // ── System preference ─────────────────────────────────────────────────────
156
176
  /** MediaQueryList for OS color scheme, created once and reused. Null in SSR. */
@@ -176,18 +196,17 @@ class CoreThemeService {
176
196
  /**
177
197
  * Whether the service has completed client-side initialization.
178
198
  *
179
- * `false` during SSR and on the very first render pass. Becomes `true`
180
- * immediately after the first browser render, once the real persisted theme
181
- * has been read from `localStorage`.
199
+ * `false` during SSR and on the very first render pass before the initial theme
200
+ * is resolved from `localStorage`. Becomes `true` immediately after the
201
+ * first browser render pass.
182
202
  *
183
- * Use this to guard any template logic that depends on `selectedTheme` or
184
- * `resolvedTheme` to avoid an SSR hydration-mismatch flash: the server
185
- * renders the default (`'system'`) while the browser may have a different
186
- * value stored.
203
+ * **Important:** Guard template elements that display `selectedTheme()` or
204
+ * `resolvedTheme()` behind this signal to prevent hydration-mismatch flashes
205
+ * (e.g. if the server renders the default 'system' but the user has 'dark' stored).
187
206
  *
188
207
  * @example
189
208
  * ```html
190
- * {{ themeService.isHydrated() ? selectedTheme() : '' }}
209
+ * {{ theme.isHydrated() ? theme.selectedTheme() : '...' }}
191
210
  * ```
192
211
  */
193
212
  isHydrated = signal(false, ...(ngDevMode ? [{ debugName: "isHydrated" }] : /* istanbul ignore next */ []));
@@ -335,21 +354,21 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.7", ngImpor
335
354
  */
336
355
  class ThemeCycleService {
337
356
  #core = inject(CoreThemeService);
338
- /** List of all configured themes for cycling. Defaults to ['light', 'dark', 'system']. */
357
+ /** List of all configured themes for cycling. Defaults to `['light', 'dark', 'system']`. */
339
358
  #cycle = this.#core.availableThemes;
340
359
  /** The theme explicitly selected by the user. May be `'system'`. */
341
360
  selectedTheme = this.#core.selectedTheme;
342
- /** Resolved theme applied to the DOM. Always concrete — never `'system'`. */
361
+ /** Resolved theme currently applied to the DOM. Always concrete — never `'system'`. */
343
362
  resolvedTheme = this.#core.resolvedTheme;
344
- /** Whether the currently applied theme is dark. */
363
+ /** Whether the currently applied theme is `'dark'`. */
345
364
  isDark = this.#core.isDark;
346
- /** Whether the currently applied theme is light. */
365
+ /** Whether the currently applied theme is `'light'`. */
347
366
  isLight = this.#core.isLight;
348
- /** Whether the currently applied theme is system. */
367
+ /** Whether the user has explicitly selected `'system'` preference. */
349
368
  isSystem = this.#core.isSystem;
350
369
  /**
351
- * Whether the service has completed client-side initialization.
352
- * `false` during SSR. Becomes `true` after the first browser render.
370
+ * Whether the service has completed client-side initialization and
371
+ * resolved the real persisted theme. Use to prevent hydration flashes.
353
372
  */
354
373
  isHydrated = this.#core.isHydrated.asReadonly();
355
374
  /**
@@ -382,21 +401,21 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.7", ngImpor
382
401
  */
383
402
  class ThemeSelectService {
384
403
  #core = inject(CoreThemeService);
385
- /** List of all configured themes. Defaults to ['light', 'dark', 'system']. */
404
+ /** List of all configured themes. Defaults to `['light', 'dark', 'system']`. */
386
405
  availableThemes = this.#core.availableThemes;
387
406
  /** The theme explicitly selected by the user. May be `'system'`. */
388
407
  selectedTheme = this.#core.selectedTheme;
389
- /** Resolved theme applied to the DOM. Always concrete — never `'system'`. */
408
+ /** Resolved theme currently applied to the DOM. Always concrete — never `'system'`. */
390
409
  resolvedTheme = this.#core.resolvedTheme;
391
- /** Whether the currently applied theme is dark. */
410
+ /** Whether the currently applied theme is `'dark'`. */
392
411
  isDark = this.#core.isDark;
393
- /** Whether the currently applied theme is light. */
412
+ /** Whether the currently applied theme is `'light'`. */
394
413
  isLight = this.#core.isLight;
395
- /** Whether the currently applied theme is system. */
414
+ /** Whether the user has explicitly selected `'system'` preference. */
396
415
  isSystem = this.#core.isSystem;
397
416
  /**
398
- * Whether the service has completed client-side initialization.
399
- * `false` during SSR. Becomes `true` after the first browser render.
417
+ * Whether the service has completed client-side initialization and
418
+ * resolved the real persisted theme. Use to prevent hydration flashes.
400
419
  */
401
420
  isHydrated = this.#core.isHydrated.asReadonly();
402
421
  /**
@@ -424,20 +443,19 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.7", ngImpor
424
443
  */
425
444
  class ThemeToggleService {
426
445
  #core = inject(CoreThemeService);
427
- /** Resolved theme applied to the DOM. Always concrete — never `'system'`. */
446
+ /** Resolved theme currently applied to the DOM. Always concrete — never `'system'`. */
428
447
  resolvedTheme = this.#core.resolvedTheme;
448
+ /** The theme explicitly selected by the user. May be `'system'`. */
429
449
  selectedTheme = this.#core.selectedTheme;
430
- /** Whether the currently applied theme is dark. */
450
+ /** Whether the currently applied theme is `'dark'`. */
431
451
  isDark = this.#core.isDark;
432
- /** Whether the currently applied theme is light. */
452
+ /** Whether the currently applied theme is `'light'`. */
433
453
  isLight = this.#core.isLight;
434
- /** Whether the currently applied theme is system. */
454
+ /** Whether the user has explicitly selected `'system'` preference. */
435
455
  isSystem = this.#core.isSystem;
436
456
  /**
437
- * Whether the service has completed client-side initialization.
438
- * `false` during SSR. Becomes `true` after the first browser render.
439
- * Guard any template logic that shows `selectedTheme` or `resolvedTheme`
440
- * behind this signal to avoid a hydration-mismatch flash.
457
+ * Whether the service has completed client-side initialization and
458
+ * resolved the real persisted theme. Use to prevent hydration flashes.
441
459
  */
442
460
  isHydrated = this.#core.isHydrated.asReadonly();
443
461
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"ngx-theme-stack.mjs","sources":["../../../projects/ngx-theme-stack/src/lib/errors.ts","../../../projects/ngx-theme-stack/src/lib/types.ts","../../../projects/ngx-theme-stack/src/lib/config/index.ts","../../../projects/ngx-theme-stack/src/lib/core/core-theme.service.ts","../../../projects/ngx-theme-stack/src/lib/services/theme-cycle.service.ts","../../../projects/ngx-theme-stack/src/lib/services/theme-select.service.ts","../../../projects/ngx-theme-stack/src/lib/services/theme-toggle.service.ts","../../../projects/ngx-theme-stack/src/public-api.ts","../../../projects/ngx-theme-stack/src/ngx-theme-stack.ts"],"sourcesContent":["/**\n * Base error class for `ngx-theme-stack`.\n *\n * Thrown when the library configuration is invalid.\n * Consumers can use `instanceof NgxThemeStackError` to catch only\n * errors originating from this library.\n *\n * @example\n * try {\n * bootstrapApplication(AppComponent, appConfig);\n * } catch (e) {\n * if (e instanceof NgxThemeStackError) {\n * console.error('Bad ngx-theme-stack config:', e.message);\n * }\n * }\n */\nexport class NgxThemeStackError extends Error {\n override readonly name = 'NgxThemeStackError';\n\n constructor(message: string) {\n super(`[ngx-theme-stack] ${message}`);\n // Restore prototype chain (required when targeting ES5 or older)\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n","/**\n * Runtime list of built-in themes.\n *\n * Lives here (and not in config/index.ts) because it defines a type:\n * config/index.ts already imports from types.ts, so placing DEFAULT_THEMES\n * here avoids any circular dependency.\n *\n * ⚠ KEEP IN SYNC with the duplicate in:\n * projects/ngx-theme-stack/schematics/ng-add/constants.ts → DEFAULT_THEMES\n *\n * Schematics compile to CommonJS and cannot import from this ESM file,\n * so the values are intentionally duplicated. Change both at the same time.\n */\nexport const DEFAULT_THEMES = ['system', 'light', 'dark'] as const;\n\n/** Literal union of built-in themes: `'system' | 'light' | 'dark'`. */\nexport type DefaultNgTheme = (typeof DEFAULT_THEMES)[number];\n\n/**\n * Theme type.\n *\n * - **Without** `T`: open union — accepts any `string` with IDE autocomplete\n * hints for the built-in themes (`'system' | 'light' | 'dark'`).\n * - **With** `T`: closed union — exactly `DefaultNgTheme | T`, enabling\n * full type-safety for custom theme sets.\n *\n * @example\n * NgTheme // 'system' | 'light' | 'dark' | (string & {})\n * NgTheme<'sepia'> // 'system' | 'light' | 'dark' | 'sepia'\n */\nexport type NgTheme<T extends string = string & {}> = DefaultNgTheme | T;\n\n/**\n * Resolved theme — always `'light'` or `'dark'`, never `'system'`.\n * Represents the value that comes from `matchMedia`, not user selection.\n */\nexport type NgSystemTheme = Exclude<DefaultNgTheme, 'system'>;\n\nexport type NgMode = 'attribute' | 'class' | 'both';\n\n/**\n * Library configuration.\n *\n * @typeParam T - Custom theme literals. Defaults to open `string`, preserving\n * backwards compatibility. Pass specific literals (e.g. `'sepia' | 'ocean'`)\n * via {@link provideThemeStack} to get a closed, type-safe theme union.\n */\nexport interface NgConfig<T extends string = string & {}> {\n defaultTheme: NgTheme<T>;\n storageKey: string;\n mode: NgMode;\n themes: NgTheme<T>[];\n}\n","import { InjectionToken } from '@angular/core';\nimport { NgxThemeStackError } from '../errors';\nimport { DEFAULT_THEMES, DefaultNgTheme, NgConfig } from '../types';\n\n/**\n * ⚠ ATTENTION: SHARED CONFIGURATION VALUES\n *\n * These defaults MUST match the schematic defaults in:\n * projects/ngx-theme-stack/schematics/ng-add/constants.ts → DEFAULTS\n *\n * Schematics compile to CommonJS and cannot import from this ESM file,\n * so the values are intentionally duplicated. Change both at the same time.\n *\n * If you change defaults here, also update:\n * schematics/ng-add/constants.ts → DEFAULTS + DEFAULT_THEMES\n */\n\nexport const DEFAULT_NG_CONFIG = {\n defaultTheme: 'system',\n storageKey: 'ngx-theme-stack-theme',\n mode: 'class',\n themes: [...DEFAULT_THEMES],\n} satisfies NgConfig;\n\n// The token uses NgConfig<string> because Angular DI resolves types at runtime\n// and cannot carry generic parameters. Type-safety is enforced at the\n// provideThemeStack() call site instead.\nexport const NGX_THEME_STACK_CONFIG = new InjectionToken<NgConfig<string>>(\n 'NGX_THEME_STACK_CONFIG',\n {\n factory: () => DEFAULT_NG_CONFIG,\n },\n);\n\n/**\n * Provides Theme Stack configuration to Angular's DI system.\n *\n * Custom `themes` are **merged** with the built-in defaults\n * (`'light'`, `'dark'`, `'system'`), so you never lose the base themes.\n *\n * The type parameter `T` is **inferred automatically** from the `themes` array\n * when passed as a `const` — no need to specify it manually.\n *\n * @typeParam T - Custom theme string literals, inferred from the `themes` option.\n *\n * @param config - Optional partial configuration. Omitted fields fall back to\n * {@link DEFAULT_NG_CONFIG}.\n *\n * @throws {@link NgxThemeStackError}\n * - If any entry in `themes` is an empty or whitespace-only string.\n * - If `defaultTheme` is not present in the resolved (merged) themes array.\n * - If `storageKey` is an empty or whitespace-only string.\n *\n * @example\n * // Default — uses built-in themes and sensible defaults\n * provideThemeStack()\n *\n * @example\n * // Closed union: TypeScript infers 'sepia' | 'ocean' from the array\n * provideThemeStack({\n * themes: ['sepia', 'ocean'] as const,\n * defaultTheme: 'sepia', // ✅ in resolved themes\n * // defaultTheme: 'nope', // ❌ throws NgxThemeStackError at runtime\n * })\n *\n * @example\n * // Custom storage key and mode\n * provideThemeStack({\n * storageKey: 'my-app-theme',\n * mode: 'class',\n * })\n */\nexport function provideThemeStack<const T extends string = DefaultNgTheme>(\n config: Partial<NgConfig<T>> = {},\n) {\n config.themes?.forEach((t) => {\n if (t.trim() === '') throw new NgxThemeStackError('Theme cannot be empty or whitespace.');\n });\n\n const themes = config.themes\n ? Array.from(new Set([...DEFAULT_NG_CONFIG.themes, ...config.themes]))\n : DEFAULT_NG_CONFIG.themes;\n\n if (config.defaultTheme && !(themes as string[]).includes(config.defaultTheme as string)) {\n throw new NgxThemeStackError(\n `\"defaultTheme\" must be one of the resolved themes: [${themes.join(', ')}].`,\n );\n }\n\n if (config.storageKey !== undefined && config.storageKey.trim() === '') {\n throw new NgxThemeStackError('\"storageKey\" cannot be empty or whitespace.');\n }\n\n return {\n provide: NGX_THEME_STACK_CONFIG,\n useValue: {\n ...DEFAULT_NG_CONFIG,\n ...config,\n themes,\n } as NgConfig<string>,\n };\n}\n","import { isPlatformBrowser } from '@angular/common';\nimport {\n afterNextRender,\n computed,\n DestroyRef,\n DOCUMENT,\n effect,\n inject,\n Injectable,\n PLATFORM_ID,\n signal,\n} from '@angular/core';\nimport { NGX_THEME_STACK_CONFIG } from '../config';\nimport { NgxThemeStackError } from '../errors';\nimport { NgSystemTheme, NgTheme } from '../types';\n\n/**\n * Core service for managing the application's color theme.\n *\n * Handles theme persistence, system preference detection, and DOM updates.\n * Supports built-in themes ('dark', 'light', 'system') and custom extensions.\n */\n@Injectable({ providedIn: 'root' })\nexport class CoreThemeService {\n // ── Dependencies ──────────────────────────────────────────────────────────\n\n readonly #config = inject(NGX_THEME_STACK_CONFIG);\n readonly #destroyRef = inject(DestroyRef);\n readonly #document = inject(DOCUMENT);\n readonly #isBrowser = isPlatformBrowser(inject(PLATFORM_ID));\n\n // ── Theme configuration ───────────────────────────────────────────────────\n\n /**\n * The initial stored theme read from localStorage.\n * This is used to determine the initial theme of the application.\n */\n readonly #initialStoredTheme = this.readStoredTheme();\n\n /** List of available themes for Select/Cycle services. Defaults to ['system', 'light', 'dark']. */\n readonly availableThemes = this.#config.themes;\n\n /** Internal Set for O(1) existence checks. */\n readonly #validThemes = new Set<NgTheme>(this.availableThemes);\n\n /** The anti-flash class to remove from the host element. */\n #antiFlashClass: string | null = null;\n\n // ── System preference ─────────────────────────────────────────────────────\n\n /** MediaQueryList for OS color scheme, created once and reused. Null in SSR. */\n readonly #mediaQuery: MediaQueryList | null = this.#isBrowser\n ? (this.#document.defaultView?.matchMedia('(prefers-color-scheme: dark)') ?? null)\n : null;\n\n readonly #systemPreference = signal<NgSystemTheme>(this.resolveSystemPreference());\n\n // ── Theme state ───────────────────────────────────────────────────────────\n\n readonly #selectedTheme = signal<NgTheme>(this.resolveInitialTheme());\n\n /** The theme explicitly selected by the user. May be `'system'`. */\n readonly selectedTheme = this.#selectedTheme.asReadonly();\n\n /** Resolved theme applied to the DOM. Always `'dark'` or `'light'` (or custom) — never `'system'`. */\n readonly resolvedTheme = computed(() => {\n const theme = this.#selectedTheme();\n return theme === 'system' ? this.#systemPreference() : theme;\n });\n\n /** Whether the currently applied theme is dark. */\n readonly isDark = computed(() => this.resolvedTheme() === 'dark');\n\n /** Whether the currently applied theme is light. */\n readonly isLight = computed(() => this.resolvedTheme() === 'light');\n\n /** Whether the currently applied theme is system. */\n readonly isSystem = computed(() => this.selectedTheme() === 'system');\n\n /**\n * Whether the service has completed client-side initialization.\n *\n * `false` during SSR and on the very first render pass. Becomes `true`\n * immediately after the first browser render, once the real persisted theme\n * has been read from `localStorage`.\n *\n * Use this to guard any template logic that depends on `selectedTheme` or\n * `resolvedTheme` to avoid an SSR hydration-mismatch flash: the server\n * renders the default (`'system'`) while the browser may have a different\n * value stored.\n *\n * @example\n * ```html\n * {{ themeService.isHydrated() ? selectedTheme() : '—' }}\n * ```\n */\n readonly isHydrated = signal(false);\n\n // ── Event handler ─────────────────────────────────────────────────────────\n\n readonly #onSystemPreferenceChange = () =>\n this.#systemPreference.set(this.resolveSystemPreference());\n\n // ── Lifecycle ─────────────────────────────────────────────────────────────\n\n constructor() {\n this.captureAntiFlashClass();\n\n if (this.#isBrowser && this.#selectedTheme() === 'system') {\n this.startSystemThemeListener();\n }\n\n effect(() => this.applyThemeToDOM(this.resolvedTheme()));\n afterNextRender(() => this.isHydrated.set(true));\n this.#destroyRef.onDestroy(() => this.stopSystemThemeListener());\n }\n\n // ── Public API ────────────────────────────────────────────────────────────\n\n /**\n * Changes the active theme.\n *\n * Persists the choice explicitly so that switching e.g. from `'system'` to\n * `'light'` is saved even when the resolved theme did not change\n * (system preference was already `'light'`).\n *\n * @param theme - The theme to apply: `'dark'`, `'light'`, `'system'`, or a custom theme name.\n * @throws If `theme` is not a valid theme according to library configuration.\n */\n public setTheme(theme: NgTheme): void {\n if (!this.#validThemes.has(theme)) {\n throw new NgxThemeStackError(\n `Invalid theme: \"${theme}\". Valid values are: ${[...this.#validThemes].join(', ')}.`,\n );\n }\n if (!this.#isBrowser) return;\n if (theme === this.#selectedTheme()) return;\n if (theme === 'system') {\n this.#systemPreference.set(this.resolveSystemPreference());\n this.startSystemThemeListener();\n } else {\n this.stopSystemThemeListener();\n }\n\n this.#selectedTheme.set(theme);\n this.saveTheme(theme);\n }\n\n // ── Private ───────────────────────────────────────────────────────────────\n\n private resolveSystemPreference(): NgSystemTheme {\n return this.#mediaQuery?.matches ? 'dark' : 'light';\n }\n\n private resolveInitialTheme(): NgTheme {\n const theme = this.#initialStoredTheme;\n if (theme && this.#validThemes.has(theme as NgTheme)) {\n return theme as NgTheme;\n }\n return this.#config.defaultTheme;\n }\n\n private startSystemThemeListener(): void {\n if (!this.#mediaQuery) return;\n this.stopSystemThemeListener();\n this.#mediaQuery.addEventListener('change', this.#onSystemPreferenceChange);\n }\n\n private stopSystemThemeListener(): void {\n this.#mediaQuery?.removeEventListener('change', this.#onSystemPreferenceChange);\n }\n\n private applyThemeToDOM(theme: NgTheme): void {\n if (!this.#isBrowser) return;\n\n const host = this.#document.documentElement;\n\n if (this.#antiFlashClass) {\n host.classList.remove(this.#antiFlashClass);\n this.#antiFlashClass = null;\n }\n\n const { mode } = this.#config;\n\n if (mode === 'attribute' || mode === 'both') {\n this.applyThemeAttribute(host, theme);\n }\n\n if (mode === 'class' || mode === 'both') {\n this.applyThemeClasses(host, theme);\n }\n\n this.applyColorSchemeHint(host, theme);\n }\n\n private applyThemeAttribute(host: HTMLElement, theme: NgTheme): void {\n host.setAttribute('data-theme', theme);\n }\n\n private applyThemeClasses(host: HTMLElement, theme: NgTheme): void {\n host.classList.remove(...this.availableThemes);\n host.classList.add(theme);\n }\n\n private applyColorSchemeHint(host: HTMLElement, theme: NgTheme): void {\n if (theme === 'dark' || theme === 'light') {\n host.style.setProperty('color-scheme', theme);\n return;\n }\n\n host.style.removeProperty('color-scheme');\n }\n\n private captureAntiFlashClass(): void {\n if (!this.#isBrowser || !this.#initialStoredTheme) return;\n\n if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(this.#initialStoredTheme)) {\n this.#antiFlashClass = null;\n return;\n }\n\n if (this.#initialStoredTheme === 'system') {\n this.#antiFlashClass = this.resolveSystemPreference();\n return;\n }\n\n this.#antiFlashClass = this.#initialStoredTheme;\n }\n\n private readStoredTheme(): string | null {\n if (!this.#isBrowser) return null;\n\n try {\n return localStorage.getItem(this.#config.storageKey);\n } catch (e) {\n console.warn('[ngx-theme-stack] Could not read theme from localStorage.', e);\n return null;\n }\n }\n\n private saveTheme(theme: NgTheme): void {\n if (!this.#isBrowser) return;\n\n try {\n localStorage.setItem(this.#config.storageKey, theme);\n } catch (e) {\n console.warn('[ngx-theme-stack] Could not save theme to localStorage.', e);\n }\n }\n}\n","import { inject, Injectable } from '@angular/core';\nimport { CoreThemeService } from '../core/core-theme.service';\n\n/**\n * Convenience service for cycling through themes in a fixed order.\n *\n * Default cycle: `'system'` → `'light'` → `'dark'` → `'system'` → ...\n *\n * Use this when you want to offer users a single button that rotates\n * through all available theme options.\n */\n@Injectable({ providedIn: 'root' })\nexport class ThemeCycleService {\n readonly #core = inject(CoreThemeService);\n\n /** List of all configured themes for cycling. Defaults to ['light', 'dark', 'system']. */\n readonly #cycle = this.#core.availableThemes;\n\n /** The theme explicitly selected by the user. May be `'system'`. */\n readonly selectedTheme = this.#core.selectedTheme;\n\n /** Resolved theme applied to the DOM. Always concrete — never `'system'`. */\n readonly resolvedTheme = this.#core.resolvedTheme;\n\n /** Whether the currently applied theme is dark. */\n readonly isDark = this.#core.isDark;\n\n /** Whether the currently applied theme is light. */\n readonly isLight = this.#core.isLight;\n\n /** Whether the currently applied theme is system. */\n readonly isSystem = this.#core.isSystem;\n\n /**\n * Whether the service has completed client-side initialization.\n * `false` during SSR. Becomes `true` after the first browser render.\n */\n readonly isHydrated = this.#core.isHydrated.asReadonly();\n\n /**\n * Advances to the next theme in the cycle.\n *\n * Cycle order is determined by the configured `themes` property in `NgConfig`.\n *\n * If the current theme is not found in the cycle (e.g. set externally),\n * the cycle restarts from the first theme.\n */\n cycle(): void {\n const current = this.#core.selectedTheme();\n const index = this.#cycle.indexOf(current);\n const next = this.#cycle[(index + 1) % this.#cycle.length];\n this.#core.setTheme(next);\n }\n}\n","import { inject, Injectable } from '@angular/core';\nimport { CoreThemeService } from '../core/core-theme.service';\nimport { NgTheme } from '../types';\n\n/**\n * Convenience service for selecting a theme from a list.\n *\n * Use this when you want to bind a `<select>` or a group of radio/tab\n * buttons to the full set of available themes.\n */\n@Injectable({ providedIn: 'root' })\nexport class ThemeSelectService {\n readonly #core = inject(CoreThemeService);\n\n /** List of all configured themes. Defaults to ['light', 'dark', 'system']. */\n readonly availableThemes = this.#core.availableThemes;\n\n /** The theme explicitly selected by the user. May be `'system'`. */\n readonly selectedTheme = this.#core.selectedTheme;\n\n /** Resolved theme applied to the DOM. Always concrete — never `'system'`. */\n readonly resolvedTheme = this.#core.resolvedTheme;\n\n /** Whether the currently applied theme is dark. */\n readonly isDark = this.#core.isDark;\n\n /** Whether the currently applied theme is light. */\n readonly isLight = this.#core.isLight;\n\n /** Whether the currently applied theme is system. */\n readonly isSystem = this.#core.isSystem;\n\n /**\n * Whether the service has completed client-side initialization.\n * `false` during SSR. Becomes `true` after the first browser render.\n */\n readonly isHydrated = this.#core.isHydrated.asReadonly();\n\n /**\n * Applies the given theme.\n *\n * @param theme - The theme to apply: `'dark'`, `'light'`, `'system'`, or custom.\n * @throws If `theme` is not a valid theme according to library configuration.\n */\n select(theme: NgTheme): void {\n this.#core.setTheme(theme);\n }\n}\n","import { inject, Injectable } from '@angular/core';\nimport { CoreThemeService } from '../core/core-theme.service';\n\n/**\n * Convenience service for toggling between `'dark'` and `'light'`.\n *\n * Use this when you only need a simple on/off switch and do not\n * need to manage `'system'` or cycle through themes.\n */\n@Injectable({ providedIn: 'root' })\nexport class ThemeToggleService {\n readonly #core = inject(CoreThemeService);\n\n /** Resolved theme applied to the DOM. Always concrete — never `'system'`. */\n readonly resolvedTheme = this.#core.resolvedTheme;\n\n readonly selectedTheme = this.#core.selectedTheme;\n\n /** Whether the currently applied theme is dark. */\n readonly isDark = this.#core.isDark;\n\n /** Whether the currently applied theme is light. */\n readonly isLight = this.#core.isLight;\n\n /** Whether the currently applied theme is system. */\n readonly isSystem = this.#core.isSystem;\n\n /**\n * Whether the service has completed client-side initialization.\n * `false` during SSR. Becomes `true` after the first browser render.\n * Guard any template logic that shows `selectedTheme` or `resolvedTheme`\n * behind this signal to avoid a hydration-mismatch flash.\n */\n readonly isHydrated = this.#core.isHydrated.asReadonly();\n\n /**\n * Toggles between `'dark'` and `'light'`.\n *\n * If the selected theme is explicitly `'dark'`, switches to `'light'`.\n * Otherwise (including `'system'`), switches to `'dark'`.\n */\n toggle(): void {\n const next = this.#core.resolvedTheme() === 'dark' ? 'light' : 'dark';\n this.#core.setTheme(next);\n }\n}\n","/*\n * Public API Surface of ngx-theme-stack\n */\n\nexport * from './lib/core/core-theme.service';\nexport * from './lib/config';\nexport * from './lib/services/theme-cycle.service';\nexport * from './lib/services/theme-select.service';\nexport * from './lib/services/theme-toggle.service';\nexport * from './lib/types';\nexport * from './lib/errors';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;AAAA;;;;;;;;;;;;;;;AAeG;AACG,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PlC;;;;;AAKG;MAEU,kBAAkB,CAAA;AACpB,IAAA,KAAK,GAAG,MAAM,CAAC,gBAAgB,CAAC;;AAGhC,IAAA,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,aAAa;AAExC,IAAA,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,aAAa;;AAGxC,IAAA,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM;;AAG1B,IAAA,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO;;AAG5B,IAAA,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ;AAEvC;;;;;AAKG;IACM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,EAAE;AAExD;;;;;AAKG;IACH,MAAM,GAAA;AACJ,QAAA,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,MAAM,GAAG,OAAO,GAAG,MAAM;AACrE,QAAA,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC;IAC3B;uGAlCW,kBAAkB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;AAAlB,IAAA,OAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,QAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,kBAAkB,cADL,MAAM,EAAA,CAAA;;2FACnB,kBAAkB,EAAA,UAAA,EAAA,CAAA;kBAD9B,UAAU;mBAAC,EAAE,UAAU,EAAE,MAAM,EAAE;;;ACTlC;;AAEG;;ACFH;;AAEG;;;;"}
1
+ {"version":3,"file":"ngx-theme-stack.mjs","sources":["../../../projects/ngx-theme-stack/src/lib/errors.ts","../../../projects/ngx-theme-stack/src/lib/types.ts","../../../projects/ngx-theme-stack/src/lib/config/index.ts","../../../projects/ngx-theme-stack/src/lib/core/core-theme.service.ts","../../../projects/ngx-theme-stack/src/lib/services/theme-cycle.service.ts","../../../projects/ngx-theme-stack/src/lib/services/theme-select.service.ts","../../../projects/ngx-theme-stack/src/lib/services/theme-toggle.service.ts","../../../projects/ngx-theme-stack/src/public-api.ts","../../../projects/ngx-theme-stack/src/ngx-theme-stack.ts"],"sourcesContent":["/**\n * Base error class for `ngx-theme-stack`.\n *\n * Thrown when the library configuration is invalid.\n * Consumers can use `instanceof NgxThemeStackError` to catch only\n * errors originating from this library.\n *\n * @example\n * try {\n * bootstrapApplication(AppComponent, appConfig);\n * } catch (e) {\n * if (e instanceof NgxThemeStackError) {\n * console.error('Bad ngx-theme-stack config:', e.message);\n * }\n * }\n */\nexport class NgxThemeStackError extends Error {\n override readonly name = 'NgxThemeStackError';\n\n constructor(message: string) {\n super(`[ngx-theme-stack] ${message}`);\n // Restore prototype chain (required when targeting ES5 or older)\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n","/**\n * Runtime list of built-in themes.\n *\n * Lives here (and not in config/index.ts) because it defines a type:\n * config/index.ts already imports from types.ts, so placing DEFAULT_THEMES\n * here avoids any circular dependency.\n *\n * ⚠ KEEP IN SYNC with the duplicate in:\n * projects/ngx-theme-stack/schematics/ng-add/constants.ts → DEFAULT_THEMES\n *\n * Schematics compile to CommonJS and cannot import from this ESM file,\n * so the values are intentionally duplicated. Change both at the same time.\n */\nexport const DEFAULT_THEMES = ['system', 'light', 'dark'] as const;\n\n/** Literal union of built-in themes: `'system' | 'light' | 'dark'`. */\nexport type DefaultNgTheme = (typeof DEFAULT_THEMES)[number];\n\n/**\n * Theme type.\n *\n * - **Without** `T`: open union — accepts any `string` with IDE autocomplete\n * hints for the built-in themes (`'system' | 'light' | 'dark'`).\n * - **With** `T`: closed union — exactly `DefaultNgTheme | T`, enabling\n * full type-safety for custom theme sets.\n *\n * @example\n * NgTheme // 'system' | 'light' | 'dark' | (string & {})\n * NgTheme<'sepia'> // 'system' | 'light' | 'dark' | 'sepia'\n */\nexport type NgTheme<T extends string = string & {}> = DefaultNgTheme | T;\n\n/**\n * Resolved theme — always `'light'` or `'dark'`, never `'system'`.\n * Represents the value that comes from `matchMedia`, not user selection.\n */\nexport type NgSystemTheme = Exclude<DefaultNgTheme, 'system'>;\n\n/**\n * Theme application mode.\n * - `'attribute'`: sets `data-theme` attribute on `<html>`\n * - `'class'`: adds theme class to `<html>`\n * - `'both'`: uses both attribute and class\n */\nexport type NgMode = 'attribute' | 'class' | 'both';\n\n/**\n * Theme application strategy.\n * - `'blocking'`: theme CSS is loaded synchronously before rendering\n * - `'critters'`: theme CSS is inlined using Critters for SSR/SSG\n */\nexport type NgStrategy = 'blocking' | 'critters';\n\n/**\n * Library configuration.\n *\n * @typeParam T - Custom theme literals. Defaults to open `string`, preserving\n * backwards compatibility. Pass specific literals (e.g. `'sepia' | 'ocean'`)\n * via {@link provideThemeStack} to get a closed, type-safe theme union.\n */\nexport interface NgConfig<T extends string = string & {}> {\n /** The theme to use on first visit or when no preference is saved. Default: 'system'. */\n defaultTheme: NgTheme<T>;\n\n /** Key used to persist theme preference in localStorage. Default: 'ngx-theme-stack-theme'. */\n storageKey: string;\n\n /** \n * How the theme should be applied to the document (via class, attribute or both). \n * Default: 'class'.\n */\n mode: NgMode;\n\n /** \n * Performance strategy for anti-flash. \n * Use 'critters' for SSG/SSR builds with inlined CSS, 'blocking' for standard CSS files.\n * Default: 'critters'.\n */\n strategy: NgStrategy;\n\n /** List of supported theme identifiers. Default: ['light', 'dark', 'system']. */\n themes: NgTheme<T>[];\n}\n","import { InjectionToken } from '@angular/core';\nimport { NgxThemeStackError } from '../errors';\nimport { DEFAULT_THEMES, DefaultNgTheme, NgConfig } from '../types';\n\n/**\n * ⚠ ATTENTION: SHARED CONFIGURATION VALUES\n *\n * These defaults MUST match the schematic defaults in:\n * projects/ngx-theme-stack/schematics/ng-add/constants.ts → DEFAULTS\n *\n * Schematics compile to CommonJS and cannot import from this ESM file,\n * so the values are intentionally duplicated. Change both at the same time.\n *\n * If you change defaults here, also update:\n * schematics/ng-add/constants.ts → DEFAULTS + DEFAULT_THEMES\n */\n\nexport const DEFAULT_NG_CONFIG = {\n defaultTheme: 'system',\n storageKey: 'ngx-theme-stack-theme',\n mode: 'class',\n strategy: 'critters',\n themes: [...DEFAULT_THEMES],\n} satisfies NgConfig;\n\n// The token uses NgConfig<string> because Angular DI resolves types at runtime\n// and cannot carry generic parameters. Type-safety is enforced at the\n// provideThemeStack() call site instead.\nexport const NGX_THEME_STACK_CONFIG = new InjectionToken<NgConfig<string>>(\n 'NGX_THEME_STACK_CONFIG',\n {\n factory: () => DEFAULT_NG_CONFIG,\n },\n);\n\n/**\n * Provides Theme Stack configuration to Angular's DI system.\n *\n * Custom `themes` are **merged** with the built-in defaults\n * (`'light'`, `'dark'`, `'system'`), so you never lose the base themes.\n *\n * **Defaults:**\n * - `themes`: `['light', 'dark', 'system']`\n * - `defaultTheme`: `'system'`\n * - `storageKey`: `'ngx-theme-stack-theme'`\n * - `mode`: `'class'`\n * - `strategy`: `'critters'`\n\n *\n * The type parameter `T` is **inferred automatically** from the `themes` array\n * when passed as a `const` — no need to specify it manually.\n *\n * @typeParam T - Custom theme string literals, inferred from the `themes` option.\n *\n * @param config - Optional partial configuration. Omitted fields fall back to\n * {@link DEFAULT_NG_CONFIG}.\n *\n * @throws {@link NgxThemeStackError}\n * - If any entry in `themes` is an empty or whitespace-only string.\n * - If `defaultTheme` is not present in the resolved (merged) themes array.\n * - If `storageKey` is an empty or whitespace-only string.\n *\n * @example\n * // Default — uses built-in themes and sensible defaults\n * provideThemeStack()\n *\n * @example\n * // SSR/SSG Optimization — uses Critters inlining strategy\n * provideThemeStack({\n * strategy: 'critters',\n * mode: 'class',\n * })\n *\n * @example\n * // Closed union: TypeScript infers 'sepia' | 'ocean' from the array\n * provideThemeStack({\n * themes: ['sepia', 'ocean'] as const,\n * defaultTheme: 'sepia', // ✅ in resolved themes\n * // defaultTheme: 'nope', // ❌ throws NgxThemeStackError at runtime\n * })\n *\n * @example\n * // Custom storage key and mode\n * provideThemeStack({\n * storageKey: 'my-app-theme',\n * mode: 'attribute',\n * })\n */\nexport function provideThemeStack<const T extends string = DefaultNgTheme>(\n config: Partial<NgConfig<T>> = {},\n) {\n config.themes?.forEach((t) => {\n if (t.trim() === '') throw new NgxThemeStackError('Theme cannot be empty or whitespace.');\n });\n\n const themes = config.themes\n ? Array.from(new Set([...DEFAULT_NG_CONFIG.themes, ...config.themes]))\n : DEFAULT_NG_CONFIG.themes;\n\n if (config.defaultTheme && !(themes as string[]).includes(config.defaultTheme as string)) {\n throw new NgxThemeStackError(\n `\"defaultTheme\" must be one of the resolved themes: [${themes.join(', ')}].`,\n );\n }\n\n if (config.storageKey !== undefined && config.storageKey.trim() === '') {\n throw new NgxThemeStackError('\"storageKey\" cannot be empty or whitespace.');\n }\n\n return {\n provide: NGX_THEME_STACK_CONFIG,\n useValue: {\n ...DEFAULT_NG_CONFIG,\n ...config,\n themes,\n } as NgConfig<string>,\n };\n}\n","import { isPlatformBrowser } from '@angular/common';\nimport {\n afterNextRender,\n computed,\n DestroyRef,\n DOCUMENT,\n effect,\n inject,\n Injectable,\n PLATFORM_ID,\n signal,\n} from '@angular/core';\nimport { NGX_THEME_STACK_CONFIG } from '../config';\nimport { NgxThemeStackError } from '../errors';\nimport { NgSystemTheme, NgTheme } from '../types';\n\n/**\n * Core service for managing the application's color theme.\n *\n * Handles theme persistence, system preference detection, and DOM updates.\n * Supports built-in themes ('dark', 'light', 'system') and custom extensions.\n */\n@Injectable({ providedIn: 'root' })\nexport class CoreThemeService {\n // ── Dependencies ──────────────────────────────────────────────────────────\n\n readonly #config = inject(NGX_THEME_STACK_CONFIG);\n readonly #destroyRef = inject(DestroyRef);\n readonly #document = inject(DOCUMENT);\n readonly #isBrowser = isPlatformBrowser(inject(PLATFORM_ID));\n\n // ── Theme configuration ───────────────────────────────────────────────────\n\n /**\n * The initial stored theme read from localStorage.\n * This is used to determine the initial theme of the application.\n */\n readonly #initialStoredTheme = this.readStoredTheme();\n\n /** List of available themes for Select/Cycle services. Defaults to ['system', 'light', 'dark']. */\n readonly availableThemes = this.#config.themes;\n\n /** Internal Set for O(1) existence checks. */\n readonly #validThemes = new Set<NgTheme>(this.availableThemes);\n\n /**\n * The anti-flash class to remove from the host element.\n * Internal mechanism to bridge the gap between the blocking script's\n * initial DOM state and Angular's first effect run.\n */\n #antiFlashClass: string | null = null;\n\n // ── System preference ─────────────────────────────────────────────────────\n\n /** MediaQueryList for OS color scheme, created once and reused. Null in SSR. */\n readonly #mediaQuery: MediaQueryList | null = this.#isBrowser\n ? (this.#document.defaultView?.matchMedia('(prefers-color-scheme: dark)') ?? null)\n : null;\n\n readonly #systemPreference = signal<NgSystemTheme>(this.resolveSystemPreference());\n\n // ── Theme state ───────────────────────────────────────────────────────────\n\n readonly #selectedTheme = signal<NgTheme>(this.resolveInitialTheme());\n\n /** The theme explicitly selected by the user. May be `'system'`. */\n readonly selectedTheme = this.#selectedTheme.asReadonly();\n\n /** Resolved theme applied to the DOM. Always `'dark'` or `'light'` (or custom) — never `'system'`. */\n readonly resolvedTheme = computed(() => {\n const theme = this.#selectedTheme();\n return theme === 'system' ? this.#systemPreference() : theme;\n });\n\n /** Whether the currently applied theme is dark. */\n readonly isDark = computed(() => this.resolvedTheme() === 'dark');\n\n /** Whether the currently applied theme is light. */\n readonly isLight = computed(() => this.resolvedTheme() === 'light');\n\n /** Whether the currently applied theme is system. */\n readonly isSystem = computed(() => this.selectedTheme() === 'system');\n\n /**\n * Whether the service has completed client-side initialization.\n *\n * `false` during SSR and on the very first render pass before the initial theme\n * is resolved from `localStorage`. Becomes `true` immediately after the\n * first browser render pass.\n *\n * **Important:** Guard template elements that display `selectedTheme()` or\n * `resolvedTheme()` behind this signal to prevent hydration-mismatch flashes\n * (e.g. if the server renders the default 'system' but the user has 'dark' stored).\n *\n * @example\n * ```html\n * {{ theme.isHydrated() ? theme.selectedTheme() : '...' }}\n * ```\n */\n readonly isHydrated = signal(false);\n\n // ── Event handler ─────────────────────────────────────────────────────────\n\n readonly #onSystemPreferenceChange = () =>\n this.#systemPreference.set(this.resolveSystemPreference());\n\n // ── Lifecycle ─────────────────────────────────────────────────────────────\n\n constructor() {\n this.captureAntiFlashClass();\n\n if (this.#isBrowser && this.#selectedTheme() === 'system') {\n this.startSystemThemeListener();\n }\n\n effect(() => this.applyThemeToDOM(this.resolvedTheme()));\n afterNextRender(() => this.isHydrated.set(true));\n this.#destroyRef.onDestroy(() => this.stopSystemThemeListener());\n }\n\n // ── Public API ────────────────────────────────────────────────────────────\n\n /**\n * Changes the active theme.\n *\n * Persists the choice explicitly so that switching e.g. from `'system'` to\n * `'light'` is saved even when the resolved theme did not change\n * (system preference was already `'light'`).\n *\n * @param theme - The theme to apply: `'dark'`, `'light'`, `'system'`, or a custom theme name.\n * @throws If `theme` is not a valid theme according to library configuration.\n */\n public setTheme(theme: NgTheme): void {\n if (!this.#validThemes.has(theme)) {\n throw new NgxThemeStackError(\n `Invalid theme: \"${theme}\". Valid values are: ${[...this.#validThemes].join(', ')}.`,\n );\n }\n if (!this.#isBrowser) return;\n if (theme === this.#selectedTheme()) return;\n if (theme === 'system') {\n this.#systemPreference.set(this.resolveSystemPreference());\n this.startSystemThemeListener();\n } else {\n this.stopSystemThemeListener();\n }\n\n this.#selectedTheme.set(theme);\n this.saveTheme(theme);\n }\n\n // ── Private ───────────────────────────────────────────────────────────────\n\n private resolveSystemPreference(): NgSystemTheme {\n return this.#mediaQuery?.matches ? 'dark' : 'light';\n }\n\n private resolveInitialTheme(): NgTheme {\n const theme = this.#initialStoredTheme;\n if (theme && this.#validThemes.has(theme as NgTheme)) {\n return theme as NgTheme;\n }\n return this.#config.defaultTheme;\n }\n\n private startSystemThemeListener(): void {\n if (!this.#mediaQuery) return;\n this.stopSystemThemeListener();\n this.#mediaQuery.addEventListener('change', this.#onSystemPreferenceChange);\n }\n\n private stopSystemThemeListener(): void {\n this.#mediaQuery?.removeEventListener('change', this.#onSystemPreferenceChange);\n }\n\n private applyThemeToDOM(theme: NgTheme): void {\n if (!this.#isBrowser) return;\n\n const host = this.#document.documentElement;\n\n if (this.#antiFlashClass) {\n host.classList.remove(this.#antiFlashClass);\n this.#antiFlashClass = null;\n }\n\n const { mode } = this.#config;\n\n if (mode === 'attribute' || mode === 'both') {\n this.applyThemeAttribute(host, theme);\n }\n\n if (mode === 'class' || mode === 'both') {\n this.applyThemeClasses(host, theme);\n }\n\n this.applyColorSchemeHint(host, theme);\n }\n\n private applyThemeAttribute(host: HTMLElement, theme: NgTheme): void {\n host.setAttribute('data-theme', theme);\n }\n\n private applyThemeClasses(host: HTMLElement, theme: NgTheme): void {\n host.classList.remove(...this.availableThemes);\n host.classList.add(theme);\n }\n\n private applyColorSchemeHint(host: HTMLElement, theme: NgTheme): void {\n if (theme === 'dark' || theme === 'light') {\n host.style.setProperty('color-scheme', theme);\n return;\n }\n\n host.style.removeProperty('color-scheme');\n }\n\n private captureAntiFlashClass(): void {\n if (!this.#isBrowser || !this.#initialStoredTheme) return;\n\n if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(this.#initialStoredTheme)) {\n this.#antiFlashClass = null;\n return;\n }\n\n if (this.#initialStoredTheme === 'system') {\n this.#antiFlashClass = this.resolveSystemPreference();\n return;\n }\n\n this.#antiFlashClass = this.#initialStoredTheme;\n }\n\n private readStoredTheme(): string | null {\n if (!this.#isBrowser) return null;\n\n try {\n return localStorage.getItem(this.#config.storageKey);\n } catch (e) {\n console.warn('[ngx-theme-stack] Could not read theme from localStorage.', e);\n return null;\n }\n }\n\n private saveTheme(theme: NgTheme): void {\n if (!this.#isBrowser) return;\n\n try {\n localStorage.setItem(this.#config.storageKey, theme);\n } catch (e) {\n console.warn('[ngx-theme-stack] Could not save theme to localStorage.', e);\n }\n }\n}\n","import { inject, Injectable } from '@angular/core';\nimport { CoreThemeService } from '../core/core-theme.service';\n\n/**\n * Convenience service for cycling through themes in a fixed order.\n *\n * Default cycle: `'system'` → `'light'` → `'dark'` → `'system'` → ...\n *\n * Use this when you want to offer users a single button that rotates\n * through all available theme options.\n */\n@Injectable({ providedIn: 'root' })\nexport class ThemeCycleService {\n readonly #core = inject(CoreThemeService);\n\n /** List of all configured themes for cycling. Defaults to `['light', 'dark', 'system']`. */\n readonly #cycle = this.#core.availableThemes;\n\n /** The theme explicitly selected by the user. May be `'system'`. */\n readonly selectedTheme = this.#core.selectedTheme;\n\n /** Resolved theme currently applied to the DOM. Always concrete — never `'system'`. */\n readonly resolvedTheme = this.#core.resolvedTheme;\n\n /** Whether the currently applied theme is `'dark'`. */\n readonly isDark = this.#core.isDark;\n\n /** Whether the currently applied theme is `'light'`. */\n readonly isLight = this.#core.isLight;\n\n /** Whether the user has explicitly selected `'system'` preference. */\n readonly isSystem = this.#core.isSystem;\n\n /**\n * Whether the service has completed client-side initialization and \n * resolved the real persisted theme. Use to prevent hydration flashes.\n */\n readonly isHydrated = this.#core.isHydrated.asReadonly();\n\n /**\n * Advances to the next theme in the cycle.\n *\n * Cycle order is determined by the configured `themes` property in `NgConfig`.\n *\n * If the current theme is not found in the cycle (e.g. set externally),\n * the cycle restarts from the first theme.\n */\n cycle(): void {\n const current = this.#core.selectedTheme();\n const index = this.#cycle.indexOf(current);\n const next = this.#cycle[(index + 1) % this.#cycle.length];\n this.#core.setTheme(next);\n }\n}\n","import { inject, Injectable } from '@angular/core';\nimport { CoreThemeService } from '../core/core-theme.service';\nimport { NgTheme } from '../types';\n\n/**\n * Convenience service for selecting a theme from a list.\n *\n * Use this when you want to bind a `<select>` or a group of radio/tab\n * buttons to the full set of available themes.\n */\n@Injectable({ providedIn: 'root' })\nexport class ThemeSelectService {\n readonly #core = inject(CoreThemeService);\n\n /** List of all configured themes. Defaults to `['light', 'dark', 'system']`. */\n readonly availableThemes = this.#core.availableThemes;\n\n /** The theme explicitly selected by the user. May be `'system'`. */\n readonly selectedTheme = this.#core.selectedTheme;\n\n /** Resolved theme currently applied to the DOM. Always concrete — never `'system'`. */\n readonly resolvedTheme = this.#core.resolvedTheme;\n\n /** Whether the currently applied theme is `'dark'`. */\n readonly isDark = this.#core.isDark;\n\n /** Whether the currently applied theme is `'light'`. */\n readonly isLight = this.#core.isLight;\n\n /** Whether the user has explicitly selected `'system'` preference. */\n readonly isSystem = this.#core.isSystem;\n\n /**\n * Whether the service has completed client-side initialization and \n * resolved the real persisted theme. Use to prevent hydration flashes.\n */\n readonly isHydrated = this.#core.isHydrated.asReadonly();\n\n /**\n * Applies the given theme.\n *\n * @param theme - The theme to apply: `'dark'`, `'light'`, `'system'`, or custom.\n * @throws If `theme` is not a valid theme according to library configuration.\n */\n select(theme: NgTheme): void {\n this.#core.setTheme(theme);\n }\n}\n","import { inject, Injectable } from '@angular/core';\nimport { CoreThemeService } from '../core/core-theme.service';\n\n/**\n * Convenience service for toggling between `'dark'` and `'light'`.\n *\n * Use this when you only need a simple on/off switch and do not\n * need to manage `'system'` or cycle through themes.\n */\n@Injectable({ providedIn: 'root' })\nexport class ThemeToggleService {\n readonly #core = inject(CoreThemeService);\n\n /** Resolved theme currently applied to the DOM. Always concrete — never `'system'`. */\n readonly resolvedTheme = this.#core.resolvedTheme;\n\n /** The theme explicitly selected by the user. May be `'system'`. */\n readonly selectedTheme = this.#core.selectedTheme;\n\n /** Whether the currently applied theme is `'dark'`. */\n readonly isDark = this.#core.isDark;\n\n /** Whether the currently applied theme is `'light'`. */\n readonly isLight = this.#core.isLight;\n\n /** Whether the user has explicitly selected `'system'` preference. */\n readonly isSystem = this.#core.isSystem;\n\n /**\n * Whether the service has completed client-side initialization and \n * resolved the real persisted theme. Use to prevent hydration flashes.\n */\n readonly isHydrated = this.#core.isHydrated.asReadonly();\n\n /**\n * Toggles between `'dark'` and `'light'`.\n *\n * If the selected theme is explicitly `'dark'`, switches to `'light'`.\n * Otherwise (including `'system'`), switches to `'dark'`.\n */\n toggle(): void {\n const next = this.#core.resolvedTheme() === 'dark' ? 'light' : 'dark';\n this.#core.setTheme(next);\n }\n}\n","/*\n * Public API Surface of ngx-theme-stack\n */\n\nexport * from './lib/core/core-theme.service';\nexport * from './lib/config';\nexport * from './lib/services/theme-cycle.service';\nexport * from './lib/services/theme-select.service';\nexport * from './lib/services/theme-toggle.service';\nexport * from './lib/types';\nexport * from './lib/errors';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;AAAA;;;;;;;;;;;;;;;AAeG;AACG,MAAO,kBAAmB,SAAQ,KAAK,CAAA;IACzB,IAAI,GAAG,oBAAoB;AAE7C,IAAA,WAAA,CAAY,OAAe,EAAA;AACzB,QAAA,KAAK,CAAC,CAAA,kBAAA,EAAqB,OAAO,CAAA,CAAE,CAAC;;QAErC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC;IACnD;AACD;;ACxBD;;;;;;;;;;;;AAYG;AACI,MAAM,cAAc,GAAG,CAAC,QAAQ,EAAE,OAAO,EAAE,MAAM;;ACTxD;;;;;;;;;;;AAWG;AAEI,MAAM,iBAAiB,GAAG;AAC/B,IAAA,YAAY,EAAE,QAAQ;AACtB,IAAA,UAAU,EAAE,uBAAuB;AACnC,IAAA,IAAI,EAAE,OAAO;AACb,IAAA,QAAQ,EAAE,UAAU;AACpB,IAAA,MAAM,EAAE,CAAC,GAAG,cAAc,CAAC;;AAG7B;AACA;AACA;MACa,sBAAsB,GAAG,IAAI,cAAc,CACtD,wBAAwB,EACxB;AACE,IAAA,OAAO,EAAE,MAAM,iBAAiB;AACjC,CAAA;AAGH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDG;AACG,SAAU,iBAAiB,CAC/B,MAAA,GAA+B,EAAE,EAAA;IAEjC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,KAAI;AAC3B,QAAA,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE;AAAE,YAAA,MAAM,IAAI,kBAAkB,C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AAkB,cADL,MAAM,EAAA,CAAA;;2FACnB,kBAAkB,EAAA,UAAA,EAAA,CAAA;kBAD9B,UAAU;mBAAC,EAAE,UAAU,EAAE,MAAM,EAAE;;;ACTlC;;AAEG;;ACFH;;AAEG;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ngx-theme-stack",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "A stack of themes for Angular applications.",
5
5
  "schematics": "./schematics/collection.json",
6
6
  "ng-add": {
@@ -21,4 +21,6 @@ export declare function patchIndexHtml(tree: Tree, context: SchematicContext, so
21
21
  storageKey: string;
22
22
  defaultTheme: string;
23
23
  mode: string;
24
+ themes: string[];
25
+ strategy: 'critters' | 'blocking';
24
26
  }): void;
@@ -55,44 +55,66 @@ function patchIndexHtml(tree, context, sourceRoot, options) {
55
55
  if (!tree.exists(path))
56
56
  continue;
57
57
  const content = tree.readText(path);
58
- // Guard: idempotent skip if script is already present
59
- if (content.includes('ngx-theme-stack-theme') ||
60
- content.includes('ngx-theme-stack anti-flash')) {
61
- context.logger.info(`✔ Anti-flash script already present in ${path} — skipping.`);
62
- return;
63
- }
64
- const script = buildAntiFlashScript({
58
+ const scriptTag = `\n <!-- ngx-theme-stack anti-flash -->\n <script>${buildAntiFlashScript({
65
59
  storageKey: options.storageKey,
66
60
  defaultTheme: options.defaultTheme,
67
61
  mode: options.mode,
68
- });
69
- // ── CSP Detection ────────────────────────────────────────────────────────
70
- // If the project uses a Content-Security-Policy meta tag, inline scripts
71
- // are blocked by default unless 'unsafe-inline' or a nonce is allowed.
72
- // We warn here; the user must add a nonce manually or adjust their CSP.
73
- const hasCspMeta = content.includes('Content-Security-Policy') || content.includes('content-security-policy');
74
- if (hasCspMeta) {
75
- context.logger.warn(`⚠ A Content-Security-Policy meta tag was detected in ${path}.\n` +
76
- ` Inline scripts may be blocked by your CSP.\n` +
77
- ` Options:\n` +
78
- ` 1. Add a nonce to the injected <script> tag and allow it in your CSP.\n` +
79
- ` 2. Add the script hash to your CSP: script-src 'sha256-<HASH>'.\n` +
80
- ` 3. Allow 'unsafe-inline' (not recommended for production).\n` +
81
- ` See: https://angular.dev/guide/security#content-security-policy`);
62
+ })}</script>`;
63
+ let updated = content;
64
+ // ── 1. Update Anti-Flash Script ──────────────────────────────────────────
65
+ const SCRIPT_BLOCK_RE = /<!--\s*ngx-theme-stack\s*anti-flash\s*-->\s*<script[^>]*>[\s\S]*?<\/script>/;
66
+ if (SCRIPT_BLOCK_RE.test(updated)) {
67
+ updated = updated.replace(SCRIPT_BLOCK_RE, scriptTag);
68
+ context.logger.info(`✔ Updated anti-flash script in ${path}`);
82
69
  }
83
- // Inject immediately after <head> so it blocks rendering before any CSS.
84
- // If your project uses CSP nonces, add nonce="<%= CSP_NONCE %>" to the tag.
85
- const scriptTag = `\n <!-- ngx-theme-stack anti-flash -->\n <script>${script}</script>`;
86
- const updated = content.replace('<head>', `<head>${scriptTag}`);
87
- tree.overwrite(path, updated);
88
- context.logger.info(`✔ Anti-flash script injected into ${path}`);
89
- if (hasCspMeta) {
90
- context.logger.warn(` → Remember to allow the inline script in your CSP before deploying.`);
70
+ else {
71
+ updated = updated.replace('<head>', `<head>${scriptTag}`);
72
+ context.logger.info(`✔ Injected anti-flash script into ${path}`);
73
+ }
74
+ // ── 2. Update Critters Trick ─────────────────────────────────────────────
75
+ const CRITTERS_BLOCK_RE = /<!-- ngx-theme-stack critters-trick -->[\s\S]*?<!-- \/ngx-theme-stack critters-trick -->/;
76
+ const CRITTERS_ID_RE = /<div id="ngx-theme-stack-critters-trick"[\s\S]*?<\/div>/;
77
+ if (options.strategy === 'critters') {
78
+ const renderableThemes = options.themes.filter((t) => t !== 'system');
79
+ const innerDivs = renderableThemes
80
+ .map((theme) => {
81
+ if (options.mode === 'class')
82
+ return ` <div class="${theme}"></div>`;
83
+ if (options.mode === 'attribute')
84
+ return ` <div data-theme="${theme}"></div>`;
85
+ return ` <div class="${theme}" data-theme="${theme}"></div>`;
86
+ })
87
+ .join('\n');
88
+ const crittersBlock = `<!-- ngx-theme-stack critters-trick -->\n` +
89
+ ` <div id="ngx-theme-stack-critters-trick" hidden aria-hidden="true" style="display: none; overflow: hidden; clip-path: inset(50%); position: absolute;">\n${innerDivs}\n </div>\n` +
90
+ ` <!-- /ngx-theme-stack critters-trick -->`;
91
+ if (CRITTERS_BLOCK_RE.test(updated)) {
92
+ updated = updated.replace(CRITTERS_BLOCK_RE, crittersBlock);
93
+ context.logger.info(`✔ Updated Critters-trick block in ${path}`);
94
+ }
95
+ else if (CRITTERS_ID_RE.test(updated)) {
96
+ updated = updated.replace(CRITTERS_ID_RE, crittersBlock);
97
+ context.logger.info(`✔ Wrapped existing Critters-trick div with markers in ${path}`);
98
+ }
99
+ else {
100
+ updated = updated.replace('</body>', ` ${crittersBlock}\n </body>`);
101
+ context.logger.info(`✔ Injected Critters-trick block before </body> in ${path}`);
102
+ }
91
103
  }
104
+ else {
105
+ // If switching to blocking strategy, remove existing trick
106
+ if (CRITTERS_BLOCK_RE.test(updated)) {
107
+ updated = updated.replace(CRITTERS_BLOCK_RE, '');
108
+ }
109
+ else if (CRITTERS_ID_RE.test(updated)) {
110
+ updated = updated.replace(CRITTERS_ID_RE, '');
111
+ }
112
+ context.logger.info(`✔ Removed Critters-trick from ${path}`);
113
+ }
114
+ tree.overwrite(path, updated);
92
115
  return;
93
116
  }
94
117
  context.logger.warn(`⚠ Could not find index.html (tried: ${candidates.join(', ')}).\n` +
95
- ` Add the anti-flash script manually to the <head> of your index.html.\n` +
96
- ` See: https://github.com/your-org/ngx-theme-stack#anti-flash`);
118
+ ` Add the anti-flash script manually to the <head> of your index.html.`);
97
119
  }
98
120
  //# sourceMappingURL=anti-flash.js.map