resonare 0.1.5 → 0.1.6

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/storage.ts","../src/index.ts"],"mappings":";;AAKA;;KAAY,cAAA;EACX,GAAA;EACA,GAAA,GAAM,KAAA,mBAAN;EAEA,SAAA,IAAa,KAAA,mBAAb;EAEA,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,oBAAA;AARL;;;;;;;;;;;;AAAA,cAsBa,mBAAA,EAAqB,qBAAA;EACjC,GAAA;EACA,IAAA;AAAA;;;;;;;;;;AAFD;;;;cAuDa,oBAAA,EAAsB,qBAAA;EAClC,GAAA;AAAA;;;KCnFI,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,GAAI,WAAA,CAAY,CAAA;EACvC,YAAA,GAAe,CAAA;AAAA;EAEb,YAAA,EAAc,CAAA;EAAG,OAAA;AAAA;AAAA,KAEV,gBAAA,GAAmB,MAAA,SAE9B,WAAA,WAAsB,WAAA,WAAsB,WAAA;AAAA,KAQjC,MAAA,WAAiB,gBAAA,kBAChB,CAAA,GAAI,CAAA,CAAE,CAAA;EAAa,OAAA,EAAS,aAAA;AAAA,IACrC,CAAA,SAAU,WAAA,GACT,CAAA,YACA,CAAA,GACD,CAAA,CAAE,CAAA;EAAa,YAAA;AAAA,IACd,CAAA,2BAEC,CAAA;AAAA,KAMD,QAAA,WAAmB,gBAAA,KAAqB,KAAA;EAC5C,MAAA,EAAQ,MAAA,CA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,QAAgB,cAAA,CAAe,CAAA;EAO/B,OAAA;EAmBA,SAAA,GAAa,QAAA,EAAU,QAAA,CAAS,CAAA;EAQhC,IAAA;EA/PA;EA0QA,OAAA;AAAA;AAAA,iBA+De,gBAAA,WAA2B,gBAAA,CAAA,CAC1C,MAAA,EAAQ,CAAA,EACR,OAAA,GAAS,iBAAA,CAAkB,CAAA,IACzB,UAAA,CAAW,CAAA;AAAA,KA+CF,oBAAA;EA1XU,sDA4XrB,GAAA;EACA,MAAA,EAAQ,gBAAA;EACR,OAAA,EAAS,QAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;AApXV;iBA8YgB,uBAAA,CACf,qBAAA,EAAuB,oBAAA,GAAuB,KAAA,CAAM,oBAAA"}
package/dist/index.js CHANGED
@@ -1,285 +1,2 @@
1
- //#region package.json
2
- var name = "resonare";
3
- //#endregion
4
- //#region src/storage.ts
5
- /**
6
- * Persists theme store in `localStorage` or `sessionStorage`.
7
- * @example
8
- * ```ts
9
- * import { createThemeStore, localStorageAdapter } from 'resonare'
10
- *
11
- * const store = createThemeStore(
12
- * { colorMode: { options: ['light', 'dark'] },
13
- * { storage: localStorageAdapter({ key: 'app', type: 'localStorage' }) },
14
- * )
15
- * ```
16
- */
17
- const localStorageAdapter = ({ key, type = "localStorage" }) => {
18
- return ({ abortController }) => {
19
- return {
20
- get: () => {
21
- return JSON.parse(window[type].getItem(key) || "null");
22
- },
23
- set: (value) => {
24
- window[type].setItem(key, JSON.stringify(value));
25
- },
26
- watch: (cb) => {
27
- const controller = new AbortController();
28
- window.addEventListener("storage", (e) => {
29
- if (e.storageArea !== window[type]) return;
30
- if (e.key !== key) return;
31
- cb(JSON.parse(e.newValue));
32
- }, { signal: AbortSignal.any([abortController.signal, controller.signal]) });
33
- return () => {
34
- controller.abort();
35
- };
36
- }
37
- };
38
- };
39
- };
40
- /**
41
- * In-memory persistence and sync via `BroadcastChannel`.
42
- * Useful with server-side persistence.
43
- * @example
44
- * ```ts
45
- * import { createThemeStore, memoryStorageAdapter } from 'resonare'
46
- *
47
- * const store = createThemeStore(
48
- * { colorMode: { options: ['light', 'dark'] },
49
- * { storage: memoryStorageAdapter({ key: 'app' }) },
50
- * )
51
- * ```
52
- */
53
- const memoryStorageAdapter = ({ key }) => {
54
- return ({ abortController }) => {
55
- const storage = /* @__PURE__ */ new Map();
56
- const channel = new BroadcastChannel(name);
57
- return {
58
- get: () => {
59
- return storage.get(key) || null;
60
- },
61
- set: (value) => {
62
- storage.set(key, value);
63
- },
64
- broadcast: (value) => {
65
- channel.postMessage({
66
- key,
67
- value
68
- });
69
- },
70
- watch: (cb) => {
71
- const controller = new AbortController();
72
- channel.addEventListener("message", (e) => {
73
- if (e.data.key !== key) return;
74
- cb(e.data.value);
75
- }, { signal: AbortSignal.any([abortController.signal, controller.signal]) });
76
- return () => {
77
- controller.abort();
78
- };
79
- }
80
- };
81
- };
82
- };
83
- //#endregion
84
- //#region src/index.ts
85
- function getThemesAndOptions(config) {
86
- return Object.entries(config).map(([themeKey, themeConfig]) => {
87
- return [themeKey, (themeConfig.options || []).map((option) => typeof option === "object" ? option.value : option)];
88
- });
89
- }
90
- function getDefaultThemes(config) {
91
- return Object.fromEntries(Object.entries(config).map(([themeKey, themeConfig]) => {
92
- return [themeKey, themeConfig.initialValue ?? (typeof themeConfig.options[0] === "object" ? themeConfig.options[0].value : themeConfig.options[0])];
93
- }));
94
- }
95
- var ThemeStore = class {
96
- #defaultThemes;
97
- #currentThemes;
98
- #keyedConfig;
99
- #systemOptions;
100
- #storage;
101
- #mediaQueryCache = {};
102
- #listeners = /* @__PURE__ */ new Set();
103
- #abortController = new AbortController();
104
- constructor(config, { initialState = {}, storage = localStorageAdapter({ key: name }) } = {}) {
105
- const systemOptions = {};
106
- this.#defaultThemes = getDefaultThemes(config);
107
- this.#currentThemes = {
108
- ...this.#defaultThemes,
109
- ...initialState.themes
110
- };
111
- this.#keyedConfig = Object.fromEntries(Object.entries(config).map(([themeKey, themeConfig]) => {
112
- const entries = (themeConfig.options || []).map((option) => {
113
- if (typeof option === "object") {
114
- if (option.media) systemOptions[themeKey] = [option.media[1], option.media[2]];
115
- return [String(option.value), option];
116
- }
117
- return [String(option), { value: option }];
118
- });
119
- return [themeKey, Object.fromEntries(entries)];
120
- }));
121
- this.#systemOptions = {
122
- ...systemOptions,
123
- ...initialState.systemOptions
124
- };
125
- this.#storage = storage?.({ abortController: this.#abortController }) ?? null;
126
- }
127
- getThemes = () => {
128
- return this.#currentThemes;
129
- };
130
- getResolvedThemes = () => {
131
- return Object.fromEntries(Object.entries(this.#currentThemes).map(([themeKey, optionKey]) => {
132
- const option = this.#keyedConfig[themeKey]?.[optionKey];
133
- return [themeKey, option ? this.#resolveThemeOption({
134
- themeKey,
135
- option
136
- }) : optionKey];
137
- }));
138
- };
139
- setThemes = (themes) => {
140
- const updatedThemes = typeof themes === "function" ? themes(this.#currentThemes) : themes;
141
- this.#setThemesAndNotify({
142
- ...this.#currentThemes,
143
- ...updatedThemes
144
- });
145
- const stateToPersist = this.toPersist();
146
- if (this.#storage) {
147
- this.#storage.set(stateToPersist);
148
- this.#storage.broadcast?.(stateToPersist);
149
- }
150
- };
151
- updateSystemOption = (themeKey, [ifMatch, ifNotMatch]) => {
152
- this.#systemOptions[themeKey] = [ifMatch, ifNotMatch];
153
- this.setThemes({ ...this.#currentThemes });
154
- };
155
- toPersist = () => {
156
- return {
157
- themes: this.#currentThemes,
158
- systemOptions: this.#systemOptions
159
- };
160
- };
161
- restore = () => {
162
- const persistedState = this.#storage?.get();
163
- if (!persistedState) {
164
- this.#setThemesAndNotify({ ...this.#defaultThemes });
165
- return;
166
- }
167
- this.#systemOptions = {
168
- ...this.#systemOptions,
169
- ...persistedState.systemOptions
170
- };
171
- this.#setThemesAndNotify({
172
- ...this.#defaultThemes,
173
- ...persistedState.themes
174
- });
175
- };
176
- subscribe = (callback) => {
177
- this.#listeners.add(callback);
178
- return () => {
179
- this.#listeners.delete(callback);
180
- };
181
- };
182
- sync = () => {
183
- if (!this.#storage?.watch) return;
184
- return this.#storage.watch((persistedState) => {
185
- this.#systemOptions = persistedState.systemOptions;
186
- this.#setThemesAndNotify(persistedState.themes);
187
- });
188
- };
189
- /** Clears subscribers and aborts media-query listeners tied to this store instance. */
190
- destroy = () => {
191
- this.#listeners.clear();
192
- this.#abortController.abort();
193
- };
194
- #setThemesAndNotify = (themes) => {
195
- this.#currentThemes = themes;
196
- for (const listener of this.#listeners) listener({
197
- themes: this.#currentThemes,
198
- resolvedThemes: this.getResolvedThemes()
199
- });
200
- };
201
- #resolveThemeOption = ({ themeKey, option }) => {
202
- if (!option.media) return option.value;
203
- if (!(typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined")) {
204
- if (!(process.env.NODE_ENV === "production")) console.warn(`[${name}] Option with key "media" cannot be resolved in server environment.`);
205
- return option.value;
206
- }
207
- const cache = this.#mediaQueryCache;
208
- const [mediaQuery] = option.media;
209
- if (!cache[mediaQuery]) {
210
- const mediaQueryList = window.matchMedia(mediaQuery);
211
- cache[mediaQuery] = mediaQueryList;
212
- mediaQueryList.addEventListener("change", () => {
213
- if (this.#currentThemes[themeKey] === option.value) this.#setThemesAndNotify({ ...this.#currentThemes });
214
- }, { signal: this.#abortController.signal });
215
- }
216
- const [ifMatch, ifNotMatch] = this.#systemOptions[themeKey];
217
- return cache[mediaQuery].matches ? ifMatch : ifNotMatch;
218
- };
219
- };
220
- function createThemeStore(config, options = {}) {
221
- return new ThemeStore(config, options);
222
- }
223
- const restoreScript = (params) => {
224
- params.forEach(([key, flattenedConfig, handler]) => {
225
- const persistedThemes = JSON.parse(localStorage.getItem(key) || "{}").themes ?? {};
226
- handler(flattenedConfig.reduce((acc, [themeKey, initial, systemOptionValue, mediaQuery, ifMatch, ifNotMatch]) => {
227
- const currentValue = persistedThemes[themeKey] ?? initial;
228
- acc.resolvedThemes[themeKey] = acc.themes[themeKey] = currentValue;
229
- if (currentValue === systemOptionValue) acc.resolvedThemes[themeKey] = matchMedia(mediaQuery).matches ? ifMatch : ifNotMatch;
230
- return acc;
231
- }, {
232
- themes: {},
233
- resolvedThemes: {}
234
- }));
235
- });
236
- };
237
- /**
238
- * Creates an IIFE script string that reads persisted themes from `localStorage` and runs your handlers immediately.
239
- *
240
- * Useful for avoiding flash of incorrect styles.
241
- * @example
242
- * ```tsx
243
- * import { createInlineThemeScript } from 'resonare'
244
- *
245
- * const inlineScript = createInlineThemeScript([
246
- * {
247
- * key: 'my-app',
248
- * config: {
249
- * colorMode: { options: ['light', 'dark'] },
250
- * },
251
- * handler: ({ resolvedThemes }) => {
252
- * document.documentElement.dataset.colorMode = String(
253
- * resolvedThemes.colorMode,
254
- * )
255
- * },
256
- * },
257
- * ])
258
- * ```
259
- */
260
- function createInlineThemeScript(themeScriptParameters) {
261
- return `(${restoreScript})([${(Array.isArray(themeScriptParameters) ? themeScriptParameters : [themeScriptParameters]).map(({ key = name, config, handler }) => {
262
- const flattenedConfig = Object.entries(config).map(([themeKey, { options, initialValue }]) => {
263
- const firstOption = options?.[0];
264
- const resolvedInitialValue = initialValue ?? (typeof firstOption === "object" ? firstOption.value : firstOption);
265
- const systemOption = options?.find((option) => typeof option === "object" && !!option.media);
266
- if (systemOption) {
267
- const { value, media: [mediaQuery, ifMatch, ifNotMatch] } = systemOption;
268
- return [
269
- themeKey,
270
- resolvedInitialValue,
271
- value,
272
- mediaQuery,
273
- ifMatch,
274
- ifNotMatch
275
- ];
276
- }
277
- return [themeKey, resolvedInitialValue];
278
- });
279
- return `['${key}', ${JSON.stringify(flattenedConfig)}, ${handler}]`;
280
- })}])`;
281
- }
282
- //#endregion
283
- export { createInlineThemeScript, createThemeStore, getDefaultThemes, getThemesAndOptions, localStorageAdapter, memoryStorageAdapter };
284
-
1
+ var e=`resonare`;const t=({key:e,type:t=`localStorage`})=>({abortController:n})=>({get:()=>JSON.parse(window[t].getItem(e)||`null`),set:n=>{window[t].setItem(e,JSON.stringify(n))},watch:r=>{let i=new AbortController;return window.addEventListener(`storage`,n=>{n.storageArea===window[t]&&n.key===e&&r(JSON.parse(n.newValue))},{signal:AbortSignal.any([n.signal,i.signal])}),()=>{i.abort()}}}),n=({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()}}}};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.initialValue??(typeof t.options[0]==`object`?t.options[0].value:t.options[0])]))}var a=class{#e;#t;#n;#r;#i;#a={};#o=new Set;#s=new AbortController;constructor(n,{initialState:r={},storage:a=t({key:e})}={}){let o={};this.#e=i(n),this.#t={...this.#e,...r.themes},this.#n=Object.fromEntries(Object.entries(n).map(([e,t])=>{let n=(t.options||[]).map(t=>typeof t==`object`?(t.media&&(o[e]=[t.media[1],t.media[2]]),[String(t.value),t]):[String(t),{value:t}]);return[e,Object.fromEntries(n)]})),this.#r={...o,...r.systemOptions},this.#i=a?.({abortController:this.#s})??null}getThemes=()=>this.#t;getResolvedThemes=()=>Object.fromEntries(Object.entries(this.#t).map(([e,t])=>{let n=this.#n[e]?.[t];return[e,n?this.#l({themeKey:e,option:n}):t]}));setThemes=e=>{let t=typeof e==`function`?e(this.#t):e;this.#c({...this.#t,...t});let n=this.toPersist();this.#i&&(this.#i.set(n),this.#i.broadcast?.(n))};updateSystemOption=(e,[t,n])=>{this.#r[e]=[t,n],this.setThemes({...this.#t})};toPersist=()=>({themes:this.#t,systemOptions:this.#r});restore=()=>{let e=this.#i?.get();if(!e){this.#c({...this.#e});return}this.#r={...this.#r,...e.systemOptions},this.#c({...this.#e,...e.themes})};subscribe=e=>(this.#o.add(e),()=>{this.#o.delete(e)});sync=()=>{if(this.#i?.watch)return this.#i.watch(e=>{this.#r=e.systemOptions,this.#c(e.themes)})};destroy=()=>{this.#o.clear(),this.#s.abort()};#c=e=>{this.#t=e;for(let e of this.#o)e({themes:this.#t,resolvedThemes:this.getResolvedThemes()})};#l=({themeKey:e,option:t})=>{if(!t.media||!(typeof window<`u`&&window.document!==void 0&&window.document.createElement!==void 0))return t.value;let n=this.#a,[r]=t.media;if(!n[r]){let i=window.matchMedia(r);n[r]=i,i.addEventListener(`change`,()=>{this.#t[e]===t.value&&this.#c({...this.#t})},{signal:this.#s.signal})}let[i,a]=this.#r[e];return n[r].matches?i:a}};function o(e,t={}){return new a(e,t)}const s=e=>{e.forEach(([e,t,n])=>{let r=JSON.parse(localStorage.getItem(e)||`{}`).themes??{};n(t.reduce((e,[t,n,i,a,o,s])=>{let c=r[t]??n;return e.resolvedThemes[t]=e.themes[t]=c,c===i&&(e.resolvedThemes[t]=matchMedia(a).matches?o:s),e},{themes:{},resolvedThemes:{}}))})};function c(t){return`(${s})([${(Array.isArray(t)?t:[t]).map(({key:t=e,config:n,handler:r})=>{let i=Object.entries(n).map(([e,{options:t,initialValue: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,r,o]}=a;return[e,i,t,n,r,o]}return[e,i]});return`['${t}', ${JSON.stringify(i)}, ${r}]`})}])`}export{c as createInlineThemeScript,o as createThemeStore,i as getDefaultThemes,r as getThemesAndOptions,t as localStorageAdapter,n as memoryStorageAdapter};
285
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","KeyedThemeStoreConfig","Themes","K","U","Listener","themes","resolvedThemes","ThemeKeysWithSystemOption","NonSystemOptionValues","SystemOptions","PersistedState","Partial","systemOptions","ThemeStoreOptions","initialState","storage","ThemeAndOptions","Array","getThemesAndOptions","config","Object","entries","map","themeKey","themeConfig","option","getDefaultThemes","fromEntries","ThemeStore","defaultThemes","currentThemes","keyedConfig","mediaQueryCache","MediaQueryList","listeners","Set","abortController","AbortController","constructor","key","String","getThemes","getResolvedThemes","optionKey","resolveThemeOption","setThemes","updatedThemes","setThemesAndNotify","stateToPersist","toPersist","set","broadcast","updateSystemOption","ifMatch","ifNotMatch","restore","persistedState","get","subscribe","callback","add","delete","sync","watch","destroy","clear","abort","#setThemesAndNotify","listener","#resolveThemeOption","window","document","createElement","PROD","console","warn","cache","mediaQuery","mediaQueryList","matchMedia","addEventListener","signal","matches","createThemeStore","restoreScript","params","forEach","flattenedConfig","handler","persistedThemes","JSON","parse","localStorage","getItem","reduce","acc","initial","systemOptionValue","currentValue","ThemeScriptParameter","createInlineThemeScript","themeScriptParameters","serializedArgs","isArray","firstOption","resolvedInitialValue","systemOption","find","Required","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\n// { [themeKey]: { [optionKey]: ThemeOption } }\ntype KeyedThemeStoreConfig<T extends ThemeStoreConfig> = {\n\t[K in keyof T]: Record<string, ThemeOption>\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 Listener<T extends ThemeStoreConfig> = (value: {\n\tthemes: Themes<T>\n\tresolvedThemes: Themes<T>\n}) => void\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 SystemOptions<T extends ThemeStoreConfig> = {\n\t[K in ThemeKeysWithSystemOption<T>]?: [\n\t\tNonSystemOptionValues<T, K>,\n\t\tNonSystemOptionValues<T, K>,\n\t]\n}\n\ntype PersistedState<T extends ThemeStoreConfig> = {\n\tthemes: Partial<Themes<T>>\n\tsystemOptions: SystemOptions<T>\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#keyedConfig: KeyedThemeStoreConfig<T>\n\n\t#systemOptions: SystemOptions<T>\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\tconst systemOptions: Record<string, [ThemeValue, ThemeValue]> = {}\n\n\t\tthis.#defaultThemes = getDefaultThemes(config)\n\n\t\tthis.#currentThemes = { ...this.#defaultThemes, ...initialState.themes }\n\n\t\tthis.#keyedConfig = Object.fromEntries(\n\t\t\tObject.entries(config).map(([themeKey, themeConfig]) => {\n\t\t\t\tconst entries = (themeConfig.options || []).map((option) => {\n\t\t\t\t\tif (typeof option === 'object') {\n\t\t\t\t\t\tif (option.media) {\n\t\t\t\t\t\t\tsystemOptions[themeKey] = [option.media[1], option.media[2]]\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn [String(option.value), option]\n\t\t\t\t\t}\n\n\t\t\t\t\treturn [String(option), { value: option }]\n\t\t\t\t})\n\n\t\t\t\treturn [themeKey, Object.fromEntries(entries)]\n\t\t\t}),\n\t\t) as KeyedThemeStoreConfig<T>\n\n\t\tthis.#systemOptions = { ...systemOptions, ...initialState.systemOptions }\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 option = this.#keyedConfig[themeKey]?.[optionKey]\n\n\t\t\t\treturn [\n\t\t\t\t\tthemeKey,\n\t\t\t\t\toption ? this.#resolveThemeOption({ themeKey, option }) : optionKey,\n\t\t\t\t]\n\t\t\t}),\n\t\t) as Themes<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\t[ifMatch, ifNotMatch]: [\n\t\t\tNonSystemOptionValues<T, K>,\n\t\t\tNonSystemOptionValues<T, K>,\n\t\t],\n\t): void => {\n\t\tthis.#systemOptions[themeKey] = [ifMatch, ifNotMatch]\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: this.#systemOptions,\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.#systemOptions = {\n\t\t\t...this.#systemOptions,\n\t\t\t...persistedState.systemOptions,\n\t\t}\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 () => {\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((persistedState) => {\n\t\t\tthis.#systemOptions = (persistedState as PersistedState<T>).systemOptions\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#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})\n\t\t}\n\t}\n\n\t#resolveThemeOption = ({\n\t\tthemeKey,\n\t\toption,\n\t}: {\n\t\tthemeKey: string\n\t\toption: ThemeOption\n\t}): string => {\n\t\tif (!option.media) return option.value\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\tif (!PROD) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`[${PACKAGE_NAME}] Option with key \"media\" cannot be resolved in server environment.`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\treturn option.value\n\t\t}\n\n\t\tconst cache = this.#mediaQueryCache\n\n\t\tconst [mediaQuery] = option.media\n\n\t\tif (!cache[mediaQuery]) {\n\t\t\tconst mediaQueryList = window.matchMedia(mediaQuery)\n\n\t\t\tcache[mediaQuery] = mediaQueryList\n\n\t\t\tmediaQueryList.addEventListener(\n\t\t\t\t'change',\n\t\t\t\t() => {\n\t\t\t\t\tif (this.#currentThemes[themeKey] === option.value) {\n\t\t\t\t\t\tthis.#setThemesAndNotify({ ...this.#currentThemes })\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\t{ signal: this.#abortController.signal },\n\t\t\t)\n\t\t}\n\n\t\tconst [ifMatch, ifNotMatch] = this.#systemOptions[themeKey]!\n\n\t\treturn cache[mediaQuery].matches ? ifMatch : ifNotMatch\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 persistedThemes =\n\t\t\tJSON.parse(localStorage.getItem(key) || '{}').themes ?? {}\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\tifMatch,\n\t\t\t\t\t\tifNotMatch,\n\t\t\t\t\t],\n\t\t\t\t) => {\n\t\t\t\t\tconst currentValue = persistedThemes[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\tacc.resolvedThemes[themeKey] = matchMedia(mediaQuery!).matches\n\t\t\t\t\t\t\t? ifMatch!\n\t\t\t\t\t\t\t: ifNotMatch!\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>\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 {\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t\tmedia: [mediaQuery, ifMatch, ifNotMatch],\n\t\t\t\t\t} = systemOption\n\n\t\t\t\t\treturn [\n\t\t\t\t\t\tthemeKey,\n\t\t\t\t\t\tresolvedInitialValue,\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t\tmediaQuery,\n\t\t\t\t\t\tifMatch,\n\t\t\t\t\t\tifNotMatch,\n\t\t\t\t\t]\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":";;;;;;;;;;;;;;;;ACoCA,MAAagB,uBAGP,EAAEC,KAAKf,OAAO,qBAAqB;AACxC,SAAQ,EAAES,sBAAsB;AAC/B,SAAO;GACNP,WAAW;AACV,WAAOc,KAAKC,MAAMC,OAAOlB,MAAMmB,QAAQJ,IAAI,IAAI,OAAO;;GAGvDZ,MAAMC,UAAkB;AACvBc,WAAOlB,MAAMoB,QAAQL,KAAKC,KAAKK,UAAUjB,MAAM,CAAC;;GAGjDE,QAAQC,OAAO;IACd,MAAMe,aAAa,IAAIZ,iBAAiB;AAExCQ,WAAOK,iBACN,YACCC,MAAM;AACN,SAAIA,EAAEC,gBAAgBP,OAAOlB,MAAO;AAEpC,SAAIwB,EAAET,QAAQA,IAAK;AAEnBR,QAAGS,KAAKC,M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ACAC,gBAEG;GACJ,MAAM4C,eAAeT,gBAAgBlE,aAAayE;AAElDD,OAAIzF,eAAeiB,YAAYwE,IAAI1F,OAAOkB,YAAY2E;AAEtD,OAAIA,iBAAiBD,kBACpBF,KAAIzF,eAAeiB,YAAYwD,WAAWF,WAAY,CAACK,UACpD7B,UACAC;AAGJ,UAAOyC;KAER;GAAE1F,QAAQ,EAAE;GAAEC,gBAAgB,EAAC;GAChC,CACD,CAAC;GACA;;;;;;;;;;;;;;;;;;;;;;;;;AAiCH,SAAgB8F,wBACfC,uBACC;AA0CD,QAAO,IAAIjB,cAAa,MAxCvBnE,MAAMsF,QAAQF,sBAAsB,GACjCA,wBACA,CAACA,sBAAsB,EACzB/E,KAAK,EAAEiB,MAAMvD,MAAcmC,QAAQqE,cAAc;EAClD,MAAMD,kBAAkBnE,OAAOC,QAAQF,OAAO,CAACG,KAC7C,CAACC,UAAU,EAAE5B,SAASE,oBAAoB;GAC1C,MAAM2G,cAAc7G,UAAU;GAE9B,MAAM8G,uBACL5G,iBACC,OAAO2G,gBAAgB,WAAWA,YAAYjH,QAAQiH;GAExD,MAAME,eAAe/G,SAASgH,MAC5BlF,WACA,OAAOA,WAAW,YAAY,CAAC,CAACA,OAAOhC,MACxC;AAED,OAAIiH,cAAc;IACjB,MAAM,EACLnH,OACAE,OAAO,CAACoF,YAAYxB,SAASC,gBAC1BoD;AAEJ,WAAO;KACNnF;KACAkF;KACAlH;KACAsF;KACAxB;KACAC;KACA;;AAGF,UAAO,CAAC/B,UAAUkF,qBAAqB;IAExC;AAED,SAAO,KAAKlE,IAAG,KAAMmD,KAAKmB,UAAUtB,gBAAgB,CAAA,IAAKC,QAAO;GAGrB,CAAA"}
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","KeyedThemeStoreConfig","Themes","K","U","Listener","themes","resolvedThemes","ThemeKeysWithSystemOption","NonSystemOptionValues","SystemOptions","PersistedState","Partial","systemOptions","ThemeStoreOptions","initialState","storage","ThemeAndOptions","Array","getThemesAndOptions","config","Object","entries","map","themeKey","themeConfig","option","getDefaultThemes","fromEntries","ThemeStore","defaultThemes","currentThemes","keyedConfig","mediaQueryCache","MediaQueryList","listeners","Set","abortController","AbortController","constructor","key","String","getThemes","getResolvedThemes","optionKey","resolveThemeOption","setThemes","updatedThemes","setThemesAndNotify","stateToPersist","toPersist","set","broadcast","updateSystemOption","ifMatch","ifNotMatch","restore","persistedState","get","subscribe","callback","add","delete","sync","watch","destroy","clear","abort","#setThemesAndNotify","listener","#resolveThemeOption","window","document","createElement","cache","mediaQuery","mediaQueryList","matchMedia","addEventListener","signal","matches","createThemeStore","restoreScript","params","forEach","flattenedConfig","handler","persistedThemes","JSON","parse","localStorage","getItem","reduce","acc","initial","systemOptionValue","currentValue","ThemeScriptParameter","createInlineThemeScript","themeScriptParameters","serializedArgs","isArray","firstOption","resolvedInitialValue","systemOption","find","Required","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\n// { [themeKey]: { [optionKey]: ThemeOption } }\ntype KeyedThemeStoreConfig<T extends ThemeStoreConfig> = {\n\t[K in keyof T]: Record<string, ThemeOption>\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 Listener<T extends ThemeStoreConfig> = (value: {\n\tthemes: Themes<T>\n\tresolvedThemes: Themes<T>\n}) => void\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 SystemOptions<T extends ThemeStoreConfig> = {\n\t[K in ThemeKeysWithSystemOption<T>]?: [\n\t\tNonSystemOptionValues<T, K>,\n\t\tNonSystemOptionValues<T, K>,\n\t]\n}\n\ntype PersistedState<T extends ThemeStoreConfig> = {\n\tthemes: Partial<Themes<T>>\n\tsystemOptions: SystemOptions<T>\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#keyedConfig: KeyedThemeStoreConfig<T>\n\n\t#systemOptions: SystemOptions<T>\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\tconst systemOptions: Record<string, [ThemeValue, ThemeValue]> = {}\n\n\t\tthis.#defaultThemes = getDefaultThemes(config)\n\n\t\tthis.#currentThemes = { ...this.#defaultThemes, ...initialState.themes }\n\n\t\tthis.#keyedConfig = Object.fromEntries(\n\t\t\tObject.entries(config).map(([themeKey, themeConfig]) => {\n\t\t\t\tconst entries = (themeConfig.options || []).map((option) => {\n\t\t\t\t\tif (typeof option === 'object') {\n\t\t\t\t\t\tif (option.media) {\n\t\t\t\t\t\t\tsystemOptions[themeKey] = [option.media[1], option.media[2]]\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn [String(option.value), option]\n\t\t\t\t\t}\n\n\t\t\t\t\treturn [String(option), { value: option }]\n\t\t\t\t})\n\n\t\t\t\treturn [themeKey, Object.fromEntries(entries)]\n\t\t\t}),\n\t\t) as KeyedThemeStoreConfig<T>\n\n\t\tthis.#systemOptions = { ...systemOptions, ...initialState.systemOptions }\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 option = this.#keyedConfig[themeKey]?.[optionKey]\n\n\t\t\t\treturn [\n\t\t\t\t\tthemeKey,\n\t\t\t\t\toption ? this.#resolveThemeOption({ themeKey, option }) : optionKey,\n\t\t\t\t]\n\t\t\t}),\n\t\t) as Themes<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\t[ifMatch, ifNotMatch]: [\n\t\t\tNonSystemOptionValues<T, K>,\n\t\t\tNonSystemOptionValues<T, K>,\n\t\t],\n\t): void => {\n\t\tthis.#systemOptions[themeKey] = [ifMatch, ifNotMatch]\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: this.#systemOptions,\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.#systemOptions = {\n\t\t\t...this.#systemOptions,\n\t\t\t...persistedState.systemOptions,\n\t\t}\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 () => {\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((persistedState) => {\n\t\t\tthis.#systemOptions = (persistedState as PersistedState<T>).systemOptions\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#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})\n\t\t}\n\t}\n\n\t#resolveThemeOption = ({\n\t\tthemeKey,\n\t\toption,\n\t}: {\n\t\tthemeKey: string\n\t\toption: ThemeOption\n\t}): string => {\n\t\tif (!option.media) return option.value\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 option.value\n\t\t}\n\n\t\tconst cache = this.#mediaQueryCache\n\n\t\tconst [mediaQuery] = option.media\n\n\t\tif (!cache[mediaQuery]) {\n\t\t\tconst mediaQueryList = window.matchMedia(mediaQuery)\n\n\t\t\tcache[mediaQuery] = mediaQueryList\n\n\t\t\tmediaQueryList.addEventListener(\n\t\t\t\t'change',\n\t\t\t\t() => {\n\t\t\t\t\tif (this.#currentThemes[themeKey] === option.value) {\n\t\t\t\t\t\tthis.#setThemesAndNotify({ ...this.#currentThemes })\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\t{ signal: this.#abortController.signal },\n\t\t\t)\n\t\t}\n\n\t\tconst [ifMatch, ifNotMatch] = this.#systemOptions[themeKey]!\n\n\t\treturn cache[mediaQuery].matches ? ifMatch : ifNotMatch\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 persistedThemes =\n\t\t\tJSON.parse(localStorage.getItem(key) || '{}').themes ?? {}\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\tifMatch,\n\t\t\t\t\t\tifNotMatch,\n\t\t\t\t\t],\n\t\t\t\t) => {\n\t\t\t\t\tconst currentValue = persistedThemes[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\tacc.resolvedThemes[themeKey] = matchMedia(mediaQuery!).matches\n\t\t\t\t\t\t\t? ifMatch!\n\t\t\t\t\t\t\t: ifNotMatch!\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>\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 {\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t\tmedia: [mediaQuery, ifMatch, ifNotMatch],\n\t\t\t\t\t} = systemOption\n\n\t\t\t\t\treturn [\n\t\t\t\t\t\tthemeKey,\n\t\t\t\t\t\tresolvedInitialValue,\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t\tmediaQuery,\n\t\t\t\t\t\tifMatch,\n\t\t\t\t\t\tifNotMatch,\n\t\t\t\t\t]\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,GAAMmB,QAAQJ,EAAI,EAAI,OAAO,CAGvDZ,IAAMC,GAAkB,CACvBc,OAAOlB,GAAMoB,QAAQL,EAAKC,KAAKK,UAAUjB,EAAM,CAAC,EAGjDE,MAAQC,GAAO,CACd,IAAMe,EAAa,IAAIZ,gBAmBvB,OAjBAQ,OAAOK,iBACN,UACCC,GAAM,CACFA,EAAEC,cAAgBP,OAAOlB,IAEzBwB,EAAET,MAAQA,GAEdR,EAAGS,KAAKC,MAAMO,EAAEE,SAAU,CAAC,EAE5B,CACCC,OAAQC,YAAYC,IAAI,CACvBpB,EAAgBkB,OAChBL,EAAWK,OACX,CAAA,CAEH,CAAC,KAEY,CACZL,EAAWQ,OAAO,GAGpB,EAiBUC,GAEP,CAAEhB,UACC,CAAEN,qBAAsB,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CAACA,EAAOhC,MACxC,CAED,GAAI8G,EAAc,CACjB,GAAM,CACLhH,QACAE,MAAO,CAACiF,EAAYrB,EAASC,IAC1BiD,EAEJ,MAAO,CACNhF,EACA+E,EACA/G,EACAmF,EACArB,EACAC,EACA,CAGF,MAAO,CAAC/B,EAAU+E,EAAqB,EAExC,CAED,MAAO,KAAK/D,EAAG,KAAMgD,KAAKmB,UAAUtB,EAAgB,CAAA,IAAKC,EAAO,IAGrB,CAAA"}
package/dist/react.js CHANGED
@@ -1,55 +1,2 @@
1
- import { c } from "react-compiler-runtime";
2
- import * as React from "react";
3
- //#region src/react.ts
4
- /**
5
- * Subscribes a React component to a theme store.
6
- * @example
7
- * ```tsx
8
- * export function ThemeToggle() {
9
- * const { themes, resolvedThemes, setThemes } = useResonare(store)
10
- *
11
- * // ...
12
- * }
13
- * ```
14
- */
15
- function useResonare(store) {
16
- const $ = c(13);
17
- const { destroy, getResolvedThemes, getThemes, restore, setThemes, subscribe, sync, toPersist, updateSystemOption } = store;
18
- const themes = React.useSyncExternalStore(subscribe, getThemes, getThemes);
19
- let t0;
20
- if ($[0] !== getResolvedThemes || $[1] !== themes) {
21
- t0 = getResolvedThemes(themes);
22
- $[0] = getResolvedThemes;
23
- $[1] = themes;
24
- $[2] = t0;
25
- } else t0 = $[2];
26
- let t1;
27
- if ($[3] !== destroy || $[4] !== restore || $[5] !== setThemes || $[6] !== subscribe || $[7] !== sync || $[8] !== t0 || $[9] !== themes || $[10] !== toPersist || $[11] !== updateSystemOption) {
28
- t1 = {
29
- themes,
30
- resolvedThemes: t0,
31
- destroy,
32
- restore,
33
- setThemes,
34
- subscribe,
35
- sync,
36
- toPersist,
37
- updateSystemOption
38
- };
39
- $[3] = destroy;
40
- $[4] = restore;
41
- $[5] = setThemes;
42
- $[6] = subscribe;
43
- $[7] = sync;
44
- $[8] = t0;
45
- $[9] = themes;
46
- $[10] = toPersist;
47
- $[11] = updateSystemOption;
48
- $[12] = t1;
49
- } else t1 = $[12];
50
- return t1;
51
- }
52
- //#endregion
53
- export { useResonare };
54
-
1
+ import{c as e}from"react-compiler-runtime";import*as t from"react";function n(n){let r=e(13),{destroy:i,getResolvedThemes:a,getThemes:o,restore:s,setThemes:c,subscribe:l,sync:u,toPersist:d,updateSystemOption:f}=n,p=t.useSyncExternalStore(l,o,o),m;r[0]!==a||r[1]!==p?(m=a(p),r[0]=a,r[1]=p,r[2]=m):m=r[2];let h;return r[3]!==i||r[4]!==s||r[5]!==c||r[6]!==l||r[7]!==u||r[8]!==m||r[9]!==p||r[10]!==d||r[11]!==f?(h={themes:p,resolvedThemes:m,destroy:i,restore:s,setThemes:c,subscribe:l,sync:u,toPersist:d,updateSystemOption:f},r[3]=i,r[4]=s,r[5]=c,r[6]=l,r[7]=u,r[8]=m,r[9]=p,r[10]=d,r[11]=f,r[12]=h):h=r[12],h}export{n as useResonare};
55
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","getResolvedThemes","getThemes","restore","setThemes","subscribe","sync","toPersist","updateSystemOption","themes","useSyncExternalStore","t0","t1","resolvedThemes"],"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, setThemes } = useResonare(store)\n *\n * // ...\n * }\n * ```\n */\nexport function useResonare<T extends ThemeStoreConfig>(store: ThemeStore<T>) {\n\tconst {\n\t\tdestroy,\n\t\tgetResolvedThemes,\n\t\tgetThemes,\n\t\trestore,\n\t\tsetThemes,\n\t\tsubscribe,\n\t\tsync,\n\t\ttoPersist,\n\t\tupdateSystemOption,\n\t} = store\n\n\tconst themes = React.useSyncExternalStore(subscribe, getThemes, getThemes)\n\n\treturn {\n\t\tthemes,\n\t\t// @ts-expect-error - workaround for React compiler as getResolvedThemes is not called again without 'themes' dependency\n\t\tresolvedThemes: getResolvedThemes(themes),\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":";;;;;;;;;;;;;;AAcA,SAAOG,YAAAC,OAAA;CAAA,MAAAC,IAAAC,EAAA,GAAA;CACN,MAAA,EAAAC,SAAAC,mBAAAC,WAAAC,SAAAC,WAAAC,WAAAC,MAAAC,WAAAC,uBAUIX;CAEJ,MAAAY,SAAehB,MAAKiB,qBAAsBL,WAAWH,WAAWA,UAAU;CAAA,IAAAS;AAAA,KAAAb,EAAA,OAAAG,qBAAAH,EAAA,OAAAW,QAAA;AAKzDE,OAAAV,kBAAkBQ,OAAO;AAAAX,IAAA,KAAAG;AAAAH,IAAA,KAAAW;AAAAX,IAAA,KAAAa;OAAAA,MAAAb,EAAA;CAAA,IAAAc;AAAA,KAAAd,EAAA,OAAAE,WAAAF,EAAA,OAAAK,WAAAL,EAAA,OAAAM,aAAAN,EAAA,OAAAO,aAAAP,EAAA,OAAAQ,QAAAR,EAAA,OAAAa,MAAAb,EAAA,OAAAW,UAAAX,EAAA,QAAAS,aAAAT,EAAA,QAAAU,oBAAA;AAHnCI,OAAA;GAAAH;GAAAI,gBAGUF;GAAyBX;GAAAG;GAAAC;GAAAC;GAAAC;GAAAC;GAAAC;GAQzC;AAAAV,IAAA,KAAAE;AAAAF,IAAA,KAAAK;AAAAL,IAAA,KAAAM;AAAAN,IAAA,KAAAO;AAAAP,IAAA,KAAAQ;AAAAR,IAAA,KAAAa;AAAAb,IAAA,KAAAW;AAAAX,IAAA,MAAAS;AAAAT,IAAA,MAAAU;AAAAV,IAAA,MAAAc;OAAAA,MAAAd,EAAA;AAAA,QAXMc"}
1
+ {"version":3,"file":"react.js","names":["React","ThemeStore","ThemeStoreConfig","useResonare","store","$","_c","destroy","getResolvedThemes","getThemes","restore","setThemes","subscribe","sync","toPersist","updateSystemOption","themes","useSyncExternalStore","t0","t1","resolvedThemes"],"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, setThemes } = useResonare(store)\n *\n * // ...\n * }\n * ```\n */\nexport function useResonare<T extends ThemeStoreConfig>(store: ThemeStore<T>) {\n\tconst {\n\t\tdestroy,\n\t\tgetResolvedThemes,\n\t\tgetThemes,\n\t\trestore,\n\t\tsetThemes,\n\t\tsubscribe,\n\t\tsync,\n\t\ttoPersist,\n\t\tupdateSystemOption,\n\t} = store\n\n\tconst themes = React.useSyncExternalStore(subscribe, getThemes, getThemes)\n\n\treturn {\n\t\tthemes,\n\t\t// @ts-expect-error - workaround for React compiler as getResolvedThemes is not called again without 'themes' dependency\n\t\tresolvedThemes: getResolvedThemes(themes),\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,GAAA,CACN,CAAAC,UAAAC,oBAAAC,YAAAC,UAAAC,YAAAC,YAAAC,OAAAC,YAAAC,sBAUIX,EAEJY,EAAehB,EAAKiB,qBAAsBL,EAAWH,EAAWA,EAAU,CAAAS,EAAAb,EAAA,KAAAG,GAAAH,EAAA,KAAAW,GAKzDE,EAAAV,EAAkBQ,EAAO,CAAAX,EAAA,GAAAG,EAAAH,EAAA,GAAAW,EAAAX,EAAA,GAAAa,GAAAA,EAAAb,EAAA,GAAA,IAAAc,EAQzC,OARyCd,EAAA,KAAAE,GAAAF,EAAA,KAAAK,GAAAL,EAAA,KAAAM,GAAAN,EAAA,KAAAO,GAAAP,EAAA,KAAAQ,GAAAR,EAAA,KAAAa,GAAAb,EAAA,KAAAW,GAAAX,EAAA,MAAAS,GAAAT,EAAA,MAAAU,GAHnCI,EAAA,CAAAH,SAAAI,eAGUF,EAAyBX,UAAAG,UAAAC,YAAAC,YAAAC,OAAAC,YAAAC,qBAQzC,CAAAV,EAAA,GAAAE,EAAAF,EAAA,GAAAK,EAAAL,EAAA,GAAAM,EAAAN,EAAA,GAAAO,EAAAP,EAAA,GAAAQ,EAAAR,EAAA,GAAAa,EAAAb,EAAA,GAAAW,EAAAX,EAAA,IAAAS,EAAAT,EAAA,IAAAU,EAAAV,EAAA,IAAAc,GAAAA,EAAAd,EAAA,IAXMc"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resonare",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Resonare",
5
5
  "keywords": [
6
6
  "theming",
@@ -64,7 +64,7 @@
64
64
  "scripts": {
65
65
  "build": "tsdown",
66
66
  "dev": "tsdown --watch",
67
- "size": "cross-env CI=true pnpm build && size-limit",
67
+ "size": "pnpm build && size-limit",
68
68
  "test": "vitest"
69
69
  }
70
70
  }