@su-labs/theme 21.0.4 → 21.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/README.md +235 -25
- package/fesm2022/su-labs-theme.mjs +31 -138
- package/fesm2022/su-labs-theme.mjs.map +1 -1
- package/package.json +6 -4
- package/types/su-labs-theme.d.ts +21 -65
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,50 @@ 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
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [22.3.1] - 2026-11-16
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
- Uses `@su-labs/core` 1.9.0 (was 1.6.0), the same version as every other `@su-labs` package, so apps using several of them install core once
|
|
14
|
+
|
|
15
|
+
## [22.3.0] - 2026-11-11
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
- Ready-made palettes as optional themes: list them in the new `palettes` option (none by default), e.g. `palettes: ['solar', 'neon']`; `SU_THEME_PALETTES`, `SU_PALETTE_NAMES` and their types are re-exported from `@su-labs/core/theme`
|
|
19
|
+
- `availableThemes()` and `isAvailable()` on `SuThemeService`
|
|
20
|
+
- A `data-theme-scheme` attribute (`light` or `dark`) beside `data-theme`
|
|
21
|
+
|
|
22
|
+
### Changed
|
|
23
|
+
- Requires `@su-labs/core` 1.6.0 exactly
|
|
24
|
+
|
|
25
|
+
## [22.2.0] - 2026-11-07
|
|
26
|
+
|
|
27
|
+
### Added
|
|
28
|
+
- `target` option: the element the theme is applied to (default: `<html>`), to theme one component instead of the whole page
|
|
29
|
+
- `storage` option: where the selected theme is saved (default: `localStorage`); pass `createSuCookieStorage({ domain })` from `@su-labs/core` to share it across subdomains
|
|
30
|
+
|
|
31
|
+
### Changed
|
|
32
|
+
- Requires `@su-labs/core` 1.1.0 or later
|
|
33
|
+
- Package homepage now points to the website with a live demo
|
|
34
|
+
|
|
35
|
+
## [22.1.0] - 2026-11-05
|
|
36
|
+
|
|
37
|
+
### Changed
|
|
38
|
+
- Theme logic moved to the framework-agnostic `@su-labs/core/theme`; `SuThemeService` is now an Angular wrapper around `SuThemeController` with the same public API
|
|
39
|
+
- Added `@su-labs/core` as a dependency
|
|
40
|
+
|
|
41
|
+
## [22.0.0] - 2026-11-04
|
|
42
|
+
|
|
43
|
+
### Changed
|
|
44
|
+
- Upgrade to Angular 22 (peer dependencies now `^22.0.0`)
|
|
45
|
+
- Remove emojis from README.md
|
|
46
|
+
|
|
47
|
+
## [21.0.5] - 2026-02-04
|
|
48
|
+
|
|
49
|
+
### Changed
|
|
50
|
+
- Update README.md
|
|
51
|
+
|
|
8
52
|
## [21.0.4] - 2026-02-03
|
|
9
53
|
|
|
10
54
|
### Changed
|
package/README.md
CHANGED
|
@@ -1,27 +1,151 @@
|
|
|
1
1
|
# SuThemeService
|
|
2
2
|
|
|
3
|
+
`@su-labs/theme`
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
10
|
+

|
|
11
|
+

|
|
12
|
+

|
|
13
|
+
|
|
3
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.
|
|
4
15
|
|
|
16
|
+
> **Not using Angular?** The same theme logic is available for React, Vue, 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
|
+
|
|
18
|
+
## Table of Contents
|
|
19
|
+
|
|
20
|
+
- [Why Use This Library?](#why-use-this-library)
|
|
21
|
+
- [Features](#features)
|
|
22
|
+
- [Available Themes](#available-themes)
|
|
23
|
+
- [Installation](#installation)
|
|
24
|
+
- [Quick Start](#quick-start)
|
|
25
|
+
- [Usage](#usage)
|
|
26
|
+
- [API Reference](#api-reference)
|
|
27
|
+
- [Configuration Options](#configuration-options)
|
|
28
|
+
- [Browser Compatibility](#browser-compatibility)
|
|
29
|
+
- [Related Libraries](#related-libraries)
|
|
30
|
+
- [Troubleshooting](#troubleshooting)
|
|
31
|
+
- [Support](#support)
|
|
32
|
+
- [Contributing](#contributing)
|
|
33
|
+
- [License](#license)
|
|
34
|
+
|
|
35
|
+
## Why Use This Library?
|
|
36
|
+
|
|
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
|
|
45
|
+
|
|
5
46
|
## Features
|
|
6
47
|
|
|
7
|
-
* Reactive State
|
|
8
|
-
* System Theme Integration
|
|
9
|
-
* Persistence
|
|
10
|
-
* Configurable
|
|
11
|
-
* Programmatic Control
|
|
48
|
+
* **Reactive State**: Uses Angular signals for efficient, reactive theme management.
|
|
49
|
+
* **System Theme Integration**: Automatically follows the user's system theme preference (`prefers-color-scheme`).
|
|
50
|
+
* **Persistence**: Remembers the user's last-selected theme using `localStorage`.
|
|
51
|
+
* **Configurable**: Customizable with a configuration object for storage keys, CSS variable prefixes, and default themes.
|
|
52
|
+
* **Programmatic Control**: Simple public API to set, get, and track the active theme.
|
|
12
53
|
|
|
13
54
|
## Available Themes
|
|
14
55
|
|
|
15
56
|
The service currently supports the following theme names by default:
|
|
16
57
|
|
|
17
|
-
* `light`
|
|
18
|
-
* `dark`
|
|
19
|
-
* `contrast`
|
|
20
|
-
* `custom`
|
|
21
|
-
* `system`
|
|
58
|
+
* `light` - Light theme
|
|
59
|
+
* `dark` - Dark theme
|
|
60
|
+
* `contrast` - High contrast theme
|
|
61
|
+
* `custom` - Custom theme with your own variables
|
|
62
|
+
* `system` - Automatically follows OS preference
|
|
22
63
|
|
|
23
64
|
You can provide custom variables for any of these themes via the `themes` property in `SuThemeConfig`.
|
|
24
65
|
|
|
66
|
+
### Ready-made palettes (optional)
|
|
67
|
+
|
|
68
|
+
Eleven palettes ship with the library, none of them shown unless you list them in `palettes`:
|
|
69
|
+
|
|
70
|
+
| Name | Scheme | Look |
|
|
71
|
+
|------|--------|------|
|
|
72
|
+
| `sunset` | dark | Midnight blue, cream and amber |
|
|
73
|
+
| `candy` | dark | Near black, white and pink |
|
|
74
|
+
| `copper` | dark | Black, teal and copper |
|
|
75
|
+
| `solar` | light | Porcelain, midnight navy and solar orange |
|
|
76
|
+
| `neon` | dark | Graphite, silver mist and hyper pink |
|
|
77
|
+
| `sunflower` | dark | Midnight navy, powder blue and sunflower yellow |
|
|
78
|
+
| `tomato` | light | Oat milk, forest green and tomato red |
|
|
79
|
+
| `blossom` | light | Butter cream, forest green and hot pink |
|
|
80
|
+
| `ember` | dark | Midnight black, electric blue and sunset orange |
|
|
81
|
+
| `storm` | dark | Storm grey, near black and safety orange |
|
|
82
|
+
| `racing` | light | Polar white, glacier blue and racing red |
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
theme.init({ palettes: ['solar', 'neon', 'racing'] });
|
|
86
|
+
theme.setTheme('neon');
|
|
87
|
+
theme.availableThemes(); // ['light', 'dark', 'contrast', 'custom', 'system', 'solar', 'neon', 'racing']
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Each palette sets `--su-theme-color`, `--su-theme-background`, `--su-theme-main-accent`, `--su-theme-second-accent`
|
|
91
|
+
and `--su-theme-button-text` (text on the accent). Text is at least 7:1 on the background and button text at least
|
|
92
|
+
4.5:1 on the accent. Besides `data-theme`, the element gets `data-theme-scheme="light"` or `"dark"`, so your own
|
|
93
|
+
colours can follow: `[data-theme-scheme='dark'] { --shadow: none; }`.
|
|
94
|
+
|
|
95
|
+
A saved palette your site doesn't list falls back to `defaultTheme` without overwriting the saved choice, so sites
|
|
96
|
+
that share one saved theme (see `storage`) can offer different sets.
|
|
97
|
+
|
|
98
|
+
## Installation
|
|
99
|
+
|
|
100
|
+
You can install this library via npm:
|
|
101
|
+
|
|
102
|
+
```sh
|
|
103
|
+
npm install @su-labs/theme@ng21
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
This is the Angular 21 version. For the newest Angular, install `@su-labs/theme` without a tag.
|
|
107
|
+
|
|
108
|
+
## Peer Dependencies
|
|
109
|
+
|
|
110
|
+
This library requires the following peer dependencies:
|
|
111
|
+
|
|
112
|
+
```sh
|
|
113
|
+
npm install @angular/common@^21 @angular/core@^21
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Quick Start
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
// 1. Install
|
|
120
|
+
npm install @su-labs/theme@ng21
|
|
121
|
+
|
|
122
|
+
// 2. Initialize in app.config.ts
|
|
123
|
+
import { provideAppInitializer, inject } from '@angular/core';
|
|
124
|
+
import { SuThemeService } from '@su-labs/theme';
|
|
125
|
+
|
|
126
|
+
export const appConfig = {
|
|
127
|
+
providers: [
|
|
128
|
+
provideAppInitializer(() => {
|
|
129
|
+
inject(SuThemeService).init();
|
|
130
|
+
}),
|
|
131
|
+
],
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// 3. Use in component
|
|
135
|
+
import { Component, inject } from '@angular/core';
|
|
136
|
+
import { SuThemeService } from '@su-labs/theme';
|
|
137
|
+
|
|
138
|
+
@Component({
|
|
139
|
+
template: `
|
|
140
|
+
<button (click)="themeService.setTheme('dark')">Dark Mode</button>
|
|
141
|
+
<p>Current: {{ themeService.theme() }}</p>
|
|
142
|
+
`
|
|
143
|
+
})
|
|
144
|
+
export class AppComponent {
|
|
145
|
+
themeService = inject(SuThemeService);
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
25
149
|
## Usage
|
|
26
150
|
|
|
27
151
|
The `SuThemeService` must be initialized at application startup to load its configuration and initial theme.
|
|
@@ -56,13 +180,13 @@ export const appConfig = {
|
|
|
56
180
|
};
|
|
57
181
|
```
|
|
58
182
|
|
|
59
|
-
|
|
183
|
+
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.
|
|
184
|
+
|
|
185
|
+
### Example 2: Using the Theme in Your Styles
|
|
60
186
|
|
|
61
|
-
### Example 2: Using the Theme in a Component
|
|
62
187
|
Your service automatically applies CSS variables to the `:root` element. Your application stylesheets can use these variables, and the service will handle updating their values when the theme changes.
|
|
63
188
|
|
|
64
189
|
```CSS
|
|
65
|
-
|
|
66
190
|
/* styles.css */
|
|
67
191
|
/* Use the CSS variables in your stylesheet */
|
|
68
192
|
body {
|
|
@@ -77,11 +201,11 @@ h1 {
|
|
|
77
201
|
|
|
78
202
|
The service dynamically updates these variables based on the active theme, applying a combination of its internal defaults and any custom overrides you provide in the configuration.
|
|
79
203
|
|
|
80
|
-
###
|
|
204
|
+
### Example 3: Dynamic Theme Switching
|
|
205
|
+
|
|
81
206
|
Use the setTheme() method to switch themes and the theme signal to read the current state.
|
|
82
207
|
|
|
83
208
|
```TypeScript
|
|
84
|
-
|
|
85
209
|
import { Component, inject } from '@angular/core';
|
|
86
210
|
import { SuThemeService, SuThemeName } from '@su-labs/theme';
|
|
87
211
|
|
|
@@ -98,7 +222,6 @@ import { SuThemeService, SuThemeName } from '@su-labs/theme';
|
|
|
98
222
|
`,
|
|
99
223
|
})
|
|
100
224
|
export class ThemeSwitcherComponent {
|
|
101
|
-
// Use the service directly in the template
|
|
102
225
|
public readonly themeService = inject(SuThemeService);
|
|
103
226
|
|
|
104
227
|
setTheme(themeName: SuThemeName) {
|
|
@@ -106,8 +229,45 @@ export class ThemeSwitcherComponent {
|
|
|
106
229
|
}
|
|
107
230
|
}
|
|
108
231
|
```
|
|
109
|
-
|
|
110
|
-
|
|
232
|
+
|
|
233
|
+
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.
|
|
234
|
+
|
|
235
|
+
## API Reference
|
|
236
|
+
|
|
237
|
+
### Signals
|
|
238
|
+
|
|
239
|
+
| Signal | Type | Description |
|
|
240
|
+
|--------|------|-------------|
|
|
241
|
+
| `theme()` | `SuThemeName` | Current active theme name |
|
|
242
|
+
|
|
243
|
+
### Methods
|
|
244
|
+
|
|
245
|
+
| Method | Parameters | Description |
|
|
246
|
+
|--------|------------|-------------|
|
|
247
|
+
| `init(config?)` | `SuThemeConfig` | Initialize with custom configuration |
|
|
248
|
+
| `setTheme(theme, persist?)` | `SuThemeName, boolean` | Change theme (persist defaults to true) |
|
|
249
|
+
| `getTheme()` | - | Returns current theme name |
|
|
250
|
+
| `availableThemes()` | - | The base themes, then the palettes listed in `palettes` |
|
|
251
|
+
| `isAvailable(theme)` | `string` | Whether a theme can be set here |
|
|
252
|
+
|
|
253
|
+
### Types
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
type SuBaseThemeName = 'light' | 'dark' | 'contrast' | 'custom' | 'system';
|
|
257
|
+
type SuThemeName = SuBaseThemeName | SuPaletteName; // SuPaletteName: 'sunset' | 'candy' | … | 'racing'
|
|
258
|
+
|
|
259
|
+
interface SuThemeConfig {
|
|
260
|
+
storageKey?: string; // Default: 'su:theme'
|
|
261
|
+
storage?: SuStorage; // Default: localStorage
|
|
262
|
+
target?: HTMLElement; // Default: <html>
|
|
263
|
+
cssVarPrefix?: string; // Default: '--su-theme-'
|
|
264
|
+
defaultTheme?: SuThemeName; // Default: 'system'
|
|
265
|
+
themes?: {
|
|
266
|
+
[key in SuThemeName]?: Record<string, string>;
|
|
267
|
+
};
|
|
268
|
+
palettes?: SuPaletteName[]; // Default: none
|
|
269
|
+
}
|
|
270
|
+
```
|
|
111
271
|
|
|
112
272
|
## Configuration Options
|
|
113
273
|
|
|
@@ -115,17 +275,68 @@ The service can be configured to fit your application's needs.
|
|
|
115
275
|
|
|
116
276
|
| Option | Description | Default Value |
|
|
117
277
|
|--------------------|---------------------------------------------------------------|------------------|
|
|
118
|
-
| `storageKey` | The key used to persist the selected theme
|
|
278
|
+
| `storageKey` | The key used to persist the selected theme. | `'su:theme'` |
|
|
279
|
+
| `storage` | Where the theme is saved. Use `createSuCookieStorage({ domain })` from `@su-labs/core` to share it across subdomains. | `localStorage` |
|
|
280
|
+
| `target` | Element the theme is applied to. Pass a component's element to theme only that part of the page. | `<html>` |
|
|
119
281
|
| `cssVarPrefix` | The prefix for all CSS variables managed by the service. | `'--su-theme-'` |
|
|
120
282
|
| `defaultTheme` | The theme to use if no preference is saved in `localStorage`. | `'system'` |
|
|
121
283
|
| `themes` | An object with custom CSS variable overrides for each theme. | `undefined` |
|
|
284
|
+
| `palettes` | Ready-made palettes this site offers as themes, e.g. `['solar', 'neon']`. | none |
|
|
122
285
|
|
|
123
|
-
##
|
|
124
|
-
- The service safely handles properties and ignores null or undefined values, preventing issues like prototype pollution.
|
|
286
|
+
## Browser Compatibility
|
|
125
287
|
|
|
126
|
-
|
|
288
|
+
Works in all modern browsers that support:
|
|
289
|
+
- CSS Custom Properties
|
|
290
|
+
- localStorage API
|
|
291
|
+
- matchMedia (for system theme)
|
|
292
|
+
- Angular 21
|
|
293
|
+
- ES2022+
|
|
127
294
|
|
|
128
|
-
|
|
295
|
+
| Browser | Version |
|
|
296
|
+
|---------|---------|
|
|
297
|
+
| Chrome | ≥ 90 |
|
|
298
|
+
| Firefox | ≥ 88 |
|
|
299
|
+
| Safari | ≥ 14 |
|
|
300
|
+
| Edge | ≥ 90 |
|
|
301
|
+
|
|
302
|
+
## Related Libraries
|
|
303
|
+
|
|
304
|
+
Part of the **@su-labs** suite:
|
|
305
|
+
|
|
306
|
+
- [@su-labs/visit-tracker](https://www.npmjs.com/package/@su-labs/visit-tracker) - User visit tracking
|
|
307
|
+
- [@su-labs/notifications](https://www.npmjs.com/package/@su-labs/notifications) - Toast notification system
|
|
308
|
+
- [@su-labs/scroll-spy](https://www.npmjs.com/package/@su-labs/scroll-spy) - Scroll position tracking
|
|
309
|
+
- [@su-labs/font-size](https://www.npmjs.com/package/@su-labs/font-size) - Accessible font size controls
|
|
310
|
+
|
|
311
|
+
## Troubleshooting
|
|
312
|
+
|
|
313
|
+
### Theme not persisting?
|
|
314
|
+
|
|
315
|
+
Make sure localStorage is enabled in your browser and not blocked by privacy settings.
|
|
316
|
+
|
|
317
|
+
### CSS variables not updating?
|
|
318
|
+
|
|
319
|
+
Ensure you're using the correct prefix in your CSS (`--su-theme-` by default) and that you've initialized the service.
|
|
320
|
+
|
|
321
|
+
### System theme not working?
|
|
322
|
+
|
|
323
|
+
Check that your OS has a theme preference set and your browser supports `prefers-color-scheme` media query.
|
|
324
|
+
|
|
325
|
+
### TypeScript errors?
|
|
326
|
+
|
|
327
|
+
Ensure you have Angular 21 and TypeScript 5.9+ installed.
|
|
328
|
+
|
|
329
|
+
## Support
|
|
330
|
+
|
|
331
|
+
- Found a bug? [Open an issue](https://github.com/Greezaaa/settings-utils-lib/issues)
|
|
332
|
+
- Have a feature request? [Start a discussion](https://github.com/Greezaaa/settings-utils-lib/discussions)
|
|
333
|
+
- Like this library? [Give it a star!](https://github.com/Greezaaa/settings-utils-lib)
|
|
334
|
+
|
|
335
|
+
## Notes
|
|
336
|
+
|
|
337
|
+
- The service safely handles properties and ignores null or undefined values, preventing issues like prototype pollution.
|
|
338
|
+
- Works seamlessly with standalone components and reactive setups thanks to its signal-based API.
|
|
339
|
+
- Persistence is handled automatically: The service saves the user's preference to `localStorage` when you call `setTheme(..., true)`. On initialization, it automatically restores this saved preference. You do not need to add this logic in your components.
|
|
129
340
|
|
|
130
341
|
## Contributing
|
|
131
342
|
|
|
@@ -135,5 +346,4 @@ To contribute code, please ensure your changes include unit tests to maintain co
|
|
|
135
346
|
|
|
136
347
|
## License
|
|
137
348
|
|
|
138
|
-
This project is licensed under the MIT License. See the [**LICENSE**](https://github.com/Greezaaa/settings-utils-lib/blob/main/projects/su-labs/theme/LICENSE) file for details.
|
|
139
|
-
|
|
349
|
+
This project is licensed under the MIT License. See the [**LICENSE**](https://github.com/Greezaaa/settings-utils-lib/blob/main/projects/su-labs/theme/LICENSE) file for details.
|
|
@@ -1,43 +1,30 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
2
|
import { signal, Injectable } from '@angular/core';
|
|
3
|
+
import { SuThemeController } from '@su-labs/core/theme';
|
|
4
|
+
export { SU_BASE_THEME_NAMES, SU_PALETTE_NAMES, SU_THEME_PALETTES, isSuBaseThemeName } from '@su-labs/core/theme';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
|
-
*
|
|
7
|
+
* Angular service for managing application themes.
|
|
6
8
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
+
* Thin wrapper around the framework-agnostic `SuThemeController` from `@su-labs/core/theme`
|
|
10
|
+
* that exposes the resolved theme as an Angular signal.
|
|
9
11
|
*/
|
|
10
12
|
class SuThemeService {
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
|
|
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" }] : []));
|
|
13
|
+
/** Framework-agnostic theme logic */
|
|
14
|
+
controller = new SuThemeController();
|
|
15
|
+
/** Current resolved theme signal, kept in sync with the controller */
|
|
16
|
+
currentThemeSignal = signal(this.controller.getTheme(), ...(ngDevMode ? [{ debugName: "currentThemeSignal" }] : /* istanbul ignore next */ []));
|
|
21
17
|
/** Read-only access to the resolved theme signal */
|
|
22
18
|
theme = this.currentThemeSignal.asReadonly();
|
|
23
|
-
/**
|
|
24
|
-
|
|
25
|
-
/** MediaQueryList instance for system theme changes */
|
|
26
|
-
mediaQuery;
|
|
19
|
+
/** Stops syncing the controller's theme into the signal */
|
|
20
|
+
unsubscribe = this.controller.theme.subscribe((theme) => this.currentThemeSignal.set(theme));
|
|
27
21
|
/**
|
|
28
22
|
* Initialize the service with optional configuration.
|
|
29
|
-
* Should be called from
|
|
23
|
+
* Should be called from an app initializer for default setup.
|
|
30
24
|
* @param config Optional configuration object.
|
|
31
25
|
*/
|
|
32
26
|
init(config) {
|
|
33
|
-
|
|
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);
|
|
27
|
+
this.controller.init(config);
|
|
41
28
|
}
|
|
42
29
|
/**
|
|
43
30
|
* Get the currently active, resolved theme.
|
|
@@ -51,136 +38,42 @@ class SuThemeService {
|
|
|
51
38
|
* @returns The user preference ('light', 'dark', 'contrast', 'custom', 'system').
|
|
52
39
|
*/
|
|
53
40
|
getUserPreference() {
|
|
54
|
-
return this.
|
|
41
|
+
return this.controller.getUserPreference();
|
|
55
42
|
}
|
|
56
|
-
/**
|
|
57
|
-
|
|
58
|
-
|
|
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';
|
|
43
|
+
/** The themes this site offers: the base themes, then the palettes listed in the config's `palettes` */
|
|
44
|
+
availableThemes() {
|
|
45
|
+
return this.controller.availableThemes();
|
|
92
46
|
}
|
|
93
47
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
48
|
+
* Whether a theme can be set here: a base theme, or a listed palette.
|
|
49
|
+
* @param theme Any theme name.
|
|
96
50
|
*/
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
}
|
|
51
|
+
isAvailable(theme) {
|
|
52
|
+
return this.controller.isAvailable(theme);
|
|
111
53
|
}
|
|
112
54
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
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.
|
|
55
|
+
* Set a new theme. A palette the config doesn't list is ignored.
|
|
56
|
+
* @param theme The theme to set ('light', 'dark', 'contrast', 'custom', 'system', or a listed palette).
|
|
57
|
+
* @param persist Whether to store the preference in localStorage (default: true).
|
|
127
58
|
*/
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
}
|
|
59
|
+
setTheme(theme, persist = true) {
|
|
60
|
+
this.controller.setTheme(theme, persist);
|
|
165
61
|
}
|
|
166
62
|
/** Angular lifecycle hook for service destruction */
|
|
167
63
|
ngOnDestroy() {
|
|
168
|
-
this.
|
|
64
|
+
this.unsubscribe();
|
|
65
|
+
this.controller.destroy();
|
|
169
66
|
}
|
|
170
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.
|
|
171
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.
|
|
67
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.26", ngImport: i0, type: SuThemeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
68
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.26", ngImport: i0, type: SuThemeService, providedIn: 'root' });
|
|
172
69
|
}
|
|
173
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.
|
|
70
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.26", ngImport: i0, type: SuThemeService, decorators: [{
|
|
174
71
|
type: Injectable,
|
|
175
72
|
args: [{
|
|
176
73
|
providedIn: 'root',
|
|
177
74
|
}]
|
|
178
75
|
}] });
|
|
179
76
|
|
|
180
|
-
/*
|
|
181
|
-
* Public API Surface of theme
|
|
182
|
-
*/
|
|
183
|
-
|
|
184
77
|
/**
|
|
185
78
|
* Generated bundle index. Do not edit.
|
|
186
79
|
*/
|
|
@@ -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\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CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAAE;YAC3D,IAAI,KAAK,EAAE;AACT,gBAAA,QAAQ,CAAC,eAAe,CAAC,KAAK,CAAC,WAAW,CACxC,CAAA,EAAG,IAAI,CAAC,YAAY,CAAA,EAAG,GAAG,EAAE,EAC5B,KAAK,CACN;YACH;QACF;IACF;;IAGA,WAAW,GAAA;QACT,IAAI,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","sources":["../../../../compat/angular-21/build/theme/src/lib/theme.service.ts","../../../../compat/angular-21/build/theme/src/su-labs-theme.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 /** The themes this site offers: the base themes, then the palettes listed in the config's `palettes` */\n public availableThemes(): SuThemeName[] {\n return this.controller.availableThemes();\n }\n\n /**\n * Whether a theme can be set here: a base theme, or a listed palette.\n * @param theme Any theme name.\n */\n public isAvailable(theme: string): theme is SuThemeName {\n return this.controller.isAvailable(theme);\n }\n\n /**\n * Set a new theme. A palette the config doesn't list is ignored.\n * @param theme The theme to set ('light', 'dark', 'contrast', 'custom', 'system', or a listed palette).\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","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;AAGA;;;;;AAKG;MAIU,cAAc,CAAA;;AAER,IAAA,UAAU,GAAG,IAAI,iBAAiB,EAAE;;IAGpC,kBAAkB,GAAG,MAAM,CAAc,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,EAAA,IAAA,SAAA,GAAA,CAAA,EAAA,SAAA,EAAA,oBAAA,EAAA,CAAA,8BAAA,EAAA,CAAA,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AAA,CAAA;;4FAEP,cAAc,EAAA,UAAA,EAAA,CAAA;kBAH1B,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA;;;ACXD;;AAEG;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@su-labs/theme",
|
|
3
|
-
"version": "21.
|
|
3
|
+
"version": "21.3.1",
|
|
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://
|
|
21
|
+
"homepage": "https://su-labs.dev/theme/",
|
|
22
22
|
"author": {
|
|
23
23
|
"name": "Yevheniy Alekseyev",
|
|
24
24
|
"email": "yev.alekseyev@gmail.com",
|
|
@@ -27,7 +27,7 @@
|
|
|
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"
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"@angular/core": "^21.0.0"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
+
"@su-labs/core": "1.9.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
|
}
|
package/types/su-labs-theme.d.ts
CHANGED
|
@@ -1,55 +1,26 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
2
|
import { OnDestroy } from '@angular/core';
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
}
|
|
3
|
+
import { SuThemeName, SuThemeConfig } from '@su-labs/core/theme';
|
|
4
|
+
export { SU_BASE_THEME_NAMES, SU_PALETTE_NAMES, SU_THEME_PALETTES, SuBaseThemeName, SuPaletteName, SuThemeConfig, SuThemeName, SuThemePalette, SuThemeScheme, ThemeCssVariables, isSuBaseThemeName } from '@su-labs/core/theme';
|
|
26
5
|
|
|
27
6
|
/**
|
|
28
|
-
*
|
|
7
|
+
* Angular service for managing application themes.
|
|
29
8
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
9
|
+
* Thin wrapper around the framework-agnostic `SuThemeController` from `@su-labs/core/theme`
|
|
10
|
+
* that exposes the resolved theme as an Angular signal.
|
|
32
11
|
*/
|
|
33
12
|
declare class SuThemeService implements OnDestroy {
|
|
34
|
-
/**
|
|
35
|
-
private
|
|
36
|
-
/**
|
|
37
|
-
private
|
|
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;
|
|
13
|
+
/** Framework-agnostic theme logic */
|
|
14
|
+
private readonly controller;
|
|
15
|
+
/** Current resolved theme signal, kept in sync with the controller */
|
|
16
|
+
private readonly currentThemeSignal;
|
|
44
17
|
/** Read-only access to the resolved theme signal */
|
|
45
18
|
readonly theme: i0.Signal<SuThemeName>;
|
|
46
|
-
/**
|
|
47
|
-
private
|
|
48
|
-
/** MediaQueryList instance for system theme changes */
|
|
49
|
-
private mediaQuery?;
|
|
19
|
+
/** Stops syncing the controller's theme into the signal */
|
|
20
|
+
private readonly unsubscribe;
|
|
50
21
|
/**
|
|
51
22
|
* Initialize the service with optional configuration.
|
|
52
|
-
* Should be called from
|
|
23
|
+
* Should be called from an app initializer for default setup.
|
|
53
24
|
* @param config Optional configuration object.
|
|
54
25
|
*/
|
|
55
26
|
init(config?: SuThemeConfig): void;
|
|
@@ -63,33 +34,19 @@ declare class SuThemeService implements OnDestroy {
|
|
|
63
34
|
* @returns The user preference ('light', 'dark', 'contrast', 'custom', 'system').
|
|
64
35
|
*/
|
|
65
36
|
getUserPreference(): SuThemeName;
|
|
37
|
+
/** The themes this site offers: the base themes, then the palettes listed in the config's `palettes` */
|
|
38
|
+
availableThemes(): SuThemeName[];
|
|
66
39
|
/**
|
|
67
|
-
*
|
|
68
|
-
* @param theme
|
|
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'.
|
|
40
|
+
* Whether a theme can be set here: a base theme, or a listed palette.
|
|
41
|
+
* @param theme Any theme name.
|
|
75
42
|
*/
|
|
76
|
-
|
|
43
|
+
isAvailable(theme: string): theme is SuThemeName;
|
|
77
44
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
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.
|
|
45
|
+
* Set a new theme. A palette the config doesn't list is ignored.
|
|
46
|
+
* @param theme The theme to set ('light', 'dark', 'contrast', 'custom', 'system', or a listed palette).
|
|
47
|
+
* @param persist Whether to store the preference in localStorage (default: true).
|
|
91
48
|
*/
|
|
92
|
-
|
|
49
|
+
setTheme(theme: SuThemeName, persist?: boolean): void;
|
|
93
50
|
/** Angular lifecycle hook for service destruction */
|
|
94
51
|
ngOnDestroy(): void;
|
|
95
52
|
static ɵfac: i0.ɵɵFactoryDeclaration<SuThemeService, never>;
|
|
@@ -97,4 +54,3 @@ declare class SuThemeService implements OnDestroy {
|
|
|
97
54
|
}
|
|
98
55
|
|
|
99
56
|
export { SuThemeService };
|
|
100
|
-
export type { SuThemeConfig, SuThemeName, ThemeCssVariables };
|