zova-module-a-style 5.0.5 → 5.0.7

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.5",
3
+ "version": "5.0.7",
4
4
  "title": "a-style",
5
5
  "zovaModule": {
6
6
  "capabilities": {
@@ -34,5 +34,5 @@
34
34
  "tsc:publish": "npm run clean && tsc -b",
35
35
  "prepublishOnly": "npm run tsc:publish"
36
36
  },
37
- "gitHead": "0e66695a8571a9e0b6a2422a4235f2e9b10166e2"
37
+ "gitHead": "f72c4e9f7fbc562a409406abbc57808610fa0b25"
38
38
  }
@@ -0,0 +1,85 @@
1
+ import { BeanBase, Cast, IBeanRecord, Store } from 'zova';
2
+ import { ScopeModule } from '../resource/this.js';
3
+ import { ThemeBase } from '../types.js';
4
+
5
+ export type ThemeDarkMode = 'auto' | boolean;
6
+
7
+ @Store()
8
+ export class StoreTheme extends BeanBase<ScopeModule> {
9
+ private _name: string;
10
+ public get name(): string {
11
+ return this._name;
12
+ }
13
+ public set name(value: string) {
14
+ this._name = value;
15
+ this.applyTheme();
16
+ }
17
+ dark: boolean;
18
+ darkMode: ThemeDarkMode; // auto/true/false
19
+ token: unknown;
20
+ private _mediaDark?: MediaQueryList;
21
+ private _onMediaDarkChange?;
22
+
23
+ protected async __init__() {
24
+ await this._setDark('auto');
25
+ await this._setTheme();
26
+ await this.applyTheme();
27
+ }
28
+
29
+ protected __dispose__() {
30
+ this._listenMediaDarkChange(false);
31
+ }
32
+
33
+ async applyTheme() {
34
+ const theme = (await this.bean._getBean(this.name, true)) as ThemeBase;
35
+ const res = await theme.apply({ name: this.name, dark: this.dark });
36
+ this.token = Cast(res).token;
37
+ }
38
+
39
+ async setTheme(name?: keyof IBeanRecord) {
40
+ await this._setTheme(name);
41
+ await this.applyTheme();
42
+ }
43
+
44
+ async _setTheme(name?: keyof IBeanRecord) {
45
+ this.name = name || this.scope.config.defaultTheme;
46
+ }
47
+
48
+ async setDark(mode: ThemeDarkMode) {
49
+ await this._setDark(mode);
50
+ await this.applyTheme();
51
+ }
52
+
53
+ async _setDark(mode: ThemeDarkMode) {
54
+ this.darkMode = mode;
55
+ if (mode === 'auto') {
56
+ this._listenMediaDarkChange(true);
57
+ this.dark = !!this._mediaDark?.matches;
58
+ } else {
59
+ this._listenMediaDarkChange(false);
60
+ this.dark = mode;
61
+ }
62
+ }
63
+
64
+ async toggleDark() {
65
+ await this.setDark(!this.dark);
66
+ }
67
+
68
+ _listenMediaDarkChange(listen: boolean) {
69
+ if (listen) {
70
+ if (!this._mediaDark) {
71
+ this._mediaDark = window.matchMedia('(prefers-color-scheme: dark)');
72
+ this._onMediaDarkChange = async () => {
73
+ await this.setDark('auto');
74
+ };
75
+ this._mediaDark.addEventListener('change', this._onMediaDarkChange);
76
+ }
77
+ } else {
78
+ if (this._mediaDark) {
79
+ this._mediaDark.removeEventListener('change', this._onMediaDarkChange);
80
+ this._onMediaDarkChange = undefined;
81
+ this._mediaDark = undefined;
82
+ }
83
+ }
84
+ }
85
+ }
@@ -3,5 +3,6 @@ import { IBeanRecord, ZovaApplication } from 'zova';
3
3
  export const config = (_app: ZovaApplication) => {
4
4
  return {
5
5
  defaultStyle: 'home-style.style.default' as keyof IBeanRecord,
6
+ defaultTheme: 'home-theme.theme.default' as keyof IBeanRecord,
6
7
  };
7
8
  };
package/src/monkey.ts CHANGED
@@ -1,13 +1,18 @@
1
1
  import { style } from 'typestyle';
2
- import { BeanBase, BeanContainerLike, BeanSimple, IMonkeySystem, SymbolModuleName } from 'zova';
2
+ import { BeanBase, BeanContainerLike, BeanSimple, IMonkeySystem, SymbolModuleName, useComputed } from 'zova';
3
3
  import { ScopeModule, __ThisModule__ } from './resource/this.js';
4
+ import { StoreTheme } from './bean/store.theme.js';
4
5
 
5
6
  export class Monkey extends BeanSimple implements IMonkeySystem {
7
+ private _storeTheme: StoreTheme;
6
8
  private _storeStyleDefault: any;
7
9
 
8
10
  async appInitialize() {
11
+ // theme
12
+ this._storeTheme = await this.bean._getBean(StoreTheme, true);
13
+ // style default
9
14
  const scope: ScopeModule = await this.bean.getScope(__ThisModule__);
10
- this._storeStyleDefault = await this.bean._newBean(scope.config.defaultStyle, true);
15
+ this._storeStyleDefault = await this.bean._getBean(scope.config.defaultStyle, true);
11
16
  }
12
17
  async appInitialized() {}
13
18
  async beanInit(bean: BeanContainerLike, beanInstance: BeanBase) {
@@ -28,6 +33,20 @@ export class Monkey extends BeanSimple implements IMonkeySystem {
28
33
  return self._storeStyleDefault;
29
34
  },
30
35
  });
36
+ bean.defineProperty(beanInstance, '$theme', {
37
+ enumerable: false,
38
+ configurable: true,
39
+ get() {
40
+ return self._storeTheme;
41
+ },
42
+ });
43
+ bean.defineProperty(beanInstance, '$token', {
44
+ enumerable: false,
45
+ configurable: true,
46
+ get() {
47
+ return useComputed(() => self._storeTheme.token);
48
+ },
49
+ });
31
50
  }
32
51
  async beanInited(_bean: BeanContainerLike, _beanInstance: BeanBase) {}
33
52
  beanDispose(_bean: BeanContainerLike, _beanInstance: BeanBase) {}
@@ -35,7 +54,7 @@ export class Monkey extends BeanSimple implements IMonkeySystem {
35
54
 
36
55
  _patchStyle(beanInstance: BeanBase, props, ...args) {
37
56
  if (props && typeof props === 'object') {
38
- props.$debugName = beanInstance[SymbolModuleName];
57
+ props = Object.assign({ $debugName: beanInstance[SymbolModuleName] }, props);
39
58
  }
40
59
  return style(props, ...args);
41
60
  }
@@ -1,4 +1,8 @@
1
+ export * from '../bean/store.theme.js';
2
+ import { StoreTheme } from '../bean/store.theme.js';
1
3
  import 'zova';
2
4
  declare module 'zova' {
3
- export interface IBeanRecord {}
5
+ export interface IBeanRecord {
6
+ 'a-style.store.theme': StoreTheme;
7
+ }
4
8
  }
package/src/types.ts CHANGED
@@ -1,7 +1,20 @@
1
1
  import { style } from 'typestyle';
2
2
  import 'zova';
3
+ import { StoreTheme } from './bean/store.theme.js';
3
4
  declare module 'zova' {
4
5
  export interface BeanBase {
5
6
  $style: typeof style;
7
+ $theme: StoreTheme;
6
8
  }
7
9
  }
10
+
11
+ export interface ThemeApplyParams {
12
+ name: string;
13
+ dark: boolean;
14
+ }
15
+
16
+ export interface ThemeApplyResult {}
17
+
18
+ export interface ThemeBase {
19
+ apply({ name, dark }: ThemeApplyParams): Promise<ThemeApplyResult>;
20
+ }
package/src/bean/.gitkeep DELETED
File without changes