@su-labs/theme 21.0.5 → 22.1.0

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/CHANGELOG.md CHANGED
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [22.1.0] - 2026-11-05
9
+
10
+ ### Changed
11
+ - Theme logic moved to the framework-agnostic `@su-labs/core/theme`; `SuThemeService` is now an Angular wrapper around `SuThemeController` with the same public API
12
+ - Added `@su-labs/core` as a dependency
13
+
14
+ ## [22.0.0] - 2026-11-04
15
+
16
+ ### Changed
17
+ - Upgrade to Angular 22 (peer dependencies now `^22.0.0`)
18
+ - Remove emojis from README.md
19
+
8
20
  ## [21.0.5] - 2026-02-04
9
21
 
10
22
  ### Changed
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  `@su-labs/theme`
4
4
 
5
5
  ![Angular](https://img.shields.io/badge/Angular-DD0031?style=for-the-badge&logo=angular&logoColor=white)
6
- ![Angular Version](https://img.shields.io/badge/Angular-v21-DD0031?style=for-the-badge&logo=angular&logoColor=white)
6
+ ![Angular Version](https://img.shields.io/badge/Angular-v22-DD0031?style=for-the-badge&logo=angular&logoColor=white)
7
7
  ![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?style=for-the-badge&logo=typescript&logoColor=white)
8
8
  ![License](https://img.shields.io/badge/License-MIT-blue.svg?style=for-the-badge)
9
9
  ![NPM](https://img.shields.io/badge/NPM-CB3837?style=for-the-badge&logo=npm&logoColor=white)
@@ -13,33 +13,35 @@
13
13
 
14
14
  A robust and reactive Angular service for managing application themes. It provides a full-featured solution that supports dynamic switching, user preference persistence, and automatic system theme detection.
15
15
 
16
- ## 📚 Table of Contents
16
+ > **Not using Angular?** The same theme logic is available for React, Svelte, vanilla JavaScript and Node in [`@su-labs/core`](https://www.npmjs.com/package/@su-labs/core). See the [installation guides](https://github.com/Greezaaa/settings-utils-lib/blob/main/docs/README.md).
17
17
 
18
- - [Why Use This Library?](#-why-use-this-library)
18
+ ## Table of Contents
19
+
20
+ - [Why Use This Library?](#why-use-this-library)
19
21
  - [Features](#features)
20
22
  - [Available Themes](#available-themes)
21
23
  - [Installation](#installation)
22
- - [Quick Start](#-quick-start)
24
+ - [Quick Start](#quick-start)
23
25
  - [Usage](#usage)
24
- - [API Reference](#-api-reference)
26
+ - [API Reference](#api-reference)
25
27
  - [Configuration Options](#configuration-options)
26
- - [Browser Compatibility](#-browser-compatibility)
27
- - [Related Libraries](#-related-libraries)
28
- - [Troubleshooting](#-troubleshooting)
29
- - [Support](#-support)
28
+ - [Browser Compatibility](#browser-compatibility)
29
+ - [Related Libraries](#related-libraries)
30
+ - [Troubleshooting](#troubleshooting)
31
+ - [Support](#support)
30
32
  - [Contributing](#contributing)
31
33
  - [License](#license)
32
34
 
33
- ## 💡 Why Use This Library?
35
+ ## Why Use This Library?
34
36
 
35
- - 🎯 **Lightweight** - Minimal bundle size
36
- - ⚡ **Reactive** - Built with Angular Signals
37
- - 🎨 **CSS Variables** - Modern CSS custom properties
38
- - 🌓 **System Theme** - Auto-detects OS preference
39
- - 💾 **Persistent** - Remembers user choice
40
- - 🔒 **Type-Safe** - Full TypeScript support
41
- - 📦 **Zero Dependencies** - No bloat
42
- - 🧪 **Well Tested** - Comprehensive test coverage
37
+ - **Lightweight** - Minimal bundle size
38
+ - **Reactive** - Built with Angular Signals
39
+ - **CSS Variables** - Modern CSS custom properties
40
+ - **System Theme** - Auto-detects OS preference
41
+ - **Persistent** - Remembers user choice
42
+ - **Type-Safe** - Full TypeScript support
43
+ - **Zero Dependencies** - No bloat
44
+ - **Well Tested** - Comprehensive test coverage
43
45
 
44
46
  ## Features
45
47
 
@@ -74,10 +76,10 @@ npm install @su-labs/theme
74
76
  This library requires the following peer dependencies:
75
77
 
76
78
  ```sh
77
- npm install @angular/common@^21 @angular/core@^21
79
+ npm install @angular/common@^22 @angular/core@^22
78
80
  ```
79
81
 
80
- ## 🚀 Quick Start
82
+ ## Quick Start
81
83
 
82
84
  ```typescript
83
85
  // 1. Install
@@ -144,7 +146,7 @@ export const appConfig = {
144
146
  };
145
147
  ```
146
148
 
147
- ✅ This ensures the theme is loaded and applied before the application renders. The `SuThemeService` handles all the logic internally, including loading from `localStorage` and applying the initial theme.
149
+ This ensures the theme is loaded and applied before the application renders. The `SuThemeService` handles all the logic internally, including loading from `localStorage` and applying the initial theme.
148
150
 
149
151
  ### Example 2: Using the Theme in Your Styles
150
152
 
@@ -194,9 +196,9 @@ export class ThemeSwitcherComponent {
194
196
  }
195
197
  ```
196
198
 
197
- ✅ This correctly shows how to use your service's public API to switch between themes. The service handles the underlying logic, including persistence and CSS variable application.
199
+ This correctly shows how to use your service's public API to switch between themes. The service handles the underlying logic, including persistence and CSS variable application.
198
200
 
199
- ## 📖 API Reference
201
+ ## API Reference
200
202
 
201
203
  ### Signals
202
204
 
@@ -238,14 +240,14 @@ The service can be configured to fit your application's needs.
238
240
  | `defaultTheme` | The theme to use if no preference is saved in `localStorage`. | `'system'` |
239
241
  | `themes` | An object with custom CSS variable overrides for each theme. | `undefined` |
240
242
 
241
- ## 🌐 Browser Compatibility
243
+ ## Browser Compatibility
242
244
 
243
245
  Works in all modern browsers that support:
244
- - ✅ CSS Custom Properties
245
- - ✅ localStorage API
246
- - ✅ matchMedia (for system theme)
247
- - ✅ Angular 21+
248
- - ✅ ES2022+
246
+ - CSS Custom Properties
247
+ - localStorage API
248
+ - matchMedia (for system theme)
249
+ - Angular 22+
250
+ - ES2022+
249
251
 
250
252
  | Browser | Version |
251
253
  |---------|---------|
@@ -254,16 +256,16 @@ Works in all modern browsers that support:
254
256
  | Safari | ≥ 14 |
255
257
  | Edge | ≥ 90 |
256
258
 
257
- ## 🔗 Related Libraries
259
+ ## Related Libraries
258
260
 
259
261
  Part of the **@su-labs** suite:
260
262
 
261
- - 👀 [@su-labs/visit-tracker](https://www.npmjs.com/package/@su-labs/visit-tracker) - User visit tracking
262
- - 🔔 [@su-labs/notifications](https://www.npmjs.com/package/@su-labs/notifications) - Toast notification system
263
- - 🔍 [@su-labs/scroll-spy](https://www.npmjs.com/package/@su-labs/scroll-spy) - Scroll position tracking
264
- - 📏 [@su-labs/font-size](https://www.npmjs.com/package/@su-labs/font-size) - Accessible font size controls
263
+ - [@su-labs/visit-tracker](https://www.npmjs.com/package/@su-labs/visit-tracker) - User visit tracking
264
+ - [@su-labs/notifications](https://www.npmjs.com/package/@su-labs/notifications) - Toast notification system
265
+ - [@su-labs/scroll-spy](https://www.npmjs.com/package/@su-labs/scroll-spy) - Scroll position tracking
266
+ - [@su-labs/font-size](https://www.npmjs.com/package/@su-labs/font-size) - Accessible font size controls
265
267
 
266
- ## 🔧 Troubleshooting
268
+ ## Troubleshooting
267
269
 
268
270
  ### Theme not persisting?
269
271
 
@@ -279,13 +281,13 @@ Check that your OS has a theme preference set and your browser supports `prefers
279
281
 
280
282
  ### TypeScript errors?
281
283
 
282
- Ensure you have Angular 21+ and TypeScript 5.7+ installed.
284
+ Ensure you have Angular 22+ and TypeScript 6.0+ installed.
283
285
 
284
- ## 💬 Support
286
+ ## Support
285
287
 
286
- - 🐛 Found a bug? [Open an issue](https://github.com/Greezaaa/settings-utils-lib/issues)
287
- - 💡 Have a feature request? [Start a discussion](https://github.com/Greezaaa/settings-utils-lib/discussions)
288
- - ⭐ Like this library? [Give it a star!](https://github.com/Greezaaa/settings-utils-lib)
288
+ - Found a bug? [Open an issue](https://github.com/Greezaaa/settings-utils-lib/issues)
289
+ - Have a feature request? [Start a discussion](https://github.com/Greezaaa/settings-utils-lib/discussions)
290
+ - Like this library? [Give it a star!](https://github.com/Greezaaa/settings-utils-lib)
289
291
 
290
292
  ## Notes
291
293
 
@@ -1,189 +1,53 @@
1
- import * as i0 from '@angular/core';
2
- import { signal, Injectable } from '@angular/core';
3
-
4
- /**
5
- * Service for managing application themes.
6
- *
7
- * Provides reactive theme state via signals and methods to set/get the theme.
8
- * Supports user preference, system-based themes, and custom CSS variable overrides.
9
- */
10
- class SuThemeService {
11
- /** Configuration for the theme, provided during initialization */
12
- suThemeConfig = {};
13
- /** CSS variable prefix, can be customized */
14
- cssVarPrefix = '--su-theme-';
15
- /** localStorage key for storing user preference, can be customized */
16
- storageKey = 'su:theme';
17
- /** User selected theme (can be 'system') */
18
- userPreference = 'light';
19
- /** Current resolved theme signal ('light' | 'dark' | 'contrast' | 'custom') */
20
- currentThemeSignal = signal('light', ...(ngDevMode ? [{ debugName: "currentThemeSignal" }] : []));
21
- /** Read-only access to the resolved theme signal */
22
- theme = this.currentThemeSignal.asReadonly();
23
- /** System media query listener callback */
24
- systemChangeHandler = null;
25
- /** MediaQueryList instance for system theme changes */
26
- mediaQuery;
27
- /**
28
- * Initialize the service with optional configuration.
29
- * Should be called from APP_INITIALIZER for default setup.
30
- * @param config Optional configuration object.
31
- */
32
- init(config) {
33
- // Apply configuration and defaults
34
- this.suThemeConfig = { ...this.suThemeConfig, ...config };
35
- this.cssVarPrefix = this.suThemeConfig.cssVarPrefix ?? this.cssVarPrefix;
36
- this.storageKey = this.suThemeConfig.storageKey ?? this.storageKey;
37
- // Set user preference from localStorage or default
38
- this.userPreference = localStorage.getItem(this.storageKey) ?? this.suThemeConfig.defaultTheme ?? 'system';
39
- // Set the initial theme based on the resolved preference
40
- this.setTheme(this.userPreference, false);
41
- }
42
- /**
43
- * Get the currently active, resolved theme.
44
- * @returns The resolved theme name ('light', 'dark', 'contrast', 'custom').
45
- */
46
- getTheme() {
47
- return this.currentThemeSignal();
48
- }
49
- /**
50
- * Get the user's theme preference (may be 'system').
51
- * @returns The user preference ('light', 'dark', 'contrast', 'custom', 'system').
52
- */
53
- getUserPreference() {
54
- return this.userPreference;
55
- }
56
- /**
57
- * Set a new theme.
58
- * @param theme The theme to set ('light', 'dark', 'contrast', 'custom', 'system').
59
- * @param persist Whether to store the preference in localStorage (default: true).
60
- */
61
- setTheme(theme, persist = true) {
62
- // Stop listening to system changes if we are setting a non-system theme
63
- if (theme !== 'system') {
64
- this.stopListeningToSystemChanges();
65
- }
66
- // Resolve the final theme name
67
- const resolvedTheme = theme === 'system' ? this.resolveSystemTheme() : theme;
68
- // Update internal state and signals
69
- this.userPreference = theme;
70
- this.currentThemeSignal.set(resolvedTheme);
71
- // Apply the theme to the document and manage system listeners
72
- document.documentElement.setAttribute('data-theme', resolvedTheme);
73
- this.applyThemeVariables(resolvedTheme);
74
- // Persist preference to localStorage
75
- if (persist) {
76
- localStorage.setItem(this.storageKey, theme);
77
- }
78
- // Start listening for system changes if the new preference is 'system'
79
- if (theme === 'system') {
80
- this.listenToSystemChanges();
81
- }
82
- }
83
- /**
84
- * Resolve the current system theme using prefers-color-scheme.
85
- * @returns 'dark' if system prefers dark mode, otherwise 'light'.
86
- */
87
- resolveSystemTheme() {
88
- if (typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
89
- return 'dark';
90
- }
91
- return 'light';
92
- }
93
- /**
94
- * Listen to system theme changes and automatically update the theme.
95
- * Only active when user preference is 'system'.
96
- */
97
- listenToSystemChanges() {
98
- // Only set up the listener if it's not already active
99
- if (!this.mediaQuery) {
100
- this.mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
101
- this.systemChangeHandler = (event) => {
102
- // Only react if the user preference is still 'system'
103
- if (this.userPreference === 'system') {
104
- this.currentThemeSignal.set(event.matches ? 'dark' : 'light');
105
- document.documentElement.setAttribute('data-theme', this.currentThemeSignal());
106
- this.applyThemeVariables(this.currentThemeSignal());
107
- }
108
- };
109
- this.mediaQuery.addEventListener('change', this.systemChangeHandler);
110
- }
111
- }
112
- /**
113
- * Stop listening to system theme changes.
114
- * Should be called when the service is destroyed or theme is no longer 'system'.
115
- */
116
- stopListeningToSystemChanges() {
117
- if (this.mediaQuery && this.systemChangeHandler) {
118
- this.mediaQuery.removeEventListener('change', this.systemChangeHandler);
119
- this.systemChangeHandler = null;
120
- this.mediaQuery = undefined;
121
- }
122
- }
123
- /**
124
- * Apply CSS variables for the given theme.
125
- * This method applies default variables if they are not overridden in the config.
126
- * @param theme The theme whose variables should be applied.
127
- */
128
- applyThemeVariables(theme) {
129
- // Map of default variables for light and dark themes
130
- const defaultThemes = {
131
- light: {
132
- color: '#333',
133
- background: '#fff',
134
- 'main-accent': '#007bff',
135
- 'second-accent': '#6c757d',
136
- 'third-accent': '#28a745',
137
- },
138
- dark: {
139
- color: '#f0f0f0',
140
- background: '#1a1a1a',
141
- 'main-accent': '#38a8e1',
142
- 'second-accent': '#a8b0b5',
143
- 'third-accent': '#2ee057',
144
- },
145
- // You can add more default themes here
146
- contrast: {
147
- color: '#000',
148
- background: '#fff',
149
- 'main-accent': '#ff0000',
150
- },
151
- custom: {},
152
- system: {},
153
- };
154
- // Use a combination of default and user-defined variables
155
- const variablesToApply = {
156
- ...defaultThemes[theme],
157
- ...(this.suThemeConfig.themes?.[theme] ?? {}),
158
- };
159
- // Apply the variables to the root element
160
- for (const [key, value] of Object.entries(variablesToApply)) {
161
- if (value) {
162
- document.documentElement.style.setProperty(`${this.cssVarPrefix}${key}`, value);
163
- }
164
- }
165
- }
166
- /** Angular lifecycle hook for service destruction */
167
- ngOnDestroy() {
168
- this.stopListeningToSystemChanges();
169
- }
170
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: SuThemeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
171
- static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: SuThemeService, providedIn: 'root' });
172
- }
173
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: SuThemeService, decorators: [{
174
- type: Injectable,
175
- args: [{
176
- providedIn: 'root',
177
- }]
178
- }] });
179
-
180
- /*
181
- * Public API Surface of theme
182
- */
183
-
184
- /**
185
- * Generated bundle index. Do not edit.
186
- */
187
-
1
+ import * as i0 from "@angular/core";
2
+ import { Injectable, signal } from "@angular/core";
3
+ import { SuThemeController } from "@su-labs/core/theme";
4
+ var SuThemeService = class SuThemeService {
5
+ controller = new SuThemeController();
6
+ currentThemeSignal = signal(this.controller.getTheme(), ...ngDevMode ? [{ debugName: "currentThemeSignal" }] : /* istanbul ignore next */ []);
7
+ theme = this.currentThemeSignal.asReadonly();
8
+ unsubscribe = this.controller.theme.subscribe((theme) => this.currentThemeSignal.set(theme));
9
+ init(config) {
10
+ this.controller.init(config);
11
+ }
12
+ getTheme() {
13
+ return this.currentThemeSignal();
14
+ }
15
+ getUserPreference() {
16
+ return this.controller.getUserPreference();
17
+ }
18
+ setTheme(theme, persist = true) {
19
+ this.controller.setTheme(theme, persist);
20
+ }
21
+ ngOnDestroy() {
22
+ this.unsubscribe();
23
+ this.controller.destroy();
24
+ }
25
+ static ɵfac = i0.ɵɵngDeclareFactory({
26
+ minVersion: "12.0.0",
27
+ version: "22.2.4",
28
+ ngImport: i0,
29
+ type: SuThemeService,
30
+ deps: [],
31
+ target: i0.ɵɵFactoryTarget.Injectable
32
+ });
33
+ static ɵprov = i0.ɵɵngDeclareInjectable({
34
+ minVersion: "12.0.0",
35
+ version: "22.2.4",
36
+ ngImport: i0,
37
+ type: SuThemeService,
38
+ providedIn: "root"
39
+ });
40
+ };
41
+ i0.ɵɵngDeclareClassMetadata({
42
+ minVersion: "12.0.0",
43
+ version: "22.2.4",
44
+ ngImport: i0,
45
+ type: SuThemeService,
46
+ decorators: [{
47
+ type: Injectable,
48
+ args: [{ providedIn: "root" }]
49
+ }]
50
+ });
188
51
  export { SuThemeService };
189
- //# sourceMappingURL=su-labs-theme.mjs.map
52
+
53
+ //# sourceMappingURL=su-labs-theme.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"su-labs-theme.mjs","sources":["../../../../projects/su-labs/theme/src/lib/theme.service.ts","../../../../projects/su-labs/theme/src/public-api.ts","../../../../projects/su-labs/theme/src/su-labs-theme.ts"],"sourcesContent":["import { Injectable, signal, WritableSignal, OnDestroy, computed } from '@angular/core';\nimport { SuThemeConfig, SuThemeName, ThemeCssVariables } from './theme.types';\n\n/**\n * Service for managing application themes.\n *\n * Provides reactive theme state via signals and methods to set/get the theme.\n * Supports user preference, system-based themes, and custom CSS variable overrides.\n */\n@Injectable({\n providedIn: 'root',\n})\nexport class SuThemeService implements OnDestroy {\n /** Configuration for the theme, provided during initialization */\n private suThemeConfig: SuThemeConfig = {};\n\n /** CSS variable prefix, can be customized */\n private cssVarPrefix = '--su-theme-';\n\n /** localStorage key for storing user preference, can be customized */\n private storageKey = 'su:theme';\n\n /** User selected theme (can be 'system') */\n private userPreference: SuThemeName = 'light';\n\n /** Current resolved theme signal ('light' | 'dark' | 'contrast' | 'custom') */\n private currentThemeSignal: WritableSignal<SuThemeName> = signal('light');\n\n /** Read-only access to the resolved theme signal */\n readonly theme = this.currentThemeSignal.asReadonly();\n\n /** System media query listener callback */\n private systemChangeHandler: ((event: MediaQueryListEvent) => void) | null = null;\n\n /** MediaQueryList instance for system theme changes */\n private mediaQuery?: MediaQueryList;\n\n /**\n * Initialize the service with optional configuration.\n * Should be called from APP_INITIALIZER for default setup.\n * @param config Optional configuration object.\n */\n public init(config?: SuThemeConfig): void {\n // Apply configuration and defaults\n this.suThemeConfig = { ...this.suThemeConfig, ...config };\n this.cssVarPrefix = this.suThemeConfig.cssVarPrefix ?? this.cssVarPrefix;\n this.storageKey = this.suThemeConfig.storageKey ?? this.storageKey;\n\n // Set user preference from localStorage or default\n this.userPreference = (localStorage.getItem(this.storageKey) as SuThemeName | null) ?? this.suThemeConfig.defaultTheme ?? 'system';\n\n // Set the initial theme based on the resolved preference\n this.setTheme(this.userPreference, false);\n }\n\n /**\n * Get the currently active, resolved theme.\n * @returns The resolved theme name ('light', 'dark', 'contrast', 'custom').\n */\n public getTheme(): SuThemeName {\n return this.currentThemeSignal();\n }\n\n /**\n * Get the user's theme preference (may be 'system').\n * @returns The user preference ('light', 'dark', 'contrast', 'custom', 'system').\n */\n public getUserPreference(): SuThemeName {\n return this.userPreference;\n }\n\n /**\n * Set a new theme.\n * @param theme The theme to set ('light', 'dark', 'contrast', 'custom', 'system').\n * @param persist Whether to store the preference in localStorage (default: true).\n */\n public setTheme(theme: SuThemeName, persist: boolean = true): void {\n // Stop listening to system changes if we are setting a non-system theme\n if (theme !== 'system') {\n this.stopListeningToSystemChanges();\n }\n\n // Resolve the final theme name\n const resolvedTheme: SuThemeName = theme === 'system' ? this.resolveSystemTheme() : theme;\n\n // Update internal state and signals\n this.userPreference = theme;\n this.currentThemeSignal.set(resolvedTheme);\n\n // Apply the theme to the document and manage system listeners\n document.documentElement.setAttribute('data-theme', resolvedTheme);\n this.applyThemeVariables(resolvedTheme);\n\n // Persist preference to localStorage\n if (persist) {\n localStorage.setItem(this.storageKey, theme);\n }\n\n // Start listening for system changes if the new preference is 'system'\n if (theme === 'system') {\n this.listenToSystemChanges();\n }\n }\n\n /**\n * Resolve the current system theme using prefers-color-scheme.\n * @returns 'dark' if system prefers dark mode, otherwise 'light'.\n */\n private resolveSystemTheme(): SuThemeName {\n if (typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches) {\n return 'dark';\n }\n return 'light';\n }\n\n /**\n * Listen to system theme changes and automatically update the theme.\n * Only active when user preference is 'system'.\n */\n private listenToSystemChanges(): void {\n // Only set up the listener if it's not already active\n if (!this.mediaQuery) {\n this.mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');\n this.systemChangeHandler = (event: MediaQueryListEvent): void => {\n // Only react if the user preference is still 'system'\n if (this.userPreference === 'system') {\n this.currentThemeSignal.set(event.matches ? 'dark' : 'light');\n document.documentElement.setAttribute('data-theme', this.currentThemeSignal());\n this.applyThemeVariables(this.currentThemeSignal());\n }\n };\n this.mediaQuery.addEventListener('change', this.systemChangeHandler);\n }\n }\n\n /**\n * Stop listening to system theme changes.\n * Should be called when the service is destroyed or theme is no longer 'system'.\n */\n private stopListeningToSystemChanges(): void {\n if (this.mediaQuery && this.systemChangeHandler) {\n this.mediaQuery.removeEventListener('change', this.systemChangeHandler);\n this.systemChangeHandler = null;\n this.mediaQuery = undefined;\n }\n }\n\n /**\n * Apply CSS variables for the given theme.\n * This method applies default variables if they are not overridden in the config.\n * @param theme The theme whose variables should be applied.\n */\n private applyThemeVariables(theme: SuThemeName): void {\n // Map of default variables for light and dark themes\n const defaultThemes: Record<SuThemeName, ThemeCssVariables> = {\n light: {\n color: '#333',\n background: '#fff',\n 'main-accent': '#007bff',\n 'second-accent': '#6c757d',\n 'third-accent': '#28a745',\n },\n dark: {\n color: '#f0f0f0',\n background: '#1a1a1a',\n 'main-accent': '#38a8e1',\n 'second-accent': '#a8b0b5',\n 'third-accent': '#2ee057',\n },\n // You can add more default themes here\n contrast: {\n color: '#000',\n background: '#fff',\n 'main-accent': '#ff0000',\n },\n custom: {},\n system: {},\n };\n\n // Use a combination of default and user-defined variables\n const variablesToApply = {\n ...defaultThemes[theme],\n ...(this.suThemeConfig.themes?.[theme] ?? {}),\n };\n\n // Apply the variables to the root element\n for (const [key, value] of Object.entries(variablesToApply)) {\n if (value) {\n document.documentElement.style.setProperty(\n `${this.cssVarPrefix}${key}`,\n value,\n );\n }\n }\n }\n\n /** Angular lifecycle hook for service destruction */\n ngOnDestroy(): void {\n this.stopListeningToSystemChanges();\n }\n}","/*\n * Public API Surface of theme\n */\n\nexport * from './lib/theme.service';\nexport * from './lib/theme.types';\n\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;AAGA;;;;;AAKG;MAIU,cAAc,CAAA;;IAEjB,aAAa,GAAkB,EAAE;;IAGjC,YAAY,GAAG,aAAa;;IAG5B,UAAU,GAAG,UAAU;;IAGvB,cAAc,GAAgB,OAAO;;AAGrC,IAAA,kBAAkB,GAAgC,MAAM,CAAC,OAAO,8DAAC;;AAGhE,IAAA,KAAK,GAAG,IAAI,CAAC,kBAAkB,C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AAI,CAAC,4BAA4B,EAAE;IACrC;uGA3LW,cAAc,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;AAAd,IAAA,OAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,QAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,cAAc,cAFb,MAAM,EAAA,CAAA;;2FAEP,cAAc,EAAA,UAAA,EAAA,CAAA;kBAH1B,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA;;;ACXD;;AAEG;;ACFH;;AAEG;;;;"}
1
+ {"version":3,"file":"su-labs-theme.mjs","names":[],"sources":["../../../../projects/su-labs/theme/src/lib/theme.service.ts"],"sourcesContent":["import { Injectable, OnDestroy, signal } from '@angular/core';\nimport { SuThemeConfig, SuThemeController, SuThemeName } from '@su-labs/core/theme';\n\n/**\n * Angular service for managing application themes.\n *\n * Thin wrapper around the framework-agnostic `SuThemeController` from `@su-labs/core/theme`\n * that exposes the resolved theme as an Angular signal.\n */\n@Injectable({\n providedIn: 'root',\n})\nexport class SuThemeService implements OnDestroy {\n /** Framework-agnostic theme logic */\n private readonly controller = new SuThemeController();\n\n /** Current resolved theme signal, kept in sync with the controller */\n private readonly currentThemeSignal = signal<SuThemeName>(this.controller.getTheme());\n\n /** Read-only access to the resolved theme signal */\n readonly theme = this.currentThemeSignal.asReadonly();\n\n /** Stops syncing the controller's theme into the signal */\n private readonly unsubscribe = this.controller.theme.subscribe((theme) => this.currentThemeSignal.set(theme));\n\n /**\n * Initialize the service with optional configuration.\n * Should be called from an app initializer for default setup.\n * @param config Optional configuration object.\n */\n public init(config?: SuThemeConfig): void {\n this.controller.init(config);\n }\n\n /**\n * Get the currently active, resolved theme.\n * @returns The resolved theme name ('light', 'dark', 'contrast', 'custom').\n */\n public getTheme(): SuThemeName {\n return this.currentThemeSignal();\n }\n\n /**\n * Get the user's theme preference (may be 'system').\n * @returns The user preference ('light', 'dark', 'contrast', 'custom', 'system').\n */\n public getUserPreference(): SuThemeName {\n return this.controller.getUserPreference();\n }\n\n /**\n * Set a new theme.\n * @param theme The theme to set ('light', 'dark', 'contrast', 'custom', 'system').\n * @param persist Whether to store the preference in localStorage (default: true).\n */\n public setTheme(theme: SuThemeName, persist: boolean = true): void {\n this.controller.setTheme(theme, persist);\n }\n\n /** Angular lifecycle hook for service destruction */\n ngOnDestroy(): void {\n this.unsubscribe();\n this.controller.destroy();\n }\n}\n"],"mappings":";;;AAYA,IAAa,iBAAb,MAAa,eAAc;CAER,aAAa,IAAI,kBAAiB;CAGlC,qBAAqB,OAAoB,KAAK,WAAW,SAAQ,GAAG,GAAA,YAAA,CAAA,EAAA,WAAA,qBAAA,CAAA,+BAAA,CAAA,CAAA;CAG5E,QAAQ,KAAK,mBAAmB,WAAU;CAGlC,cAAc,KAAK,WAAW,MAAM,WAAW,UAAU,KAAK,mBAAmB,IAAI,KAAK,CAAC;CAOrG,KAAK,QAAsB;EAChC,KAAK,WAAW,KAAK,MAAM;CAC7B;CAMO,WAAQ;EACb,OAAO,KAAK,mBAAkB;CAChC;CAMO,oBAAiB;EACtB,OAAO,KAAK,WAAW,kBAAiB;CAC1C;CAOO,SAAS,OAAoB,UAAmB,MAAI;EACzD,KAAK,WAAW,SAAS,OAAO,OAAO;CACzC;CAGA,cAAW;EACT,KAAK,YAAW;EAChB,KAAK,WAAW,QAAO;CACzB;CAnDW,OAAA,OAAA,GAAA,mBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;EAAc,MAAA,CAAA;EAAA,QAAA,GAAA,gBAAA;CAAA,CAAA;CAAd,OAAA,QAAA,GAAA,sBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;EAAc,YAFb;CAAM,CAAA;;AAEP,GAAA,yBAAA;CAAA,YAAA;CAAA,SAAA;CAAA,UAAA;CAAA,MAAA;CAAc,YAAA,CAAA;EAH1B,MAAA;EAAW,MAAA,CAAA,EACV,YAAY,OACb,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@su-labs/theme",
3
- "version": "21.0.5",
3
+ "version": "22.1.0",
4
4
  "keywords": [
5
5
  "angular",
6
6
  "theme",
@@ -18,7 +18,7 @@
18
18
  "system-theme",
19
19
  "prefers-color-scheme"
20
20
  ],
21
- "homepage": "https://github.com/Greezaaa/settings-utils-lib/tree/main/projects/su-labs/font-size#readme",
21
+ "homepage": "https://github.com/Greezaaa/settings-utils-lib/tree/main/projects/su-labs/theme#readme",
22
22
  "author": {
23
23
  "name": "Yevheniy Alekseyev",
24
24
  "email": "yev.alekseyev@gmail.com",
@@ -27,16 +27,17 @@
27
27
  "repository": {
28
28
  "type": "git",
29
29
  "url": "https://github.com/Greezaaa/settings-utils-lib.git",
30
- "directory": "projects/su-labs/font-size"
30
+ "directory": "projects/su-labs/theme"
31
31
  },
32
32
  "bugs": {
33
33
  "url": "https://github.com/Greezaaa/settings-utils-lib/issues"
34
34
  },
35
35
  "peerDependencies": {
36
- "@angular/common": "^21.0.0",
37
- "@angular/core": "^21.0.0"
36
+ "@angular/common": "^22.0.0",
37
+ "@angular/core": "^22.0.0"
38
38
  },
39
39
  "dependencies": {
40
+ "@su-labs/core": "^1.0.0",
40
41
  "tslib": "^2.3.0"
41
42
  },
42
43
  "sideEffects": false,
@@ -51,5 +52,6 @@
51
52
  "types": "./types/su-labs-theme.d.ts",
52
53
  "default": "./fesm2022/su-labs-theme.mjs"
53
54
  }
54
- }
55
+ },
56
+ "type": "module"
55
57
  }
@@ -1,100 +1,46 @@
1
- import * as i0 from '@angular/core';
2
- import { OnDestroy } from '@angular/core';
3
-
4
- /** Available theme names */
5
- type SuThemeName = 'light' | 'dark' | 'contrast' | 'custom' | 'system';
6
- /** Theme configuration */
7
- interface SuThemeConfig {
8
- /** localStorage key for storing selected theme */
9
- storageKey?: string;
10
- /** CSS variable prefix, applied to all theme variables */
11
- cssVarPrefix?: string;
12
- /** Default theme if nothing saved in localStorage */
13
- defaultTheme?: SuThemeName;
14
- /** Optional CSS variable overrides for specific themes */
15
- themes?: Partial<Record<SuThemeName, ThemeCssVariables>>;
16
- }
17
- /** Available CSS variables for theming */
18
- interface ThemeCssVariables {
19
- color?: string;
20
- background?: string;
21
- 'main-accent'?: string;
22
- 'second-accent'?: string;
23
- 'third-accent'?: string;
24
- [key: string]: string | undefined;
25
- }
26
-
1
+ import * as i0 from "@angular/core";
2
+ import { OnDestroy } from "@angular/core";
3
+ import { SuThemeConfig, SuThemeConfig as SuThemeConfig$1, SuThemeName, SuThemeName as SuThemeName$1, ThemeCssVariables } from "@su-labs/core/theme";
27
4
  /**
28
- * Service for managing application themes.
5
+ * Angular service for managing application themes.
29
6
  *
30
- * Provides reactive theme state via signals and methods to set/get the theme.
31
- * Supports user preference, system-based themes, and custom CSS variable overrides.
7
+ * Thin wrapper around the framework-agnostic `SuThemeController` from `@su-labs/core/theme`
8
+ * that exposes the resolved theme as an Angular signal.
32
9
  */
33
- declare class SuThemeService implements OnDestroy {
34
- /** Configuration for the theme, provided during initialization */
35
- private suThemeConfig;
36
- /** CSS variable prefix, can be customized */
37
- private cssVarPrefix;
38
- /** localStorage key for storing user preference, can be customized */
39
- private storageKey;
40
- /** User selected theme (can be 'system') */
41
- private userPreference;
42
- /** Current resolved theme signal ('light' | 'dark' | 'contrast' | 'custom') */
43
- private currentThemeSignal;
44
- /** Read-only access to the resolved theme signal */
45
- readonly theme: i0.Signal<SuThemeName>;
46
- /** System media query listener callback */
47
- private systemChangeHandler;
48
- /** MediaQueryList instance for system theme changes */
49
- private mediaQuery?;
50
- /**
51
- * Initialize the service with optional configuration.
52
- * Should be called from APP_INITIALIZER for default setup.
53
- * @param config Optional configuration object.
54
- */
55
- init(config?: SuThemeConfig): void;
56
- /**
57
- * Get the currently active, resolved theme.
58
- * @returns The resolved theme name ('light', 'dark', 'contrast', 'custom').
59
- */
60
- getTheme(): SuThemeName;
61
- /**
62
- * Get the user's theme preference (may be 'system').
63
- * @returns The user preference ('light', 'dark', 'contrast', 'custom', 'system').
64
- */
65
- getUserPreference(): SuThemeName;
66
- /**
67
- * Set a new theme.
68
- * @param theme The theme to set ('light', 'dark', 'contrast', 'custom', 'system').
69
- * @param persist Whether to store the preference in localStorage (default: true).
70
- */
71
- setTheme(theme: SuThemeName, persist?: boolean): void;
72
- /**
73
- * Resolve the current system theme using prefers-color-scheme.
74
- * @returns 'dark' if system prefers dark mode, otherwise 'light'.
75
- */
76
- private resolveSystemTheme;
77
- /**
78
- * Listen to system theme changes and automatically update the theme.
79
- * Only active when user preference is 'system'.
80
- */
81
- private listenToSystemChanges;
82
- /**
83
- * Stop listening to system theme changes.
84
- * Should be called when the service is destroyed or theme is no longer 'system'.
85
- */
86
- private stopListeningToSystemChanges;
87
- /**
88
- * Apply CSS variables for the given theme.
89
- * This method applies default variables if they are not overridden in the config.
90
- * @param theme The theme whose variables should be applied.
91
- */
92
- private applyThemeVariables;
93
- /** Angular lifecycle hook for service destruction */
94
- ngOnDestroy(): void;
95
- static ɵfac: i0.ɵɵFactoryDeclaration<SuThemeService, never>;
96
- static ɵprov: i0.ɵɵInjectableDeclaration<SuThemeService>;
10
+ export declare class SuThemeService implements OnDestroy {
11
+ /** Framework-agnostic theme logic */
12
+ private readonly controller;
13
+ /** Current resolved theme signal, kept in sync with the controller */
14
+ private readonly currentThemeSignal;
15
+ /** Read-only access to the resolved theme signal */
16
+ readonly theme: import("@angular/core").Signal<SuThemeName$1>;
17
+ /** Stops syncing the controller's theme into the signal */
18
+ private readonly unsubscribe;
19
+ /**
20
+ * Initialize the service with optional configuration.
21
+ * Should be called from an app initializer for default setup.
22
+ * @param config Optional configuration object.
23
+ */
24
+ init(config?: SuThemeConfig$1): void;
25
+ /**
26
+ * Get the currently active, resolved theme.
27
+ * @returns The resolved theme name ('light', 'dark', 'contrast', 'custom').
28
+ */
29
+ getTheme(): SuThemeName$1;
30
+ /**
31
+ * Get the user's theme preference (may be 'system').
32
+ * @returns The user preference ('light', 'dark', 'contrast', 'custom', 'system').
33
+ */
34
+ getUserPreference(): SuThemeName$1;
35
+ /**
36
+ * Set a new theme.
37
+ * @param theme The theme to set ('light', 'dark', 'contrast', 'custom', 'system').
38
+ * @param persist Whether to store the preference in localStorage (default: true).
39
+ */
40
+ setTheme(theme: SuThemeName$1, persist?: boolean): void;
41
+ /** Angular lifecycle hook for service destruction */
42
+ ngOnDestroy(): void;
43
+ static ɵfac: i0.ɵɵFactoryDeclaration<SuThemeService, never>;
44
+ static ɵprov: i0.ɵɵInjectableDeclaration<any>;
97
45
  }
98
-
99
- export { SuThemeService };
100
- export type { SuThemeConfig, SuThemeName, ThemeCssVariables };
46
+ export type { SuThemeConfig, SuThemeName, ThemeCssVariables };