@su-labs/theme 21.0.5 → 22.0.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 +6 -0
- package/README.md +33 -33
- package/fesm2022/su-labs-theme.mjs +121 -188
- package/fesm2022/su-labs-theme.mjs.map +1 -1
- package/package.json +7 -6
- package/types/su-labs-theme.d.ts +84 -89
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,12 @@ 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.0.0] - 2026-11-04
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
- Upgrade to Angular 22 (peer dependencies now `^22.0.0`)
|
|
12
|
+
- Remove emojis from README.md
|
|
13
|
+
|
|
8
14
|
## [21.0.5] - 2026-02-04
|
|
9
15
|
|
|
10
16
|
### Changed
|
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
`@su-labs/theme`
|
|
4
4
|
|
|
5
5
|

|
|
6
|
-

|
|
7
7
|

|
|
8
8
|

|
|
9
9
|

|
|
@@ -13,7 +13,7 @@
|
|
|
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
|
-
##
|
|
16
|
+
## Table of Contents
|
|
17
17
|
|
|
18
18
|
- [Why Use This Library?](#-why-use-this-library)
|
|
19
19
|
- [Features](#features)
|
|
@@ -30,16 +30,16 @@ A robust and reactive Angular service for managing application themes. It provid
|
|
|
30
30
|
- [Contributing](#contributing)
|
|
31
31
|
- [License](#license)
|
|
32
32
|
|
|
33
|
-
##
|
|
33
|
+
## Why Use This Library?
|
|
34
34
|
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
42
|
-
-
|
|
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
|
|
43
43
|
|
|
44
44
|
## Features
|
|
45
45
|
|
|
@@ -74,10 +74,10 @@ npm install @su-labs/theme
|
|
|
74
74
|
This library requires the following peer dependencies:
|
|
75
75
|
|
|
76
76
|
```sh
|
|
77
|
-
npm install @angular/common@^
|
|
77
|
+
npm install @angular/common@^22 @angular/core@^22
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
-
##
|
|
80
|
+
## Quick Start
|
|
81
81
|
|
|
82
82
|
```typescript
|
|
83
83
|
// 1. Install
|
|
@@ -144,7 +144,7 @@ export const appConfig = {
|
|
|
144
144
|
};
|
|
145
145
|
```
|
|
146
146
|
|
|
147
|
-
|
|
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.
|
|
148
148
|
|
|
149
149
|
### Example 2: Using the Theme in Your Styles
|
|
150
150
|
|
|
@@ -194,9 +194,9 @@ export class ThemeSwitcherComponent {
|
|
|
194
194
|
}
|
|
195
195
|
```
|
|
196
196
|
|
|
197
|
-
|
|
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.
|
|
198
198
|
|
|
199
|
-
##
|
|
199
|
+
## API Reference
|
|
200
200
|
|
|
201
201
|
### Signals
|
|
202
202
|
|
|
@@ -238,14 +238,14 @@ The service can be configured to fit your application's needs.
|
|
|
238
238
|
| `defaultTheme` | The theme to use if no preference is saved in `localStorage`. | `'system'` |
|
|
239
239
|
| `themes` | An object with custom CSS variable overrides for each theme. | `undefined` |
|
|
240
240
|
|
|
241
|
-
##
|
|
241
|
+
## Browser Compatibility
|
|
242
242
|
|
|
243
243
|
Works in all modern browsers that support:
|
|
244
|
-
-
|
|
245
|
-
-
|
|
246
|
-
-
|
|
247
|
-
-
|
|
248
|
-
-
|
|
244
|
+
- CSS Custom Properties
|
|
245
|
+
- localStorage API
|
|
246
|
+
- matchMedia (for system theme)
|
|
247
|
+
- Angular 22+
|
|
248
|
+
- ES2022+
|
|
249
249
|
|
|
250
250
|
| Browser | Version |
|
|
251
251
|
|---------|---------|
|
|
@@ -254,16 +254,16 @@ Works in all modern browsers that support:
|
|
|
254
254
|
| Safari | ≥ 14 |
|
|
255
255
|
| Edge | ≥ 90 |
|
|
256
256
|
|
|
257
|
-
##
|
|
257
|
+
## Related Libraries
|
|
258
258
|
|
|
259
259
|
Part of the **@su-labs** suite:
|
|
260
260
|
|
|
261
|
-
-
|
|
262
|
-
-
|
|
263
|
-
-
|
|
264
|
-
-
|
|
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
|
|
265
265
|
|
|
266
|
-
##
|
|
266
|
+
## Troubleshooting
|
|
267
267
|
|
|
268
268
|
### Theme not persisting?
|
|
269
269
|
|
|
@@ -279,13 +279,13 @@ Check that your OS has a theme preference set and your browser supports `prefers
|
|
|
279
279
|
|
|
280
280
|
### TypeScript errors?
|
|
281
281
|
|
|
282
|
-
Ensure you have Angular
|
|
282
|
+
Ensure you have Angular 22+ and TypeScript 6.0+ installed.
|
|
283
283
|
|
|
284
|
-
##
|
|
284
|
+
## Support
|
|
285
285
|
|
|
286
|
-
-
|
|
287
|
-
-
|
|
288
|
-
-
|
|
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)
|
|
289
289
|
|
|
290
290
|
## Notes
|
|
291
291
|
|
|
@@ -1,189 +1,122 @@
|
|
|
1
|
-
import * as i0 from
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
+
var SuThemeService = class SuThemeService {
|
|
4
|
+
suThemeConfig = {};
|
|
5
|
+
cssVarPrefix = "--su-theme-";
|
|
6
|
+
storageKey = "su:theme";
|
|
7
|
+
userPreference = "light";
|
|
8
|
+
currentThemeSignal = signal("light", ...ngDevMode ? [{ debugName: "currentThemeSignal" }] : /* istanbul ignore next */ []);
|
|
9
|
+
theme = this.currentThemeSignal.asReadonly();
|
|
10
|
+
systemChangeHandler = null;
|
|
11
|
+
mediaQuery;
|
|
12
|
+
init(config) {
|
|
13
|
+
this.suThemeConfig = {
|
|
14
|
+
...this.suThemeConfig,
|
|
15
|
+
...config
|
|
16
|
+
};
|
|
17
|
+
this.cssVarPrefix = this.suThemeConfig.cssVarPrefix ?? this.cssVarPrefix;
|
|
18
|
+
this.storageKey = this.suThemeConfig.storageKey ?? this.storageKey;
|
|
19
|
+
this.userPreference = localStorage.getItem(this.storageKey) ?? this.suThemeConfig.defaultTheme ?? "system";
|
|
20
|
+
this.setTheme(this.userPreference, false);
|
|
21
|
+
}
|
|
22
|
+
getTheme() {
|
|
23
|
+
return this.currentThemeSignal();
|
|
24
|
+
}
|
|
25
|
+
getUserPreference() {
|
|
26
|
+
return this.userPreference;
|
|
27
|
+
}
|
|
28
|
+
setTheme(theme, persist = true) {
|
|
29
|
+
if (theme !== "system") this.stopListeningToSystemChanges();
|
|
30
|
+
const resolvedTheme = theme === "system" ? this.resolveSystemTheme() : theme;
|
|
31
|
+
this.userPreference = theme;
|
|
32
|
+
this.currentThemeSignal.set(resolvedTheme);
|
|
33
|
+
document.documentElement.setAttribute("data-theme", resolvedTheme);
|
|
34
|
+
this.applyThemeVariables(resolvedTheme);
|
|
35
|
+
if (persist) localStorage.setItem(this.storageKey, theme);
|
|
36
|
+
if (theme === "system") this.listenToSystemChanges();
|
|
37
|
+
}
|
|
38
|
+
resolveSystemTheme() {
|
|
39
|
+
if (typeof window !== "undefined" && window.matchMedia("(prefers-color-scheme: dark)").matches) return "dark";
|
|
40
|
+
return "light";
|
|
41
|
+
}
|
|
42
|
+
listenToSystemChanges() {
|
|
43
|
+
if (!this.mediaQuery) {
|
|
44
|
+
this.mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
45
|
+
this.systemChangeHandler = (event) => {
|
|
46
|
+
if (this.userPreference === "system") {
|
|
47
|
+
this.currentThemeSignal.set(event.matches ? "dark" : "light");
|
|
48
|
+
document.documentElement.setAttribute("data-theme", this.currentThemeSignal());
|
|
49
|
+
this.applyThemeVariables(this.currentThemeSignal());
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
this.mediaQuery.addEventListener("change", this.systemChangeHandler);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
stopListeningToSystemChanges() {
|
|
56
|
+
if (this.mediaQuery && this.systemChangeHandler) {
|
|
57
|
+
this.mediaQuery.removeEventListener("change", this.systemChangeHandler);
|
|
58
|
+
this.systemChangeHandler = null;
|
|
59
|
+
this.mediaQuery = void 0;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
applyThemeVariables(theme) {
|
|
63
|
+
const variablesToApply = {
|
|
64
|
+
...{
|
|
65
|
+
light: {
|
|
66
|
+
color: "#333",
|
|
67
|
+
background: "#fff",
|
|
68
|
+
"main-accent": "#007bff",
|
|
69
|
+
"second-accent": "#6c757d",
|
|
70
|
+
"third-accent": "#28a745"
|
|
71
|
+
},
|
|
72
|
+
dark: {
|
|
73
|
+
color: "#f0f0f0",
|
|
74
|
+
background: "#1a1a1a",
|
|
75
|
+
"main-accent": "#38a8e1",
|
|
76
|
+
"second-accent": "#a8b0b5",
|
|
77
|
+
"third-accent": "#2ee057"
|
|
78
|
+
},
|
|
79
|
+
contrast: {
|
|
80
|
+
color: "#000",
|
|
81
|
+
background: "#fff",
|
|
82
|
+
"main-accent": "#ff0000"
|
|
83
|
+
},
|
|
84
|
+
custom: {},
|
|
85
|
+
system: {}
|
|
86
|
+
}[theme],
|
|
87
|
+
...this.suThemeConfig.themes?.[theme] ?? {}
|
|
88
|
+
};
|
|
89
|
+
for (const [key, value] of Object.entries(variablesToApply)) if (value) document.documentElement.style.setProperty(`${this.cssVarPrefix}${key}`, value);
|
|
90
|
+
}
|
|
91
|
+
ngOnDestroy() {
|
|
92
|
+
this.stopListeningToSystemChanges();
|
|
93
|
+
}
|
|
94
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
95
|
+
minVersion: "12.0.0",
|
|
96
|
+
version: "22.2.4",
|
|
97
|
+
ngImport: i0,
|
|
98
|
+
type: SuThemeService,
|
|
99
|
+
deps: [],
|
|
100
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
101
|
+
});
|
|
102
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({
|
|
103
|
+
minVersion: "12.0.0",
|
|
104
|
+
version: "22.2.4",
|
|
105
|
+
ngImport: i0,
|
|
106
|
+
type: SuThemeService,
|
|
107
|
+
providedIn: "root"
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
111
|
+
minVersion: "12.0.0",
|
|
112
|
+
version: "22.2.4",
|
|
113
|
+
ngImport: i0,
|
|
114
|
+
type: SuThemeService,
|
|
115
|
+
decorators: [{
|
|
116
|
+
type: Injectable,
|
|
117
|
+
args: [{ providedIn: "root" }]
|
|
118
|
+
}]
|
|
119
|
+
});
|
|
188
120
|
export { SuThemeService };
|
|
189
|
-
|
|
121
|
+
|
|
122
|
+
//# 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,CAAC,UAAU,EAAE;;IAG7C,mBAAmB,GAAkD,IAAI;;AAGzE,IAAA,UAAU;AAElB;;;;AAIG;AACI,IAAA,IAAI,CAAC,MAAsB,EAAA;;AAEhC,QAAA,IAAI,CAAC,aAAa,GAAG,EAAE,GAAG,IAAI,CAAC,aAAa,EAAE,GAAG,MAAM,EA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,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, 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}"],"mappings":";;AAYA,IAAa,iBAAb,MAAa,eAAc;CAEjB,gB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HANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@su-labs/theme",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "22.0.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/
|
|
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,14 +27,14 @@
|
|
|
27
27
|
"repository": {
|
|
28
28
|
"type": "git",
|
|
29
29
|
"url": "https://github.com/Greezaaa/settings-utils-lib.git",
|
|
30
|
-
"directory": "projects/su-labs/
|
|
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": "^
|
|
37
|
-
"@angular/core": "^
|
|
36
|
+
"@angular/common": "^22.0.0",
|
|
37
|
+
"@angular/core": "^22.0.0"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"tslib": "^2.3.0"
|
|
@@ -51,5 +51,6 @@
|
|
|
51
51
|
"types": "./types/su-labs-theme.d.ts",
|
|
52
52
|
"default": "./fesm2022/su-labs-theme.mjs"
|
|
53
53
|
}
|
|
54
|
-
}
|
|
54
|
+
},
|
|
55
|
+
"type": "module"
|
|
55
56
|
}
|
package/types/su-labs-theme.d.ts
CHANGED
|
@@ -1,100 +1,95 @@
|
|
|
1
|
-
import * as i0 from
|
|
2
|
-
import { OnDestroy } from
|
|
3
|
-
|
|
1
|
+
import * as i0 from "@angular/core";
|
|
2
|
+
import { OnDestroy } from "@angular/core";
|
|
4
3
|
/** Available theme names */
|
|
5
|
-
type SuThemeName = 'light' | 'dark' | 'contrast' | 'custom' | 'system';
|
|
4
|
+
export type SuThemeName = 'light' | 'dark' | 'contrast' | 'custom' | 'system';
|
|
6
5
|
/** Theme configuration */
|
|
7
|
-
interface SuThemeConfig {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
export interface SuThemeConfig {
|
|
7
|
+
/** localStorage key for storing selected theme */
|
|
8
|
+
storageKey?: string;
|
|
9
|
+
/** CSS variable prefix, applied to all theme variables */
|
|
10
|
+
cssVarPrefix?: string;
|
|
11
|
+
/** Default theme if nothing saved in localStorage */
|
|
12
|
+
defaultTheme?: SuThemeName;
|
|
13
|
+
/** Optional CSS variable overrides for specific themes */
|
|
14
|
+
themes?: Partial<Record<SuThemeName, ThemeCssVariables>>;
|
|
16
15
|
}
|
|
17
16
|
/** Available CSS variables for theming */
|
|
18
|
-
interface ThemeCssVariables {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
export interface ThemeCssVariables {
|
|
18
|
+
color?: string;
|
|
19
|
+
background?: string;
|
|
20
|
+
'main-accent'?: string;
|
|
21
|
+
'second-accent'?: string;
|
|
22
|
+
'third-accent'?: string;
|
|
23
|
+
[key: string]: string | undefined;
|
|
25
24
|
}
|
|
26
|
-
|
|
27
25
|
/**
|
|
28
26
|
* Service for managing application themes.
|
|
29
27
|
*
|
|
30
28
|
* Provides reactive theme state via signals and methods to set/get the theme.
|
|
31
29
|
* Supports user preference, system-based themes, and custom CSS variable overrides.
|
|
32
30
|
*/
|
|
33
|
-
declare class SuThemeService implements OnDestroy {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export { SuThemeService };
|
|
100
|
-
export type { SuThemeConfig, SuThemeName, ThemeCssVariables };
|
|
31
|
+
export declare class SuThemeService implements OnDestroy {
|
|
32
|
+
/** Configuration for the theme, provided during initialization */
|
|
33
|
+
private suThemeConfig;
|
|
34
|
+
/** CSS variable prefix, can be customized */
|
|
35
|
+
private cssVarPrefix;
|
|
36
|
+
/** localStorage key for storing user preference, can be customized */
|
|
37
|
+
private storageKey;
|
|
38
|
+
/** User selected theme (can be 'system') */
|
|
39
|
+
private userPreference;
|
|
40
|
+
/** Current resolved theme signal ('light' | 'dark' | 'contrast' | 'custom') */
|
|
41
|
+
private currentThemeSignal;
|
|
42
|
+
/** Read-only access to the resolved theme signal */
|
|
43
|
+
readonly theme: import("@angular/core").Signal<SuThemeName>;
|
|
44
|
+
/** System media query listener callback */
|
|
45
|
+
private systemChangeHandler;
|
|
46
|
+
/** MediaQueryList instance for system theme changes */
|
|
47
|
+
private mediaQuery?;
|
|
48
|
+
/**
|
|
49
|
+
* Initialize the service with optional configuration.
|
|
50
|
+
* Should be called from APP_INITIALIZER for default setup.
|
|
51
|
+
* @param config Optional configuration object.
|
|
52
|
+
*/
|
|
53
|
+
init(config?: SuThemeConfig): void;
|
|
54
|
+
/**
|
|
55
|
+
* Get the currently active, resolved theme.
|
|
56
|
+
* @returns The resolved theme name ('light', 'dark', 'contrast', 'custom').
|
|
57
|
+
*/
|
|
58
|
+
getTheme(): SuThemeName;
|
|
59
|
+
/**
|
|
60
|
+
* Get the user's theme preference (may be 'system').
|
|
61
|
+
* @returns The user preference ('light', 'dark', 'contrast', 'custom', 'system').
|
|
62
|
+
*/
|
|
63
|
+
getUserPreference(): SuThemeName;
|
|
64
|
+
/**
|
|
65
|
+
* Set a new theme.
|
|
66
|
+
* @param theme The theme to set ('light', 'dark', 'contrast', 'custom', 'system').
|
|
67
|
+
* @param persist Whether to store the preference in localStorage (default: true).
|
|
68
|
+
*/
|
|
69
|
+
setTheme(theme: SuThemeName, persist?: boolean): void;
|
|
70
|
+
/**
|
|
71
|
+
* Resolve the current system theme using prefers-color-scheme.
|
|
72
|
+
* @returns 'dark' if system prefers dark mode, otherwise 'light'.
|
|
73
|
+
*/
|
|
74
|
+
private resolveSystemTheme;
|
|
75
|
+
/**
|
|
76
|
+
* Listen to system theme changes and automatically update the theme.
|
|
77
|
+
* Only active when user preference is 'system'.
|
|
78
|
+
*/
|
|
79
|
+
private listenToSystemChanges;
|
|
80
|
+
/**
|
|
81
|
+
* Stop listening to system theme changes.
|
|
82
|
+
* Should be called when the service is destroyed or theme is no longer 'system'.
|
|
83
|
+
*/
|
|
84
|
+
private stopListeningToSystemChanges;
|
|
85
|
+
/**
|
|
86
|
+
* Apply CSS variables for the given theme.
|
|
87
|
+
* This method applies default variables if they are not overridden in the config.
|
|
88
|
+
* @param theme The theme whose variables should be applied.
|
|
89
|
+
*/
|
|
90
|
+
private applyThemeVariables;
|
|
91
|
+
/** Angular lifecycle hook for service destruction */
|
|
92
|
+
ngOnDestroy(): void;
|
|
93
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<SuThemeService, never>;
|
|
94
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<any>;
|
|
95
|
+
}
|