zova-module-a-style 5.0.18 → 5.0.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zova-module-a-style",
3
- "version": "5.0.18",
3
+ "version": "5.0.20",
4
4
  "title": "a-style",
5
5
  "zovaModule": {
6
6
  "capabilities": {
@@ -37,5 +37,5 @@
37
37
  "tsc:publish": "npm run clean && tsc -b",
38
38
  "prepublishOnly": "npm run tsc:publish"
39
39
  },
40
- "gitHead": "99ce5c5f49c91443406af02c63b555063d5eb3f6"
40
+ "gitHead": "d7ecefeb8b5f566e4b4831d7b3a02e9e7a765c15"
41
41
  }
@@ -2,35 +2,32 @@ import { Bean, Cast, IBeanRecord } from 'zova';
2
2
  import { ScopeModule } from '../resource/this.js';
3
3
  import { ThemeBase, ThemeHandler } from '../types.js';
4
4
  import { BeanModelBase } from 'zova-module-a-model';
5
+ import { watch } from 'vue';
5
6
 
6
7
  export type ThemeDarkMode = 'auto' | boolean;
7
8
 
8
9
  @Bean({ containerScope: 'app' })
9
10
  export class BeanTheme extends BeanModelBase<ScopeModule> {
10
- private _name: keyof IBeanRecord;
11
- public get name(): keyof IBeanRecord {
12
- return this._name;
13
- }
11
+ name: keyof IBeanRecord;
12
+ darkMode: ThemeDarkMode; // auto/true/false
13
+
14
14
  private _dark: boolean;
15
- public get dark(): boolean {
15
+ get dark() {
16
16
  return this._dark;
17
17
  }
18
- private _darkMode: ThemeDarkMode; // auto/true/false
19
- public get darkMode(): ThemeDarkMode {
20
- return this._darkMode;
21
- }
18
+
22
19
  token: unknown;
23
20
  private _mediaDark?: MediaQueryList;
24
21
  private _onMediaDarkChange?;
25
22
 
26
23
  protected async __init__() {
27
- this._name = this.$useQueryCookie({
24
+ this.name = this.$useQueryCookie({
28
25
  queryKey: ['themename'],
26
+ meta: {
27
+ defaultData: this.scope.config.defaultTheme,
28
+ },
29
29
  });
30
- this._dark = this.$useQueryCookie({
31
- queryKey: ['themedark'],
32
- });
33
- this._darkMode = this.$useQueryCookie({
30
+ this.darkMode = this.$useQueryCookie({
34
31
  queryKey: ['themedarkmode'],
35
32
  meta: {
36
33
  persister: {
@@ -39,64 +36,69 @@ export class BeanTheme extends BeanModelBase<ScopeModule> {
39
36
  return this.$deserializeCookie(value);
40
37
  },
41
38
  },
39
+ defaultData: 'auto',
42
40
  },
43
41
  });
44
- await this._setDark(this._darkMode);
45
- await this._setTheme(this._name);
46
- await this.applyTheme();
42
+ this._dark = this.$useQueryCookie({
43
+ queryKey: ['themedark'],
44
+ meta: {
45
+ defaultData: () => {
46
+ return this._getDarkFromDarkMode(this.darkMode);
47
+ },
48
+ },
49
+ });
50
+
51
+ watch(
52
+ () => this.darkMode,
53
+ () => {
54
+ this._dark = this._getDarkFromDarkMode(this.darkMode);
55
+ },
56
+ );
57
+
58
+ watch([() => this.name, () => this._dark], () => {
59
+ this._applyTheme();
60
+ });
61
+ await this._applyTheme();
47
62
  }
48
63
 
49
64
  protected __dispose__() {
50
65
  this._listenMediaDarkChange(false);
51
66
  }
52
67
 
53
- async applyTheme() {
54
- const theme = (await this.bean._getBean(this.name as any, true)) as ThemeBase;
55
- const res = await theme.apply({ name: this.name, dark: this.dark });
68
+ async _applyTheme() {
69
+ const name = this.name;
70
+ const dark = this._dark;
71
+ const theme = (await this.bean._getBean(name as any, true)) as ThemeBase;
72
+ const res = await theme.apply({ name, dark });
56
73
  this.token = Cast(res).token;
57
74
  const handler = res.handler ?? this.scope.config.defaultThemeHandler;
58
75
  if (handler) {
59
76
  const themeHandler = (await this.bean._getBean(handler, true)) as unknown as ThemeHandler;
60
- await themeHandler.apply({ name: this.name, dark: this.dark, token: this.token } as any);
77
+ await themeHandler.apply({ name, dark, token: this.token } as any);
61
78
  }
62
79
  }
63
80
 
64
- async setTheme(name?: keyof IBeanRecord) {
65
- await this._setTheme(name);
66
- await this.applyTheme();
81
+ toggleDark() {
82
+ this.darkMode = !this._dark;
67
83
  }
68
84
 
69
- async _setTheme(name?: keyof IBeanRecord) {
70
- this._name = name || this.scope.config.defaultTheme;
71
- }
72
-
73
- async setDark(mode: ThemeDarkMode) {
74
- await this._setDark(mode);
75
- await this.applyTheme();
76
- }
77
-
78
- async _setDark(mode?: ThemeDarkMode) {
85
+ _getDarkFromDarkMode(mode?: ThemeDarkMode) {
79
86
  if (mode === undefined) mode = 'auto';
80
- this._darkMode = mode;
81
87
  if (mode === 'auto') {
82
88
  this._listenMediaDarkChange(true);
83
- this._dark = !!this._mediaDark?.matches;
89
+ return !!this._mediaDark?.matches;
84
90
  } else {
85
91
  this._listenMediaDarkChange(false);
86
- this._dark = mode;
92
+ return mode;
87
93
  }
88
94
  }
89
95
 
90
- async toggleDark() {
91
- await this.setDark(!this.dark);
92
- }
93
-
94
96
  _listenMediaDarkChange(listen: boolean) {
95
97
  if (listen) {
96
98
  if (!this._mediaDark) {
97
99
  this._mediaDark = window.matchMedia('(prefers-color-scheme: dark)');
98
100
  this._onMediaDarkChange = async () => {
99
- await this.setDark('auto');
101
+ this._applyTheme();
100
102
  };
101
103
  this._mediaDark.addEventListener('change', this._onMediaDarkChange);
102
104
  }