This repository was archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy paththeme-picker.ts
95 lines (86 loc) · 2.29 KB
/
theme-picker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {Component, ViewEncapsulation, ChangeDetectionStrategy, NgModule} from '@angular/core';
import {StyleManager} from '../style-manager/style-manager';
import {ThemeStorage, DocsSiteTheme} from './theme-storage/theme-storage';
import {
MatButtonModule, MatGridListModule, MatIconModule, MatMenuModule,
MatTooltipModule
} from '@angular/material';
import {CommonModule} from '@angular/common';
@Component({
selector: 'theme-picker',
templateUrl: 'theme-picker.html',
styleUrls: ['theme-picker.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
host: {'aria-hidden': 'true'},
})
export class ThemePicker {
currentTheme;
themes = [
{
primary: '#673AB7',
accent: '#FFC107',
href: 'deeppurple-amber.css',
isDark: false,
},
{
primary: '#3F51B5',
accent: '#E91E63',
href: 'indigo-pink.css',
isDark: false,
isDefault: true,
},
{
primary: '#E91E63',
accent: '#607D8B',
href: 'pink-bluegrey.css',
isDark: true,
},
{
primary: '#9C27B0',
accent: '#4CAF50',
href: 'purple-green.css',
isDark: true,
},
];
constructor(
public styleManager: StyleManager,
private _themeStorage: ThemeStorage
) {
const currentTheme = this._themeStorage.getStoredTheme();
if (currentTheme) {
this.installTheme(currentTheme);
}
}
installTheme(theme: DocsSiteTheme) {
this.currentTheme = this._getCurrentThemeFromHref(theme.href);
if (theme.isDefault) {
this.styleManager.removeStyle('theme');
} else {
// use timout to fix https://github.com/angular/material.angular.io/issues/495
setTimeout(() => {
this.styleManager.setStyle('theme', `assets/${theme.href}`);
});
}
if (this.currentTheme) {
this._themeStorage.storeTheme(this.currentTheme);
}
}
private _getCurrentThemeFromHref(href: string): DocsSiteTheme {
return this.themes.find(theme => theme.href === href);
}
}
@NgModule({
imports: [
MatButtonModule,
MatIconModule,
MatMenuModule,
MatGridListModule,
MatTooltipModule,
CommonModule
],
exports: [ThemePicker],
declarations: [ThemePicker],
providers: [StyleManager, ThemeStorage],
})
export class ThemePickerModule { }