zova-module-a-style 5.0.24 → 5.0.26

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.24",
3
+ "version": "5.0.26",
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": "38b917802862663cf6c2e6829857de9a2a742dcd"
40
+ "gitHead": "6ab950f2066f8feb2dfddb94575fcbb2cf07afe7"
41
41
  }
@@ -21,43 +21,29 @@ export class BeanTheme extends BeanModelBase<ScopeModule> {
21
21
  private _onMediaDarkChange?;
22
22
 
23
23
  protected async __init__() {
24
- this.name = this.$useQueryCookie({
24
+ // support admin
25
+ const useQueryMethod = this.app.config.ssr.cookieThemeName ? '$useQueryCookie' : '$useQueryLocal';
26
+ this.name = this[useQueryMethod]({
25
27
  queryKey: ['themename'],
26
- meta: {
27
- defaultData: this.scope.config.defaultTheme,
28
- },
29
- });
30
- this.darkMode = this.$useQueryCookie({
31
- queryKey: ['themedarkmode'],
32
28
  meta: {
33
29
  persister: {
34
- deserialize: value => {
35
- value = value === 'true' ? true : value === 'false' ? false : !value ? undefined : value;
36
- return this.$deserializeCookie(value);
37
- },
30
+ maxAge: this.scope.config.model.themename.persister.maxAge,
38
31
  },
39
- defaultData: 'auto',
32
+ defaultData: this.scope.config.defaultTheme,
40
33
  },
41
34
  });
42
- this._dark = this.$useQueryCookie({
35
+ this.darkMode = this.$useQueryLocal({
43
36
  queryKey: ['themedark'],
44
37
  meta: {
45
- persister: {
46
- deserialize: value => {
47
- value = value === 'true' ? true : value === 'false' ? false : !value ? undefined : value;
48
- return this.$deserializeCookie(value);
49
- },
50
- },
51
- defaultData: () => {
52
- return this._getDarkFromDarkMode(this.darkMode);
53
- },
38
+ defaultData: 'auto',
54
39
  },
55
40
  });
41
+ this._updateDark();
56
42
 
57
43
  watch(
58
44
  () => this.darkMode,
59
45
  () => {
60
- this._dark = this._getDarkFromDarkMode(this.darkMode);
46
+ this._updateDark();
61
47
  },
62
48
  );
63
49
 
@@ -72,6 +58,10 @@ export class BeanTheme extends BeanModelBase<ScopeModule> {
72
58
  this._listenMediaDarkChange(false);
73
59
  }
74
60
 
61
+ private _updateDark() {
62
+ this._dark = this._getDarkFromDarkMode(this.darkMode);
63
+ }
64
+
75
65
  async _applyTheme() {
76
66
  const name = this.name;
77
67
  const dark = this._dark;
@@ -101,10 +91,12 @@ export class BeanTheme extends BeanModelBase<ScopeModule> {
101
91
  }
102
92
 
103
93
  _listenMediaDarkChange(listen: boolean) {
94
+ if (process.env.SERVER) return;
104
95
  if (listen) {
105
96
  if (!this._mediaDark) {
106
97
  this._mediaDark = window.matchMedia('(prefers-color-scheme: dark)');
107
98
  this._onMediaDarkChange = async () => {
99
+ this._updateDark();
108
100
  this._applyTheme();
109
101
  };
110
102
  this._mediaDark.addEventListener('change', this._onMediaDarkChange);
@@ -5,5 +5,12 @@ export const config = (_app: ZovaApplication) => {
5
5
  defaultStyle: 'home-style.style.default' as keyof IBeanRecord,
6
6
  defaultTheme: 'home-theme.theme.default' as keyof IBeanRecord,
7
7
  defaultThemeHandler: '' as keyof IBeanRecord,
8
+ model: {
9
+ themename: {
10
+ persister: {
11
+ maxAge: Infinity,
12
+ },
13
+ },
14
+ },
8
15
  };
9
16
  };
package/src/monkey.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { style } from 'typestyle';
1
+ import { style, createTypeStyle, TypeStyle } from 'typestyle';
2
2
  import { BeanBase, BeanContainer, BeanSimple, IMonkeySystem, SymbolModuleName, useComputed } from 'zova';
3
3
  import { ScopeModule, __ThisModule__ } from './resource/this.js';
4
4
  import { BeanTheme } from './bean/bean.theme.js';
@@ -6,8 +6,23 @@ import { BeanTheme } from './bean/bean.theme.js';
6
6
  export class Monkey extends BeanSimple implements IMonkeySystem {
7
7
  private _beanTheme: BeanTheme;
8
8
  private _beanStyleDefault: any;
9
+ private _styleInstance: TypeStyle;
9
10
 
10
- async appInitialize(_bean: BeanContainer) {}
11
+ async appInitialize(_bean: BeanContainer) {
12
+ if (process.env.SERVER) {
13
+ this._styleInstance = createTypeStyle();
14
+ this.ctx.meta.ssr.context.onRendered(() => {
15
+ const styles = this._styleInstance.getStyles();
16
+ this.ctx.meta.ssr.context._meta.endingHeadTags += `<style id="styles-target">${styles}</style>`;
17
+ });
18
+ }
19
+ if (process.env.CLIENT && this.ctx.meta.ssr.isRuntimeSsrPreHydration) {
20
+ this._styleInstance = createTypeStyle();
21
+ this.ctx.meta.ssr.onHydrated(() => {
22
+ this._styleInstance.setStylesTarget(document.getElementById('styles-target')!);
23
+ });
24
+ }
25
+ }
11
26
  async appInitialized(bean: BeanContainer) {
12
27
  // theme
13
28
  this._beanTheme = await bean._getBean(BeanTheme, true);
@@ -54,9 +69,15 @@ export class Monkey extends BeanSimple implements IMonkeySystem {
54
69
  beanDisposed(_bean: BeanContainer, _beanInstance: BeanBase) {}
55
70
 
56
71
  _patchStyle(beanInstance: BeanBase, props, ...args) {
57
- if (props && typeof props === 'object') {
58
- props = Object.assign({ $debugName: beanInstance[SymbolModuleName] }, props);
72
+ if (process.env.dev) {
73
+ if (props && typeof props === 'object') {
74
+ props = Object.assign({ $debugName: beanInstance[SymbolModuleName] }, props);
75
+ }
76
+ }
77
+ if (this._styleInstance) {
78
+ return this._styleInstance.style(props, ...args);
79
+ } else {
80
+ return style(props, ...args);
59
81
  }
60
- return style(props, ...args);
61
82
  }
62
83
  }