@su-labs/theme 1.0.3 → 20.3.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 ADDED
@@ -0,0 +1,101 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [22.3.0] - 2026-11-11
11
+
12
+ ### Added
13
+ - 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`
14
+ - `availableThemes()` and `isAvailable()` on `SuThemeService`
15
+ - A `data-theme-scheme` attribute (`light` or `dark`) beside `data-theme`
16
+
17
+ ### Changed
18
+ - Requires `@su-labs/core` 1.6.0 exactly
19
+
20
+ ## [22.2.0] - 2026-11-07
21
+
22
+ ### Added
23
+ - `target` option: the element the theme is applied to (default: `<html>`), to theme one component instead of the whole page
24
+ - `storage` option: where the selected theme is saved (default: `localStorage`); pass `createSuCookieStorage({ domain })` from `@su-labs/core` to share it across subdomains
25
+
26
+ ### Changed
27
+ - Requires `@su-labs/core` 1.1.0 or later
28
+ - Package homepage now points to the website with a live demo
29
+
30
+ ## [22.1.0] - 2026-11-05
31
+
32
+ ### Changed
33
+ - Theme logic moved to the framework-agnostic `@su-labs/core/theme`; `SuThemeService` is now an Angular wrapper around `SuThemeController` with the same public API
34
+ - Added `@su-labs/core` as a dependency
35
+
36
+ ## [22.0.0] - 2026-11-04
37
+
38
+ ### Changed
39
+ - Upgrade to Angular 22 (peer dependencies now `^22.0.0`)
40
+ - Remove emojis from README.md
41
+
42
+ ## [21.0.5] - 2026-02-04
43
+
44
+ ### Changed
45
+ - Update README.md
46
+
47
+ ## [21.0.4] - 2026-02-03
48
+
49
+ ### Changed
50
+ - **BREAKING:** Updated to Angular 21 support. This version requires Angular 21.0.0 or higher.
51
+ - Updated peer dependencies to `@angular/common: ^21.0.0` and `@angular/core: ^21.0.0`.
52
+ - Aligned package version with Angular major version for clearer compatibility (v21.x.x = Angular 21 support).
53
+
54
+ ### Notes
55
+ - Projects using Angular 20 or below should continue using version 1.0.3.
56
+ - This release maintains full backward compatibility with the 1.x API - only the Angular version requirement has changed.
57
+
58
+
59
+ ## [1.0.3] - 2025-08-30
60
+
61
+ ### Fixed
62
+ - Ensured `"license": "MIT"` is correctly reflected in the published package metadata.
63
+ - Removed unintended `.spec.ts` exports from the public API surface.
64
+
65
+ ## [1.0.2] - 2025-08-30
66
+
67
+ ### Note
68
+ - This version was published with incorrect package content and is deprecated. Please use `1.0.4`.
69
+
70
+ ## [1.0.1] - 2025-08-27
71
+
72
+ ### Changed
73
+ - Updated documentation (`README.md`) to reflect new scoped package import:
74
+ from `./path/to/su-theme.service` → `@su-labs/theme`.
75
+
76
+
77
+
78
+ ## [1.0.0] - 2024-08-26
79
+ ### Added
80
+ - Initial public release of the **@su-labs/theme** library.
81
+
82
+ - Introduction of **SuThemeService**, a robust and reactive service for managing application themes.
83
+
84
+ - Reactive state management with Angular `Signals`.
85
+
86
+ - Seamless integration with system theme preference via `prefers-color-scheme`.
87
+
88
+ - Automatic persistence of user theme preference in `localStorage`.
89
+
90
+ - Highly configurable service with customizable storage keys, CSS variable prefixes, and theme variable overrides.
91
+
92
+ - Public API for programmatic control, including `setTheme()` and `getTheme()`.
93
+
94
+ - Support for default themes: `light`, `dark`, `contrast`, `custom`, and `system`.
95
+
96
+
97
+
98
+
99
+
100
+
101
+
package/README.md CHANGED
@@ -1,27 +1,149 @@
1
1
  # SuThemeService
2
2
 
3
+ `@su-labs/theme`
4
+
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-v22-DD0031?style=for-the-badge&logo=angular&logoColor=white)
7
+ ![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?style=for-the-badge&logo=typescript&logoColor=white)
8
+ ![License](https://img.shields.io/badge/License-MIT-blue.svg?style=for-the-badge)
9
+ ![NPM](https://img.shields.io/badge/NPM-CB3837?style=for-the-badge&logo=npm&logoColor=white)
10
+ ![Size](https://img.shields.io/badge/Size-Small-green.svg?style=for-the-badge)
11
+ ![Downloads](https://img.shields.io/npm/dt/@su-labs/theme?style=for-the-badge)
12
+ ![Version](https://img.shields.io/npm/v/@su-labs/theme?style=for-the-badge)
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: Uses Angular signals for efficient, reactive theme management.
8
- * System Theme Integration: Automatically follows the user's system theme preference (`prefers-color-scheme`).
9
- * Persistence: Remembers the user's last-selected theme using `localStorage`.
10
- * Configurable: Customizable with a configuration object for storage keys, CSS variable prefixes, and default themes.
11
- * Programmatic Control: Simple public API to set, get, and track the active theme.
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` (follows OS preference)
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
104
+ ```
105
+
106
+ ## Peer Dependencies
107
+
108
+ This library requires the following peer dependencies:
109
+
110
+ ```sh
111
+ npm install @angular/common@^22 @angular/core@^22
112
+ ```
113
+
114
+ ## Quick Start
115
+
116
+ ```typescript
117
+ // 1. Install
118
+ npm install @su-labs/theme
119
+
120
+ // 2. Initialize in app.config.ts
121
+ import { provideAppInitializer, inject } from '@angular/core';
122
+ import { SuThemeService } from '@su-labs/theme';
123
+
124
+ export const appConfig = {
125
+ providers: [
126
+ provideAppInitializer(() => {
127
+ inject(SuThemeService).init();
128
+ }),
129
+ ],
130
+ };
131
+
132
+ // 3. Use in component
133
+ import { Component, inject } from '@angular/core';
134
+ import { SuThemeService } from '@su-labs/theme';
135
+
136
+ @Component({
137
+ template: `
138
+ <button (click)="themeService.setTheme('dark')">Dark Mode</button>
139
+ <p>Current: {{ themeService.theme() }}</p>
140
+ `
141
+ })
142
+ export class AppComponent {
143
+ themeService = inject(SuThemeService);
144
+ }
145
+ ```
146
+
25
147
  ## Usage
26
148
 
27
149
  The `SuThemeService` must be initialized at application startup to load its configuration and initial theme.
@@ -56,13 +178,13 @@ export const appConfig = {
56
178
  };
57
179
  ```
58
180
 
59
- ✅ 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.
181
+ 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.
182
+
183
+ ### Example 2: Using the Theme in Your Styles
60
184
 
61
- ### Example 2: Using the Theme in a Component
62
185
  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
186
 
64
187
  ```CSS
65
-
66
188
  /* styles.css */
67
189
  /* Use the CSS variables in your stylesheet */
68
190
  body {
@@ -77,11 +199,11 @@ h1 {
77
199
 
78
200
  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
201
 
80
- ### Example 3: Dynamic Theme Switching
202
+ ### Example 3: Dynamic Theme Switching
203
+
81
204
  Use the setTheme() method to switch themes and the theme signal to read the current state.
82
205
 
83
206
  ```TypeScript
84
-
85
207
  import { Component, inject } from '@angular/core';
86
208
  import { SuThemeService, SuThemeName } from '@su-labs/theme';
87
209
 
@@ -98,7 +220,6 @@ import { SuThemeService, SuThemeName } from '@su-labs/theme';
98
220
  `,
99
221
  })
100
222
  export class ThemeSwitcherComponent {
101
- // Use the service directly in the template
102
223
  public readonly themeService = inject(SuThemeService);
103
224
 
104
225
  setTheme(themeName: SuThemeName) {
@@ -106,8 +227,45 @@ export class ThemeSwitcherComponent {
106
227
  }
107
228
  }
108
229
  ```
109
-
110
- ✅ 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.
230
+
231
+ 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.
232
+
233
+ ## API Reference
234
+
235
+ ### Signals
236
+
237
+ | Signal | Type | Description |
238
+ |--------|------|-------------|
239
+ | `theme()` | `SuThemeName` | Current active theme name |
240
+
241
+ ### Methods
242
+
243
+ | Method | Parameters | Description |
244
+ |--------|------------|-------------|
245
+ | `init(config?)` | `SuThemeConfig` | Initialize with custom configuration |
246
+ | `setTheme(theme, persist?)` | `SuThemeName, boolean` | Change theme (persist defaults to true) |
247
+ | `getTheme()` | - | Returns current theme name |
248
+ | `availableThemes()` | - | The base themes, then the palettes listed in `palettes` |
249
+ | `isAvailable(theme)` | `string` | Whether a theme can be set here |
250
+
251
+ ### Types
252
+
253
+ ```typescript
254
+ type SuBaseThemeName = 'light' | 'dark' | 'contrast' | 'custom' | 'system';
255
+ type SuThemeName = SuBaseThemeName | SuPaletteName; // SuPaletteName: 'sunset' | 'candy' | … | 'racing'
256
+
257
+ interface SuThemeConfig {
258
+ storageKey?: string; // Default: 'su:theme'
259
+ storage?: SuStorage; // Default: localStorage
260
+ target?: HTMLElement; // Default: <html>
261
+ cssVarPrefix?: string; // Default: '--su-theme-'
262
+ defaultTheme?: SuThemeName; // Default: 'system'
263
+ themes?: {
264
+ [key in SuThemeName]?: Record<string, string>;
265
+ };
266
+ palettes?: SuPaletteName[]; // Default: none
267
+ }
268
+ ```
111
269
 
112
270
  ## Configuration Options
113
271
 
@@ -115,17 +273,68 @@ The service can be configured to fit your application's needs.
115
273
 
116
274
  | Option | Description | Default Value |
117
275
  |--------------------|---------------------------------------------------------------|------------------|
118
- | `storageKey` | The key used to persist the selected theme in `localStorage`. | `'su:theme'` |
276
+ | `storageKey` | The key used to persist the selected theme. | `'su:theme'` |
277
+ | `storage` | Where the theme is saved. Use `createSuCookieStorage({ domain })` from `@su-labs/core` to share it across subdomains. | `localStorage` |
278
+ | `target` | Element the theme is applied to. Pass a component's element to theme only that part of the page. | `<html>` |
119
279
  | `cssVarPrefix` | The prefix for all CSS variables managed by the service. | `'--su-theme-'` |
120
280
  | `defaultTheme` | The theme to use if no preference is saved in `localStorage`. | `'system'` |
121
281
  | `themes` | An object with custom CSS variable overrides for each theme. | `undefined` |
282
+ | `palettes` | Ready-made palettes this site offers as themes, e.g. `['solar', 'neon']`. | none |
122
283
 
123
- ## Notes
124
- - The service safely handles properties and ignores null or undefined values, preventing issues like prototype pollution.
284
+ ## Browser Compatibility
125
285
 
126
- - Works seamlessly with standalone components and reactive setups thanks to its signal-based API.
286
+ Works in all modern browsers that support:
287
+ - CSS Custom Properties
288
+ - localStorage API
289
+ - matchMedia (for system theme)
290
+ - Angular 22+
291
+ - ES2022+
127
292
 
128
- - 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.
293
+ | Browser | Version |
294
+ |---------|---------|
295
+ | Chrome | ≥ 90 |
296
+ | Firefox | ≥ 88 |
297
+ | Safari | ≥ 14 |
298
+ | Edge | ≥ 90 |
299
+
300
+ ## Related Libraries
301
+
302
+ Part of the **@su-labs** suite:
303
+
304
+ - [@su-labs/visit-tracker](https://www.npmjs.com/package/@su-labs/visit-tracker) - User visit tracking
305
+ - [@su-labs/notifications](https://www.npmjs.com/package/@su-labs/notifications) - Toast notification system
306
+ - [@su-labs/scroll-spy](https://www.npmjs.com/package/@su-labs/scroll-spy) - Scroll position tracking
307
+ - [@su-labs/font-size](https://www.npmjs.com/package/@su-labs/font-size) - Accessible font size controls
308
+
309
+ ## Troubleshooting
310
+
311
+ ### Theme not persisting?
312
+
313
+ Make sure localStorage is enabled in your browser and not blocked by privacy settings.
314
+
315
+ ### CSS variables not updating?
316
+
317
+ Ensure you're using the correct prefix in your CSS (`--su-theme-` by default) and that you've initialized the service.
318
+
319
+ ### System theme not working?
320
+
321
+ Check that your OS has a theme preference set and your browser supports `prefers-color-scheme` media query.
322
+
323
+ ### TypeScript errors?
324
+
325
+ Ensure you have Angular 22+ and TypeScript 6.0+ installed.
326
+
327
+ ## Support
328
+
329
+ - Found a bug? [Open an issue](https://github.com/Greezaaa/settings-utils-lib/issues)
330
+ - Have a feature request? [Start a discussion](https://github.com/Greezaaa/settings-utils-lib/discussions)
331
+ - Like this library? [Give it a star!](https://github.com/Greezaaa/settings-utils-lib)
332
+
333
+ ## Notes
334
+
335
+ - The service safely handles properties and ignores null or undefined values, preventing issues like prototype pollution.
336
+ - Works seamlessly with standalone components and reactive setups thanks to its signal-based API.
337
+ - 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
338
 
130
339
  ## Contributing
131
340
 
@@ -135,5 +344,4 @@ To contribute code, please ensure your changes include unit tests to maintain co
135
344
 
136
345
  ## License
137
346
 
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
-
347
+ 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
- * Service for managing application themes.
7
+ * Angular service for managing application themes.
6
8
  *
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
+ * 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
- /** 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" }] : []));
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" }] : []));
21
17
  /** Read-only access to the resolved theme signal */
22
18
  theme = this.currentThemeSignal.asReadonly();
23
- /** System media query listener callback */
24
- systemChangeHandler = null;
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 APP_INITIALIZER for default setup.
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
- // 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);
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.userPreference;
41
+ return this.controller.getUserPreference();
55
42
  }
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';
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
- * Listen to system theme changes and automatically update the theme.
95
- * Only active when user preference is 'system'.
48
+ * Whether a theme can be set here: a base theme, or a listed palette.
49
+ * @param theme Any theme name.
96
50
  */
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
- }
51
+ isAvailable(theme) {
52
+ return this.controller.isAvailable(theme);
111
53
  }
112
54
  /**
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.
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
- 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
- }
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.stopListeningToSystemChanges();
64
+ this.unsubscribe();
65
+ this.controller.destroy();
169
66
  }
170
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.7", ngImport: i0, type: SuThemeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
171
- static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.7", ngImport: i0, type: SuThemeService, providedIn: 'root' });
67
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: SuThemeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
68
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: SuThemeService, providedIn: 'root' });
172
69
  }
173
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.7", ngImport: i0, type: SuThemeService, decorators: [{
70
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", 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\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,EAAE;AACzD,QAAA,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,aAAa,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY;AACxE,QAAA,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,IAAI,IAAI,CAAC,UAAU;;AAGlE,QAAA,IAAI,CAAC,cAAc,GAAI,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAwB,IAAI,IAAI,CAAC,aAAa,CAAC,YAAY,IAAI,QAAQ;;QAGlI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,cA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AA,UAAU,EAAE,MAAM;AAClB,gBAAA,aAAa,EAAE,SAAS;AACzB,aAAA;AACD,YAAA,MAAM,EAAE,EAAE;AACV,YAAA,MAAM,EAAE,EAAE;SACX;;AAGD,QAAA,MAAM,gBAAgB,GAAG;YACvB,GAAG,aAAa,CAAC,KAAK,CAAC;AACvB,YAAA,IAAI,IAAI,CAAC,aAAa,CAAC,MAAM,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;SAC9C;;AAGD,QAAA,KAAK,MAAM,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;;;;;IAMP,WAAW,GAAA;QACT,IAAI,CAAC,4BAA4B,EAAE;;uGA1L1B,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-20/build/theme/src/lib/theme.service.ts","../../../../compat/angular-20/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 b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U,CAAC,OAAO,EAAE;IAC3B;wGAhEW,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,SAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,cAAc,cAFb,MAAM,EAAA,CAAA;;4FAEP,cAAc,EAAA,UAAA,EAAA,CAAA;kBAH1B,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA;;;ACXD;;AAEG;;;;"}
package/index.d.ts CHANGED
@@ -1,55 +1,26 @@
1
1
  import * as i0 from '@angular/core';
2
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
- }
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
- * Service for managing application themes.
7
+ * Angular service for managing application themes.
29
8
  *
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.
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
- /** 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;
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
- /** System media query listener callback */
47
- private systemChangeHandler;
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 APP_INITIALIZER for default setup.
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
- * 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'.
40
+ * Whether a theme can be set here: a base theme, or a listed palette.
41
+ * @param theme Any theme name.
75
42
  */
76
- private resolveSystemTheme;
43
+ isAvailable(theme: string): theme is SuThemeName;
77
44
  /**
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.
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
- private applyThemeVariables;
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@su-labs/theme",
3
- "version": "1.0.3",
3
+ "version": "20.3.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://su-labs.dev/theme/",
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": "^20.1.0",
37
- "@angular/core": "^20.1.0"
36
+ "@angular/common": "^20.0.0",
37
+ "@angular/core": "^20.0.0"
38
38
  },
39
39
  "dependencies": {
40
+ "@su-labs/core": "1.6.0",
40
41
  "tslib": "^2.3.0"
41
42
  },
42
43
  "sideEffects": false,