ngx-theme-stack 0.0.12 → 0.0.14
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 +41 -38
- package/fesm2022/ngx-theme-stack.mjs +16 -12
- package/fesm2022/ngx-theme-stack.mjs.map +1 -1
- package/package.json +9 -8
- package/schematics/collection.json +4 -4
- package/schematics/ng-add/index.d.ts +1 -12
- package/schematics/ng-add/index.js +3 -131
- package/schematics/ng-add/index.js.map +1 -1
- package/schematics/ng-add/index.ts +5 -0
- package/schematics/ng-add/schema.d.ts +0 -0
- package/schematics/ng-add/schema.js +1 -0
- package/schematics/ng-add/schema.js.map +1 -0
- package/schematics/ng-add/schema.json +0 -42
- package/schematics/ng-add/schema.ts +0 -0
- package/schematics/package.json +3 -0
package/README.md
CHANGED
|
@@ -1,61 +1,64 @@
|
|
|
1
|
-
#
|
|
1
|
+
# NgxThemeStack
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This project was generated using [Angular CLI](https://github.com/angular/angular-cli) version 21.2.0.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Code scaffolding
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
- 🚦 **Signal-based**: Built with modern Angular Signals for reactive theme management.
|
|
9
|
-
- 🛠️ **Schematics Support**: Easy installation and configuration via `ng add`.
|
|
10
|
-
- 🧬 **Flexible**: Support for custom themes and system theme detection.
|
|
7
|
+
Angular CLI includes powerful code scaffolding tools. To generate a new component, run:
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
```bash
|
|
10
|
+
ng generate component component-name
|
|
11
|
+
```
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
For a complete list of available schematics (such as `components`, `directives`, or `pipes`), run:
|
|
15
14
|
|
|
16
15
|
```bash
|
|
17
|
-
ng
|
|
16
|
+
ng generate --help
|
|
18
17
|
```
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
import { ThemeService } from 'ngx-theme-stack';
|
|
27
|
-
|
|
28
|
-
@Component({
|
|
29
|
-
selector: 'app-root',
|
|
30
|
-
template: `
|
|
31
|
-
<button (click)="theme.toggleTheme()">Toggle Theme</button>
|
|
32
|
-
<p>Current theme: {{ theme.currentTheme() }}</p>
|
|
33
|
-
`,
|
|
34
|
-
imports: [],
|
|
35
|
-
})
|
|
36
|
-
export class AppComponent {
|
|
37
|
-
theme = inject(ThemeService);
|
|
38
|
-
}
|
|
19
|
+
## Building
|
|
20
|
+
|
|
21
|
+
To build the library, run:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
ng build ngx-theme-stack
|
|
39
25
|
```
|
|
40
26
|
|
|
41
|
-
|
|
27
|
+
This command will compile your project, and the build artifacts will be placed in the `dist/` directory.
|
|
42
28
|
|
|
43
|
-
###
|
|
29
|
+
### Publishing the Library
|
|
44
30
|
|
|
45
|
-
|
|
31
|
+
Once the project is built, you can publish your library by following these steps:
|
|
32
|
+
|
|
33
|
+
1. Navigate to the `dist` directory:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
cd dist/ngx-theme-stack
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
2. Run the `npm publish` command to publish your library to the npm registry:
|
|
40
|
+
```bash
|
|
41
|
+
npm publish
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Running unit tests
|
|
45
|
+
|
|
46
|
+
To execute unit tests with the [Karma](https://karma-runner.github.io) test runner, use the following command:
|
|
46
47
|
|
|
47
48
|
```bash
|
|
48
|
-
ng
|
|
49
|
+
ng test
|
|
49
50
|
```
|
|
50
51
|
|
|
51
|
-
|
|
52
|
+
## Running end-to-end tests
|
|
52
53
|
|
|
53
|
-
|
|
54
|
+
For end-to-end (e2e) testing, run:
|
|
54
55
|
|
|
55
56
|
```bash
|
|
56
|
-
ng
|
|
57
|
+
ng e2e
|
|
57
58
|
```
|
|
58
59
|
|
|
59
|
-
|
|
60
|
+
Angular CLI does not come with an end-to-end testing framework by default. You can choose one that suits your needs.
|
|
61
|
+
|
|
62
|
+
## Additional Resources
|
|
60
63
|
|
|
61
|
-
|
|
64
|
+
For more information on using the Angular CLI, including detailed command references, visit the [Angular CLI Overview and Command Reference](https://angular.dev/tools/cli) page.
|
|
@@ -168,10 +168,10 @@ class CoreThemeService {
|
|
|
168
168
|
console.warn('[ngx-theme-stack] Could not save theme to localStorage.', e);
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.
|
|
172
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.
|
|
171
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: CoreThemeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
172
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: CoreThemeService, providedIn: 'root' });
|
|
173
173
|
}
|
|
174
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.
|
|
174
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: CoreThemeService, decorators: [{
|
|
175
175
|
type: Injectable,
|
|
176
176
|
args: [{ providedIn: 'root' }]
|
|
177
177
|
}], ctorParameters: () => [] });
|
|
@@ -210,10 +210,10 @@ class ThemeCycleService {
|
|
|
210
210
|
const next = this.#cycle[(index + 1) % this.#cycle.length];
|
|
211
211
|
this.#core.setTheme(next);
|
|
212
212
|
}
|
|
213
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.
|
|
214
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.
|
|
213
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ThemeCycleService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
214
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ThemeCycleService, providedIn: 'root' });
|
|
215
215
|
}
|
|
216
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.
|
|
216
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ThemeCycleService, decorators: [{
|
|
217
217
|
type: Injectable,
|
|
218
218
|
args: [{ providedIn: 'root' }]
|
|
219
219
|
}] });
|
|
@@ -245,10 +245,10 @@ class ThemeSelectService {
|
|
|
245
245
|
select(theme) {
|
|
246
246
|
this.#core.setTheme(theme);
|
|
247
247
|
}
|
|
248
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.
|
|
249
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.
|
|
248
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ThemeSelectService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
249
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ThemeSelectService, providedIn: 'root' });
|
|
250
250
|
}
|
|
251
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.
|
|
251
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ThemeSelectService, decorators: [{
|
|
252
252
|
type: Injectable,
|
|
253
253
|
args: [{ providedIn: 'root' }]
|
|
254
254
|
}] });
|
|
@@ -277,14 +277,18 @@ class ThemeToggleService {
|
|
|
277
277
|
const next = this.#core.selectedTheme() === 'dark' ? 'light' : 'dark';
|
|
278
278
|
this.#core.setTheme(next);
|
|
279
279
|
}
|
|
280
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.
|
|
281
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.
|
|
280
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ThemeToggleService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
281
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ThemeToggleService, providedIn: 'root' });
|
|
282
282
|
}
|
|
283
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.
|
|
283
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ThemeToggleService, decorators: [{
|
|
284
284
|
type: Injectable,
|
|
285
285
|
args: [{ providedIn: 'root' }]
|
|
286
286
|
}] });
|
|
287
287
|
|
|
288
|
+
/*
|
|
289
|
+
* Public API Surface of ngx-theme-stack
|
|
290
|
+
*/
|
|
291
|
+
|
|
288
292
|
/**
|
|
289
293
|
* Generated bundle index. Do not edit.
|
|
290
294
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ngx-theme-stack.mjs","sources":["../../../projects/ngx-theme-stack/src/lib/types.ts","../../../projects/ngx-theme-stack/src/lib/theme-stack.config.ts","../../../projects/ngx-theme-stack/src/lib/core-theme.service.ts","../../../projects/ngx-theme-stack/src/lib/theme-cycle.service.ts","../../../projects/ngx-theme-stack/src/lib/theme-select.service.ts","../../../projects/ngx-theme-stack/src/lib/theme-toggle.service.ts","../../../projects/ngx-theme-stack/src/ngx-theme-stack.ts"],"sourcesContent":["/** Built-in themes. All other values are considered custom themes. */\nexport const DEFAULT_THEMES = ['light', 'dark', 'system'] as const;\n\n/** String union with autocompletion for defaults + any string support for customization. */\nexport type NgTheme = (typeof DEFAULT_THEMES)[number] | (string & {});\n\nexport type NgSystemTheme = Exclude<NgTheme, 'system'>;\nexport type NgMode = 'attribute' | 'class' | 'both';\n\nexport interface NgConfig {\n theme: NgTheme;\n storageKey: string;\n mode: NgMode;\n themes: NgTheme[];\n}\n","import { InjectionToken } from '@angular/core';\nimport { DEFAULT_THEMES, NgConfig } from './types';\n\nexport const DEFAULT_NG_CONFIG = {\n theme: 'system',\n storageKey: 'ngx-theme-stack-theme',\n mode: 'class',\n themes: [...DEFAULT_THEMES],\n} satisfies NgConfig;\n\nexport const NGX_THEME_STACK_CONFIG = new InjectionToken<NgConfig>('NGX_THEME_STACK_CONFIG', {\n factory: () => DEFAULT_NG_CONFIG,\n});\n\n/**\n * Helper function to provide Theme Stack configuration.\n */\nexport function provideThemeStack(config: Partial<NgConfig> = {}) {\n return {\n provide: NGX_THEME_STACK_CONFIG,\n useValue: {\n ...DEFAULT_NG_CONFIG,\n ...config,\n } satisfies NgConfig,\n };\n}\n","import { isPlatformBrowser } from '@angular/common';\nimport {\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 './theme-stack.config';\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 /** List of available themes for Select/Cycle services. Defaults to ['light', 'dark', 'system']. */\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 // ── 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 userTheme = 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.userTheme() === 'dark');\n\n /** Whether the currently applied theme is light. */\n readonly isLight = computed(() => this.userTheme() === 'light');\n\n // ── Event handler ─────────────────────────────────────────────────────────\n\n readonly #onSystemPreferenceChange = () =>\n this.#systemPreference.set(this.resolveSystemPreference());\n\n // ── Lifecycle ─────────────────────────────────────────────────────────────\n\n constructor() {\n if (this.#isBrowser && this.#selectedTheme() === 'system') {\n this.startSystemThemeListener();\n }\n\n effect(() => this.applyThemeToDOM(this.userTheme()));\n\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 `userTheme` 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.#isBrowser) return;\n\n if (!this.#validThemes.has(theme)) {\n throw new Error(\n `[ngx-theme-stack] Invalid theme: \"${theme}\". Valid values are: ${[...this.#validThemes].join(', ')}.`,\n );\n }\n\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 if (!this.#isBrowser) return this.#config.theme;\n return this.readStoredTheme() ?? this.#config.theme;\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(userTheme: NgTheme): void {\n if (!this.#isBrowser) return;\n\n const host = this.#document.documentElement;\n const { mode } = this.#config;\n\n if (mode === 'attribute' || mode === 'both') {\n this.applyThemeAttribute(host, userTheme);\n }\n\n if (mode === 'class' || mode === 'both') {\n this.applyThemeClasses(host, userTheme);\n }\n\n this.applyColorSchemeHint(host, userTheme);\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 for (const t of this.availableThemes) {\n host.classList.remove(t);\n }\n\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 readStoredTheme(): NgTheme | null {\n try {\n const stored = localStorage.getItem(this.#config.storageKey);\n if (stored && this.#validThemes.has(stored as NgTheme)) {\n return stored as NgTheme;\n }\n return null;\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 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-theme.service';\n\n/**\n * Convenience service for cycling through themes in a fixed order.\n *\n * Default cycle: `'light'` → `'dark'` → `'system'` → `'light'` → ...\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\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 userTheme = this.#core.userTheme;\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 /**\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-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\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 userTheme = this.#core.userTheme;\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 /**\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-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\n readonly #core = inject(CoreThemeService);\n\n /** Resolved theme applied to the DOM. Always concrete — never `'system'`. */\n readonly userTheme = this.#core.userTheme;\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 /**\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.selectedTheme() === 'dark' ? 'light' : 'dark';\n this.#core.setTheme(next);\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;AAAA;AACO,MAAM,cAAc,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ;;ACEjD,MAAM,iBAAiB,GAAG;AAC/B,IAAA,KAAK,EAAE,QAAQ;AACf,IAAA,UAAU,EAAE,uBAAuB;AACnC,IAAA,IAAI,EAAE,OAAO;AACb,IAAA,MAAM,EAAE,CAAC,GAAG,cAAc,CAAC;;MAGhB,sBAAsB,GAAG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B,IAAA,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO;AAErC;;;;;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;uGAtBW,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;;;;"}
|
|
1
|
+
{"version":3,"file":"ngx-theme-stack.mjs","sources":["../../../projects/ngx-theme-stack/src/lib/types.ts","../../../projects/ngx-theme-stack/src/lib/services/theme-stack.config.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":["/** Built-in themes. All other values are considered custom themes. */\nexport const DEFAULT_THEMES = ['light', 'dark', 'system'] as const;\n\n/** String union with autocompletion for defaults + any string support for customization. */\nexport type NgTheme = (typeof DEFAULT_THEMES)[number] | (string & {});\n\nexport type NgSystemTheme = Exclude<NgTheme, 'system'>;\nexport type NgMode = 'attribute' | 'class' | 'both';\n\nexport interface NgConfig {\n theme: NgTheme;\n storageKey: string;\n mode: NgMode;\n themes: NgTheme[];\n}\n","import { InjectionToken } from '@angular/core';\nimport { DEFAULT_THEMES, NgConfig } from '../types';\n\nexport const DEFAULT_NG_CONFIG = {\n theme: 'system',\n storageKey: 'ngx-theme-stack-theme',\n mode: 'class',\n themes: [...DEFAULT_THEMES],\n} satisfies NgConfig;\n\nexport const NGX_THEME_STACK_CONFIG = new InjectionToken<NgConfig>('NGX_THEME_STACK_CONFIG', {\n factory: () => DEFAULT_NG_CONFIG,\n});\n\n/**\n * Helper function to provide Theme Stack configuration.\n */\nexport function provideThemeStack(config: Partial<NgConfig> = {}) {\n return {\n provide: NGX_THEME_STACK_CONFIG,\n useValue: {\n ...DEFAULT_NG_CONFIG,\n ...config,\n } satisfies NgConfig,\n };\n}\n","import { isPlatformBrowser } from '@angular/common';\nimport {\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 '../services/theme-stack.config';\nimport { NgTheme, NgSystemTheme } from '../types';\n\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 /** List of available themes for Select/Cycle services. Defaults to ['light', 'dark', 'system']. */\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 // ── 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 userTheme = 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.userTheme() === 'dark');\n\n /** Whether the currently applied theme is light. */\n readonly isLight = computed(() => this.userTheme() === 'light');\n\n // ── Event handler ─────────────────────────────────────────────────────────\n\n readonly #onSystemPreferenceChange = () =>\n this.#systemPreference.set(this.resolveSystemPreference());\n\n // ── Lifecycle ─────────────────────────────────────────────────────────────\n\n constructor() {\n if (this.#isBrowser && this.#selectedTheme() === 'system') {\n this.startSystemThemeListener();\n }\n\n effect(() => this.applyThemeToDOM(this.userTheme()));\n\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 `userTheme` 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.#isBrowser) return;\n\n if (!this.#validThemes.has(theme)) {\n throw new Error(\n `[ngx-theme-stack] Invalid theme: \"${theme}\". Valid values are: ${[...this.#validThemes].join(', ')}.`,\n );\n }\n\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 if (!this.#isBrowser) return this.#config.theme;\n return this.readStoredTheme() ?? this.#config.theme;\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(userTheme: NgTheme): void {\n if (!this.#isBrowser) return;\n\n const host = this.#document.documentElement;\n const { mode } = this.#config;\n\n if (mode === 'attribute' || mode === 'both') {\n this.applyThemeAttribute(host, userTheme);\n }\n\n if (mode === 'class' || mode === 'both') {\n this.applyThemeClasses(host, userTheme);\n }\n\n this.applyColorSchemeHint(host, userTheme);\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 for (const t of this.availableThemes) {\n host.classList.remove(t);\n }\n\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 readStoredTheme(): NgTheme | null {\n try {\n const stored = localStorage.getItem(this.#config.storageKey);\n if (stored && this.#validThemes.has(stored as NgTheme)) {\n return stored as NgTheme;\n }\n return null;\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 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: `'light'` → `'dark'` → `'system'` → `'light'` → ...\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\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 userTheme = this.#core.userTheme;\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 /**\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/**\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\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 userTheme = this.#core.userTheme;\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 /**\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\n readonly #core = inject(CoreThemeService);\n\n /** Resolved theme applied to the DOM. Always concrete — never `'system'`. */\n readonly userTheme = this.#core.userTheme;\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 /**\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.selectedTheme() === '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/services/theme-stack.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';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;AAAA;AACO,MAAM,cAAc,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ;;ACEjD,MAAM,iBAAiB,GAAG;AAC/B,IAAA,KAAK,EAAE,QAAQ;AACf,IAAA,UAAU,EAAE,uBAAuB;AACnC,IAAA,IAAI,EAAE,OAAO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SAAS;;AAGhC,IAAA,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM;;AAG1B,IAAA,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO;AAErC;;;;;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;uGAtBW,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;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ngx-theme-stack",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
|
+
"description": "A stack of themes for Angular applications.",
|
|
5
|
+
"schematics": "./schematics/collection.json",
|
|
6
|
+
"ng-add": {
|
|
7
|
+
"save": "devDependencies"
|
|
8
|
+
},
|
|
4
9
|
"peerDependencies": {
|
|
5
10
|
"@angular/common": "^21.2.0",
|
|
6
11
|
"@angular/core": "^21.2.0"
|
|
@@ -8,12 +13,8 @@
|
|
|
8
13
|
"dependencies": {
|
|
9
14
|
"tslib": "^2.3.0"
|
|
10
15
|
},
|
|
11
|
-
"license": "MIT",
|
|
12
16
|
"sideEffects": false,
|
|
13
|
-
"
|
|
14
|
-
"ng-add": {
|
|
15
|
-
"save": "dependencies"
|
|
16
|
-
},
|
|
17
|
+
"license": "MIT",
|
|
17
18
|
"module": "fesm2022/ngx-theme-stack.mjs",
|
|
18
19
|
"typings": "types/ngx-theme-stack.d.ts",
|
|
19
20
|
"exports": {
|
|
@@ -25,5 +26,5 @@
|
|
|
25
26
|
"default": "./fesm2022/ngx-theme-stack.mjs"
|
|
26
27
|
}
|
|
27
28
|
},
|
|
28
|
-
"type": "
|
|
29
|
-
}
|
|
29
|
+
"type": "module"
|
|
30
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"$schema": "
|
|
2
|
+
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
|
|
3
3
|
"schematics": {
|
|
4
4
|
"ng-add": {
|
|
5
|
+
"description": "Add my library to the project.",
|
|
5
6
|
"factory": "./ng-add/index#ngAdd",
|
|
6
|
-
"schema": "./ng-add/schema.json"
|
|
7
|
-
"description": "Configure ngx-theme-stack in the Angular project"
|
|
7
|
+
"schema": "./ng-add/schema.json"
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
-
}
|
|
10
|
+
}
|
|
@@ -1,13 +1,2 @@
|
|
|
1
1
|
import { Rule } from '@angular-devkit/schematics';
|
|
2
|
-
|
|
3
|
-
setupMode: string;
|
|
4
|
-
storageKey: string;
|
|
5
|
-
defaultTheme: string;
|
|
6
|
-
mode: string;
|
|
7
|
-
themes: string[];
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Main schematic rule for ng-add.
|
|
11
|
-
*/
|
|
12
|
-
export declare function ngAdd(options: SchemaOptions): Rule;
|
|
13
|
-
export {};
|
|
2
|
+
export declare function ngAdd(): Rule;
|
|
@@ -2,136 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ngAdd = ngAdd;
|
|
4
4
|
const schematics_1 = require("@angular-devkit/schematics");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* Returns the minified script to be injected into index.html.
|
|
9
|
-
* Inspired by next-themes to prevent flickering.
|
|
10
|
-
*/
|
|
11
|
-
function getBlockingScript(options) {
|
|
12
|
-
const allThemes = Array.from(new Set(['light', 'dark', 'system', ...(options.themes || [])]));
|
|
13
|
-
const themesJson = JSON.stringify(allThemes);
|
|
14
|
-
const script = `
|
|
15
|
-
(function() {
|
|
16
|
-
try {
|
|
17
|
-
var k = '${options.storageKey}';
|
|
18
|
-
var d = '${options.defaultTheme}';
|
|
19
|
-
var m = '${options.mode}';
|
|
20
|
-
var ths = ${themesJson};
|
|
21
|
-
var s = localStorage.getItem(k);
|
|
22
|
-
var t = s || d;
|
|
23
|
-
if (t === 'system') {
|
|
24
|
-
t = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
25
|
-
}
|
|
26
|
-
var el = document.documentElement;
|
|
27
|
-
if (m === 'class' || m === 'both') {
|
|
28
|
-
for (var i = 0; i < ths.length; i++) {
|
|
29
|
-
el.classList.remove(ths[i]);
|
|
30
|
-
}
|
|
31
|
-
el.classList.add(t);
|
|
32
|
-
}
|
|
33
|
-
if (m === 'attribute' || m === 'both') {
|
|
34
|
-
el.setAttribute('data-theme', t);
|
|
35
|
-
}
|
|
36
|
-
if (t === 'dark' || t === 'light') {
|
|
37
|
-
el.style.setProperty('color-scheme', t);
|
|
38
|
-
}
|
|
39
|
-
} catch (e) {}
|
|
40
|
-
})();
|
|
41
|
-
`;
|
|
42
|
-
return script.replace(/\n\s*/g, '').trim();
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Injects the theme detection script into the index.html file of the project.
|
|
46
|
-
*/
|
|
47
|
-
function injectScript(options) {
|
|
48
|
-
return async (tree) => {
|
|
49
|
-
const workspace = await (0, workspace_1.getWorkspace)(tree);
|
|
50
|
-
const projects = workspace.projects;
|
|
51
|
-
projects.forEach((project) => {
|
|
52
|
-
if (project.extensions['projectType'] !== 'application') {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
const buildTarget = project.targets.get('build');
|
|
56
|
-
const options_ = buildTarget?.options;
|
|
57
|
-
if (!options_) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
const indexHtmlPath = options_['index'];
|
|
61
|
-
if (!indexHtmlPath || !tree.exists(indexHtmlPath)) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
const content = tree.readText(indexHtmlPath);
|
|
65
|
-
const scriptId = 'ngx-theme-stack-initial';
|
|
66
|
-
if (content.includes(scriptId)) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const scriptTag = `<script id="${scriptId}">${getBlockingScript(options)}</script>`;
|
|
70
|
-
const headEndIndex = content.indexOf('</head>');
|
|
71
|
-
if (headEndIndex === -1) {
|
|
72
|
-
throw new schematics_1.SchematicsException(`Could not find </head> tag in ${indexHtmlPath}`);
|
|
73
|
-
}
|
|
74
|
-
const updatedContent = content.slice(0, headEndIndex) + scriptTag + content.slice(headEndIndex);
|
|
75
|
-
tree.overwrite(indexHtmlPath, updatedContent);
|
|
76
|
-
});
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Main schematic rule for ng-add.
|
|
81
|
-
*/
|
|
82
|
-
function ngAdd(options) {
|
|
83
|
-
return async () => {
|
|
84
|
-
await askQuestions(options);
|
|
85
|
-
return (0, schematics_1.chain)([injectScript(options)]);
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Asks interactive questions only in "custom" mode.
|
|
90
|
-
* In "quick" mode, schema.json defaults are used as-is.
|
|
91
|
-
*
|
|
92
|
-
* Note: `@inquirer/prompts` is a transitive dependency of `@angular/cli`,
|
|
93
|
-
* so it is available in any project using `ng add`.
|
|
94
|
-
*/
|
|
95
|
-
async function askQuestions(options) {
|
|
96
|
-
if (options.setupMode === 'custom') {
|
|
97
|
-
options.storageKey = await (0, prompts_1.input)({
|
|
98
|
-
message: 'Which key would you like to use for localStorage?',
|
|
99
|
-
default: options.storageKey || 'ngx-theme-stack-theme',
|
|
100
|
-
});
|
|
101
|
-
options.mode = await (0, prompts_1.select)({
|
|
102
|
-
message: 'How should the theme be applied to the DOM?',
|
|
103
|
-
choices: [
|
|
104
|
-
{ value: 'class', name: 'CSS Class (.dark/.light)' },
|
|
105
|
-
{ value: 'attribute', name: 'Data Attribute (data-theme)' },
|
|
106
|
-
{ value: 'both', name: 'Both' },
|
|
107
|
-
],
|
|
108
|
-
default: options.mode || 'class',
|
|
109
|
-
});
|
|
110
|
-
const customThemes = await (0, prompts_1.input)({
|
|
111
|
-
message: 'Add custom themes? (comma-separated, e.g. "sepia, ocean") — press Enter to skip:',
|
|
112
|
-
default: '',
|
|
113
|
-
});
|
|
114
|
-
// Parse and merge custom themes with defaults
|
|
115
|
-
const parsed = customThemes
|
|
116
|
-
.split(',')
|
|
117
|
-
.map((t) => t.trim())
|
|
118
|
-
.filter((t) => t.length > 0);
|
|
119
|
-
options.themes = Array.from(new Set(['light', 'dark', 'system', ...parsed]));
|
|
120
|
-
// Build choices for defaultTheme including any custom themes
|
|
121
|
-
const defaultChoices = [
|
|
122
|
-
{ value: 'system', name: 'System (OS Preference)' },
|
|
123
|
-
{ value: 'dark', name: 'Dark' },
|
|
124
|
-
{ value: 'light', name: 'Light' },
|
|
125
|
-
...parsed.map((t) => ({ value: t, name: t.charAt(0).toUpperCase() + t.slice(1) })),
|
|
126
|
-
];
|
|
127
|
-
options.defaultTheme = await (0, prompts_1.select)({
|
|
128
|
-
message: 'Which should be the default theme?',
|
|
129
|
-
choices: defaultChoices,
|
|
130
|
-
default: options.defaultTheme || 'system',
|
|
131
|
-
});
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
// Quick mode: merge themes with defaults
|
|
135
|
-
options.themes = Array.from(new Set(['light', 'dark', 'system', ...(options.themes || [])]));
|
|
5
|
+
function ngAdd() {
|
|
6
|
+
// Add an import `MyLibModule` from `ngx-theme-stack` to the root of the user's project.
|
|
7
|
+
return (0, schematics_1.chain)([]);
|
|
136
8
|
}
|
|
137
9
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../projects/ngx-theme-stack/schematics/ng-add/index.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../projects/ngx-theme-stack/schematics/ng-add/index.ts"],"names":[],"mappings":";;AACA,sBAGC;AAJD,2DAAuD;AACvD,SAAgB,KAAK;IACnB,wFAAwF;IACxF,OAAO,IAAA,kBAAK,EAAC,EAAE,CAAC,CAAC;AACnB,CAAC"}
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../../projects/ngx-theme-stack/schematics/ng-add/schema.ts"],"names":[],"mappings":""}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "http://json-schema.org/schema",
|
|
3
|
-
"$id": "NgxThemeStackNgAdd",
|
|
4
|
-
"title": "ngx-theme-stack Add Options",
|
|
5
|
-
"type": "object",
|
|
6
|
-
"properties": {
|
|
7
|
-
"setupMode": {
|
|
8
|
-
"type": "string",
|
|
9
|
-
"default": "quick",
|
|
10
|
-
"x-prompt": {
|
|
11
|
-
"message": "Choose setup mode:",
|
|
12
|
-
"type": "list",
|
|
13
|
-
"items": [
|
|
14
|
-
{ "value": "quick", "label": "Quick (Default settings - Class mode, 'ngx-theme-stack-theme' key)" },
|
|
15
|
-
{ "value": "custom", "label": "Custom (Configure storage, themes, mode)" }
|
|
16
|
-
]
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
"storageKey": {
|
|
20
|
-
"type": "string",
|
|
21
|
-
"description": "LocalStorage key to persist the theme",
|
|
22
|
-
"default": "ngx-theme-stack-theme"
|
|
23
|
-
},
|
|
24
|
-
"defaultTheme": {
|
|
25
|
-
"type": "string",
|
|
26
|
-
"description": "Default theme to be applied",
|
|
27
|
-
"default": "system"
|
|
28
|
-
},
|
|
29
|
-
"themes": {
|
|
30
|
-
"type": "array",
|
|
31
|
-
"description": "All supported themes (including custom ones)",
|
|
32
|
-
"items": { "type": "string" },
|
|
33
|
-
"default": ["dark", "light", "system"]
|
|
34
|
-
},
|
|
35
|
-
"mode": {
|
|
36
|
-
"type": "string",
|
|
37
|
-
"description": "How the theme should be applied to the DOM",
|
|
38
|
-
"enum": ["class", "attribute", "both"],
|
|
39
|
-
"default": "class"
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
File without changes
|