@sixbell-telco/sdk 1.3.0 → 1.5.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 +81 -0
- package/README.md +437 -472
- package/_index.css +109 -11
- package/assets/styles/toast.css +8 -0
- package/components/forms/date-picker/constants/date-picker.constants.d.ts +13 -0
- package/components/forms/date-picker/index.d.ts +5 -0
- package/components/forms/date-picker/public-api.d.ts +3 -0
- package/components/forms/date-picker/src/date-picker.component.d.ts +323 -0
- package/components/forms/date-picker/types/date-picker.types.d.ts +39 -0
- package/components/{forms/datepicker → sonner}/index.d.ts +1 -1
- package/components/sonner/public-api.d.ts +2 -0
- package/components/sonner/src/sonner.component.d.ts +39 -0
- package/components/sonner/toaster/index.d.ts +5 -0
- package/components/sonner/toaster/public-api.d.ts +1 -0
- package/components/sonner/toaster/src/toaster.component.d.ts +89 -0
- package/components/toast/public-api.d.ts +9 -0
- package/components/toast/services/toast.service.d.ts +3 -0
- package/components/toast/toast-message/toast-message.component.d.ts +3 -0
- package/components/toast/toasts-container/toasts-container.component.d.ts +3 -0
- package/fesm2022/sixbell-telco-sdk-components-forms-date-picker.mjs +885 -0
- package/fesm2022/sixbell-telco-sdk-components-forms-date-picker.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner-toaster.mjs +132 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner-toaster.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner.mjs +179 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-components-toast.mjs +14 -0
- package/fesm2022/sixbell-telco-sdk-components-toast.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-utils-date.mjs +90 -2
- package/fesm2022/sixbell-telco-sdk-utils-date.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-utils-logger.mjs +578 -0
- package/fesm2022/sixbell-telco-sdk-utils-logger.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-utils-resource-loader.mjs +1222 -0
- package/fesm2022/sixbell-telco-sdk-utils-resource-loader.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-utils-theme.mjs +1434 -180
- package/fesm2022/sixbell-telco-sdk-utils-theme.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-utils-translation.mjs +734 -182
- package/fesm2022/sixbell-telco-sdk-utils-translation.mjs.map +1 -1
- package/package.json +68 -51
- package/utils/date/src/date-utils.d.ts +44 -0
- package/utils/logger/README.md +539 -0
- package/utils/logger/index.d.ts +5 -0
- package/utils/logger/public-api.d.ts +3 -0
- package/utils/logger/src/constants.d.ts +15 -0
- package/utils/logger/src/logger.service.d.ts +123 -0
- package/utils/logger/src/models/logger.d.ts +25 -0
- package/utils/logger/src/providers/logger.provider.d.ts +3 -0
- package/utils/resource-loader/README.md +460 -0
- package/utils/resource-loader/index.d.ts +5 -0
- package/utils/resource-loader/public-api.d.ts +1 -0
- package/utils/resource-loader/src/cache/index.d.ts +2 -0
- package/utils/resource-loader/src/cache/layers/factory.d.ts +38 -0
- package/utils/resource-loader/src/cache/layers/index.d.ts +3 -0
- package/utils/resource-loader/src/cache/layers/indexed-db-cache.d.ts +118 -0
- package/utils/resource-loader/src/cache/layers/local-storage-cache.d.ts +86 -0
- package/utils/resource-loader/src/cache/layers/memory-cache.d.ts +76 -0
- package/utils/resource-loader/src/cache/types.d.ts +107 -0
- package/utils/resource-loader/src/config/index.d.ts +1 -0
- package/utils/resource-loader/src/config/load-config.d.ts +32 -0
- package/utils/resource-loader/src/constants.d.ts +38 -0
- package/utils/resource-loader/src/http/index.d.ts +1 -0
- package/utils/resource-loader/src/http/retryable-client.d.ts +43 -0
- package/utils/resource-loader/src/index.d.ts +1 -0
- package/utils/resource-loader/src/resource-loader.d.ts +67 -0
- package/utils/resource-loader/src/types.d.ts +152 -0
- package/utils/resource-loader/src/utils/cache-validator.d.ts +72 -0
- package/utils/resource-loader/src/utils/helpers.d.ts +15 -0
- package/utils/resource-loader/src/utils/index.d.ts +2 -0
- package/utils/theme/README.md +1054 -109
- package/utils/theme/public-api.d.ts +9 -1
- package/utils/theme/src/constants.d.ts +74 -0
- package/utils/theme/src/models/theme.d.ts +288 -0
- package/utils/theme/src/providers/constants.d.ts +7 -0
- package/utils/theme/src/providers/theme.provider.d.ts +13 -0
- package/utils/theme/src/providers/theme.provider.types.d.ts +29 -0
- package/utils/theme/src/theme-assets/theme-assets.service.d.ts +214 -0
- package/utils/theme/src/theme-assets/theme-assets.types.d.ts +10 -0
- package/utils/theme/src/theme-dom/theme-dom.service.d.ts +87 -0
- package/utils/theme/src/theme-scheme/theme-scheme.service.d.ts +44 -0
- package/utils/theme/src/theme-storage/theme-storage.service.d.ts +47 -0
- package/utils/theme/src/theme.service.d.ts +40 -186
- package/utils/theme/src/utils/utils.d.ts +86 -0
- package/utils/translation/README.md +1414 -176
- package/utils/translation/public-api.d.ts +6 -0
- package/utils/translation/src/constants.d.ts +52 -0
- package/utils/translation/src/models/translation.d.ts +59 -0
- package/utils/translation/src/providers/constants.d.ts +11 -0
- package/utils/translation/src/providers/translation.provider.d.ts +73 -0
- package/utils/translation/src/providers/translation.provider.types.d.ts +26 -0
- package/utils/translation/src/translation-dom/translation-dom.service.d.ts +26 -0
- package/utils/translation/src/translation-storage/translation-storage.service.d.ts +34 -0
- package/utils/translation/src/translation.service.d.ts +19 -166
- package/components/forms/datepicker/constants/datepicker.constants.d.ts +0 -5
- package/components/forms/datepicker/public-api.d.ts +0 -3
- package/components/forms/datepicker/src/datepicker.component.d.ts +0 -165
- package/components/forms/datepicker/types/datepicker.types.d.ts +0 -7
- package/fesm2022/sixbell-telco-sdk-components-forms-datepicker.mjs +0 -487
- package/fesm2022/sixbell-telco-sdk-components-forms-datepicker.mjs.map +0 -1
package/utils/theme/README.md
CHANGED
|
@@ -1,185 +1,1130 @@
|
|
|
1
|
-
# Theme
|
|
1
|
+
# Theme Utility
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A comprehensive, signal-based theme management system for Angular 19 applications with multi-tenant support, color scheme detection, runtime configuration loading, and persistent storage.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Quick Start
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
- **Reactive theme management** using Angular signals
|
|
10
|
-
- **TypeScript IntelliSense** for theme codes and color schemes
|
|
11
|
-
- **Automatic persistence** to localStorage
|
|
12
|
-
- **System theme detection** for light/dark preferences
|
|
13
|
-
- **Provider-based setup** for Angular applications
|
|
14
|
-
|
|
15
|
-
## How to use
|
|
16
|
-
|
|
17
|
-
### Basic setup
|
|
7
|
+
### Basic Setup
|
|
18
8
|
|
|
19
9
|
```typescript
|
|
20
|
-
// app.config.ts
|
|
21
10
|
import { provideTheme } from '@sixbell-telco/sdk/utils/theme';
|
|
22
11
|
|
|
23
|
-
|
|
12
|
+
bootstrapApplication(AppComponent, {
|
|
24
13
|
providers: [
|
|
25
14
|
provideTheme({
|
|
15
|
+
defaultTheme: 'sixbell_telco',
|
|
26
16
|
defaultScheme: 'system',
|
|
27
|
-
defaultTheme: 'sixbell_telco', // IntelliSense: 'sixbell_telco'
|
|
28
17
|
}),
|
|
29
18
|
],
|
|
30
|
-
};
|
|
19
|
+
});
|
|
31
20
|
```
|
|
32
21
|
|
|
33
|
-
###
|
|
22
|
+
### Using the Theme Service
|
|
34
23
|
|
|
35
24
|
```typescript
|
|
36
|
-
|
|
37
|
-
import {
|
|
25
|
+
import { Component, inject } from '@angular/core';
|
|
26
|
+
import { ThemeService } from '@sixbell-telco/sdk/utils/theme';
|
|
27
|
+
|
|
28
|
+
@Component({
|
|
29
|
+
selector: 'app-theme-switcher',
|
|
30
|
+
template: `
|
|
31
|
+
<select (change)="onThemeChange($event)">
|
|
32
|
+
<option *ngFor="let theme of themeService.getAvailableThemes()">
|
|
33
|
+
{{ theme }}
|
|
34
|
+
</option>
|
|
35
|
+
</select>
|
|
36
|
+
<button (click)="toggleScheme()">Current: {{ themeService.resolvedScheme() }}</button>
|
|
37
|
+
`,
|
|
38
|
+
})
|
|
39
|
+
export class ThemeSwitcherComponent {
|
|
40
|
+
protected readonly themeService = inject(ThemeService);
|
|
41
|
+
|
|
42
|
+
onThemeChange(event: Event): void {
|
|
43
|
+
const theme = (event.target as HTMLSelectElement).value;
|
|
44
|
+
this.themeService.setTheme(theme);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
toggleScheme(): void {
|
|
48
|
+
const current = this.themeService.selectedScheme();
|
|
49
|
+
const next = current === 'dark' ? 'light' : current === 'light' ? 'dark' : 'system';
|
|
50
|
+
this.themeService.setScheme(next);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Features
|
|
56
|
+
|
|
57
|
+
### 🎨 Theme Management
|
|
58
|
+
|
|
59
|
+
- **Multiple Themes**: Support for base theme + additional themes
|
|
60
|
+
- **Color Schemes**: Light, Dark, and System (automatic detection)
|
|
61
|
+
- **Signals-Based**: Reactive theme and scheme selection
|
|
62
|
+
- **Computed Properties**: Derived values like `isDarkTheme()` and `resolvedScheme()`
|
|
63
|
+
|
|
64
|
+
### 💾 Persistence
|
|
65
|
+
|
|
66
|
+
- **localStorage Integration**: Automatic saving/loading of user preferences
|
|
67
|
+
- **Error Handling**: Graceful fallback when storage is unavailable (SSR, private browsing)
|
|
68
|
+
- **Multi-Key Storage**: Separate keys for theme and scheme with namespace isolation
|
|
69
|
+
|
|
70
|
+
### 🚀 Runtime Configuration
|
|
71
|
+
|
|
72
|
+
- **Dynamic Theme Loading**: Load theme configs from remote URLs
|
|
73
|
+
- **Smart Caching**: IndexedDB for long-term persistence + in-memory for current session
|
|
74
|
+
- **Fallback Strategies**: Stale-while-revalidate, use-stale-on-error for resilience
|
|
75
|
+
- **Asset Management**: Dynamic logo/banner/illustration loading based on theme and scheme
|
|
76
|
+
|
|
77
|
+
### 🌐 System Integration
|
|
78
|
+
|
|
79
|
+
- **OS Preference Detection**: Automatically detect system dark mode preference
|
|
80
|
+
- **Live Updates**: Listen to OS theme preference changes
|
|
81
|
+
- **DOM Injection**: Automatic CSS variable and font-face injection
|
|
82
|
+
|
|
83
|
+
### 🏢 Multi-Tenant Support
|
|
84
|
+
|
|
85
|
+
- **Theme Variants**: Define light/dark variants for each theme
|
|
86
|
+
- **Dynamic Assets**: Map brand-specific assets to themes
|
|
87
|
+
- **Global Variables**: Set theme-specific CSS variables
|
|
88
|
+
- **Font Management**: Load theme-specific fonts and styles
|
|
89
|
+
|
|
90
|
+
## Installation
|
|
91
|
+
|
|
92
|
+
The theme utility is built into `@sixbell-telco/sdk`:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
npm install @sixbell-telco/sdk
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Import from the specific entry point:
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { ThemeService, provideTheme } from '@sixbell-telco/sdk/utils/theme';
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## API Reference
|
|
105
|
+
|
|
106
|
+
### ThemeService
|
|
107
|
+
|
|
108
|
+
Main service for managing theme state and operations.
|
|
109
|
+
|
|
110
|
+
#### Signals (Reactive)
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
// Current selected theme (e.g., 'sixbell_telco')
|
|
114
|
+
selectedTheme: Signal<string>;
|
|
115
|
+
|
|
116
|
+
// Current selected scheme ('system', 'light', or 'dark')
|
|
117
|
+
selectedScheme: Signal<ThemeScheme>;
|
|
118
|
+
|
|
119
|
+
// Resolved scheme (always 'light' or 'dark', never 'system')
|
|
120
|
+
resolvedScheme: Computed<ResolvedThemeScheme>;
|
|
121
|
+
|
|
122
|
+
// Whether current scheme is dark
|
|
123
|
+
isDarkTheme: Computed<boolean>;
|
|
124
|
+
|
|
125
|
+
// Final computed theme identifier (e.g., 'sixbell_telco__dark')
|
|
126
|
+
finalTheme: Computed<string>;
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
#### Methods
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
// Configure static theme settings
|
|
133
|
+
configure(config?: ThemeConfig): void;
|
|
134
|
+
|
|
135
|
+
// Load and configure runtime theme from remote source
|
|
136
|
+
configureRuntime(config: RuntimeThemeConfig): void;
|
|
137
|
+
|
|
138
|
+
// Change the active theme
|
|
139
|
+
setTheme(theme: string): void;
|
|
140
|
+
|
|
141
|
+
// Change the color scheme
|
|
142
|
+
setScheme(scheme: ThemeScheme): void;
|
|
143
|
+
|
|
144
|
+
// Get all available themes
|
|
145
|
+
getAvailableThemes(): string[];
|
|
146
|
+
|
|
147
|
+
// Get available schemes
|
|
148
|
+
getAvailableSchemes(): ThemeScheme[];
|
|
149
|
+
|
|
150
|
+
// Get default theme
|
|
151
|
+
getDefaultTheme(): string;
|
|
152
|
+
|
|
153
|
+
// Get default scheme
|
|
154
|
+
getDefaultScheme(): ThemeScheme;
|
|
155
|
+
|
|
156
|
+
// Get localized theme name (respects translation service)
|
|
157
|
+
getLocalizedThemeName(theme: string): string;
|
|
158
|
+
|
|
159
|
+
// Get localized scheme name (respects translation service)
|
|
160
|
+
getLocalizedSchemeName(scheme: string): string;
|
|
161
|
+
|
|
162
|
+
// Get current theme identifier
|
|
163
|
+
getCurrentThemeIdentifier(): string;
|
|
164
|
+
|
|
165
|
+
// Load theme from saved preferences
|
|
166
|
+
loadSavedPreferences(): void;
|
|
167
|
+
|
|
168
|
+
// Set custom resource loader for runtime configs
|
|
169
|
+
setRuntimeLoader(
|
|
170
|
+
configPath: string,
|
|
171
|
+
loader: ResourceLoader,
|
|
172
|
+
loaderOptions?: ResourceLoaderRequestOptions<RuntimeThemeConfig>
|
|
173
|
+
): void;
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### ThemeStorageService
|
|
177
|
+
|
|
178
|
+
Handles persistent storage of theme preferences.
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
// Save current theme to localStorage
|
|
182
|
+
saveTheme(theme: string): void;
|
|
183
|
+
|
|
184
|
+
// Retrieve saved theme
|
|
185
|
+
getSavedTheme(): string | null;
|
|
186
|
+
|
|
187
|
+
// Save current scheme to localStorage
|
|
188
|
+
saveScheme(scheme: ThemeScheme): void;
|
|
189
|
+
|
|
190
|
+
// Retrieve saved scheme
|
|
191
|
+
getSavedScheme(): ThemeScheme | null;
|
|
192
|
+
|
|
193
|
+
// Clear all saved preferences
|
|
194
|
+
clear(): void;
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### ThemeSchemeService
|
|
198
|
+
|
|
199
|
+
Handles color scheme detection and resolution.
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
// Resolve 'system' scheme to actual 'light' or 'dark'
|
|
203
|
+
resolveScheme(scheme: ThemeScheme): ResolvedThemeScheme;
|
|
204
|
+
|
|
205
|
+
// Detect system preferred color scheme
|
|
206
|
+
detectSystemScheme(): ResolvedThemeScheme;
|
|
207
|
+
|
|
208
|
+
// Listen to system scheme changes
|
|
209
|
+
listenToSystemSchemeChanges(
|
|
210
|
+
listener: (scheme: ResolvedThemeScheme) => void
|
|
211
|
+
): () => void;
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### ThemeDomService
|
|
215
|
+
|
|
216
|
+
Handles DOM manipulation and CSS injection.
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
// Inject font-face declarations
|
|
220
|
+
injectFonts(fonts: FontDefinition[]): void;
|
|
221
|
+
|
|
222
|
+
// Inject global CSS variables
|
|
223
|
+
injectGlobalVariables(variables: Record<string, string>): void;
|
|
224
|
+
|
|
225
|
+
// Inject theme-specific CSS variables
|
|
226
|
+
injectThemeStyles(themes: Record<string, ThemeVariants>): void;
|
|
227
|
+
|
|
228
|
+
// Set active theme on document body
|
|
229
|
+
setActiveTheme(themeIdentifier: string): void;
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### ThemeAssetsService
|
|
38
233
|
|
|
39
|
-
|
|
234
|
+
Manages dynamic asset loading based on theme and scheme.
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
// Get asset URL for current theme and scheme
|
|
238
|
+
getAssetUrl(assetKey: string, theme?: string): string | null;
|
|
239
|
+
|
|
240
|
+
// Get full asset object with light/dark variants
|
|
241
|
+
getAsset(assetKey: string, theme?: string): ThemeAsset | null;
|
|
242
|
+
|
|
243
|
+
// Check if asset exists
|
|
244
|
+
hasAsset(assetKey: string, theme?: string): boolean;
|
|
245
|
+
|
|
246
|
+
// Get all assets for specific theme
|
|
247
|
+
getThemeAssets(theme?: string): ThemeAssets | null;
|
|
248
|
+
|
|
249
|
+
// Clear asset cache
|
|
250
|
+
clearCache(): void;
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Providers
|
|
254
|
+
|
|
255
|
+
#### provideTheme(config?)
|
|
256
|
+
|
|
257
|
+
Configure static theme settings.
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
providers: [
|
|
261
|
+
provideTheme({
|
|
262
|
+
additionalThemes: ['custom', 'premium'],
|
|
263
|
+
defaultTheme: 'sixbell_telco',
|
|
264
|
+
defaultScheme: 'system',
|
|
265
|
+
excludeThemes: ['draft'],
|
|
266
|
+
}),
|
|
267
|
+
];
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
#### provideRuntimeTheme(configPath, options?)
|
|
271
|
+
|
|
272
|
+
Load theme configuration from a remote URL.
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
providers: [
|
|
276
|
+
provideRuntimeTheme('/assets/themes/config.json', {
|
|
277
|
+
freshOnNavigation: false,
|
|
278
|
+
cacheTTL: 1000 * 60 * 60 * 24, // 24 hours
|
|
279
|
+
useStaleOnError: true,
|
|
280
|
+
}),
|
|
281
|
+
];
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## Common Scenarios
|
|
285
|
+
|
|
286
|
+
### Scenario 1: Dynamic Theme Switching
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
import { Component, ChangeDetectionStrategy, inject } from '@angular/core';
|
|
290
|
+
import { CommonModule } from '@angular/common';
|
|
291
|
+
import { ThemeService } from '@sixbell-telco/sdk/utils/theme';
|
|
292
|
+
import { ButtonComponent } from '@sixbell-telco/sdk/components/button';
|
|
293
|
+
import {
|
|
294
|
+
DropdownComponent,
|
|
295
|
+
DropdownTriggerComponent,
|
|
296
|
+
DropdownContentComponent,
|
|
297
|
+
DropdownItemRadioComponent,
|
|
298
|
+
} from '@sixbell-telco/sdk/components/dropdown';
|
|
299
|
+
|
|
300
|
+
@Component({
|
|
301
|
+
selector: 'app-theme-switcher',
|
|
302
|
+
standalone: true,
|
|
303
|
+
imports: [CommonModule, ButtonComponent, DropdownComponent, DropdownTriggerComponent, DropdownContentComponent, DropdownItemRadioComponent],
|
|
304
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
305
|
+
template: `
|
|
306
|
+
<div [attr.data-theme]="themeService.finalTheme()">
|
|
307
|
+
<st-dropdown>
|
|
308
|
+
<st-dropdown-trigger>
|
|
309
|
+
<st-button>
|
|
310
|
+
{{ themeService.getLocalizedThemeName(themeService.selectedTheme()) }}
|
|
311
|
+
</st-button>
|
|
312
|
+
</st-dropdown-trigger>
|
|
313
|
+
<st-dropdown-content>
|
|
314
|
+
@for (theme of themeService.getAvailableThemes(); track theme) {
|
|
315
|
+
<st-dropdown-item-radio
|
|
316
|
+
[value]="theme"
|
|
317
|
+
[checked]="themeService.selectedTheme() === theme"
|
|
318
|
+
(checkedChanged)="themeService.setTheme($event)"
|
|
319
|
+
>
|
|
320
|
+
{{ themeService.getLocalizedThemeName(theme) }}
|
|
321
|
+
</st-dropdown-item-radio>
|
|
322
|
+
}
|
|
323
|
+
</st-dropdown-content>
|
|
324
|
+
</st-dropdown>
|
|
325
|
+
</div>
|
|
326
|
+
`,
|
|
327
|
+
})
|
|
328
|
+
export class ThemeSwitcherComponent {
|
|
329
|
+
protected readonly themeService = inject(ThemeService);
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
### Scenario 2: System Preference with Override
|
|
334
|
+
|
|
335
|
+
```typescript
|
|
336
|
+
import { Component, ChangeDetectionStrategy, computed, inject } from '@angular/core';
|
|
337
|
+
import { CommonModule } from '@angular/common';
|
|
338
|
+
import { ThemeService, ThemeScheme } from '@sixbell-telco/sdk/utils/theme';
|
|
339
|
+
import { ButtonComponent } from '@sixbell-telco/sdk/components/button';
|
|
340
|
+
|
|
341
|
+
@Component({
|
|
342
|
+
selector: 'app-scheme-controller',
|
|
343
|
+
standalone: true,
|
|
344
|
+
imports: [CommonModule, ButtonComponent],
|
|
345
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
346
|
+
template: `
|
|
347
|
+
<div class="controls">
|
|
348
|
+
<label>
|
|
349
|
+
<input type="radio" value="system" [checked]="themeService.selectedScheme() === 'system'" (change)="themeService.setScheme('system')" />
|
|
350
|
+
System Preference
|
|
351
|
+
</label>
|
|
352
|
+
<label>
|
|
353
|
+
<input type="radio" value="light" [checked]="themeService.selectedScheme() === 'light'" (change)="themeService.setScheme('light')" />
|
|
354
|
+
Light Mode
|
|
355
|
+
</label>
|
|
356
|
+
<label>
|
|
357
|
+
<input type="radio" value="dark" [checked]="themeService.selectedScheme() === 'dark'" (change)="themeService.setScheme('dark')" />
|
|
358
|
+
Dark Mode
|
|
359
|
+
</label>
|
|
360
|
+
</div>
|
|
361
|
+
<div>Active: {{ themeService.resolvedScheme() }}</div>
|
|
362
|
+
`,
|
|
363
|
+
})
|
|
364
|
+
export class SchemeControllerComponent {
|
|
365
|
+
protected readonly themeService = inject(ThemeService);
|
|
366
|
+
}
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
### Scenario 3: Multi-Tenant Asset Loading
|
|
370
|
+
|
|
371
|
+
```typescript
|
|
372
|
+
import { Component, ChangeDetectionStrategy, computed, inject } from '@angular/core';
|
|
373
|
+
import { ThemeAssetsService } from '@sixbell-telco/sdk/utils/theme';
|
|
374
|
+
import { ThemeService } from '@sixbell-telco/sdk/utils/theme';
|
|
375
|
+
|
|
376
|
+
@Component({
|
|
377
|
+
selector: 'app-brand-header',
|
|
378
|
+
standalone: true,
|
|
379
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
380
|
+
template: `
|
|
381
|
+
<div class="brand-header">
|
|
382
|
+
<img [src]="logoUrl()" alt="Logo" [style.height.px]="logoHeight()" />
|
|
383
|
+
</div>
|
|
384
|
+
`,
|
|
385
|
+
})
|
|
386
|
+
export class BrandHeaderComponent {
|
|
387
|
+
private readonly assetsService = inject(ThemeAssetsService);
|
|
388
|
+
private readonly themeService = inject(ThemeService);
|
|
389
|
+
|
|
390
|
+
logoUrl = computed(() => {
|
|
391
|
+
return this.assetsService.getAssetUrl('logo') || '/fallback-logo.svg';
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
logoHeight = computed(() => {
|
|
395
|
+
return this.themeService.isDarkTheme() ? 48 : 40;
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### Scenario 4: Theme-Aware Styling
|
|
401
|
+
|
|
402
|
+
```typescript
|
|
403
|
+
import { Component, ChangeDetectionStrategy, computed, inject } from '@angular/core';
|
|
404
|
+
import { cn } from '@sixbell-telco/sdk/utils/cn';
|
|
405
|
+
import { ThemeService } from '@sixbell-telco/sdk/utils/theme';
|
|
406
|
+
|
|
407
|
+
@Component({
|
|
408
|
+
selector: 'app-adaptive',
|
|
409
|
+
standalone: true,
|
|
410
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
411
|
+
template: ` <div [class]="containerClass()">Content adapts to theme</div> `,
|
|
412
|
+
})
|
|
413
|
+
export class AdaptiveComponent {
|
|
414
|
+
private readonly themeService = inject(ThemeService);
|
|
415
|
+
|
|
416
|
+
containerClass = computed(() =>
|
|
417
|
+
cn('p-4 rounded-lg transition-colors', this.themeService.isDarkTheme() ? 'bg-slate-900 text-white' : 'bg-white text-slate-900'),
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
## Real-World Examples
|
|
423
|
+
|
|
424
|
+
### Example 1: Complete App Setup with Runtime Config
|
|
425
|
+
|
|
426
|
+
```typescript
|
|
427
|
+
import { bootstrapApplication } from '@angular/platform-browser';
|
|
428
|
+
import { provideHttpClient } from '@angular/common/http';
|
|
429
|
+
import { provideTheme, provideRuntimeTheme } from '@sixbell-telco/sdk/utils/theme';
|
|
430
|
+
import { ResourceLoader } from '@sixbell-telco/sdk/utils/resource-loader';
|
|
431
|
+
import { AppComponent } from './app.component';
|
|
432
|
+
|
|
433
|
+
bootstrapApplication(AppComponent, {
|
|
40
434
|
providers: [
|
|
435
|
+
provideHttpClient(),
|
|
41
436
|
provideTheme({
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
437
|
+
defaultTheme: 'sixbell_telco',
|
|
438
|
+
defaultScheme: 'system',
|
|
439
|
+
additionalThemes: ['wom', 'custom'],
|
|
440
|
+
}),
|
|
441
|
+
provideRuntimeTheme('/api/v1/themes/config', {
|
|
442
|
+
freshOnNavigation: false,
|
|
443
|
+
cacheTTL: 1000 * 60 * 60 * 24,
|
|
444
|
+
staleWhileRevalidate: true,
|
|
445
|
+
useStaleOnError: true,
|
|
46
446
|
}),
|
|
47
447
|
],
|
|
48
|
-
};
|
|
448
|
+
});
|
|
49
449
|
```
|
|
50
450
|
|
|
51
|
-
###
|
|
451
|
+
### Example 2: Theme Service in Layout Component
|
|
52
452
|
|
|
53
453
|
```typescript
|
|
54
|
-
|
|
454
|
+
import { Component, ChangeDetectionStrategy, computed, inject } from '@angular/core';
|
|
455
|
+
import { CommonModule } from '@angular/common';
|
|
456
|
+
import { ThemeService, ThemeScheme } from '@sixbell-telco/sdk/utils/theme';
|
|
457
|
+
import { ButtonComponent } from '@sixbell-telco/sdk/components/button';
|
|
458
|
+
import {
|
|
459
|
+
DropdownComponent,
|
|
460
|
+
DropdownTriggerComponent,
|
|
461
|
+
DropdownContentComponent,
|
|
462
|
+
DropdownItemRadioComponent,
|
|
463
|
+
} from '@sixbell-telco/sdk/components/dropdown';
|
|
464
|
+
|
|
465
|
+
@Component({
|
|
466
|
+
selector: 'app-layout',
|
|
467
|
+
standalone: true,
|
|
468
|
+
imports: [CommonModule, ButtonComponent, DropdownComponent, DropdownTriggerComponent, DropdownContentComponent, DropdownItemRadioComponent],
|
|
469
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
470
|
+
template: `
|
|
471
|
+
<header [attr.data-theme]="themeService.finalTheme()">
|
|
472
|
+
<div class="theme-switcher">
|
|
473
|
+
<!-- Theme Selector -->
|
|
474
|
+
<st-dropdown>
|
|
475
|
+
<st-dropdown-trigger>
|
|
476
|
+
<st-button>Select Theme</st-button>
|
|
477
|
+
</st-dropdown-trigger>
|
|
478
|
+
<st-dropdown-content>
|
|
479
|
+
@for (theme of themeService.getAvailableThemes(); track theme) {
|
|
480
|
+
<st-dropdown-item-radio
|
|
481
|
+
[value]="theme"
|
|
482
|
+
[checked]="themeService.selectedTheme() === theme"
|
|
483
|
+
(checkedChanged)="themeService.setTheme($event)"
|
|
484
|
+
>
|
|
485
|
+
{{ themeService.getLocalizedThemeName(theme) }}
|
|
486
|
+
</st-dropdown-item-radio>
|
|
487
|
+
}
|
|
488
|
+
</st-dropdown-content>
|
|
489
|
+
</st-dropdown>
|
|
490
|
+
|
|
491
|
+
<!-- Scheme Selector -->
|
|
492
|
+
<st-dropdown>
|
|
493
|
+
<st-dropdown-trigger>
|
|
494
|
+
<st-button>Select Scheme</st-button>
|
|
495
|
+
</st-dropdown-trigger>
|
|
496
|
+
<st-dropdown-content>
|
|
497
|
+
@for (scheme of themeService.getAvailableSchemes(); track scheme) {
|
|
498
|
+
<st-dropdown-item-radio
|
|
499
|
+
[value]="scheme"
|
|
500
|
+
[checked]="themeService.selectedScheme() === scheme"
|
|
501
|
+
(checkedChanged)="themeService.setScheme($event)"
|
|
502
|
+
>
|
|
503
|
+
{{ themeService.getLocalizedSchemeName(scheme) }}
|
|
504
|
+
</st-dropdown-item-radio>
|
|
505
|
+
}
|
|
506
|
+
</st-dropdown-content>
|
|
507
|
+
</st-dropdown>
|
|
508
|
+
|
|
509
|
+
<div class="status">Current: {{ themeService.selectedTheme() }} - {{ themeService.resolvedScheme() }}</div>
|
|
510
|
+
</div>
|
|
511
|
+
</header>
|
|
512
|
+
|
|
513
|
+
<main>
|
|
514
|
+
<ng-content></ng-content>
|
|
515
|
+
</main>
|
|
516
|
+
`,
|
|
517
|
+
})
|
|
518
|
+
export class LayoutComponent {
|
|
519
|
+
protected readonly themeService = inject(ThemeService);
|
|
520
|
+
}
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
### Example 3: Multi-Tenant Brand Switcher
|
|
524
|
+
|
|
525
|
+
````typescript
|
|
526
|
+
import { Component, ChangeDetectionStrategy, inject } from '@angular/core';
|
|
527
|
+
import { CommonModule } from '@angular/common';
|
|
55
528
|
import { ThemeService } from '@sixbell-telco/sdk/utils/theme';
|
|
529
|
+
import { ThemeAssetsService } from '@sixbell-telco/sdk/utils/theme';
|
|
530
|
+
import { ButtonComponent } from '@sixbell-telco/sdk/components/button';
|
|
56
531
|
|
|
57
532
|
@Component({
|
|
533
|
+
selector: 'app-brand-switcher',
|
|
534
|
+
standalone: true,
|
|
535
|
+
imports: [CommonModule, ButtonComponent],
|
|
536
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
58
537
|
template: `
|
|
59
|
-
<div
|
|
60
|
-
|
|
61
|
-
|
|
538
|
+
<div class="brand-grid">
|
|
539
|
+
@for (brand of brands; track brand) {
|
|
540
|
+
<st-button
|
|
541
|
+
[variant]="currentBrand() === brand ? 'primary' : 'base'"
|
|
542
|
+
[style.background-image]="'url(' + logoFor(brand) + ')'"
|
|
543
|
+
(click)="switchBrand(brand)"
|
|
544
|
+
>
|
|
545
|
+
{{ brand }}
|
|
546
|
+
</st-button>
|
|
547
|
+
}
|
|
62
548
|
</div>
|
|
63
549
|
`,
|
|
64
550
|
})
|
|
65
|
-
export class
|
|
66
|
-
|
|
551
|
+
export class BrandSwitcherComponent {
|
|
552
|
+
private readonly themeService = inject(ThemeService);
|
|
553
|
+
private readonly assetsService = inject(ThemeAssetsService);
|
|
554
|
+
|
|
555
|
+
brands = this.themeService.getAvailableThemes();
|
|
556
|
+
currentBrand = this.themeService.selectedTheme;
|
|
67
557
|
|
|
68
|
-
|
|
69
|
-
this.
|
|
558
|
+
logoFor(brand: string): string {
|
|
559
|
+
return this.assetsService.getAssetUrl('logo', brand) || '/default-logo.svg';
|
|
70
560
|
}
|
|
71
561
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.theme.setScheme(current === 'dark' ? 'light' : 'dark');
|
|
562
|
+
switchBrand(brand: string): void {
|
|
563
|
+
this.themeService.setTheme(brand);
|
|
75
564
|
}
|
|
76
565
|
}
|
|
77
|
-
|
|
566
|
+
```## Configuration Guide
|
|
567
|
+
|
|
568
|
+
### Static Theme Configuration
|
|
569
|
+
|
|
570
|
+
```typescript
|
|
571
|
+
interface ThemeConfig {
|
|
572
|
+
// Additional themes beyond 'sixbell_telco'
|
|
573
|
+
additionalThemes?: readonly string[];
|
|
78
574
|
|
|
79
|
-
|
|
575
|
+
// Themes to exclude from available options
|
|
576
|
+
excludeThemes?: readonly string[];
|
|
80
577
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
<div [class]="theme.finalTheme()">Content with reactive theme classes</div>
|
|
578
|
+
// Default theme on first load
|
|
579
|
+
defaultTheme?: string;
|
|
84
580
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
<p>Dark mode is active</p>
|
|
88
|
-
} @else {
|
|
89
|
-
<p>Light mode is active</p>
|
|
581
|
+
// Default color scheme on first load
|
|
582
|
+
defaultScheme?: ThemeScheme; // 'system' | 'light' | 'dark'
|
|
90
583
|
}
|
|
584
|
+
````
|
|
91
585
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
586
|
+
### Runtime Theme Configuration
|
|
587
|
+
|
|
588
|
+
```typescript
|
|
589
|
+
interface RuntimeThemeConfig {
|
|
590
|
+
// Map of theme name to light/dark variants
|
|
591
|
+
themes: {
|
|
592
|
+
[themeName: string]: {
|
|
593
|
+
light: ThemeVariant;
|
|
594
|
+
dark: ThemeVariant;
|
|
595
|
+
};
|
|
596
|
+
};
|
|
597
|
+
|
|
598
|
+
// Assets mapped to themes (logos, banners, illustrations)
|
|
599
|
+
assets?: {
|
|
600
|
+
[themeName: string]: {
|
|
601
|
+
[assetKey: string]: ThemeAsset | NestedAssets;
|
|
602
|
+
};
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
// Global CSS variables for all themes
|
|
606
|
+
globalVariables?: Record<string, string>;
|
|
607
|
+
|
|
608
|
+
// Font definitions to inject
|
|
609
|
+
fonts?: FontDefinition[];
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
interface ThemeVariant {
|
|
613
|
+
daisyuiName: string; // DaisyUI theme name
|
|
614
|
+
prefersDark: boolean; // Whether this is dark variant
|
|
615
|
+
variables?: Record<string, string>; // Theme-specific CSS variables
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
interface ThemeAsset {
|
|
619
|
+
light: string; // URL for light scheme
|
|
620
|
+
dark?: string; // URL for dark scheme (optional)
|
|
621
|
+
}
|
|
98
622
|
```
|
|
99
623
|
|
|
100
|
-
|
|
624
|
+
### How It Works
|
|
625
|
+
|
|
626
|
+
#### Theme Resolution Flow
|
|
627
|
+
|
|
628
|
+
```
|
|
629
|
+
User selects theme/scheme
|
|
630
|
+
↓
|
|
631
|
+
ThemeService.setTheme() / setScheme()
|
|
632
|
+
↓
|
|
633
|
+
Signals updated (selectedTheme, selectedScheme)
|
|
634
|
+
↓
|
|
635
|
+
Computed values recalculated (resolvedScheme, isDarkTheme, finalTheme)
|
|
636
|
+
↓
|
|
637
|
+
Effect listeners triggered (DOM updates)
|
|
638
|
+
↓
|
|
639
|
+
Theme persisted to localStorage (async)
|
|
640
|
+
↓
|
|
641
|
+
CSS variables injected (ThemeDomService)
|
|
642
|
+
↓
|
|
643
|
+
data-theme attribute set on document body
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
#### Storage & Caching Strategy
|
|
647
|
+
|
|
648
|
+
The theme system uses a simplified two-layer storage strategy for maximum reliability:
|
|
649
|
+
|
|
650
|
+
**Layer 1: In-Memory (Runtime Cache)**
|
|
101
651
|
|
|
102
|
-
-
|
|
103
|
-
-
|
|
104
|
-
-
|
|
105
|
-
-
|
|
106
|
-
-
|
|
107
|
-
- **Automatic persistence** - Saves user preferences to localStorage
|
|
652
|
+
- Current active theme and scheme
|
|
653
|
+
- Last working configuration
|
|
654
|
+
- Asset cache with light/dark variants
|
|
655
|
+
- Session-scoped, cleared on page refresh
|
|
656
|
+
- Provides instant reactivity and computed values
|
|
108
657
|
|
|
109
|
-
|
|
658
|
+
**Layer 2: IndexedDB (Persistent Cache)**
|
|
110
659
|
|
|
111
|
-
-
|
|
660
|
+
- Runtime theme configurations loaded from remote
|
|
661
|
+
- Long-term theme asset URLs and metadata
|
|
662
|
+
- Multi-day retention with automatic cleanup
|
|
663
|
+
- Survives page refreshes and sessions
|
|
664
|
+
- Used as fallback when remote config unavailable
|
|
112
665
|
|
|
113
|
-
|
|
666
|
+
**Why This Design:**
|
|
114
667
|
|
|
115
|
-
|
|
668
|
+
- **Simplified**: No localStorage race conditions or privacy mode issues
|
|
669
|
+
- **Fast**: In-memory layer provides instant access to current state
|
|
670
|
+
- **Persistent**: IndexedDB stores heavy runtime configs for recovery
|
|
671
|
+
- **Resilient**: Falls back to last working config automatically
|
|
672
|
+
- **Safe**: No sensitive data stored, graceful degradation in private browsing
|
|
116
673
|
|
|
117
|
-
|
|
118
|
-
- `light` - Force light mode
|
|
119
|
-
- `dark` - Force dark mode
|
|
674
|
+
**When Each Layer Is Used:**
|
|
120
675
|
|
|
121
|
-
|
|
676
|
+
- **Runtime Flow**: In-memory signals + computed values (no I/O)
|
|
677
|
+
- **Config Loading**: Fetch remote → IndexedDB → In-memory → fallback
|
|
678
|
+
- **Asset Resolution**: In-memory cache (with scheme variants)
|
|
679
|
+
- **Session Persistence**: localStorage (user preferences only, not config)
|
|
680
|
+
- **Config Recovery**: Last working IndexedDB entry if remote fails
|
|
122
681
|
|
|
123
|
-
|
|
682
|
+
### Signal Reactivity
|
|
124
683
|
|
|
125
|
-
|
|
126
|
-
- `selectedScheme()` - Current selected scheme ('system' | 'light' | 'dark')
|
|
127
|
-
- `resolvedScheme()` - Actual resolved scheme ('light' | 'dark')
|
|
128
|
-
- `isDarkTheme()` - Boolean indicating if current resolved scheme is dark
|
|
129
|
-
- `finalTheme()` - Complete theme string (e.g., 'sixbell_telco\_\_dark')
|
|
684
|
+
All theme changes flow through signals for maximum performance:
|
|
130
685
|
|
|
131
|
-
|
|
686
|
+
```
|
|
687
|
+
selectedTheme/Scheme signals (user input)
|
|
688
|
+
↓
|
|
689
|
+
Effects trigger (auto-persist to localStorage)
|
|
690
|
+
↓
|
|
691
|
+
Computed values update (resolvedScheme, isDarkTheme, finalTheme)
|
|
692
|
+
↓
|
|
693
|
+
Components subscribed via signals detect change
|
|
694
|
+
↓
|
|
695
|
+
OnPush change detection → Template update (instant!)
|
|
696
|
+
↓
|
|
697
|
+
DOM updates apply via ThemeDomService
|
|
698
|
+
```
|
|
699
|
+
|
|
700
|
+
**Why Signals?**
|
|
132
701
|
|
|
133
|
-
-
|
|
134
|
-
-
|
|
135
|
-
-
|
|
136
|
-
-
|
|
137
|
-
-
|
|
702
|
+
- No manual unsubscribes needed (no memory leaks)
|
|
703
|
+
- Automatic fine-grained reactivity
|
|
704
|
+
- Computed values only recalculate when dependencies change
|
|
705
|
+
- Optimal change detection with OnPush strategy
|
|
706
|
+
- Better performance than RxJS Observables for this use case
|
|
138
707
|
|
|
139
|
-
##
|
|
708
|
+
## Performance Tips
|
|
140
709
|
|
|
141
|
-
|
|
142
|
-
| ------------------ | ------------------------------- | ------------------------------------------------------------- |
|
|
143
|
-
| `additionalThemes` | `readonly string[]` | Additional themes beyond the default `sixbell_telco` |
|
|
144
|
-
| `defaultTheme` | `string` | Default theme to use (with IntelliSense for available themes) |
|
|
145
|
-
| `excludeThemes` | `readonly string[]` | Themes to exclude from available themes |
|
|
146
|
-
| `defaultScheme` | `'system' \| 'light' \| 'dark'` | Default color scheme preference |
|
|
710
|
+
### 1. Use Computed Values Carefully
|
|
147
711
|
|
|
148
|
-
|
|
712
|
+
```typescript
|
|
713
|
+
// ✅ Good: Single computed value
|
|
714
|
+
isDarkTheme = this.themeService.isDarkTheme;
|
|
149
715
|
|
|
150
|
-
|
|
716
|
+
// ❌ Avoid: Complex computation in template
|
|
717
|
+
isDarkTheme() ? 'dark' : 'light';
|
|
718
|
+
```
|
|
151
719
|
|
|
152
|
-
|
|
720
|
+
### 2. Cache Theme-Specific Data
|
|
721
|
+
|
|
722
|
+
```typescript
|
|
723
|
+
// ✅ Cache loaded theme config
|
|
724
|
+
private runtimeConfig = signal<RuntimeThemeConfig | null>(null);
|
|
725
|
+
|
|
726
|
+
// ❌ Avoid: Reloading on every change
|
|
727
|
+
this.themeService.configureRuntime(config);
|
|
728
|
+
```
|
|
729
|
+
|
|
730
|
+
### 3. Use OnPush Change Detection
|
|
153
731
|
|
|
154
732
|
```typescript
|
|
155
733
|
@Component({
|
|
156
|
-
|
|
157
|
-
<p>System prefers: {{ theme.resolvedScheme() }}</p>
|
|
158
|
-
<p>User selected: {{ theme.selectedScheme() }}</p>
|
|
159
|
-
`,
|
|
734
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
160
735
|
})
|
|
161
|
-
export class
|
|
162
|
-
|
|
736
|
+
export class MyComponent {
|
|
737
|
+
// Signals are always detected efficiently
|
|
738
|
+
}
|
|
739
|
+
```
|
|
740
|
+
|
|
741
|
+
### 4. Lazy Load Heavy Assets
|
|
742
|
+
|
|
743
|
+
```typescript
|
|
744
|
+
// ✅ Load logos only when needed
|
|
745
|
+
logoUrl = computed(() => {
|
|
746
|
+
if (!this.isVisible()) return null;
|
|
747
|
+
return this.assetsService.getAssetUrl('logo');
|
|
748
|
+
});
|
|
749
|
+
```
|
|
750
|
+
|
|
751
|
+
## Architecture Deep Dive
|
|
752
|
+
|
|
753
|
+
### ThemeService Core
|
|
754
|
+
|
|
755
|
+
The main service orchestrates all theme operations through signals:
|
|
756
|
+
|
|
757
|
+
```typescript
|
|
758
|
+
// Signal-based reactive state (auto-persisted)
|
|
759
|
+
selectedTheme: Signal<string>; // Currently active theme
|
|
760
|
+
selectedScheme: Signal<ThemeScheme>; // Currently active scheme ('system'|'light'|'dark')
|
|
761
|
+
|
|
762
|
+
// Computed derived values (auto-memoized, no extra I/O)
|
|
763
|
+
resolvedScheme: Computed<ResolvedThemeScheme>; // Always 'light' or 'dark'
|
|
764
|
+
isDarkTheme: Computed<boolean>; // Quick dark mode check
|
|
765
|
+
finalTheme: Computed<string>; // Complete identifier like 'sixbell_telco__dark'
|
|
766
|
+
```
|
|
767
|
+
|
|
768
|
+
### Multi-Service Architecture
|
|
769
|
+
|
|
770
|
+
The theme system is composed of focused, single-responsibility services:
|
|
771
|
+
|
|
772
|
+
**ThemeService** (Orchestrator)
|
|
773
|
+
|
|
774
|
+
- Manages signals and main theme state
|
|
775
|
+
- Coordinates configuration loading
|
|
776
|
+
- Exposes high-level API for components
|
|
777
|
+
|
|
778
|
+
**ThemeStorageService** (Preferences)
|
|
779
|
+
|
|
780
|
+
- Persists user choices (theme, scheme) to localStorage
|
|
781
|
+
- Reads saved preferences on app startup
|
|
782
|
+
- Includes error handling for private browsing mode
|
|
783
|
+
|
|
784
|
+
**ThemeSchemeService** (OS Integration)
|
|
785
|
+
|
|
786
|
+
- Detects system dark mode preference via matchMedia
|
|
787
|
+
- Resolves 'system' scheme to actual 'light'/'dark' value
|
|
788
|
+
- Listens to OS theme changes for live updates
|
|
789
|
+
|
|
790
|
+
**ThemeDomService** (DOM Manipulation)
|
|
791
|
+
|
|
792
|
+
- Injects CSS variables into document head
|
|
793
|
+
- Manages font-face declarations
|
|
794
|
+
- Sets data-theme attribute on document body
|
|
795
|
+
|
|
796
|
+
**ThemeAssetsService** (Dynamic Assets)
|
|
797
|
+
|
|
798
|
+
- Retrieves logos/banners/illustrations for current theme
|
|
799
|
+
- Provides automatic light/dark scheme variants
|
|
800
|
+
- In-memory cache prevents repeated lookups
|
|
801
|
+
|
|
802
|
+
### Runtime Config Loading Flow
|
|
803
|
+
|
|
804
|
+
```
|
|
805
|
+
Remote Config File (JSON)
|
|
806
|
+
↓
|
|
807
|
+
ResourceLoader fetches with smart defaults
|
|
808
|
+
↓
|
|
809
|
+
IndexedDB Layer stores for recovery
|
|
810
|
+
↓
|
|
811
|
+
In-Memory Cache provides instant access
|
|
812
|
+
↓
|
|
813
|
+
ThemeService signals updated
|
|
814
|
+
↓
|
|
815
|
+
Components automatically re-render via signals
|
|
816
|
+
↓
|
|
817
|
+
Assets resolve from nested config by theme/scheme
|
|
818
|
+
```
|
|
819
|
+
|
|
820
|
+
**Resilience Features:**
|
|
821
|
+
|
|
822
|
+
- Automatic retry with exponential backoff
|
|
823
|
+
- Stale-while-revalidate (always try fresh first)
|
|
824
|
+
- Use-stale-on-error (use cached if fresh fails)
|
|
825
|
+
- Last-working-config fallback
|
|
826
|
+
- Per-theme asset resolution
|
|
827
|
+
|
|
828
|
+
### In-Memory vs IndexedDB Usage
|
|
829
|
+
|
|
830
|
+
**In-Memory Layer (Runtime Cache):**
|
|
831
|
+
|
|
832
|
+
- ✅ Instant access (no await)
|
|
833
|
+
- ✅ Current active theme/scheme
|
|
834
|
+
- ✅ Asset URL lookups
|
|
835
|
+
- ✅ Computed values
|
|
836
|
+
- ❌ Lost on page refresh
|
|
837
|
+
- ❌ Session-scoped only
|
|
838
|
+
|
|
839
|
+
**IndexedDB Layer (Persistent Cache):**
|
|
840
|
+
|
|
841
|
+
- ✅ Survives page refresh
|
|
842
|
+
- ✅ Stores heavy theme configs
|
|
843
|
+
- ✅ Recovery when remote unavailable
|
|
844
|
+
- ✅ Multi-day retention
|
|
845
|
+
- ❌ Async (requires await)
|
|
846
|
+
- ❌ Larger storage overhead
|
|
847
|
+
|
|
848
|
+
## Error Handling
|
|
849
|
+
|
|
850
|
+
### Storage Errors
|
|
851
|
+
|
|
852
|
+
Storage errors are logged but don't break the app - graceful fallback ensures functionality:
|
|
853
|
+
|
|
854
|
+
```typescript
|
|
855
|
+
// Graceful fallback if localStorage unavailable
|
|
856
|
+
getSavedTheme(): string | null {
|
|
857
|
+
try {
|
|
858
|
+
return localStorage.getItem('theme:selectedTheme');
|
|
859
|
+
} catch (error) {
|
|
860
|
+
logger.error('Failed to read theme from storage', error, {
|
|
861
|
+
component: 'ThemeStorageService',
|
|
862
|
+
action: 'getSavedTheme'
|
|
863
|
+
});
|
|
864
|
+
return null; // Falls back to default theme
|
|
865
|
+
}
|
|
163
866
|
}
|
|
164
867
|
```
|
|
165
868
|
|
|
166
|
-
|
|
869
|
+
**Scenarios Handled:**
|
|
870
|
+
|
|
871
|
+
- Private/Incognito browsing (no localStorage)
|
|
872
|
+
- Storage quota exceeded
|
|
873
|
+
- Cross-origin restrictions
|
|
874
|
+
- Server-side rendering (no localStorage)
|
|
875
|
+
|
|
876
|
+
### Runtime Config Loading Errors
|
|
877
|
+
|
|
878
|
+
If runtime config loading fails, the system automatically uses the last working configuration:
|
|
879
|
+
|
|
880
|
+
```typescript
|
|
881
|
+
// Failed fetch? Use cached config
|
|
882
|
+
configureRuntime(config: RuntimeThemeConfig): void {
|
|
883
|
+
if (!validateThemeConfig(config)) {
|
|
884
|
+
logger.error('Invalid runtime theme config', {
|
|
885
|
+
component: 'ThemeService',
|
|
886
|
+
action: 'configureRuntime'
|
|
887
|
+
});
|
|
888
|
+
// Automatically falls back to previous working config
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
// Apply new config
|
|
892
|
+
}
|
|
893
|
+
```
|
|
894
|
+
|
|
895
|
+
**Resilience Strategies:**
|
|
896
|
+
|
|
897
|
+
- Network retry with exponential backoff
|
|
898
|
+
- Stale-while-revalidate (always try fresh, use stale if fresh fails)
|
|
899
|
+
- IndexedDB recovery (last known good config)
|
|
900
|
+
- Fallback to in-memory (current session config)
|
|
901
|
+
|
|
902
|
+
## Troubleshooting
|
|
903
|
+
|
|
904
|
+
### Theme Not Persisting
|
|
905
|
+
|
|
906
|
+
**Problem**: Theme changes are lost on page refresh.
|
|
907
|
+
|
|
908
|
+
**Solution**: Ensure `ThemeStorageService` is provided and localStorage is available:
|
|
909
|
+
|
|
910
|
+
```typescript
|
|
911
|
+
// Check localStorage is accessible
|
|
912
|
+
const hasStorage = () => {
|
|
913
|
+
try {
|
|
914
|
+
localStorage.setItem('test', 'test');
|
|
915
|
+
localStorage.removeItem('test');
|
|
916
|
+
return true;
|
|
917
|
+
} catch (e) {
|
|
918
|
+
return false; // Private browsing, quota exceeded, etc.
|
|
919
|
+
}
|
|
920
|
+
};
|
|
921
|
+
|
|
922
|
+
// In your component, verify theme is being saved
|
|
923
|
+
this.themeService.setTheme('custom');
|
|
924
|
+
const saved = localStorage.getItem('theme:selectedTheme');
|
|
925
|
+
console.log('Saved theme:', saved); // Should be 'custom'
|
|
926
|
+
```
|
|
927
|
+
|
|
928
|
+
### System Scheme Not Detecting
|
|
929
|
+
|
|
930
|
+
**Problem**: OS dark mode preference not being detected.
|
|
931
|
+
|
|
932
|
+
**Solution**: Verify matchMedia support and check system preference:
|
|
933
|
+
|
|
934
|
+
```typescript
|
|
935
|
+
// Check if system supports media queries
|
|
936
|
+
const supportsMediaQuery = () => {
|
|
937
|
+
return window.matchMedia != null;
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
// Check current system preference
|
|
941
|
+
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
942
|
+
console.log('System prefers dark:', isDarkMode);
|
|
943
|
+
|
|
944
|
+
// Verify theme service is using system setting
|
|
945
|
+
console.log('Selected scheme:', this.themeService.selectedScheme()); // Should be 'system'
|
|
946
|
+
console.log('Resolved scheme:', this.themeService.resolvedScheme()); // Should be 'light' or 'dark'
|
|
947
|
+
```
|
|
948
|
+
|
|
949
|
+
### Runtime Config Not Loading
|
|
950
|
+
|
|
951
|
+
**Problem**: Remote theme config not loading or showing stale data.
|
|
952
|
+
|
|
953
|
+
**Solution**: Check ResourceLoader configuration and network:
|
|
954
|
+
|
|
955
|
+
```typescript
|
|
956
|
+
// Verify config endpoint is accessible
|
|
957
|
+
const configUrl = '/api/v1/themes/config';
|
|
958
|
+
const response = await fetch(configUrl);
|
|
959
|
+
console.log('Config fetch:', response.ok, response.status);
|
|
960
|
+
|
|
961
|
+
// Check cache settings
|
|
962
|
+
provideRuntimeTheme(configUrl, {
|
|
963
|
+
freshOnNavigation: false, // Try fresh first, fall back to cache
|
|
964
|
+
cacheTTL: 1000 * 60 * 60 * 24, // 24 hour validity
|
|
965
|
+
retries: 3, // Retry failed requests
|
|
966
|
+
});
|
|
967
|
+
|
|
968
|
+
// Clear IndexedDB cache to force fresh fetch
|
|
969
|
+
await indexedDB.databases().forEach((db) => {
|
|
970
|
+
indexedDB.deleteDatabase(db.name);
|
|
971
|
+
});
|
|
972
|
+
```
|
|
973
|
+
|
|
974
|
+
### Assets Not Loading
|
|
975
|
+
|
|
976
|
+
**Problem**: Theme-specific logos/banners not displaying.
|
|
977
|
+
|
|
978
|
+
**Solution**: Verify asset configuration and URLs:
|
|
979
|
+
|
|
980
|
+
```typescript
|
|
981
|
+
// Check asset exists in config
|
|
982
|
+
const hasLogo = this.assetsService.hasAsset('logo');
|
|
983
|
+
console.log('Has logo asset:', hasLogo);
|
|
984
|
+
|
|
985
|
+
// Get asset URL and verify it's correct
|
|
986
|
+
const url = this.assetsService.getAssetUrl('logo');
|
|
987
|
+
console.log('Logo URL:', url);
|
|
988
|
+
|
|
989
|
+
// Verify for specific theme
|
|
990
|
+
const womLogo = this.assetsService.getAssetUrl('logo', 'wom');
|
|
991
|
+
console.log('WOM logo URL:', womLogo);
|
|
992
|
+
|
|
993
|
+
// Check RuntimeThemeConfig has assets defined
|
|
994
|
+
console.log('Runtime config:', this.themeService.runtimeConfig);
|
|
995
|
+
```
|
|
996
|
+
|
|
997
|
+
### CSS Variables Not Applying
|
|
998
|
+
|
|
999
|
+
**Problem**: Theme CSS variables not updating when theme changes.
|
|
1000
|
+
|
|
1001
|
+
**Solution**: Verify DOM injection and CSS variable names:
|
|
1002
|
+
|
|
1003
|
+
```typescript
|
|
1004
|
+
// Check data-theme attribute is set correctly
|
|
1005
|
+
const themeAttr = document.body.getAttribute('data-theme');
|
|
1006
|
+
console.log('Current data-theme:', themeAttr); // Should be like 'sixbell_telco__light'
|
|
1007
|
+
|
|
1008
|
+
// Check CSS variables exist
|
|
1009
|
+
const vars = getComputedStyle(document.documentElement);
|
|
1010
|
+
console.log('CSS variables:', vars.getPropertyValue('--primary-color'));
|
|
1011
|
+
|
|
1012
|
+
// Verify theme styles were injected
|
|
1013
|
+
const styleSheets = Array.from(document.styleSheets);
|
|
1014
|
+
const themeSheet = styleSheets.find((sheet) => {
|
|
1015
|
+
try {
|
|
1016
|
+
return sheet.cssRules?.toString().includes('data-theme');
|
|
1017
|
+
} catch (e) {
|
|
1018
|
+
return false;
|
|
1019
|
+
}
|
|
1020
|
+
});
|
|
1021
|
+
console.log('Theme stylesheet found:', !!themeSheet);
|
|
1022
|
+
```
|
|
1023
|
+
|
|
1024
|
+
### Performance Issues
|
|
1025
|
+
|
|
1026
|
+
**Problem**: App feels sluggish when switching themes.
|
|
167
1027
|
|
|
168
|
-
|
|
1028
|
+
**Solution**: Optimize with OnPush change detection and signal usage:
|
|
169
1029
|
|
|
170
1030
|
```typescript
|
|
1031
|
+
// ✅ Good: OnPush with signals
|
|
171
1032
|
@Component({
|
|
172
|
-
|
|
173
|
-
<div [class]="'app-container ' + theme.finalTheme()">
|
|
174
|
-
<div [class.dark-mode]="theme.isDarkTheme()">Content adapts to theme automatically</div>
|
|
175
|
-
</div>
|
|
176
|
-
`,
|
|
1033
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
177
1034
|
})
|
|
178
|
-
export class
|
|
179
|
-
|
|
1035
|
+
export class ThemeAwareComponent {
|
|
1036
|
+
isDark = this.themeService.isDarkTheme; // Memoized!
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// ❌ Avoid: Default change detection with method calls
|
|
1040
|
+
@Component({
|
|
1041
|
+
changeDetection: ChangeDetectionStrategy.Default,
|
|
1042
|
+
})
|
|
1043
|
+
export class SlowComponent {
|
|
1044
|
+
isDark() {
|
|
1045
|
+
// Called on every digest!
|
|
1046
|
+
return this.themeService.isDarkTheme();
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
// Monitor theme loading performance
|
|
1051
|
+
console.time('theme-load');
|
|
1052
|
+
await this.themeService.configureRuntime(config);
|
|
1053
|
+
console.timeEnd('theme-load');
|
|
1054
|
+
```
|
|
1055
|
+
|
|
1056
|
+
## Best Practices
|
|
1057
|
+
|
|
1058
|
+
### 1. Define Themes at Bootstrap
|
|
1059
|
+
|
|
1060
|
+
```typescript
|
|
1061
|
+
// ✅ Good: Define all themes during app init
|
|
1062
|
+
bootstrapApplication(AppComponent, {
|
|
1063
|
+
providers: [
|
|
1064
|
+
provideTheme({
|
|
1065
|
+
additionalThemes: ['wom', 'custom'],
|
|
1066
|
+
defaultTheme: 'sixbell_telco',
|
|
1067
|
+
}),
|
|
1068
|
+
],
|
|
1069
|
+
});
|
|
1070
|
+
```
|
|
1071
|
+
|
|
1072
|
+
### 2. Use Type-Safe Theme Names
|
|
1073
|
+
|
|
1074
|
+
```typescript
|
|
1075
|
+
// ✅ Good: Enum for theme names
|
|
1076
|
+
enum BrandTheme {
|
|
1077
|
+
SIXBELL = 'sixbell_telco',
|
|
1078
|
+
WOM = 'wom',
|
|
1079
|
+
CUSTOM = 'custom',
|
|
180
1080
|
}
|
|
1081
|
+
|
|
1082
|
+
themeService.setTheme(BrandTheme.WOM);
|
|
1083
|
+
```
|
|
1084
|
+
|
|
1085
|
+
### 3. Handle System Preference Changes
|
|
1086
|
+
|
|
1087
|
+
```typescript
|
|
1088
|
+
// ✅ Good: Listen to OS theme changes
|
|
1089
|
+
effect(() => {
|
|
1090
|
+
if (this.selectedScheme() === 'system') {
|
|
1091
|
+
// App will automatically update via computed resolvedScheme
|
|
1092
|
+
}
|
|
1093
|
+
});
|
|
1094
|
+
```
|
|
1095
|
+
|
|
1096
|
+
### 4. Preload Runtime Config Early
|
|
1097
|
+
|
|
1098
|
+
```typescript
|
|
1099
|
+
// ✅ Good: Load config as early as possible
|
|
1100
|
+
bootstrapApplication(AppComponent, {
|
|
1101
|
+
providers: [
|
|
1102
|
+
provideRuntimeTheme('/api/v1/themes/config', {
|
|
1103
|
+
freshOnNavigation: false,
|
|
1104
|
+
retries: 3,
|
|
1105
|
+
}),
|
|
1106
|
+
],
|
|
1107
|
+
});
|
|
181
1108
|
```
|
|
182
1109
|
|
|
183
|
-
### Theme
|
|
1110
|
+
### 5. Test Theme Persistence
|
|
1111
|
+
|
|
1112
|
+
```typescript
|
|
1113
|
+
// ✅ Good: Verify saved preferences work
|
|
1114
|
+
it('should persist theme selection', () => {
|
|
1115
|
+
themeService.setTheme('custom');
|
|
1116
|
+
themeService.loadSavedPreferences();
|
|
1117
|
+
expect(themeService.selectedTheme()).toBe('custom');
|
|
1118
|
+
});
|
|
1119
|
+
```
|
|
1120
|
+
|
|
1121
|
+
## Related Utilities
|
|
1122
|
+
|
|
1123
|
+
- **[ThemeAssetsService](./theme-assets/)**: Dynamic asset loading based on theme
|
|
1124
|
+
- **[cn Utility](../cn/)**: Conditional CSS class merging
|
|
1125
|
+
- **[ResourceLoader](../resource-loader/)**: Runtime config loading with caching
|
|
1126
|
+
- **[TranslationService](../translation/)**: Localized theme and scheme names
|
|
1127
|
+
|
|
1128
|
+
## License
|
|
184
1129
|
|
|
185
|
-
|
|
1130
|
+
MIT - See LICENSE file in the root of the project.
|