-
Notifications
You must be signed in to change notification settings - Fork 14
/
global-style.ts
57 lines (50 loc) · 1.11 KB
/
global-style.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
import { ELEMENT_ID } from '../config/config.js';
import { generateGlobalStyle } from '../config/style.js';
import { DefaultOptions } from '../types/index.js';
import { createElement } from '../utils/index.js';
// 全局样式
export class GlobalStyle {
styleSheet?: HTMLElement;
constructor(private options: DefaultOptions) {}
/**
* 创建
*/
create(): void {
this.styleSheet = createElement({
tagName: 'style',
id: ELEMENT_ID.globalStyle,
innerHtml: generateGlobalStyle(this.options.primaryColor)
});
}
/**
* 挂载
*/
mount(): void {
if (this.styleSheet) {
document.head.append(this.styleSheet);
}
}
/**
* 卸载
*/
unMount(): void {
this.styleSheet?.remove();
}
/**
* 更新
* @param options
*/
update(options: DefaultOptions): void {
this.options = options;
this.reloadStyleSheet();
}
initializeStyle(): void {
this.reloadStyleSheet();
}
// 重载样式表
reloadStyleSheet(): void {
if (this.styleSheet) {
this.styleSheet.innerHTML = generateGlobalStyle(this.options.primaryColor);
}
}
}