zova-module-a-style 5.0.19 → 5.0.21

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.19",
3
+ "version": "5.0.21",
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": "e21dbb40dc8e68df8da56c7584c7a6811b599171"
40
+ "gitHead": "6196ff776f5f337fce224c6ff2b5d19647ad0a6e"
41
41
  }
@@ -2,36 +2,45 @@ 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
+ });
30
+ this.darkMode = this.$useQueryCookie({
31
+ queryKey: ['themedarkmode'],
32
+ meta: {
33
+ persister: {
34
+ deserialize: value => {
35
+ value = value === 'true' ? true : value === 'false' ? false : !value ? undefined : value;
36
+ return this.$deserializeCookie(value);
37
+ },
38
+ },
39
+ defaultData: 'auto',
40
+ },
29
41
  });
30
42
  this._dark = this.$useQueryCookie({
31
43
  queryKey: ['themedark'],
32
- });
33
- this._darkMode = this.$useQueryCookie({
34
- queryKey: ['themedarkmode'],
35
44
  meta: {
36
45
  persister: {
37
46
  deserialize: value => {
@@ -39,64 +48,63 @@ export class BeanTheme extends BeanModelBase<ScopeModule> {
39
48
  return this.$deserializeCookie(value);
40
49
  },
41
50
  },
51
+ defaultData: () => {
52
+ return this._getDarkFromDarkMode(this.darkMode);
53
+ },
54
+ },
55
+ });
56
+
57
+ watch(
58
+ () => this.darkMode,
59
+ () => {
60
+ this._dark = this._getDarkFromDarkMode(this.darkMode);
42
61
  },
62
+ );
63
+
64
+ watch([() => this.name, () => this._dark], () => {
65
+ this._applyTheme();
43
66
  });
44
- await this._setDark(this._darkMode);
45
- await this._setTheme(this._name);
46
- await this.applyTheme();
67
+ await this._applyTheme();
47
68
  }
48
69
 
49
70
  protected __dispose__() {
50
71
  this._listenMediaDarkChange(false);
51
72
  }
52
73
 
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 });
74
+ async _applyTheme() {
75
+ const name = this.name;
76
+ const dark = this._dark;
77
+ const theme = (await this.bean._getBean(name as any, true)) as ThemeBase;
78
+ const res = await theme.apply({ name, dark });
56
79
  this.token = Cast(res).token;
57
80
  const handler = res.handler ?? this.scope.config.defaultThemeHandler;
58
81
  if (handler) {
59
82
  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);
83
+ await themeHandler.apply({ name, dark, token: this.token } as any);
61
84
  }
62
85
  }
63
86
 
64
- async setTheme(name?: keyof IBeanRecord) {
65
- await this._setTheme(name);
66
- await this.applyTheme();
67
- }
68
-
69
- async _setTheme(name?: keyof IBeanRecord) {
70
- this._name = name || this.scope.config.defaultTheme;
87
+ toggleDark() {
88
+ this.darkMode = !this._dark;
71
89
  }
72
90
 
73
- async setDark(mode?: ThemeDarkMode) {
74
- await this._setDark(mode);
75
- await this.applyTheme();
76
- }
77
-
78
- async _setDark(mode?: ThemeDarkMode) {
91
+ _getDarkFromDarkMode(mode?: ThemeDarkMode) {
79
92
  if (mode === undefined) mode = 'auto';
80
- this._darkMode = mode;
81
93
  if (mode === 'auto') {
82
94
  this._listenMediaDarkChange(true);
83
- this._dark = !!this._mediaDark?.matches;
95
+ return !!this._mediaDark?.matches;
84
96
  } else {
85
97
  this._listenMediaDarkChange(false);
86
- this._dark = mode;
98
+ return mode;
87
99
  }
88
100
  }
89
101
 
90
- async toggleDark() {
91
- await this.setDark(!this.dark);
92
- }
93
-
94
102
  _listenMediaDarkChange(listen: boolean) {
95
103
  if (listen) {
96
104
  if (!this._mediaDark) {
97
105
  this._mediaDark = window.matchMedia('(prefers-color-scheme: dark)');
98
106
  this._onMediaDarkChange = async () => {
99
- await this.setDark('auto');
107
+ this._applyTheme();
100
108
  };
101
109
  this._mediaDark.addEventListener('change', this._onMediaDarkChange);
102
110
  }