@mfeflow/theme 0.2.0 → 0.3.0

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/dist/index.js CHANGED
@@ -67,6 +67,6 @@ function noFlashScript(themes, options) {
67
67
  }
68
68
  ]))
69
69
  }).replace(/</g, '\\u003c');
70
- return `(function(){try{var o=${data};var n;try{n=JSON.parse(localStorage.getItem(o.k)||'null')}catch(e){}if(!o.t[n]){n=o.d;if(o.s&&window.matchMedia&&window.matchMedia('(prefers-color-scheme: dark)').matches){for(var x in o.t){if(o.t[x].c==='dark'){n=x;break}}}}var t=o.t[n],r=document.documentElement;for(var v in t.v){r.style.setProperty(v,t.v[v])}r.setAttribute('data-theme',n);r.style.colorScheme=t.c}catch(e){}})();`;
70
+ return `(function(){try{var o=${data};var n;try{n=JSON.parse(localStorage.getItem(o.k)||'null')}catch(e){}if(!Object.prototype.hasOwnProperty.call(o.t,n)){n=o.d;if(o.s&&window.matchMedia&&window.matchMedia('(prefers-color-scheme: dark)').matches){for(var x in o.t){if(o.t[x].c==='dark'){n=x;break}}}}var t=o.t[n],r=document.documentElement;for(var v in t.v){r.style.setProperty(v,t.v[v])}r.setAttribute('data-theme',n);r.style.colorScheme=t.c}catch(e){}})();`;
71
71
  }
72
72
  export { CSS_VAR_PREFIX, THEME_STORAGE_KEY, THEME_STORAGE_MFE, applyTheme, cssVar, cssVarName, defineTheme, noFlashScript, resolveTheme, themeToCssVars };
package/dist/react.js CHANGED
@@ -15,7 +15,7 @@ function ThemeProvider({ themes, defaultTheme, followSystem = true, storage, roo
15
15
  storage
16
16
  ]);
17
17
  const [stored, setStored] = useStoredState(store, THEME_STORAGE_KEY, null, isThemeName);
18
- const theme = resolveTheme(themes, stored ?? void 0, defaultTheme, followSystem && null === stored && prefersDark());
18
+ const theme = resolveTheme(themes, stored ?? void 0, defaultTheme, followSystem && prefersDark());
19
19
  useLayoutEffect(()=>{
20
20
  applyTheme(theme, root);
21
21
  }, [
@@ -0,0 +1,14 @@
1
+ import { type Theme } from './index.js';
2
+ export interface StartingThemeOptions {
3
+ defaultTheme: string;
4
+ /** Use the OS dark-mode preference when no theme is stored. Default true, as for `ThemeProvider`. */
5
+ followSystem?: boolean;
6
+ /** Where the choice is read from. Default `localStorage`. */
7
+ storage?: Pick<Storage, 'getItem'>;
8
+ }
9
+ /**
10
+ * The theme to show before React renders: the stored choice if it names a theme in the list (a stray value such as
11
+ * "constructor" is not one), else the system dark preference when `followSystem` is on, else the default. The same
12
+ * order `noFlashScript` and `ThemeProvider` use. For the shell's first boot step.
13
+ */
14
+ export declare function startingTheme(themes: readonly Theme[], options: StartingThemeOptions): Theme;
@@ -0,0 +1,18 @@
1
+ import { THEME_STORAGE_KEY, THEME_STORAGE_MFE, resolveTheme } from "./index.js";
2
+ function storedName(storage) {
3
+ try {
4
+ const raw = storage?.getItem(`${THEME_STORAGE_MFE}:${THEME_STORAGE_KEY}`);
5
+ const value = null == raw ? void 0 : JSON.parse(raw);
6
+ return 'string' == typeof value ? value : void 0;
7
+ } catch {
8
+ return;
9
+ }
10
+ }
11
+ function prefersDark() {
12
+ return 'function' == typeof matchMedia && matchMedia('(prefers-color-scheme: dark)').matches;
13
+ }
14
+ function startingTheme(themes, options) {
15
+ const storage = options.storage ?? ("u" < typeof localStorage ? void 0 : localStorage);
16
+ return resolveTheme(themes, storedName(storage), options.defaultTheme, (options.followSystem ?? true) && prefersDark());
17
+ }
18
+ export { startingTheme };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mfeflow/theme",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Design tokens as CSS variables for micro-frontends: the shell sets the theme once, every remote inherits it. ThemeProvider, useTheme, persisted choice, no-flash script.",
5
5
  "keywords": [
6
6
  "micro-frontend",
@@ -34,6 +34,11 @@
34
34
  "types": "./dist/react.d.ts",
35
35
  "import": "./dist/react.js",
36
36
  "default": "./dist/react.js"
37
+ },
38
+ "./starting": {
39
+ "types": "./dist/starting.d.ts",
40
+ "import": "./dist/starting.js",
41
+ "default": "./dist/starting.js"
37
42
  }
38
43
  },
39
44
  "files": [
@@ -47,7 +52,7 @@
47
52
  "provenance": true
48
53
  },
49
54
  "dependencies": {
50
- "@mfeflow/storage": "0.2.0"
55
+ "@mfeflow/storage": "0.3.0"
51
56
  },
52
57
  "peerDependencies": {
53
58
  "react": "^19.0.0"