resonare 0.1.7 → 0.2.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/README.md +1 -1
- package/dist/index.d.ts +12 -84
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lite-react.d.ts +36 -0
- package/dist/lite-react.d.ts.map +1 -0
- package/dist/lite-react.js +2 -0
- package/dist/lite-react.js.map +1 -0
- package/dist/lite.d.ts +43 -0
- package/dist/lite.d.ts.map +1 -0
- package/dist/lite.js +2 -0
- package/dist/lite.js.map +1 -0
- package/dist/react.d.ts +17 -17
- package/dist/react.js +1 -1
- package/dist/react.js.map +1 -1
- package/dist/storage-B5xjLRee.js +2 -0
- package/dist/storage-B5xjLRee.js.map +1 -0
- package/dist/storage-DGYV332J.d.ts +23 -0
- package/dist/storage-DGYV332J.d.ts.map +1 -0
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Resonare [](https://www.npmjs.com/package/resonare) [](https://bundlephobia.com/package/resonare)
|
|
2
2
|
|
|
3
3
|
A state store for multi-dimensional themes and user preferences. [Check out the demo](https://resonare.phuoccss.workers.dev).
|
|
4
4
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,52 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* Pluggable persistence for `createThemeStore`.
|
|
4
|
-
*/
|
|
5
|
-
type StorageAdapter = {
|
|
6
|
-
get: () => object | null;
|
|
7
|
-
set: (value: object) => void; /** Optional: notify other tabs/contexts after local `set` operation. */
|
|
8
|
-
broadcast?: (value: object) => void; /** Optional: subscribes to other tabs/contexts' updates, returns `unsubscribe` function. */
|
|
9
|
-
watch?: (cb: (value: object) => void) => () => void;
|
|
10
|
-
};
|
|
11
|
-
type StorageAdapterCreate = ({
|
|
12
|
-
abortController
|
|
13
|
-
}: {
|
|
14
|
-
abortController: AbortController;
|
|
15
|
-
}) => StorageAdapter;
|
|
16
|
-
type StorageAdapterCreator<Options> = (options: Options) => StorageAdapterCreate;
|
|
17
|
-
/**
|
|
18
|
-
* Persists theme store in `localStorage` or `sessionStorage`.
|
|
19
|
-
* @example
|
|
20
|
-
* ```ts
|
|
21
|
-
* import { createThemeStore, localStorageAdapter } from 'resonare'
|
|
22
|
-
*
|
|
23
|
-
* const store = createThemeStore(
|
|
24
|
-
* { colorMode: { options: ['light', 'dark'] },
|
|
25
|
-
* { storage: localStorageAdapter({ key: 'app', type: 'localStorage' }) },
|
|
26
|
-
* )
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
declare const localStorageAdapter: StorageAdapterCreator<{
|
|
30
|
-
key: string;
|
|
31
|
-
type?: 'localStorage' | 'sessionStorage';
|
|
32
|
-
}>;
|
|
33
|
-
/**
|
|
34
|
-
* In-memory persistence and sync via `BroadcastChannel`.
|
|
35
|
-
* Useful with server-side persistence.
|
|
36
|
-
* @example
|
|
37
|
-
* ```ts
|
|
38
|
-
* import { createThemeStore, memoryStorageAdapter } from 'resonare'
|
|
39
|
-
*
|
|
40
|
-
* const store = createThemeStore(
|
|
41
|
-
* { colorMode: { options: ['light', 'dark'] },
|
|
42
|
-
* { storage: memoryStorageAdapter({ key: 'app' }) },
|
|
43
|
-
* )
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
declare const memoryStorageAdapter: StorageAdapterCreator<{
|
|
47
|
-
key: string;
|
|
48
|
-
}>;
|
|
49
|
-
//#endregion
|
|
1
|
+
import { a as memoryStorageAdapter, i as localStorageAdapter, n as StorageAdapterCreate, r as StorageAdapterCreator, t as StorageAdapter } from "./storage-DGYV332J.js";
|
|
2
|
+
|
|
50
3
|
//#region src/index.d.ts
|
|
51
4
|
type ThemeValue = string | number | boolean;
|
|
52
5
|
type ThemeOption<T extends ThemeValue = string> = {
|
|
@@ -55,16 +8,16 @@ type ThemeOption<T extends ThemeValue = string> = {
|
|
|
55
8
|
};
|
|
56
9
|
type ThemeConfig<T extends ThemeValue = string> = {
|
|
57
10
|
options: ReadonlyArray<T | ThemeOption<T>>;
|
|
58
|
-
|
|
11
|
+
defaultValue?: T;
|
|
59
12
|
} | {
|
|
60
|
-
|
|
13
|
+
defaultValue: T;
|
|
61
14
|
options?: never;
|
|
62
15
|
};
|
|
63
16
|
type ThemeStoreConfig = Record<string, ThemeConfig<string> | ThemeConfig<number> | ThemeConfig<boolean>>;
|
|
64
17
|
type Themes<T extends ThemeStoreConfig> = { [K in keyof T]: T[K] extends {
|
|
65
18
|
options: ReadonlyArray<infer U>;
|
|
66
19
|
} ? U extends ThemeOption ? U['value'] : U : T[K] extends {
|
|
67
|
-
|
|
20
|
+
defaultValue: infer U;
|
|
68
21
|
} ? U extends string ? string : U extends number ? number : boolean : never };
|
|
69
22
|
type ThemeKeysWithSystemOption<T extends ThemeStoreConfig> = { [K in keyof T]: T[K] extends {
|
|
70
23
|
options: ReadonlyArray<infer U>;
|
|
@@ -77,21 +30,19 @@ type NonSystemOptionValues<T extends ThemeStoreConfig, K extends keyof T> = T[K]
|
|
|
77
30
|
media: [string, string, string];
|
|
78
31
|
} ? never : U['value'] : never : never;
|
|
79
32
|
type ResolvedSystemThemes<T extends ThemeStoreConfig> = { [K in ThemeKeysWithSystemOption<T>]: NonSystemOptionValues<T, K> };
|
|
80
|
-
type
|
|
81
|
-
themes: Themes<T>;
|
|
82
|
-
resolvedThemes: Themes<T>;
|
|
83
|
-
} : {
|
|
33
|
+
type Snapshot<T extends ThemeStoreConfig> = {
|
|
84
34
|
themes: Themes<T>;
|
|
85
35
|
resolvedThemes: Themes<T>;
|
|
86
36
|
resolvedSystemThemes: ResolvedSystemThemes<T>;
|
|
87
|
-
}
|
|
37
|
+
};
|
|
38
|
+
type Listener<T extends ThemeStoreConfig> = (value: Snapshot<T>) => void;
|
|
88
39
|
type PersistedSystemOptionPairs = Record<string, [string, string]>;
|
|
89
40
|
type PersistedState<T extends ThemeStoreConfig> = {
|
|
90
41
|
themes: Partial<Themes<T>>;
|
|
91
42
|
systemOptions: PersistedSystemOptionPairs;
|
|
92
43
|
};
|
|
93
44
|
type ThemeStoreOptions<T extends ThemeStoreConfig> = {
|
|
94
|
-
|
|
45
|
+
persisted?: Partial<PersistedState<T>>;
|
|
95
46
|
storage?: StorageAdapterCreate | null;
|
|
96
47
|
};
|
|
97
48
|
type ThemeAndOptions<T extends ThemeStoreConfig> = Array<{ [K in keyof T]: [K, Array<T[K] extends {
|
|
@@ -102,50 +53,27 @@ declare function getDefaultThemes<T extends ThemeStoreConfig>(config: T): Themes
|
|
|
102
53
|
declare class ThemeStore<T extends ThemeStoreConfig> {
|
|
103
54
|
#private;
|
|
104
55
|
constructor(config: T, {
|
|
105
|
-
|
|
56
|
+
persisted,
|
|
106
57
|
storage
|
|
107
58
|
}?: ThemeStoreOptions<T>);
|
|
108
59
|
getThemes: () => Themes<T>;
|
|
109
60
|
getResolvedThemes: () => Themes<T>;
|
|
110
61
|
getResolvedSystemThemes: () => ResolvedSystemThemes<T>;
|
|
62
|
+
getSnapshot: () => Snapshot<T>;
|
|
111
63
|
setThemes: (themes: Partial<Themes<T>> | ((currentThemes: Themes<T>) => Partial<Themes<T>>)) => void;
|
|
112
64
|
updateSystemOption: <K extends ThemeKeysWithSystemOption<T>>(themeKey: K, pair: [NonSystemOptionValues<T, K>, NonSystemOptionValues<T, K>]) => void;
|
|
113
65
|
toPersist: () => PersistedState<T>;
|
|
114
66
|
restore: () => void;
|
|
115
67
|
subscribe: (callback: Listener<T>) => (() => void);
|
|
116
68
|
sync: () => (() => void) | undefined;
|
|
117
|
-
/** Clears subscribers and aborts media-query listeners tied to this store instance. */
|
|
118
69
|
destroy: () => void;
|
|
119
70
|
}
|
|
120
71
|
declare function createThemeStore<T extends ThemeStoreConfig>(config: T, options?: ThemeStoreOptions<T>): ThemeStore<T>;
|
|
121
72
|
type ThemeScriptParameter = {
|
|
122
73
|
/** `localStorage` key; defaults to the 'resonare'. */key?: string;
|
|
123
74
|
config: ThemeStoreConfig;
|
|
124
|
-
handler: Listener<any
|
|
75
|
+
handler: Listener<any>;
|
|
125
76
|
};
|
|
126
|
-
/**
|
|
127
|
-
* Creates an IIFE script string that reads persisted themes from `localStorage` and runs your handlers immediately.
|
|
128
|
-
*
|
|
129
|
-
* Useful for avoiding flash of incorrect styles.
|
|
130
|
-
* @example
|
|
131
|
-
* ```tsx
|
|
132
|
-
* import { createInlineThemeScript } from 'resonare'
|
|
133
|
-
*
|
|
134
|
-
* const inlineScript = createInlineThemeScript([
|
|
135
|
-
* {
|
|
136
|
-
* key: 'my-app',
|
|
137
|
-
* config: {
|
|
138
|
-
* colorMode: { options: ['light', 'dark'] },
|
|
139
|
-
* },
|
|
140
|
-
* handler: ({ resolvedThemes }) => {
|
|
141
|
-
* document.documentElement.dataset.colorMode = String(
|
|
142
|
-
* resolvedThemes.colorMode,
|
|
143
|
-
* )
|
|
144
|
-
* },
|
|
145
|
-
* },
|
|
146
|
-
* ])
|
|
147
|
-
* ```
|
|
148
|
-
*/
|
|
149
77
|
declare function createInlineThemeScript(themeScriptParameters: ThemeScriptParameter | Array<ThemeScriptParameter>): string;
|
|
150
78
|
//#endregion
|
|
151
79
|
export { StorageAdapter, StorageAdapterCreate, StorageAdapterCreator, ThemeScriptParameter, type ThemeStore, ThemeStoreConfig, Themes, createInlineThemeScript, createThemeStore, getDefaultThemes, getThemesAndOptions, localStorageAdapter, memoryStorageAdapter };
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;KASK,UAAA;AAAA,KAEA,WAAA,WAAsB,UAAA;EAC1B,KAAA,EAAO,CAAA;EACP,KAAA,YAAiB,CAAA,EAAG,CAAA;AAAA;AAAA,KAGhB,WAAA,WAAsB,UAAA;EAExB,OAAA,EAAS,aAAA,CAAc,CAAA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GAAS,iBAAA,CAAkB,CAAA,IACzB,UAAA,CAAW,CAAA;AAAA,KA0DF,oBAAA;EAnYL,sDAqYN,GAAA;EACA,MAAA,EAAQ,gBAAA;EACR,OAAA,EAAS,QAAQ;AAAA;AAAA,iBAGF,uBAAA,CACf,qBAAA,EAAuB,oBAAA,GAAuB,KAAA,CAAM,oBAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import{n as e,r as t,t as n}from"./storage-B5xjLRee.js";function r(e){return Object.entries(e).map(([e,t])=>[e,(t.options||[]).map(e=>typeof e==`object`?e.value:e)])}function i(e){return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,t.defaultValue??(typeof t.options[0]==`object`?t.options[0].value:t.options[0])]))}var a=class{#e;#t;#n={};#r;#i={};#a=new Set;#o=new AbortController;constructor(e,{persisted:r={},storage:a=n({key:t})}={}){this.#e={...i(e),...r.themes},this.#n=Object.fromEntries(Object.entries(e).flatMap(([e,t])=>{let n=(t.options||[]).find(e=>typeof e==`object`&&!!e.media);if(!n)return[];let{value:i,media:[a,o,s]}=n;return[[e,{value:i,mediaQuery:a,pair:r.systemOptions?.[e]??[o,s]}]]})),this.#r=a?.({abortController:this.#o})??null,this.#t=this.#c()}getThemes=()=>this.#e;getResolvedThemes=()=>Object.fromEntries(Object.entries(this.#e).map(([e,t])=>{let n=this.#n[e];return[e,n&&t===n.value?this.#u(e):t]}));getResolvedSystemThemes=()=>Object.fromEntries(Object.keys(this.#n).map(e=>[e,this.#u(e)]));getSnapshot=()=>this.#t;setThemes=e=>{let t=typeof e==`function`?e(this.#e):e;if(this.#l(t),this.#r){let e=JSON.stringify(this.toPersist());this.#r.set(e),this.#r.broadcast?.(e)}};updateSystemOption=(e,t)=>{this.#n[e].pair=t,this.setThemes({})};toPersist=()=>({themes:this.#e,systemOptions:Object.fromEntries(Object.entries(this.#n).map(([e,{pair:t}])=>[e,t]))});restore=()=>{let e=this.#r?.get();if(!e){this.#l();return}let t=JSON.parse(e);this.#s(t.systemOptions),this.#l(t.themes)};subscribe=e=>(this.#a.add(e),()=>this.#a.delete(e));sync=()=>{if(this.#r?.watch)return this.#r.watch(e=>{if(!e)return;let t=JSON.parse(e);this.#s(t.systemOptions),this.#l(t.themes)})};destroy=()=>{this.#a.clear(),this.#o.abort()};#s=(e={})=>{Object.entries(e).forEach(([e,t])=>{!t||!this.#n[e]||(this.#n[e].pair=t)})};#c=()=>({themes:this.#e,resolvedThemes:this.getResolvedThemes(),resolvedSystemThemes:this.getResolvedSystemThemes()});#l=e=>{this.#e={...this.#e,...e},this.#t=this.#c();for(let e of this.#a)e(this.#t)};#u=e=>{let{value:t,mediaQuery:n,pair:r}=this.#n[e];return typeof matchMedia>`u`?t:(this.#i[n]||(this.#i[n]=window.matchMedia(n),this.#i[n].addEventListener(`change`,()=>{this.#l()},{signal:this.#o.signal})),this.#i[n].matches?r[0]:r[1])}};function o(e,t={}){return new a(e,t)}const s=e=>{e.forEach(([e,t,n])=>{let r=JSON.parse(localStorage.getItem(e)||`{}`);n(t.reduce((e,[t,n,i,a,o,s])=>{let c=r.themes?.[t]??n;if(e.resolvedThemes[t]=e.themes[t]=c,a!=null){let n=r.systemOptions?.[t]??[o,s],l=matchMedia(a).matches?n[0]:n[1];e.resolvedSystemThemes[t]=l,c===i&&(e.resolvedThemes[t]=l)}return e},{themes:{},resolvedThemes:{},resolvedSystemThemes:{}}))})};function c(e){return`(${s})([${(Array.isArray(e)?e:[e]).map(({key:e=t,config:n,handler:r})=>{let i=Object.entries(n).map(([e,{options:t,defaultValue:n}])=>{let r=t?.[0],i=n??(typeof r==`object`?r.value:r),a=t?.find(e=>typeof e==`object`&&!!e.media);if(a){let{value:t,media:n}=a;return[e,i,t,...n]}return[e,i]});return`['${e}',${JSON.stringify(i)},${r}]`})}])`}export{c as createInlineThemeScript,o as createThemeStore,i as getDefaultThemes,r as getThemesAndOptions,n as localStorageAdapter,e as memoryStorageAdapter};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["name","PACKAGE_NAME","type","StorageAdapter","get","set","value","broadcast","watch","cb","StorageAdapterCreate","abortController","AbortController","StorageAdapterCreator","options","Options","localStorageAdapter","key","JSON","parse","window","getItem","setItem","stringify","controller","addEventListener","e","storageArea","newValue","signal","AbortSignal","any","abort","memoryStorageAdapter","storage","Map","channel","BroadcastChannel","postMessage","data","name","PACKAGE_NAME","type","localStorageAdapter","StorageAdapter","StorageAdapterCreate","ThemeValue","ThemeOption","value","T","media","ThemeConfig","options","ReadonlyArray","initialValue","ThemeStoreConfig","Record","Themes","K","U","ThemeKeysWithSystemOption","NonSystemOptionValues","ResolvedSystemThemes","SystemOptions","mediaQuery","pair","Listener","O","themes","resolvedThemes","resolvedSystemThemes","PersistedSystemOptionPairs","PersistedState","Partial","systemOptions","ThemeStoreOptions","initialState","storage","ThemeAndOptions","Array","getThemesAndOptions","config","Object","entries","map","themeKey","themeConfig","option","getDefaultThemes","fromEntries","ThemeStore","defaultThemes","currentThemes","mediaQueryCache","MediaQueryList","listeners","Set","abortController","AbortController","constructor","key","flatMap","systemOption","find","Required","optionWhenMatched","optionWhenNotMatched","getThemes","getResolvedThemes","optionKey","resolveSystemOption","getResolvedSystemThemes","keys","setThemes","updatedThemes","setThemesAndNotify","stateToPersist","toPersist","set","broadcast","updateSystemOption","restore","persistedState","get","applyPersistedSystemOptions","subscribe","callback","add","delete","sync","watch","destroy","clear","abort","#applyPersistedSystemOptions","forEach","#setThemesAndNotify","listener","#resolveSystemOption","window","document","createElement","matchMedia","addEventListener","signal","matches","createThemeStore","restoreScript","params","flattenedConfig","handler","persisted","JSON","parse","localStorage","getItem","reduce","acc","initial","systemOptionValue","currentValue","ThemeScriptParameter","createInlineThemeScript","themeScriptParameters","serializedArgs","isArray","firstOption","resolvedInitialValue","stringify"],"sources":["../package.json","../src/storage.ts","../src/index.ts"],"sourcesContent":["","import { name as PACKAGE_NAME } from '../package.json' with { type: 'json' }\n\n/**\n * Pluggable persistence for `createThemeStore`.\n */\nexport type StorageAdapter = {\n\tget: () => object | null\n\tset: (value: object) => void\n\t/** Optional: notify other tabs/contexts after local `set` operation. */\n\tbroadcast?: (value: object) => void\n\t/** Optional: subscribes to other tabs/contexts' updates, returns `unsubscribe` function. */\n\twatch?: (cb: (value: object) => void) => () => void\n}\n\nexport type StorageAdapterCreate = ({\n\tabortController,\n}: {\n\tabortController: AbortController\n}) => StorageAdapter\n\nexport type StorageAdapterCreator<Options> = (\n\toptions: Options,\n) => StorageAdapterCreate\n\n/**\n * Persists theme store in `localStorage` or `sessionStorage`.\n * @example\n * ```ts\n * import { createThemeStore, localStorageAdapter } from 'resonare'\n *\n * const store = createThemeStore(\n * { colorMode: { options: ['light', 'dark'] },\n * { storage: localStorageAdapter({ key: 'app', type: 'localStorage' }) },\n * )\n * ```\n */\nexport const localStorageAdapter: StorageAdapterCreator<{\n\tkey: string\n\ttype?: 'localStorage' | 'sessionStorage'\n}> = ({ key, type = 'localStorage' }) => {\n\treturn ({ abortController }) => {\n\t\treturn {\n\t\t\tget: () => {\n\t\t\t\treturn JSON.parse(window[type].getItem(key) || 'null')\n\t\t\t},\n\n\t\t\tset: (value: object) => {\n\t\t\t\twindow[type].setItem(key, JSON.stringify(value))\n\t\t\t},\n\n\t\t\twatch: (cb) => {\n\t\t\t\tconst controller = new AbortController()\n\n\t\t\t\twindow.addEventListener(\n\t\t\t\t\t'storage',\n\t\t\t\t\t(e) => {\n\t\t\t\t\t\tif (e.storageArea !== window[type]) return\n\n\t\t\t\t\t\tif (e.key !== key) return\n\n\t\t\t\t\t\tcb(JSON.parse(e.newValue!))\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tsignal: AbortSignal.any([\n\t\t\t\t\t\t\tabortController.signal,\n\t\t\t\t\t\t\tcontroller.signal,\n\t\t\t\t\t\t]),\n\t\t\t\t\t},\n\t\t\t\t)\n\n\t\t\t\treturn () => {\n\t\t\t\t\tcontroller.abort()\n\t\t\t\t}\n\t\t\t},\n\t\t}\n\t}\n}\n\n/**\n * In-memory persistence and sync via `BroadcastChannel`.\n * Useful with server-side persistence.\n * @example\n * ```ts\n * import { createThemeStore, memoryStorageAdapter } from 'resonare'\n *\n * const store = createThemeStore(\n * { colorMode: { options: ['light', 'dark'] },\n * { storage: memoryStorageAdapter({ key: 'app' }) },\n * )\n * ```\n */\nexport const memoryStorageAdapter: StorageAdapterCreator<{\n\tkey: string\n}> = ({ key }) => {\n\treturn ({ abortController }) => {\n\t\tconst storage = new Map<string, object>()\n\t\tconst channel = new BroadcastChannel(PACKAGE_NAME)\n\n\t\treturn {\n\t\t\tget: () => {\n\t\t\t\treturn storage.get(key) || null\n\t\t\t},\n\n\t\t\tset: (value: object) => {\n\t\t\t\tstorage.set(key, value)\n\t\t\t},\n\n\t\t\tbroadcast: (value: object) => {\n\t\t\t\tchannel.postMessage({ key, value })\n\t\t\t},\n\n\t\t\twatch: (cb) => {\n\t\t\t\tconst controller = new AbortController()\n\n\t\t\t\tchannel.addEventListener(\n\t\t\t\t\t'message',\n\t\t\t\t\t(e) => {\n\t\t\t\t\t\tif (e.data.key !== key) return\n\n\t\t\t\t\t\tcb(e.data.value)\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tsignal: AbortSignal.any([\n\t\t\t\t\t\t\tabortController.signal,\n\t\t\t\t\t\t\tcontroller.signal,\n\t\t\t\t\t\t]),\n\t\t\t\t\t},\n\t\t\t\t)\n\n\t\t\t\treturn () => {\n\t\t\t\t\tcontroller.abort()\n\t\t\t\t}\n\t\t\t},\n\t\t}\n\t}\n}\n","import { name as PACKAGE_NAME } from '../package.json' with { type: 'json' }\nimport {\n\tlocalStorageAdapter,\n\ttype StorageAdapter,\n\ttype StorageAdapterCreate,\n} from './storage'\n\nexport * from './storage'\n\ntype ThemeValue = string | number | boolean\n\ntype ThemeOption<T extends ThemeValue = string> = {\n\tvalue: T\n\tmedia?: [string, T, T]\n}\n\ntype ThemeConfig<T extends ThemeValue = string> =\n\t| {\n\t\t\toptions: ReadonlyArray<T | ThemeOption<T>>\n\t\t\tinitialValue?: T\n\t }\n\t| { initialValue: T; options?: never }\n\nexport type ThemeStoreConfig = Record<\n\tstring,\n\tThemeConfig<string> | ThemeConfig<number> | ThemeConfig<boolean>\n>\n\nexport type Themes<T extends ThemeStoreConfig> = {\n\t[K in keyof T]: T[K] extends { options: ReadonlyArray<infer U> }\n\t\t? U extends ThemeOption\n\t\t\t? U['value']\n\t\t\t: U\n\t\t: T[K] extends { initialValue: infer U }\n\t\t\t? U extends string\n\t\t\t\t? string\n\t\t\t\t: U extends number\n\t\t\t\t\t? number\n\t\t\t\t\t: boolean\n\t\t\t: never\n}\n\ntype ThemeKeysWithSystemOption<T extends ThemeStoreConfig> = {\n\t[K in keyof T]: T[K] extends { options: ReadonlyArray<infer U> }\n\t\t? U extends { media: ReadonlyArray<unknown> }\n\t\t\t? K\n\t\t\t: never\n\t\t: never\n}[keyof T]\n\ntype NonSystemOptionValues<\n\tT extends ThemeStoreConfig,\n\tK extends keyof T,\n> = T[K] extends { options: ReadonlyArray<infer U> }\n\t? U extends ThemeValue\n\t\t? U\n\t\t: U extends ThemeOption\n\t\t\t? U extends { media: [string, string, string] }\n\t\t\t\t? never\n\t\t\t\t: U['value']\n\t\t\t: never\n\t: never\n\ntype ResolvedSystemThemes<T extends ThemeStoreConfig> = {\n\t[K in ThemeKeysWithSystemOption<T>]: NonSystemOptionValues<T, K>\n}\n\ntype SystemOptions = Record<\n\tstring,\n\t{\n\t\tvalue: string\n\t\tmediaQuery: string\n\t\tpair: [string, string]\n\t}\n>\n\ntype Listener<T extends ThemeStoreConfig, O extends boolean = false> = (\n\tvalue: O extends true\n\t\t? {\n\t\t\t\tthemes: Themes<T>\n\t\t\t\tresolvedThemes: Themes<T>\n\t\t\t}\n\t\t: {\n\t\t\t\tthemes: Themes<T>\n\t\t\t\tresolvedThemes: Themes<T>\n\t\t\t\tresolvedSystemThemes: ResolvedSystemThemes<T>\n\t\t\t},\n) => void\n\ntype PersistedSystemOptionPairs = Record<string, [string, string]>\n\ntype PersistedState<T extends ThemeStoreConfig> = {\n\tthemes: Partial<Themes<T>>\n\tsystemOptions: PersistedSystemOptionPairs\n}\n\ntype ThemeStoreOptions<T extends ThemeStoreConfig> = {\n\tinitialState?: Partial<PersistedState<T>>\n\tstorage?: StorageAdapterCreate | null\n}\n\ntype ThemeAndOptions<T extends ThemeStoreConfig> = Array<\n\t{\n\t\t[K in keyof T]: [\n\t\t\tK,\n\t\t\tArray<\n\t\t\t\tT[K] extends { options: ReadonlyArray<infer U> }\n\t\t\t\t\t? U extends ThemeOption\n\t\t\t\t\t\t? U['value']\n\t\t\t\t\t\t: U\n\t\t\t\t\t: never\n\t\t\t>,\n\t\t]\n\t}[keyof T]\n>\n\nexport function getThemesAndOptions<T extends ThemeStoreConfig>(config: T) {\n\treturn Object.entries(config).map(([themeKey, themeConfig]) => {\n\t\treturn [\n\t\t\tthemeKey,\n\t\t\t(themeConfig.options || []).map((option) =>\n\t\t\t\ttypeof option === 'object' ? option.value : option,\n\t\t\t),\n\t\t]\n\t}) as ThemeAndOptions<T>\n}\n\nexport function getDefaultThemes<T extends ThemeStoreConfig>(config: T) {\n\treturn Object.fromEntries(\n\t\tObject.entries(config).map(([themeKey, themeConfig]) => {\n\t\t\treturn [\n\t\t\t\tthemeKey,\n\t\t\t\tthemeConfig.initialValue ??\n\t\t\t\t\t(typeof themeConfig.options[0] === 'object'\n\t\t\t\t\t\t? themeConfig.options[0].value\n\t\t\t\t\t\t: themeConfig.options[0]),\n\t\t\t]\n\t\t}),\n\t) as Themes<T>\n}\n\nclass ThemeStore<T extends ThemeStoreConfig> {\n\t#defaultThemes: Themes<T>\n\n\t#currentThemes: Themes<T>\n\n\t#systemOptions: SystemOptions = {}\n\n\t#storage: StorageAdapter | null\n\n\t#mediaQueryCache: Record<string, MediaQueryList> = {}\n\n\t#listeners: Set<Listener<T>> = new Set<Listener<T>>()\n\n\t#abortController = new AbortController()\n\n\tconstructor(\n\t\tconfig: T,\n\t\t{\n\t\t\tinitialState = {},\n\t\t\tstorage = localStorageAdapter({ key: PACKAGE_NAME }),\n\t\t}: ThemeStoreOptions<T> = {},\n\t) {\n\t\tthis.#defaultThemes = getDefaultThemes(config)\n\n\t\tthis.#currentThemes = { ...this.#defaultThemes, ...initialState.themes }\n\n\t\tthis.#systemOptions = Object.fromEntries(\n\t\t\tObject.entries(config).flatMap(([themeKey, themeConfig]) => {\n\t\t\t\tconst systemOption = (themeConfig.options || []).find(\n\t\t\t\t\t(option): option is Required<ThemeOption> =>\n\t\t\t\t\t\ttypeof option === 'object' && !!option.media,\n\t\t\t\t)\n\n\t\t\t\tif (!systemOption) return []\n\n\t\t\t\tconst {\n\t\t\t\t\tvalue,\n\t\t\t\t\tmedia: [mediaQuery, optionWhenMatched, optionWhenNotMatched],\n\t\t\t\t} = systemOption\n\n\t\t\t\treturn [\n\t\t\t\t\t[\n\t\t\t\t\t\tthemeKey,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\tmediaQuery,\n\t\t\t\t\t\t\tpair: initialState.systemOptions?.[themeKey] ?? [\n\t\t\t\t\t\t\t\toptionWhenMatched,\n\t\t\t\t\t\t\t\toptionWhenNotMatched,\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t]\n\t\t\t}),\n\t\t)\n\n\t\tthis.#storage =\n\t\t\tstorage?.({\n\t\t\t\tabortController: this.#abortController,\n\t\t\t}) ?? null\n\t}\n\n\tgetThemes = (): Themes<T> => {\n\t\treturn this.#currentThemes\n\t}\n\n\tgetResolvedThemes = (): Themes<T> => {\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries(this.#currentThemes).map(([themeKey, optionKey]) => {\n\t\t\t\tconst systemOption = this.#systemOptions[themeKey]\n\n\t\t\t\treturn [\n\t\t\t\t\tthemeKey,\n\t\t\t\t\tsystemOption && optionKey === systemOption.value\n\t\t\t\t\t\t? this.#resolveSystemOption(themeKey)\n\t\t\t\t\t\t: optionKey,\n\t\t\t\t]\n\t\t\t}),\n\t\t) as Themes<T>\n\t}\n\n\tgetResolvedSystemThemes = (): ResolvedSystemThemes<T> => {\n\t\treturn Object.fromEntries(\n\t\t\tObject.keys(this.#systemOptions).map((themeKey) => [\n\t\t\t\tthemeKey,\n\t\t\t\tthis.#resolveSystemOption(themeKey),\n\t\t\t]),\n\t\t) as ResolvedSystemThemes<T>\n\t}\n\n\tsetThemes = (\n\t\tthemes:\n\t\t\t| Partial<Themes<T>>\n\t\t\t| ((currentThemes: Themes<T>) => Partial<Themes<T>>),\n\t): void => {\n\t\tconst updatedThemes =\n\t\t\ttypeof themes === 'function' ? themes(this.#currentThemes) : themes\n\n\t\tthis.#setThemesAndNotify({ ...this.#currentThemes, ...updatedThemes })\n\n\t\tconst stateToPersist = this.toPersist()\n\n\t\tif (this.#storage) {\n\t\t\tthis.#storage.set(stateToPersist)\n\t\t\tthis.#storage.broadcast?.(stateToPersist)\n\t\t}\n\t}\n\n\tupdateSystemOption = <K extends ThemeKeysWithSystemOption<T>>(\n\t\tthemeKey: K,\n\t\tpair: [NonSystemOptionValues<T, K>, NonSystemOptionValues<T, K>],\n\t): void => {\n\t\tthis.#systemOptions[themeKey]!.pair = pair\n\n\t\tthis.setThemes({ ...this.#currentThemes })\n\t}\n\n\ttoPersist = (): PersistedState<T> => {\n\t\treturn {\n\t\t\tthemes: this.#currentThemes,\n\t\t\tsystemOptions: Object.fromEntries(\n\t\t\t\tObject.entries(this.#systemOptions).map(([themeKey, { pair }]) => [\n\t\t\t\t\tthemeKey,\n\t\t\t\t\tpair,\n\t\t\t\t]),\n\t\t\t),\n\t\t}\n\t}\n\n\trestore = (): void => {\n\t\tconst persistedState = this.#storage?.get()\n\n\t\tif (!persistedState) {\n\t\t\tthis.#setThemesAndNotify({ ...this.#defaultThemes })\n\t\t\treturn\n\t\t}\n\n\t\tthis.#applyPersistedSystemOptions(persistedState.systemOptions)\n\n\t\tthis.#setThemesAndNotify({\n\t\t\t...this.#defaultThemes,\n\t\t\t...persistedState.themes,\n\t\t})\n\t}\n\n\tsubscribe = (callback: Listener<T>): (() => void) => {\n\t\tthis.#listeners.add(callback)\n\n\t\treturn () => this.#listeners.delete(callback)\n\t}\n\n\tsync = (): (() => void) | undefined => {\n\t\tif (!this.#storage?.watch) return\n\n\t\treturn this.#storage.watch((persistedState) => {\n\t\t\tthis.#applyPersistedSystemOptions(\n\t\t\t\t(persistedState as PersistedState<T>).systemOptions,\n\t\t\t)\n\n\t\t\tthis.#setThemesAndNotify((persistedState as PersistedState<T>).themes)\n\t\t})\n\t}\n\n\t/** Clears subscribers and aborts media-query listeners tied to this store instance. */\n\tdestroy = (): void => {\n\t\tthis.#listeners.clear()\n\t\tthis.#abortController.abort()\n\t}\n\n\t#applyPersistedSystemOptions = (\n\t\tsystemOptions: PersistedSystemOptionPairs = {},\n\t): void => {\n\t\tObject.entries(systemOptions).forEach(([themeKey, pair]) => {\n\t\t\tif (!pair || !this.#systemOptions[themeKey]) return\n\n\t\t\tthis.#systemOptions[themeKey].pair = pair\n\t\t})\n\t}\n\n\t#setThemesAndNotify = (themes: Themes<T>): void => {\n\t\tthis.#currentThemes = themes\n\n\t\tfor (const listener of this.#listeners) {\n\t\t\tlistener({\n\t\t\t\tthemes: this.#currentThemes,\n\t\t\t\tresolvedThemes: this.getResolvedThemes(),\n\t\t\t\tresolvedSystemThemes: this.getResolvedSystemThemes(),\n\t\t\t})\n\t\t}\n\t}\n\n\t#resolveSystemOption = (themeKey: string): string => {\n\t\tconst { value, mediaQuery, pair } = this.#systemOptions[themeKey]!\n\n\t\tif (\n\t\t\t!(\n\t\t\t\ttypeof window !== 'undefined' &&\n\t\t\t\ttypeof window.document !== 'undefined' &&\n\t\t\t\ttypeof window.document.createElement !== 'undefined'\n\t\t\t)\n\t\t) {\n\t\t\treturn value\n\t\t}\n\n\t\tif (!this.#mediaQueryCache[mediaQuery]) {\n\t\t\tthis.#mediaQueryCache[mediaQuery] = window.matchMedia(mediaQuery)\n\n\t\t\tthis.#mediaQueryCache[mediaQuery].addEventListener(\n\t\t\t\t'change',\n\t\t\t\t() => {\n\t\t\t\t\tthis.#setThemesAndNotify({ ...this.#currentThemes })\n\t\t\t\t},\n\t\t\t\t{ signal: this.#abortController.signal },\n\t\t\t)\n\t\t}\n\n\t\treturn this.#mediaQueryCache[mediaQuery].matches ? pair[0] : pair[1]\n\t}\n}\n\nexport type { ThemeStore }\n\nexport function createThemeStore<T extends ThemeStoreConfig>(\n\tconfig: T,\n\toptions: ThemeStoreOptions<T> = {},\n): ThemeStore<T> {\n\treturn new ThemeStore<T>(config, options)\n}\n\nconst restoreScript = <T extends ThemeValue>(\n\tparams: Array<\n\t\t[\n\t\t\tstring,\n\t\t\tArray<[string, T] | [string, T, T, string, T, T]>,\n\t\t\tListener<any, true>,\n\t\t]\n\t>,\n) => {\n\tparams.forEach(([key, flattenedConfig, handler]) => {\n\t\tconst persisted = JSON.parse(localStorage.getItem(key) || '{}')\n\n\t\thandler(\n\t\t\tflattenedConfig.reduce<{\n\t\t\t\tthemes: Record<string, ThemeValue>\n\t\t\t\tresolvedThemes: Record<string, ThemeValue>\n\t\t\t}>(\n\t\t\t\t(\n\t\t\t\t\tacc,\n\t\t\t\t\t[\n\t\t\t\t\t\tthemeKey,\n\t\t\t\t\t\tinitial,\n\t\t\t\t\t\tsystemOptionValue,\n\t\t\t\t\t\tmediaQuery,\n\t\t\t\t\t\toptionWhenMatched,\n\t\t\t\t\t\toptionWhenNotMatched,\n\t\t\t\t\t],\n\t\t\t\t) => {\n\t\t\t\t\tconst currentValue = persisted.themes?.[themeKey] ?? initial\n\n\t\t\t\t\tacc.resolvedThemes[themeKey] = acc.themes[themeKey] = currentValue\n\n\t\t\t\t\tif (currentValue === systemOptionValue) {\n\t\t\t\t\t\tconst pair = persisted.systemOptions?.[themeKey] ?? [\n\t\t\t\t\t\t\toptionWhenMatched,\n\t\t\t\t\t\t\toptionWhenNotMatched,\n\t\t\t\t\t\t]\n\n\t\t\t\t\t\tacc.resolvedThemes[themeKey] = matchMedia(mediaQuery!).matches\n\t\t\t\t\t\t\t? pair[0]\n\t\t\t\t\t\t\t: pair[1]\n\t\t\t\t\t}\n\n\t\t\t\t\treturn acc\n\t\t\t\t},\n\t\t\t\t{ themes: {}, resolvedThemes: {} },\n\t\t\t),\n\t\t)\n\t})\n}\n\nexport type ThemeScriptParameter = {\n\t/** `localStorage` key; defaults to the 'resonare'. */\n\tkey?: string\n\tconfig: ThemeStoreConfig\n\thandler: Listener<any, true>\n}\n\n/**\n * Creates an IIFE script string that reads persisted themes from `localStorage` and runs your handlers immediately.\n *\n * Useful for avoiding flash of incorrect styles.\n * @example\n * ```tsx\n * import { createInlineThemeScript } from 'resonare'\n *\n * const inlineScript = createInlineThemeScript([\n * {\n * key: 'my-app',\n * config: {\n * colorMode: { options: ['light', 'dark'] },\n * },\n * handler: ({ resolvedThemes }) => {\n * document.documentElement.dataset.colorMode = String(\n * resolvedThemes.colorMode,\n * )\n * },\n * },\n * ])\n * ```\n */\nexport function createInlineThemeScript(\n\tthemeScriptParameters: ThemeScriptParameter | Array<ThemeScriptParameter>,\n) {\n\tconst serializedArgs = (\n\t\tArray.isArray(themeScriptParameters)\n\t\t\t? themeScriptParameters\n\t\t\t: [themeScriptParameters]\n\t).map(({ key = PACKAGE_NAME, config, handler }) => {\n\t\tconst flattenedConfig = Object.entries(config).map(\n\t\t\t([themeKey, { options, initialValue }]) => {\n\t\t\t\tconst firstOption = options?.[0]\n\n\t\t\t\tconst resolvedInitialValue =\n\t\t\t\t\tinitialValue ??\n\t\t\t\t\t(typeof firstOption === 'object' ? firstOption.value : firstOption!)\n\n\t\t\t\tconst systemOption = options?.find(\n\t\t\t\t\t(option): option is Required<ThemeOption> =>\n\t\t\t\t\t\ttypeof option === 'object' && !!option.media,\n\t\t\t\t)\n\n\t\t\t\tif (systemOption) {\n\t\t\t\t\tconst { value, media } = systemOption\n\n\t\t\t\t\treturn [themeKey, resolvedInitialValue, value, ...media]\n\t\t\t\t}\n\n\t\t\t\treturn [themeKey, resolvedInitialValue]\n\t\t\t},\n\t\t)\n\n\t\treturn `['${key}',${JSON.stringify(flattenedConfig)},${handler}]`\n\t})\n\n\treturn `(${restoreScript})([${serializedArgs}])`\n}\n"],"mappings":"iBCoCA,MAAagB,GAGP,CAAEC,MAAKf,OAAO,mBACX,CAAES,sBACF,CACNP,QACQc,KAAKC,MAAMC,OAAOlB,EAAK,CAACmB,QAAQJ,CAAG,GAAK,MAAM,EAGtDZ,IAAMC,GAAkB,CACvBc,OAAOlB,EAAK,CAACoB,QAAQL,EAAKC,KAAKK,UAAUjB,CAAK,CAAC,CAChD,EAEAE,MAAQC,GAAO,CACd,IAAMe,EAAa,IAAIZ,gBAmBvB,OAjBAQ,OAAOK,iBACN,UACCC,GAAM,CACFA,EAAEC,cAAgBP,OAAOlB,IAEzBwB,EAAET,MAAQA,GAEdR,EAAGS,KAAKC,MAAMO,EAAEE,QAAS,CAAC,CAC3B,EACA,CACCC,OAAQC,YAAYC,IAAI,CACvBpB,EAAgBkB,OAChBL,EAAWK,MAAM,CACjB,CACF,CACD,MAEa,CACZL,EAAWQ,MAAM,CAClB,CACD,CACD,GAiBWC,GAEP,CAAEhB,UACC,CAAEN,qBAAsB,CAC/B,IAAMuB,EAAU,IAAIC,IACdC,EAAU,IAAIC,iBAAiBpC,CAAY,EAEjD,MAAO,CACNG,QACQ8B,EAAQ9B,IAAIa,CAAG,GAAK,KAG5BZ,IAAMC,GAAkB,CACvB4B,EAAQ7B,IAAIY,EAAKX,CAAK,CACvB,EAEAC,UAAYD,GAAkB,CAC7B8B,EAAQE,YAAY,CAAErB,MAAKX,OAAM,CAAC,CACnC,EAEAE,MAAQC,GAAO,CACd,IAAMe,EAAa,IAAIZ,gBAiBvB,OAfAwB,EAAQX,iB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oB,CAC1C,IAAMgH,EAAclH,IAAU,GAExBmH,EACLjH,IACC,OAAOgH,GAAgB,SAAWA,EAAYtH,MAAQsH,GAElDhE,EAAelD,GAASmD,KAC5BhB,GACA,OAAOA,GAAW,UAAY,CAAC,CAACA,EAAOrC,KACzC,EAEA,GAAIoD,EAAc,CACjB,GAAM,CAAEtD,QAAOE,SAAUoD,EAEzB,MAAO,CAACjB,EAAUkF,EAAsBvH,EAAO,GAAGE,CAAK,CACxD,CAEA,MAAO,CAACmC,EAAUkF,CAAoB,CACvC,CACD,EAEA,MAAO,KAAKnE,EAAG,IAAKoD,KAAKgB,UAAUnB,CAAe,EAAC,GAAIC,EAAO,EAC/D,CAE8Bc,EAAc,GAC7C"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["DEFAULT_KEY","localStorageAdapter","StorageAdapter","StorageAdapterCreate","ThemeValue","ThemeOption","value","T","media","ThemeConfig","options","ReadonlyArray","defaultValue","ThemeStoreConfig","Record","Themes","K","U","ThemeKeysWithSystemOption","NonSystemOptionValues","ResolvedSystemThemes","SystemOptions","mediaQuery","pair","Snapshot","themes","resolvedThemes","resolvedSystemThemes","Listener","PersistedSystemOptionPairs","PersistedState","Partial","systemOptions","ThemeStoreOptions","persisted","storage","ThemeAndOptions","Array","getThemesAndOptions","config","Object","entries","map","themeKey","themeConfig","option","getDefaultThemes","fromEntries","ThemeStore","currentThemes","snapshot","mediaQueryCache","MediaQueryList","listeners","Set","abortController","AbortController","constructor","key","flatMap","systemOption","find","Required","optionWhenMatched","optionWhenNotMatched","createSnapshot","getThemes","getResolvedThemes","optionKey","resolveSystemOption","getResolvedSystemThemes","keys","getSnapshot","setThemes","updatedThemes","setThemesAndNotify","stored","JSON","stringify","toPersist","set","broadcast","updateSystemOption","restore","get","parse","applyPersistedSystemOptions","subscribe","callback","add","delete","sync","watch","destroy","clear","abort","#applyPersistedSystemOptions","forEach","#createSnapshot","#setThemesAndNotify","listener","#resolveSystemOption","matchMedia","window","addEventListener","signal","matches","createThemeStore","restoreScript","params","flattenedConfig","handler","localStorage","getItem","reduce","acc","initial","systemOptionValue","currentValue","systemTheme","ThemeScriptParameter","createInlineThemeScript","themeScriptParameters","serializedArgs","isArray","firstOption","resolvedInitialValue"],"sources":["../src/index.ts"],"sourcesContent":["import { DEFAULT_KEY } from './constants'\nimport {\n\tlocalStorageAdapter,\n\ttype StorageAdapter,\n\ttype StorageAdapterCreate,\n} from './storage'\n\nexport * from './storage'\n\ntype ThemeValue = string | number | boolean\n\ntype ThemeOption<T extends ThemeValue = string> = {\n\tvalue: T\n\tmedia?: [string, T, T]\n}\n\ntype ThemeConfig<T extends ThemeValue = string> =\n\t| {\n\t\t\toptions: ReadonlyArray<T | ThemeOption<T>>\n\t\t\tdefaultValue?: T\n\t }\n\t| { defaultValue: T; options?: never }\n\nexport type ThemeStoreConfig = Record<\n\tstring,\n\tThemeConfig<string> | ThemeConfig<number> | ThemeConfig<boolean>\n>\n\nexport type Themes<T extends ThemeStoreConfig> = {\n\t[K in keyof T]: T[K] extends { options: ReadonlyArray<infer U> }\n\t\t? U extends ThemeOption\n\t\t\t? U['value']\n\t\t\t: U\n\t\t: T[K] extends { defaultValue: infer U }\n\t\t\t? U extends string\n\t\t\t\t? string\n\t\t\t\t: U extends number\n\t\t\t\t\t? number\n\t\t\t\t\t: boolean\n\t\t\t: never\n}\n\ntype ThemeKeysWithSystemOption<T extends ThemeStoreConfig> = {\n\t[K in keyof T]: T[K] extends { options: ReadonlyArray<infer U> }\n\t\t? U extends { media: ReadonlyArray<unknown> }\n\t\t\t? K\n\t\t\t: never\n\t\t: never\n}[keyof T]\n\ntype NonSystemOptionValues<\n\tT extends ThemeStoreConfig,\n\tK extends keyof T,\n> = T[K] extends { options: ReadonlyArray<infer U> }\n\t? U extends ThemeValue\n\t\t? U\n\t\t: U extends ThemeOption\n\t\t\t? U extends { media: [string, string, string] }\n\t\t\t\t? never\n\t\t\t\t: U['value']\n\t\t\t: never\n\t: never\n\ntype ResolvedSystemThemes<T extends ThemeStoreConfig> = {\n\t[K in ThemeKeysWithSystemOption<T>]: NonSystemOptionValues<T, K>\n}\n\ntype SystemOptions = Record<\n\tstring,\n\t{\n\t\tvalue: string\n\t\tmediaQuery: string\n\t\tpair: [string, string]\n\t}\n>\n\ntype Snapshot<T extends ThemeStoreConfig> = {\n\tthemes: Themes<T>\n\tresolvedThemes: Themes<T>\n\tresolvedSystemThemes: ResolvedSystemThemes<T>\n}\n\ntype Listener<T extends ThemeStoreConfig> = (value: Snapshot<T>) => void\n\ntype PersistedSystemOptionPairs = Record<string, [string, string]>\n\ntype PersistedState<T extends ThemeStoreConfig> = {\n\tthemes: Partial<Themes<T>>\n\tsystemOptions: PersistedSystemOptionPairs\n}\n\ntype ThemeStoreOptions<T extends ThemeStoreConfig> = {\n\tpersisted?: Partial<PersistedState<T>>\n\tstorage?: StorageAdapterCreate | null\n}\n\ntype ThemeAndOptions<T extends ThemeStoreConfig> = Array<\n\t{\n\t\t[K in keyof T]: [\n\t\t\tK,\n\t\t\tArray<\n\t\t\t\tT[K] extends { options: ReadonlyArray<infer U> }\n\t\t\t\t\t? U extends ThemeOption\n\t\t\t\t\t\t? U['value']\n\t\t\t\t\t\t: U\n\t\t\t\t\t: never\n\t\t\t>,\n\t\t]\n\t}[keyof T]\n>\n\nexport function getThemesAndOptions<T extends ThemeStoreConfig>(config: T) {\n\treturn Object.entries(config).map(([themeKey, themeConfig]) => {\n\t\treturn [\n\t\t\tthemeKey,\n\t\t\t(themeConfig.options || []).map((option) =>\n\t\t\t\ttypeof option === 'object' ? option.value : option,\n\t\t\t),\n\t\t]\n\t}) as ThemeAndOptions<T>\n}\n\nexport function getDefaultThemes<T extends ThemeStoreConfig>(config: T) {\n\treturn Object.fromEntries(\n\t\tObject.entries(config).map(([themeKey, themeConfig]) => {\n\t\t\treturn [\n\t\t\t\tthemeKey,\n\t\t\t\tthemeConfig.defaultValue ??\n\t\t\t\t\t(typeof themeConfig.options[0] === 'object'\n\t\t\t\t\t\t? themeConfig.options[0].value\n\t\t\t\t\t\t: themeConfig.options[0]),\n\t\t\t]\n\t\t}),\n\t) as Themes<T>\n}\n\nclass ThemeStore<T extends ThemeStoreConfig> {\n\t#currentThemes: Themes<T>\n\n\t#snapshot: Snapshot<T>\n\n\t#systemOptions: SystemOptions = {}\n\n\t#storage: StorageAdapter | null\n\n\t#mediaQueryCache: Record<string, MediaQueryList> = {}\n\n\t#listeners: Set<Listener<T>> = new Set<Listener<T>>()\n\n\t#abortController = new AbortController()\n\n\tconstructor(\n\t\tconfig: T,\n\t\t{\n\t\t\tpersisted = {},\n\t\t\tstorage = localStorageAdapter({ key: DEFAULT_KEY }),\n\t\t}: ThemeStoreOptions<T> = {},\n\t) {\n\t\tthis.#currentThemes = { ...getDefaultThemes(config), ...persisted.themes }\n\n\t\tthis.#systemOptions = Object.fromEntries(\n\t\t\tObject.entries(config).flatMap(([themeKey, themeConfig]) => {\n\t\t\t\tconst systemOption = (themeConfig.options || []).find(\n\t\t\t\t\t(option): option is Required<ThemeOption> =>\n\t\t\t\t\t\ttypeof option === 'object' && !!option.media,\n\t\t\t\t)\n\n\t\t\t\tif (!systemOption) return []\n\n\t\t\t\tconst {\n\t\t\t\t\tvalue,\n\t\t\t\t\tmedia: [mediaQuery, optionWhenMatched, optionWhenNotMatched],\n\t\t\t\t} = systemOption\n\n\t\t\t\treturn [\n\t\t\t\t\t[\n\t\t\t\t\t\tthemeKey,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\tmediaQuery,\n\t\t\t\t\t\t\tpair: persisted.systemOptions?.[themeKey] ?? [\n\t\t\t\t\t\t\t\toptionWhenMatched,\n\t\t\t\t\t\t\t\toptionWhenNotMatched,\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t]\n\t\t\t}),\n\t\t)\n\n\t\tthis.#storage =\n\t\t\tstorage?.({\n\t\t\t\tabortController: this.#abortController,\n\t\t\t}) ?? null\n\n\t\tthis.#snapshot = this.#createSnapshot()\n\t}\n\n\tgetThemes = (): Themes<T> => {\n\t\treturn this.#currentThemes\n\t}\n\n\tgetResolvedThemes = (): Themes<T> => {\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries(this.#currentThemes).map(([themeKey, optionKey]) => {\n\t\t\t\tconst systemOption = this.#systemOptions[themeKey]\n\n\t\t\t\treturn [\n\t\t\t\t\tthemeKey,\n\t\t\t\t\tsystemOption && optionKey === systemOption.value\n\t\t\t\t\t\t? this.#resolveSystemOption(themeKey)\n\t\t\t\t\t\t: optionKey,\n\t\t\t\t]\n\t\t\t}),\n\t\t) as Themes<T>\n\t}\n\n\tgetResolvedSystemThemes = (): ResolvedSystemThemes<T> => {\n\t\treturn Object.fromEntries(\n\t\t\tObject.keys(this.#systemOptions).map((themeKey) => [\n\t\t\t\tthemeKey,\n\t\t\t\tthis.#resolveSystemOption(themeKey),\n\t\t\t]),\n\t\t) as ResolvedSystemThemes<T>\n\t}\n\n\tgetSnapshot = (): Snapshot<T> => {\n\t\treturn this.#snapshot\n\t}\n\n\tsetThemes = (\n\t\tthemes:\n\t\t\t| Partial<Themes<T>>\n\t\t\t| ((currentThemes: Themes<T>) => Partial<Themes<T>>),\n\t): void => {\n\t\tconst updatedThemes =\n\t\t\ttypeof themes === 'function' ? themes(this.#currentThemes) : themes\n\n\t\tthis.#setThemesAndNotify(updatedThemes)\n\n\t\tif (this.#storage) {\n\t\t\tconst stored = JSON.stringify(this.toPersist())\n\n\t\t\tthis.#storage.set(stored)\n\n\t\t\tthis.#storage.broadcast?.(stored)\n\t\t}\n\t}\n\n\tupdateSystemOption = <K extends ThemeKeysWithSystemOption<T>>(\n\t\tthemeKey: K,\n\t\tpair: [NonSystemOptionValues<T, K>, NonSystemOptionValues<T, K>],\n\t): void => {\n\t\tthis.#systemOptions[themeKey]!.pair = pair\n\n\t\tthis.setThemes({})\n\t}\n\n\ttoPersist = (): PersistedState<T> => {\n\t\treturn {\n\t\t\tthemes: this.#currentThemes,\n\t\t\tsystemOptions: Object.fromEntries(\n\t\t\t\tObject.entries(this.#systemOptions).map(([themeKey, { pair }]) => [\n\t\t\t\t\tthemeKey,\n\t\t\t\t\tpair,\n\t\t\t\t]),\n\t\t\t),\n\t\t}\n\t}\n\n\trestore = (): void => {\n\t\tconst stored = this.#storage?.get()\n\n\t\tif (!stored) {\n\t\t\tthis.#setThemesAndNotify()\n\t\t\treturn\n\t\t}\n\n\t\tconst persisted = JSON.parse(stored) as PersistedState<T>\n\n\t\tthis.#applyPersistedSystemOptions(persisted.systemOptions)\n\n\t\tthis.#setThemesAndNotify(persisted.themes)\n\t}\n\n\tsubscribe = (callback: Listener<T>): (() => void) => {\n\t\tthis.#listeners.add(callback)\n\n\t\treturn () => this.#listeners.delete(callback)\n\t}\n\n\tsync = (): (() => void) | undefined => {\n\t\tif (!this.#storage?.watch) return\n\n\t\treturn this.#storage.watch((stored) => {\n\t\t\tif (!stored) return\n\n\t\t\tconst persisted = JSON.parse(stored) as PersistedState<T>\n\n\t\t\tthis.#applyPersistedSystemOptions(persisted.systemOptions)\n\n\t\t\tthis.#setThemesAndNotify(persisted.themes)\n\t\t})\n\t}\n\n\tdestroy = (): void => {\n\t\tthis.#listeners.clear()\n\t\tthis.#abortController.abort()\n\t}\n\n\t#applyPersistedSystemOptions = (\n\t\tsystemOptions: PersistedSystemOptionPairs = {},\n\t): void => {\n\t\tObject.entries(systemOptions).forEach(([themeKey, pair]) => {\n\t\t\tif (!pair || !this.#systemOptions[themeKey]) return\n\n\t\t\tthis.#systemOptions[themeKey].pair = pair\n\t\t})\n\t}\n\n\t#createSnapshot = (): Snapshot<T> => ({\n\t\tthemes: this.#currentThemes,\n\t\tresolvedThemes: this.getResolvedThemes(),\n\t\tresolvedSystemThemes: this.getResolvedSystemThemes(),\n\t})\n\n\t#setThemesAndNotify = (themes?: Partial<Themes<T>>): void => {\n\t\tthis.#currentThemes = { ...this.#currentThemes, ...themes }\n\n\t\tthis.#snapshot = this.#createSnapshot()\n\n\t\tfor (const listener of this.#listeners) {\n\t\t\tlistener(this.#snapshot)\n\t\t}\n\t}\n\n\t#resolveSystemOption = (themeKey: string): string => {\n\t\tconst { value, mediaQuery, pair } = this.#systemOptions[themeKey]!\n\n\t\tif (typeof matchMedia === 'undefined') return value\n\n\t\tif (!this.#mediaQueryCache[mediaQuery]) {\n\t\t\tthis.#mediaQueryCache[mediaQuery] = window.matchMedia(mediaQuery)\n\n\t\t\tthis.#mediaQueryCache[mediaQuery].addEventListener(\n\t\t\t\t'change',\n\t\t\t\t() => {\n\t\t\t\t\tthis.#setThemesAndNotify()\n\t\t\t\t},\n\t\t\t\t{ signal: this.#abortController.signal },\n\t\t\t)\n\t\t}\n\n\t\treturn this.#mediaQueryCache[mediaQuery].matches ? pair[0] : pair[1]\n\t}\n}\n\nexport type { ThemeStore }\n\nexport function createThemeStore<T extends ThemeStoreConfig>(\n\tconfig: T,\n\toptions: ThemeStoreOptions<T> = {},\n): ThemeStore<T> {\n\treturn new ThemeStore<T>(config, options)\n}\n\nconst restoreScript = <T extends ThemeValue>(\n\tparams: Array<\n\t\t[string, Array<[string, T] | [string, T, T, string, T, T]>, Listener<any>]\n\t>,\n) => {\n\tparams.forEach(([key, flattenedConfig, handler]) => {\n\t\tconst persisted = JSON.parse(localStorage.getItem(key) || '{}')\n\n\t\thandler(\n\t\t\tflattenedConfig.reduce<{\n\t\t\t\tthemes: Record<string, ThemeValue>\n\t\t\t\tresolvedThemes: Record<string, ThemeValue>\n\t\t\t\tresolvedSystemThemes: Record<string, ThemeValue>\n\t\t\t}>(\n\t\t\t\t(\n\t\t\t\t\tacc,\n\t\t\t\t\t[\n\t\t\t\t\t\tthemeKey,\n\t\t\t\t\t\tinitial,\n\t\t\t\t\t\tsystemOptionValue,\n\t\t\t\t\t\tmediaQuery,\n\t\t\t\t\t\toptionWhenMatched,\n\t\t\t\t\t\toptionWhenNotMatched,\n\t\t\t\t\t],\n\t\t\t\t) => {\n\t\t\t\t\tconst currentValue = persisted.themes?.[themeKey] ?? initial\n\n\t\t\t\t\tacc.resolvedThemes[themeKey] = acc.themes[themeKey] = currentValue\n\n\t\t\t\t\tif (mediaQuery != null) {\n\t\t\t\t\t\tconst pair = persisted.systemOptions?.[themeKey] ?? [\n\t\t\t\t\t\t\toptionWhenMatched!,\n\t\t\t\t\t\t\toptionWhenNotMatched!,\n\t\t\t\t\t\t]\n\n\t\t\t\t\t\tconst systemTheme = matchMedia(mediaQuery).matches\n\t\t\t\t\t\t\t? pair[0]\n\t\t\t\t\t\t\t: pair[1]\n\n\t\t\t\t\t\tacc.resolvedSystemThemes[themeKey] = systemTheme\n\n\t\t\t\t\t\tif (currentValue === systemOptionValue) {\n\t\t\t\t\t\t\tacc.resolvedThemes[themeKey] = systemTheme\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn acc\n\t\t\t\t},\n\t\t\t\t{ themes: {}, resolvedThemes: {}, resolvedSystemThemes: {} },\n\t\t\t),\n\t\t)\n\t})\n}\n\nexport type ThemeScriptParameter = {\n\t/** `localStorage` key; defaults to the 'resonare'. */\n\tkey?: string\n\tconfig: ThemeStoreConfig\n\thandler: Listener<any>\n}\n\nexport function createInlineThemeScript(\n\tthemeScriptParameters: ThemeScriptParameter | Array<ThemeScriptParameter>,\n) {\n\tconst serializedArgs = (\n\t\tArray.isArray(themeScriptParameters)\n\t\t\t? themeScriptParameters\n\t\t\t: [themeScriptParameters]\n\t).map(({ key = DEFAULT_KEY, config, handler }) => {\n\t\tconst flattenedConfig = Object.entries(config).map(\n\t\t\t([themeKey, { options, defaultValue }]) => {\n\t\t\t\tconst firstOption = options?.[0]\n\n\t\t\t\tconst resolvedInitialValue =\n\t\t\t\t\tdefaultValue ??\n\t\t\t\t\t(typeof firstOption === 'object' ? firstOption.value : firstOption!)\n\n\t\t\t\tconst systemOption = options?.find(\n\t\t\t\t\t(option): option is Required<ThemeOption> =>\n\t\t\t\t\t\ttypeof option === 'object' && !!option.media,\n\t\t\t\t)\n\n\t\t\t\tif (systemOption) {\n\t\t\t\t\tconst { value, media } = systemOption\n\n\t\t\t\t\treturn [themeKey, resolvedInitialValue, value, ...media]\n\t\t\t\t}\n\n\t\t\t\treturn [themeKey, resolvedInitialValue]\n\t\t\t},\n\t\t)\n\n\t\treturn `['${key}',${JSON.stringify(flattenedConfig)},${handler}]`\n\t})\n\n\treturn `(${restoreScript})([${serializedArgs}])`\n}\n"],"mappings":"wDA+GA,SAAgBsC,EAAgDC,EAAW,CAC1E,OAAOC,OAAOC,QAAQF,CAAM,CAAC,CAACG,KAAK,CAACC,EAAUC,KACtC,CACND,GACCC,EAAYlC,SAAW,CAAA,EAAA,CAAIgC,IAAKG,GAChC,OAAOA,GAAW,SAAWA,EAAOvC,MAAQuC,CAC7C,CAAC,CAEF,CACF,CAEA,SAAgBC,EAA6CP,EAAW,CACvE,OAAOC,OAAOO,YACbP,OAAOC,QAAQF,CAAM,CAAC,CAACG,KAAK,CAACC,EAAUC,KAC/B,CACND,EACAC,EAAYhC,eACV,OAAOgC,EAAYlC,QAAQ,IAAO,SAChCkC,EAAYlC,QAAQ,EAAE,CAACJ,MACvBsC,EAAYlC,QAAQ,GAAG,CAE5B,CACF,CACD,CAEA,IAAMsC,EAAN,KAA6C,CAC5C,GAEA,GAEA,GAAgC,CAAC,EAEjC,GAEA,GAAmD,CAAC,EAEpD,GAA+B,IAAIM,IAEnC,GAAmB,IAAIE,gBAEvBC,YACClB,EACA,CACCL,YAAY,CAAC,EACbC,UAAUlC,EAAoB,CAAEyD,IAAK1D,CAAY,CAAC,GACzB,CAAC,EAC1B,CACD,KAAK,GAAiB,CAAE,GAAG8C,EAAiBP,CAAM,EAAG,GAAGL,EAAUT,MAAO,EAEzE,KAAK,GAAiBe,OAAOO,YAC5BP,OAAOC,QAAQF,CAAM,CAAC,CAACoB,SAAS,CAAChB,EAAUC,KAAiB,CAC3D,IAAMgB,GAAgBhB,EAAYlC,SAAW,CAAA,EAAA,CAAImD,KA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EAAa,MA9BvBvE,MAAMuF,QAAQF,CAAqB,EAChCA,EACA,CAACA,CAAqB,EAAA,CACxBhF,KAAK,CAAEgB,MAAM1D,EAAauC,SAAQwE,aAAc,CACjD,IAAMD,EAAkBtE,OAAOC,QAAQF,CAAM,CAAC,CAACG,KAC7C,CAACC,EAAU,CAAEjC,UAASE,mBAAoB,CAC1C,IAAMiH,EAAcnH,IAAU,GAExBoH,EACLlH,IACC,OAAOiH,GAAgB,SAAWA,EAAYvH,MAAQuH,GAElDjE,EAAelD,GAASmD,KAC5BhB,GACA,OAAOA,GAAW,UAAY,CAAC,CAACA,EAAOrC,KACzC,EAEA,GAAIoD,EAAc,CACjB,GAAM,CAAEtD,QAAOE,SAAUoD,EAEzB,MAAO,CAACjB,EAAUmF,EAAsBxH,EAAO,GAAGE,CAAK,CACxD,CAEA,MAAO,CAACmC,EAAUmF,CAAoB,CACvC,CACD,EAEA,MAAO,KAAKpE,EAAG,IAAKmB,KAAKC,UAAUgC,CAAe,EAAC,GAAIC,EAAO,EAC/D,CAE8BY,EAAc,GAC7C"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ThemeStore } from "./lite.js";
|
|
2
|
+
|
|
3
|
+
//#region src/lite-react.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Subscribes a React component to a lite (light/dark/system) theme store.
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* import { createThemeStore } from 'resonare/lite'
|
|
9
|
+
* import { useResonare } from 'resonare/lite/react'
|
|
10
|
+
*
|
|
11
|
+
* const store = createThemeStore()
|
|
12
|
+
*
|
|
13
|
+
* export function ThemeToggle() {
|
|
14
|
+
* const { theme, resolvedTheme, systemTheme, setTheme } = useResonare(store)
|
|
15
|
+
*
|
|
16
|
+
* // ...
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
declare function useResonare(store: ThemeStore): {
|
|
21
|
+
destroy: () => void;
|
|
22
|
+
restore: () => void;
|
|
23
|
+
setTheme: (theme: ("system" | "light" | "dark") | ((current: "system" | "light" | "dark") => "system" | "light" | "dark")) => void;
|
|
24
|
+
subscribe: (callback: (value: {
|
|
25
|
+
theme: "system" | "light" | "dark";
|
|
26
|
+
resolvedTheme: "light" | "dark";
|
|
27
|
+
systemTheme: "light" | "dark";
|
|
28
|
+
}) => void) => (() => void);
|
|
29
|
+
sync: () => (() => void) | undefined;
|
|
30
|
+
theme: "system" | "light" | "dark";
|
|
31
|
+
resolvedTheme: "light" | "dark";
|
|
32
|
+
systemTheme: "light" | "dark";
|
|
33
|
+
};
|
|
34
|
+
//#endregion
|
|
35
|
+
export { useResonare };
|
|
36
|
+
//# sourceMappingURL=lite-react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lite-react.d.ts","names":[],"sources":["../src/lite-react.ts"],"mappings":";;;;;AAmBA;;;;;;;;;;;;;;iBAAgB,WAAA,CAAY,KAAA,EAAO,UAAU"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{c as e}from"react-compiler-runtime";import*as t from"react";function n(n){let r=e(7),{destroy:i,getSnapshot:a,restore:o,setTheme:s,subscribe:c,sync:l}=n,u=t.useSyncExternalStore(c,a,a),d;return r[0]!==i||r[1]!==o||r[2]!==s||r[3]!==u||r[4]!==c||r[5]!==l?(d={...u,destroy:i,restore:o,setTheme:s,subscribe:c,sync:l},r[0]=i,r[1]=o,r[2]=s,r[3]=u,r[4]=c,r[5]=l,r[6]=d):d=r[6],d}export{n as useResonare};
|
|
2
|
+
//# sourceMappingURL=lite-react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lite-react.js","names":["React","ThemeStore","useResonare","store","$","_c","destroy","getSnapshot","restore","setTheme","subscribe","sync","snapshot","useSyncExternalStore","t0"],"sources":["../src/lite-react.ts"],"sourcesContent":["import * as React from 'react'\nimport type { ThemeStore } from './lite'\n\n/**\n * Subscribes a React component to a lite (light/dark/system) theme store.\n * @example\n * ```tsx\n * import { createThemeStore } from 'resonare/lite'\n * import { useResonare } from 'resonare/lite/react'\n *\n * const store = createThemeStore()\n *\n * export function ThemeToggle() {\n * const { theme, resolvedTheme, systemTheme, setTheme } = useResonare(store)\n *\n * // ...\n * }\n * ```\n */\nexport function useResonare(store: ThemeStore) {\n\tconst { destroy, getSnapshot, restore, setTheme, subscribe, sync } = store\n\n\tconst snapshot = React.useSyncExternalStore(\n\t\tsubscribe,\n\t\tgetSnapshot,\n\t\tgetSnapshot,\n\t)\n\n\treturn { ...snapshot, destroy, restore, setTheme, subscribe, sync }\n}\n"],"mappings":"mEAmBA,SAAOE,EAAAC,EAAA,CAAA,IAAAC,EAAAC,EAAA,CAAA,EACN,CAAAC,UAAAC,cAAAC,UAAAC,WAAAC,YAAAC,QAAqER,EAErES,EAAiBZ,EAAKa,qBACrBH,EACAH,EACAA,CACD,EAACO,EAEkE,OAFlEV,EAAA,KAAAE,GAAAF,EAAA,KAAAI,GAAAJ,EAAA,KAAAK,GAAAL,EAAA,KAAAQ,GAAAR,EAAA,KAAAM,GAAAN,EAAA,KAAAO,GAEMG,EAAA,CAAA,GAAKF,EAAQN,UAAAE,UAAAC,WAAAC,YAAAC,MAA8C,EAACP,EAAA,GAAAE,EAAAF,EAAA,GAAAI,EAAAJ,EAAA,GAAAK,EAAAL,EAAA,GAAAQ,EAAAR,EAAA,GAAAM,EAAAN,EAAA,GAAAO,EAAAP,EAAA,GAAAU,GAAAA,EAAAV,EAAA,GAA5DU,CAA4D"}
|
package/dist/lite.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { a as memoryStorageAdapter, i as localStorageAdapter, n as StorageAdapterCreate, r as StorageAdapterCreator, t as StorageAdapter } from "./storage-DGYV332J.js";
|
|
2
|
+
|
|
3
|
+
//#region src/lite.d.ts
|
|
4
|
+
type Theme = 'system' | 'light' | 'dark';
|
|
5
|
+
type ResolvedTheme = 'light' | 'dark';
|
|
6
|
+
type Snapshot = {
|
|
7
|
+
theme: Theme;
|
|
8
|
+
resolvedTheme: ResolvedTheme;
|
|
9
|
+
systemTheme: ResolvedTheme;
|
|
10
|
+
};
|
|
11
|
+
type Listener = (value: Snapshot) => void;
|
|
12
|
+
type ThemeStoreOptions = {
|
|
13
|
+
persisted?: Theme;
|
|
14
|
+
storage?: StorageAdapterCreate | null;
|
|
15
|
+
};
|
|
16
|
+
declare class ThemeStore {
|
|
17
|
+
#private;
|
|
18
|
+
constructor({
|
|
19
|
+
persisted,
|
|
20
|
+
storage
|
|
21
|
+
}?: ThemeStoreOptions);
|
|
22
|
+
getTheme: () => Theme;
|
|
23
|
+
getResolvedTheme: () => ResolvedTheme;
|
|
24
|
+
getSystemTheme: () => ResolvedTheme;
|
|
25
|
+
getSnapshot: () => Snapshot;
|
|
26
|
+
setTheme: (theme: Theme | ((current: Theme) => Theme)) => void;
|
|
27
|
+
restore: () => void;
|
|
28
|
+
subscribe: (callback: Listener) => (() => void);
|
|
29
|
+
sync: () => (() => void) | undefined;
|
|
30
|
+
destroy: () => void;
|
|
31
|
+
}
|
|
32
|
+
declare function createThemeStore(options?: ThemeStoreOptions): ThemeStore;
|
|
33
|
+
type ThemeScriptParameter = {
|
|
34
|
+
key?: string;
|
|
35
|
+
handler: Listener;
|
|
36
|
+
};
|
|
37
|
+
declare function createInlineThemeScript({
|
|
38
|
+
key,
|
|
39
|
+
handler
|
|
40
|
+
}: ThemeScriptParameter): string;
|
|
41
|
+
//#endregion
|
|
42
|
+
export { StorageAdapter, StorageAdapterCreate, StorageAdapterCreator, ThemeScriptParameter, type ThemeStore, createInlineThemeScript, createThemeStore, localStorageAdapter, memoryStorageAdapter };
|
|
43
|
+
//# sourceMappingURL=lite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lite.d.ts","names":[],"sources":["../src/lite.ts"],"mappings":";;;KASK,KAAA;AAAA,KAEA,aAAA;AAAA,KAEA,QAAA;EACJ,KAAA,EAAO,KAAA;EACP,aAAA,EAAe,aAAA;EACf,WAAA,EAAa,aAAA;AAAA;AAAA,KAGT,QAAA,IAAY,KAAe,EAAR,QAAQ;AAAA,KAE3B,iBAAA;EACJ,SAAA,GAAY,KAAA;EACZ,OAAA,GAAU,oBAAoB;AAAA;AAAA,cAGzB,UAAA;EAAA;;IAcJ,SAAA;IACA;EAAA,IACE,iBAAA;EAWH,QAAA,QAAe,KAAA;EAIf,gBAAA,QAAuB,aAAA;EAIvB,cAAA,QAAqB,aAAA;EAIrB,WAAA,QAAkB,QAAA;EAIlB,QAAA,GAAY,KAAA,EAAO,KAAA,KAAU,OAAA,EAAS,KAAA,KAAU,KAAA;EAWhD,OAAA;EAIA,SAAA,GAAa,QAAA,EAAU,QAAA;EAQvB,IAAA;EAQA,OAAA;AAAA;AAAA,iBAyCe,gBAAA,CAAiB,OAAA,GAAS,iBAAA,GAAyB,UAAU;AAAA,KAcjE,oBAAA;EACX,GAAA;EACA,OAAA,EAAS,QAAQ;AAAA;AAAA,iBAGF,uBAAA;EACf,GAAA;EACA;AAAA,GACE,oBAAA"}
|
package/dist/lite.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{i as e,n as t,t as n}from"./storage-B5xjLRee.js";var r=class{#e;#t;#n;#r;#i=new Set;#a=new AbortController;constructor({persisted:t,storage:r=n({key:e})}={}){this.#e=t??`system`,this.#n=r?.({abortController:this.#a})??null,this.#t=this.#o()}getTheme=()=>this.#e;getResolvedTheme=()=>this.#e===`system`?this.#c():this.#e;getSystemTheme=()=>this.#c();getSnapshot=()=>this.#t;setTheme=e=>{this.#s(typeof e==`function`?e(this.#e):e),this.#n&&(this.#n.set(this.#e),this.#n.broadcast?.(this.#e))};restore=()=>{this.#s(this.#n?.get()??`system`)};subscribe=e=>(this.#i.add(e),()=>{this.#i.delete(e)});sync=()=>{if(this.#n?.watch)return this.#n.watch(e=>{this.#s(e??`system`)})};destroy=()=>{this.#i.clear(),this.#a.abort()};#o=()=>({theme:this.#e,resolvedTheme:this.getResolvedTheme(),systemTheme:this.getSystemTheme()});#s=e=>{this.#e=e,this.#t=this.#o();for(let e of this.#i)e(this.#t)};#c=()=>typeof matchMedia>`u`?`light`:(this.#r||(this.#r=window.matchMedia(`(prefers-color-scheme: dark)`),this.#r.addEventListener(`change`,()=>{this.#s(this.#e)},{signal:this.#a.signal})),this.#r.matches?`dark`:`light`)};function i(e={}){return new r(e)}const a=(e,t)=>{let n=localStorage.getItem(e)??`system`,r=matchMedia(`(prefers-color-scheme: dark)`).matches?`dark`:`light`;t({theme:n,resolvedTheme:n===`system`?r:n,systemTheme:r})};function o({key:t=e,handler:n}){return`(${a})('${t}',${n})`}export{o as createInlineThemeScript,i as createThemeStore,n as localStorageAdapter,t as memoryStorageAdapter};
|
|
2
|
+
//# sourceMappingURL=lite.js.map
|
package/dist/lite.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lite.js","names":["DEFAULT_LITE_KEY","localStorageAdapter","StorageAdapter","StorageAdapterCreate","Theme","ResolvedTheme","Snapshot","theme","resolvedTheme","systemTheme","Listener","value","ThemeStoreOptions","persisted","storage","ThemeStore","snapshot","mediaQuery","MediaQueryList","listeners","Set","abortController","AbortController","constructor","key","createSnapshot","getTheme","getResolvedTheme","resolveSystemTheme","getSystemTheme","getSnapshot","setTheme","current","setThemeAndNotify","set","broadcast","restore","get","subscribe","callback","add","delete","sync","watch","destroy","clear","abort","#createSnapshot","#setThemeAndNotify","listener","#resolveSystemTheme","matchMedia","window","addEventListener","signal","matches","createThemeStore","options","restoreScript","handler","localStorage","getItem","ThemeScriptParameter","createInlineThemeScript"],"sources":["../src/lite.ts"],"sourcesContent":["import { DEFAULT_LITE_KEY } from './constants'\nimport {\n\tlocalStorageAdapter,\n\ttype StorageAdapter,\n\ttype StorageAdapterCreate,\n} from './storage'\n\nexport * from './storage'\n\ntype Theme = 'system' | 'light' | 'dark'\n\ntype ResolvedTheme = 'light' | 'dark'\n\ntype Snapshot = {\n\ttheme: Theme\n\tresolvedTheme: ResolvedTheme\n\tsystemTheme: ResolvedTheme\n}\n\ntype Listener = (value: Snapshot) => void\n\ntype ThemeStoreOptions = {\n\tpersisted?: Theme\n\tstorage?: StorageAdapterCreate | null\n}\n\nclass ThemeStore {\n\t#theme: Theme\n\n\t#snapshot: Snapshot\n\n\t#storage: StorageAdapter | null\n\n\t#mediaQuery: MediaQueryList | undefined\n\n\t#listeners = new Set<Listener>()\n\n\t#abortController = new AbortController()\n\n\tconstructor({\n\t\tpersisted,\n\t\tstorage = localStorageAdapter({ key: DEFAULT_LITE_KEY }),\n\t}: ThemeStoreOptions = {}) {\n\t\tthis.#theme = persisted ?? 'system'\n\n\t\tthis.#storage =\n\t\t\tstorage?.({\n\t\t\t\tabortController: this.#abortController,\n\t\t\t}) ?? null\n\n\t\tthis.#snapshot = this.#createSnapshot()\n\t}\n\n\tgetTheme = (): Theme => {\n\t\treturn this.#theme\n\t}\n\n\tgetResolvedTheme = (): ResolvedTheme => {\n\t\treturn this.#theme === 'system' ? this.#resolveSystemTheme() : this.#theme\n\t}\n\n\tgetSystemTheme = (): ResolvedTheme => {\n\t\treturn this.#resolveSystemTheme()\n\t}\n\n\tgetSnapshot = (): Snapshot => {\n\t\treturn this.#snapshot\n\t}\n\n\tsetTheme = (theme: Theme | ((current: Theme) => Theme)): void => {\n\t\tthis.#setThemeAndNotify(\n\t\t\ttypeof theme === 'function' ? theme(this.#theme) : theme,\n\t\t)\n\n\t\tif (this.#storage) {\n\t\t\tthis.#storage.set(this.#theme)\n\t\t\tthis.#storage.broadcast?.(this.#theme)\n\t\t}\n\t}\n\n\trestore = (): void => {\n\t\tthis.#setThemeAndNotify((this.#storage?.get() ?? 'system') as Theme)\n\t}\n\n\tsubscribe = (callback: Listener): (() => void) => {\n\t\tthis.#listeners.add(callback)\n\n\t\treturn () => {\n\t\t\tthis.#listeners.delete(callback)\n\t\t}\n\t}\n\n\tsync = (): (() => void) | undefined => {\n\t\tif (!this.#storage?.watch) return\n\n\t\treturn this.#storage.watch((theme) => {\n\t\t\tthis.#setThemeAndNotify((theme ?? 'system') as Theme)\n\t\t})\n\t}\n\n\tdestroy = (): void => {\n\t\tthis.#listeners.clear()\n\t\tthis.#abortController.abort()\n\t}\n\n\t#createSnapshot = (): Snapshot => ({\n\t\ttheme: this.#theme,\n\t\tresolvedTheme: this.getResolvedTheme(),\n\t\tsystemTheme: this.getSystemTheme(),\n\t})\n\n\t#setThemeAndNotify = (theme: Theme): void => {\n\t\tthis.#theme = theme\n\t\tthis.#snapshot = this.#createSnapshot()\n\n\t\tfor (const listener of this.#listeners) {\n\t\t\tlistener(this.#snapshot)\n\t\t}\n\t}\n\n\t#resolveSystemTheme = (): ResolvedTheme => {\n\t\tif (typeof matchMedia === 'undefined') return 'light'\n\n\t\tif (!this.#mediaQuery) {\n\t\t\tthis.#mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')\n\n\t\t\tthis.#mediaQuery.addEventListener(\n\t\t\t\t'change',\n\t\t\t\t() => {\n\t\t\t\t\tthis.#setThemeAndNotify(this.#theme)\n\t\t\t\t},\n\t\t\t\t{ signal: this.#abortController.signal },\n\t\t\t)\n\t\t}\n\n\t\treturn this.#mediaQuery.matches ? 'dark' : 'light'\n\t}\n}\n\nexport type { ThemeStore }\n\nexport function createThemeStore(options: ThemeStoreOptions = {}): ThemeStore {\n\treturn new ThemeStore(options)\n}\n\nconst restoreScript = (key: string, handler: Listener) => {\n\tconst theme = (localStorage.getItem(key) ?? 'system') as Theme\n\tconst systemTheme = matchMedia('(prefers-color-scheme: dark)').matches\n\t\t? 'dark'\n\t\t: 'light'\n\tconst resolvedTheme = theme === 'system' ? systemTheme : theme\n\n\thandler({ theme, resolvedTheme, systemTheme })\n}\n\nexport type ThemeScriptParameter = {\n\tkey?: string\n\thandler: Listener\n}\n\nexport function createInlineThemeScript({\n\tkey = DEFAULT_LITE_KEY,\n\thandler,\n}: ThemeScriptParameter) {\n\treturn `(${restoreScript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,KAAK,EAAS,CAEzB,EAEA,OACK,OAAOE,WAAe,IAAoB,SAEzC,KAAK,KACT,KAAK,GAAcC,OAAOD,WAAW,8BAA8B,EAEnE,KAAK,GAAYE,iBAChB,aACM,CACL,KAAK,GAAmB,KAAK,EAAM,CACpC,EACA,CAAEC,OAAQ,KAAK,GAAiBA,MAAO,CACxC,GAGM,KAAK,GAAYC,QAAU,OAAS,QAE7C,EAIA,SAAgBC,EAAiBC,EAA6B,CAAC,EAAe,CAC7E,OAAO,IAAI1C,EAAW0C,CAAO,CAC9B,CAEA,MAAMC,GAAiBlC,EAAamC,IAAsB,CACzD,IAAMpD,EAASqD,aAAaC,QAAQrC,CAAG,GAAK,SACtCf,EAAc0C,WAAW,8BAA8B,CAAC,CAACI,QAC5D,OACA,QAGHI,EAAQ,CAAEpD,QAAOC,cAFKD,IAAU,SAAWE,EAAcF,EAEzBE,aAAY,CAAC,CAC9C,EAOA,SAAgBsD,EAAwB,CACvCvC,MAAMxB,EACN2D,WACwB,CACxB,MAAO,IAAID,EAAa,KAAMlC,EAAG,IAAKmC,EAAO,EAC9C"}
|
package/dist/react.d.ts
CHANGED
|
@@ -13,20 +13,6 @@ import { ThemeStore, ThemeStoreConfig, Themes } from "./index.js";
|
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
15
|
declare function useResonare<T extends ThemeStoreConfig>(store: ThemeStore<T>): {
|
|
16
|
-
themes: Themes<T>;
|
|
17
|
-
resolvedThemes: Themes<T>;
|
|
18
|
-
resolvedSystemThemes: { [K_1 in { [K in keyof T]: T[K] extends {
|
|
19
|
-
options: ReadonlyArray<infer U>;
|
|
20
|
-
} ? U extends {
|
|
21
|
-
media: ReadonlyArray<unknown>;
|
|
22
|
-
} ? K : never : never }[keyof T]]: T[K_1] extends {
|
|
23
|
-
options: ReadonlyArray<infer U>;
|
|
24
|
-
} ? U extends string | number | boolean ? U : U extends {
|
|
25
|
-
value: string;
|
|
26
|
-
media?: [string, string, string];
|
|
27
|
-
} ? U extends {
|
|
28
|
-
media: [string, string, string];
|
|
29
|
-
} ? never : U["value"] : never : never };
|
|
30
16
|
destroy: () => void;
|
|
31
17
|
restore: () => void;
|
|
32
18
|
setThemes: (themes: Partial<Themes<T>> | ((currentThemes: Themes<T>) => Partial<Themes<T>>)) => void;
|
|
@@ -53,18 +39,18 @@ declare function useResonare<T extends ThemeStoreConfig>(store: ThemeStore<T>):
|
|
|
53
39
|
[x: string]: [string, string];
|
|
54
40
|
};
|
|
55
41
|
};
|
|
56
|
-
updateSystemOption: <
|
|
42
|
+
updateSystemOption: <K extends { [K_1 in keyof T]: T[K_1] extends {
|
|
57
43
|
options: ReadonlyArray<infer U>;
|
|
58
44
|
} ? U extends {
|
|
59
45
|
media: ReadonlyArray<unknown>;
|
|
60
|
-
} ?
|
|
46
|
+
} ? K_1 : never : never }[keyof T]>(themeKey: K, pair: [T[K] extends {
|
|
61
47
|
options: ReadonlyArray<infer U>;
|
|
62
48
|
} ? U extends string | number | boolean ? U : U extends {
|
|
63
49
|
value: string;
|
|
64
50
|
media?: [string, string, string];
|
|
65
51
|
} ? U extends {
|
|
66
52
|
media: [string, string, string];
|
|
67
|
-
} ? never : U["value"] : never : never, T[
|
|
53
|
+
} ? never : U["value"] : never : never, T[K] extends {
|
|
68
54
|
options: ReadonlyArray<infer U>;
|
|
69
55
|
} ? U extends string | number | boolean ? U : U extends {
|
|
70
56
|
value: string;
|
|
@@ -72,6 +58,20 @@ declare function useResonare<T extends ThemeStoreConfig>(store: ThemeStore<T>):
|
|
|
72
58
|
} ? U extends {
|
|
73
59
|
media: [string, string, string];
|
|
74
60
|
} ? never : U["value"] : never : never]) => void;
|
|
61
|
+
themes: Themes<T>;
|
|
62
|
+
resolvedThemes: Themes<T>;
|
|
63
|
+
resolvedSystemThemes: { [K_1 in { [K in keyof T]: T[K] extends {
|
|
64
|
+
options: ReadonlyArray<infer U>;
|
|
65
|
+
} ? U extends {
|
|
66
|
+
media: ReadonlyArray<unknown>;
|
|
67
|
+
} ? K : never : never }[keyof T]]: T[K_1] extends {
|
|
68
|
+
options: ReadonlyArray<infer U>;
|
|
69
|
+
} ? U extends string | number | boolean ? U : U extends {
|
|
70
|
+
value: string;
|
|
71
|
+
media?: [string, string, string];
|
|
72
|
+
} ? U extends {
|
|
73
|
+
media: [string, string, string];
|
|
74
|
+
} ? never : U["value"] : never : never };
|
|
75
75
|
};
|
|
76
76
|
//#endregion
|
|
77
77
|
export { useResonare };
|
package/dist/react.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{c as e}from"react-compiler-runtime";import*as t from"react";function n(n){let r=e(
|
|
1
|
+
import{c as e}from"react-compiler-runtime";import*as t from"react";function n(n){let r=e(9),{destroy:i,getSnapshot:a,restore:o,setThemes:s,subscribe:c,sync:l,toPersist:u,updateSystemOption:d}=n,f=t.useSyncExternalStore(c,a,a),p;return r[0]!==i||r[1]!==o||r[2]!==s||r[3]!==f||r[4]!==c||r[5]!==l||r[6]!==u||r[7]!==d?(p={...f,destroy:i,restore:o,setThemes:s,subscribe:c,sync:l,toPersist:u,updateSystemOption:d},r[0]=i,r[1]=o,r[2]=s,r[3]=f,r[4]=c,r[5]=l,r[6]=u,r[7]=d,r[8]=p):p=r[8],p}export{n as useResonare};
|
|
2
2
|
//# sourceMappingURL=react.js.map
|
package/dist/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.js","names":["React","ThemeStore","ThemeStoreConfig","useResonare","store","$","_c","destroy","
|
|
1
|
+
{"version":3,"file":"react.js","names":["React","ThemeStore","ThemeStoreConfig","useResonare","store","$","_c","destroy","getSnapshot","restore","setThemes","subscribe","sync","toPersist","updateSystemOption","snapshot","useSyncExternalStore","t0"],"sources":["../src/react.ts"],"sourcesContent":["import * as React from 'react'\nimport type { ThemeStore, ThemeStoreConfig } from '.'\n\n/**\n * Subscribes a React component to a theme store.\n * @example\n * ```tsx\n * export function ThemeToggle() {\n * const { themes, resolvedThemes, resolvedSystemThemes, setThemes } = useResonare(store)\n *\n * // ...\n * }\n * ```\n */\nexport function useResonare<T extends ThemeStoreConfig>(store: ThemeStore<T>) {\n\tconst {\n\t\tdestroy,\n\t\tgetSnapshot,\n\t\trestore,\n\t\tsetThemes,\n\t\tsubscribe,\n\t\tsync,\n\t\ttoPersist,\n\t\tupdateSystemOption,\n\t} = store\n\n\tconst snapshot = React.useSyncExternalStore(\n\t\tsubscribe,\n\t\tgetSnapshot,\n\t\tgetSnapshot,\n\t)\n\n\treturn {\n\t\t...snapshot,\n\t\tdestroy,\n\t\trestore,\n\t\tsetThemes,\n\t\tsubscribe,\n\t\tsync,\n\t\ttoPersist,\n\t\tupdateSystemOption,\n\t}\n}\n"],"mappings":"mEAcA,SAAOG,EAAAC,EAAA,CAAA,IAAAC,EAAAC,EAAA,CAAA,EACN,CAAAC,UAAAC,cAAAC,UAAAC,YAAAC,YAAAC,OAAAC,YAAAC,sBASIV,EAEJW,EAAiBf,EAAKgB,qBACrBL,EACAH,EACAA,CACD,EAACS,EAWA,OAXAZ,EAAA,KAAAE,GAAAF,EAAA,KAAAI,GAAAJ,EAAA,KAAAK,GAAAL,EAAA,KAAAU,GAAAV,EAAA,KAAAM,GAAAN,EAAA,KAAAO,GAAAP,EAAA,KAAAQ,GAAAR,EAAA,KAAAS,GAEMG,EAAA,CAAA,GACHF,EAAQR,UAAAE,UAAAC,YAAAC,YAAAC,OAAAC,YAAAC,oBAQZ,EAACT,EAAA,GAAAE,EAAAF,EAAA,GAAAI,EAAAJ,EAAA,GAAAK,EAAAL,EAAA,GAAAU,EAAAV,EAAA,GAAAM,EAAAN,EAAA,GAAAO,EAAAP,EAAA,GAAAQ,EAAAR,EAAA,GAAAS,EAAAT,EAAA,GAAAY,GAAAA,EAAAZ,EAAA,GATMY,CASN"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e=`resonare`,t=`resonare/lite`,n=({key:e,type:t=`localStorage`})=>({abortController:n})=>({get:()=>window[t].getItem(e),set:n=>{window[t].setItem(e,n)},watch:r=>{let i=new AbortController;return window.addEventListener(`storage`,n=>{n.storageArea===window[t]&&n.key===e&&r(n.newValue)},{signal:AbortSignal.any([n.signal,i.signal])}),()=>{i.abort()}}}),r=({key:t})=>({abortController:n})=>{let r=new Map,i=new BroadcastChannel(e);return{get:()=>r.get(t)??null,set:e=>{r.set(t,e)},broadcast:e=>{i.postMessage({key:t,value:e})},watch:e=>{let r=new AbortController;return i.addEventListener(`message`,n=>{n.data.key===t&&e(n.data.value)},{signal:AbortSignal.any([n.signal,r.signal])}),()=>{r.abort()}}}};export{t as i,r as n,e as r,n as t};
|
|
2
|
+
//# sourceMappingURL=storage-B5xjLRee.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage-B5xjLRee.js","names":["DEFAULT_KEY","DEFAULT_LITE_KEY","DEFAULT_KEY","StorageAdapter","get","set","value","broadcast","watch","cb","StorageAdapterCreate","abortController","AbortController","StorageAdapterCreator","options","Options","localStorageAdapter","key","type","window","getItem","setItem","controller","addEventListener","e","storageArea","newValue","signal","AbortSignal","any","abort","memoryStorageAdapter","storage","Map","channel","BroadcastChannel","postMessage","data"],"sources":["../src/constants.ts","../src/storage.ts"],"sourcesContent":["export const DEFAULT_KEY = 'resonare'\nexport const DEFAULT_LITE_KEY = 'resonare/lite'\n","import { DEFAULT_KEY } from './constants'\n\nexport type StorageAdapter = {\n\tget: () => string | null\n\tset: (value: string) => void\n\tbroadcast?: (value: string) => void\n\twatch?: (cb: (value: string | null) => void) => () => void\n}\n\nexport type StorageAdapterCreate = ({\n\tabortController,\n}: {\n\tabortController: AbortController\n}) => StorageAdapter\n\nexport type StorageAdapterCreator<Options> = (\n\toptions: Options,\n) => StorageAdapterCreate\n\nexport const localStorageAdapter: StorageAdapterCreator<{\n\tkey: string\n\ttype?: 'localStorage' | 'sessionStorage'\n}> = ({ key, type = 'localStorage' }) => {\n\treturn ({ abortController }) => {\n\t\treturn {\n\t\t\tget: () => {\n\t\t\t\treturn window[type].getItem(key)\n\t\t\t},\n\n\t\t\tset: (value) => {\n\t\t\t\twindow[type].setItem(key, value)\n\t\t\t},\n\n\t\t\twatch: (cb) => {\n\t\t\t\tconst controller = new AbortController()\n\n\t\t\t\twindow.addEventListener(\n\t\t\t\t\t'storage',\n\t\t\t\t\t(e) => {\n\t\t\t\t\t\tif (e.storageArea !== window[type]) return\n\n\t\t\t\t\t\tif (e.key !== key) return\n\n\t\t\t\t\t\tcb(e.newValue)\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tsignal: AbortSignal.any([\n\t\t\t\t\t\t\tabortController.signal,\n\t\t\t\t\t\t\tcontroller.signal,\n\t\t\t\t\t\t]),\n\t\t\t\t\t},\n\t\t\t\t)\n\n\t\t\t\treturn () => {\n\t\t\t\t\tcontroller.abort()\n\t\t\t\t}\n\t\t\t},\n\t\t}\n\t}\n}\n\nexport const memoryStorageAdapter: StorageAdapterCreator<{\n\tkey: string\n}> = ({ key }) => {\n\treturn ({ abortController }) => {\n\t\tconst storage = new Map<string, string>()\n\n\t\tconst channel = new BroadcastChannel(DEFAULT_KEY)\n\n\t\treturn {\n\t\t\tget: () => {\n\t\t\t\treturn storage.get(key) ?? null\n\t\t\t},\n\n\t\t\tset: (value) => {\n\t\t\t\tstorage.set(key, value)\n\t\t\t},\n\n\t\t\tbroadcast: (value) => {\n\t\t\t\tchannel.postMessage({ key, value })\n\t\t\t},\n\n\t\t\twatch: (cb) => {\n\t\t\t\tconst controller = new AbortController()\n\n\t\t\t\tchannel.addEventListener(\n\t\t\t\t\t'message',\n\t\t\t\t\t(e) => {\n\t\t\t\t\t\tif (e.data.key !== key) return\n\n\t\t\t\t\t\tcb(e.data.value)\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tsignal: AbortSignal.any([\n\t\t\t\t\t\t\tabortController.signal,\n\t\t\t\t\t\t\tcontroller.signal,\n\t\t\t\t\t\t]),\n\t\t\t\t\t},\n\t\t\t\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,23 @@
|
|
|
1
|
+
//#region src/storage.d.ts
|
|
2
|
+
type StorageAdapter = {
|
|
3
|
+
get: () => string | null;
|
|
4
|
+
set: (value: string) => void;
|
|
5
|
+
broadcast?: (value: string) => void;
|
|
6
|
+
watch?: (cb: (value: string | null) => void) => () => void;
|
|
7
|
+
};
|
|
8
|
+
type StorageAdapterCreate = ({
|
|
9
|
+
abortController
|
|
10
|
+
}: {
|
|
11
|
+
abortController: AbortController;
|
|
12
|
+
}) => StorageAdapter;
|
|
13
|
+
type StorageAdapterCreator<Options> = (options: Options) => StorageAdapterCreate;
|
|
14
|
+
declare const localStorageAdapter: StorageAdapterCreator<{
|
|
15
|
+
key: string;
|
|
16
|
+
type?: 'localStorage' | 'sessionStorage';
|
|
17
|
+
}>;
|
|
18
|
+
declare const memoryStorageAdapter: StorageAdapterCreator<{
|
|
19
|
+
key: string;
|
|
20
|
+
}>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { memoryStorageAdapter as a, localStorageAdapter as i, StorageAdapterCreate as n, StorageAdapterCreator as r, StorageAdapter as t };
|
|
23
|
+
//# sourceMappingURL=storage-DGYV332J.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage-DGYV332J.d.ts","names":[],"sources":["../src/storage.ts"],"mappings":";KAEY,cAAA;EACX,GAAA;EACA,GAAA,GAAM,KAAA;EACN,SAAA,IAAa,KAAA;EACb,KAAA,IAAS,EAAA,GAAK,KAAA;AAAA;AAAA,KAGH,oBAAA;EACX;AAAA;EAEA,eAAA,EAAiB,eAAA;AAAA,MACZ,cAAA;AAAA,KAEM,qBAAA,aACX,OAAA,EAAS,OAAA,KACL,oBAAoB;AAAA,cAEZ,mBAAA,EAAqB,qBAAqB;EACtD,GAAA;EACA,IAAA;AAAA;AAAA,cAwCY,oBAAA,EAAsB,qBAAqB;EACvD,GAAG;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resonare",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Resonare",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"theming",
|
|
@@ -24,6 +24,14 @@
|
|
|
24
24
|
"./react": {
|
|
25
25
|
"types": "./dist/react.d.ts",
|
|
26
26
|
"import": "./dist/react.js"
|
|
27
|
+
},
|
|
28
|
+
"./lite": {
|
|
29
|
+
"types": "./dist/lite.d.ts",
|
|
30
|
+
"import": "./dist/lite.js"
|
|
31
|
+
},
|
|
32
|
+
"./lite/react": {
|
|
33
|
+
"types": "./dist/lite-react.d.ts",
|
|
34
|
+
"import": "./dist/lite-react.js"
|
|
27
35
|
}
|
|
28
36
|
},
|
|
29
37
|
"types": "./dist/index.d.ts",
|