resonare 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +6 -6
- package/dist/index.js +8 -9
- package/dist/react.d.ts +7 -7
- package/dist/resonare.iife.min.js +2 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -22,24 +22,24 @@ type ThemeOption = {
|
|
|
22
22
|
media?: [string, string, string];
|
|
23
23
|
};
|
|
24
24
|
type ThemeConfig = Record<string, {
|
|
25
|
-
options:
|
|
25
|
+
options: ReadonlyArray<string | ThemeOption>;
|
|
26
26
|
defaultOption?: string;
|
|
27
27
|
}>;
|
|
28
|
-
type Themes<T extends ThemeConfig> = { [K in keyof T]: T[K]['options'] extends
|
|
28
|
+
type Themes<T extends ThemeConfig> = { [K in keyof T]: T[K]['options'] extends ReadonlyArray<infer U> ? U extends string ? U : U extends ThemeOption ? U['value'] : never : never };
|
|
29
29
|
type Listener<T extends ThemeConfig> = (value: {
|
|
30
30
|
themes: Themes<T>;
|
|
31
31
|
resolvedThemes: Themes<T>;
|
|
32
32
|
}) => void;
|
|
33
|
-
type ThemeKeysWithSystemOption<T extends ThemeConfig> = { [K in keyof T]: T[K]['options'] extends
|
|
33
|
+
type ThemeKeysWithSystemOption<T extends ThemeConfig> = { [K in keyof T]: T[K]['options'] extends ReadonlyArray<infer U> ? U extends {
|
|
34
34
|
media: [string, string, string];
|
|
35
35
|
} ? K : never : never }[keyof T];
|
|
36
|
-
type NonSystemOptionValues<T extends ThemeConfig, K$1 extends keyof T> = T[K$1]['options'] extends
|
|
36
|
+
type NonSystemOptionValues<T extends ThemeConfig, K$1 extends keyof T> = T[K$1]['options'] extends ReadonlyArray<infer U> ? U extends string ? U : U extends ThemeOption ? U extends {
|
|
37
37
|
media: [string, string, string];
|
|
38
38
|
} ? never : U['value'] : never : never;
|
|
39
39
|
type SystemOptions<T extends ThemeConfig> = { [K in ThemeKeysWithSystemOption<T>]?: [NonSystemOptionValues<T, K>, NonSystemOptionValues<T, K>] };
|
|
40
40
|
type PersistedState<T extends ThemeConfig> = {
|
|
41
41
|
version: 1;
|
|
42
|
-
themes: Themes<T
|
|
42
|
+
themes: Partial<Themes<T>>;
|
|
43
43
|
systemOptions: SystemOptions<T>;
|
|
44
44
|
};
|
|
45
45
|
type ThemeStoreOptions<T extends ThemeConfig> = {
|
|
@@ -48,7 +48,7 @@ type ThemeStoreOptions<T extends ThemeConfig> = {
|
|
|
48
48
|
initialState?: Partial<PersistedState<T>>;
|
|
49
49
|
storage?: StorageAdapterCreate;
|
|
50
50
|
};
|
|
51
|
-
type ThemeAndOptions<T extends ThemeConfig> = Array<{ [K in keyof T]: [K, Array<T[K]['options'] extends
|
|
51
|
+
type ThemeAndOptions<T extends ThemeConfig> = Array<{ [K in keyof T]: [K, Array<T[K]['options'] extends ReadonlyArray<infer U> ? U extends string ? U : U extends ThemeOption ? U['value'] : never : never>] }[keyof T]>;
|
|
52
52
|
declare function getThemesAndOptions<T extends ThemeConfig>(config: T): ThemeAndOptions<T>;
|
|
53
53
|
declare function getDefaultThemes<T extends ThemeConfig>(config: T): Themes<T>;
|
|
54
54
|
declare class ThemeStore<T extends ThemeConfig> {
|
package/dist/index.js
CHANGED
|
@@ -57,7 +57,6 @@ const memoryStorageAdapter = () => {
|
|
|
57
57
|
|
|
58
58
|
//#endregion
|
|
59
59
|
//#region src/index.ts
|
|
60
|
-
const PACKAGE_NAME = "resonare";
|
|
61
60
|
function getThemesAndOptions(config) {
|
|
62
61
|
return Object.entries(config).map(([themeKey, { options }]) => {
|
|
63
62
|
return [themeKey, options.map((option) => typeof option === "string" ? option : option.value)];
|
|
@@ -78,7 +77,7 @@ var ThemeStore = class {
|
|
|
78
77
|
#listeners = /* @__PURE__ */ new Set();
|
|
79
78
|
#mediaQueryCache;
|
|
80
79
|
#abortController = new AbortController();
|
|
81
|
-
constructor({ key =
|
|
80
|
+
constructor({ key = name, config, initialState = {}, storage = localStorageAdapter() }) {
|
|
82
81
|
const keyedConfig = {};
|
|
83
82
|
const systemOptions = { ...initialState.systemOptions };
|
|
84
83
|
Object.entries(config).forEach(([themeKey, { options }]) => {
|
|
@@ -164,7 +163,7 @@ var ThemeStore = class {
|
|
|
164
163
|
};
|
|
165
164
|
sync = () => {
|
|
166
165
|
if (!this.#storage.watch) {
|
|
167
|
-
console.warn(`[${
|
|
166
|
+
console.warn(`[${name}] No watch method was provided for storage.`);
|
|
168
167
|
return;
|
|
169
168
|
}
|
|
170
169
|
return this.#storage.watch((key, persistedState) => {
|
|
@@ -193,7 +192,7 @@ var ThemeStore = class {
|
|
|
193
192
|
#resolveThemeOption = ({ themeKey, option }) => {
|
|
194
193
|
if (!option.media) return option.value;
|
|
195
194
|
if (!isClient) {
|
|
196
|
-
console.warn(`[${
|
|
195
|
+
console.warn(`[${name}] Option with key "media" cannot be resolved in server environment.`);
|
|
197
196
|
return option.value;
|
|
198
197
|
}
|
|
199
198
|
const [mediaQuery] = option.media;
|
|
@@ -217,7 +216,7 @@ var ThemeStore = class {
|
|
|
217
216
|
var Registry = class {
|
|
218
217
|
#registry = /* @__PURE__ */ new Map();
|
|
219
218
|
create = (options) => {
|
|
220
|
-
const storeKey = options.key ||
|
|
219
|
+
const storeKey = options.key || name;
|
|
221
220
|
let themeStore = this.#registry.get(storeKey);
|
|
222
221
|
if (!themeStore) {
|
|
223
222
|
themeStore = new ThemeStore(options);
|
|
@@ -226,13 +225,13 @@ var Registry = class {
|
|
|
226
225
|
return themeStore;
|
|
227
226
|
};
|
|
228
227
|
get = (key) => {
|
|
229
|
-
const storeKey = key ||
|
|
230
|
-
if (!this.#registry.has(storeKey)) throw new Error(`[${
|
|
228
|
+
const storeKey = key || name;
|
|
229
|
+
if (!this.#registry.has(storeKey)) throw new Error(`[${name}] Theme store with key '${storeKey}' could not be found. Please run \`createThemeStore\` with key '${storeKey}' first.`);
|
|
231
230
|
return this.#registry.get(storeKey);
|
|
232
231
|
};
|
|
233
232
|
destroy = (key) => {
|
|
234
|
-
const storeKey = key ||
|
|
235
|
-
if (!this.#registry.has(storeKey)) throw new Error(`[${
|
|
233
|
+
const storeKey = key || name;
|
|
234
|
+
if (!this.#registry.has(storeKey)) throw new Error(`[${name}] Theme store with key '${storeKey}' could not be found. Please run \`createThemeStore\` with key '${storeKey}' first.`);
|
|
236
235
|
this.#registry.get(storeKey).___destroy();
|
|
237
236
|
this.#registry.delete(storeKey);
|
|
238
237
|
};
|
package/dist/react.d.ts
CHANGED
|
@@ -9,14 +9,14 @@ declare function useResonare<T extends ThemeConfig>(getStore: () => ThemeStore<T
|
|
|
9
9
|
themes: Themes<T>;
|
|
10
10
|
resolvedThemes: Themes<T>;
|
|
11
11
|
setThemes: (themes: Partial<Themes<T>> | ((currentThemes: Themes<T>) => Partial<Themes<T>>)) => Promise<void>;
|
|
12
|
-
updateSystemOption: <K extends { [K_1 in keyof T]: T[K_1]["options"] extends
|
|
12
|
+
updateSystemOption: <K extends { [K_1 in keyof T]: T[K_1]["options"] extends readonly (infer U)[] ? U extends {
|
|
13
13
|
media: [string, string, string];
|
|
14
|
-
} ? K_1 : never : never }[keyof T]>(themeKey: K, [ifMatch, ifNotMatch]: [T[K]["options"] extends
|
|
14
|
+
} ? K_1 : never : never }[keyof T]>(themeKey: K, [ifMatch, ifNotMatch]: [T[K]["options"] extends readonly (infer U)[] ? U extends string ? U : U extends {
|
|
15
15
|
value: string;
|
|
16
16
|
media?: [string, string, string];
|
|
17
17
|
} ? U extends {
|
|
18
18
|
media: [string, string, string];
|
|
19
|
-
} ? never : U["value"] : never : never, T[K]["options"] extends
|
|
19
|
+
} ? never : U["value"] : never : never, T[K]["options"] extends readonly (infer U)[] ? U extends string ? U : U extends {
|
|
20
20
|
value: string;
|
|
21
21
|
media?: [string, string, string];
|
|
22
22
|
} ? U extends {
|
|
@@ -24,15 +24,15 @@ declare function useResonare<T extends ThemeConfig>(getStore: () => ThemeStore<T
|
|
|
24
24
|
} ? never : U["value"] : never : never]) => void;
|
|
25
25
|
getStateToPersist: () => {
|
|
26
26
|
version: 1;
|
|
27
|
-
themes: Themes<T
|
|
28
|
-
systemOptions: { [K_1 in { [K in keyof T]: T[K]["options"] extends
|
|
27
|
+
themes: Partial<Themes<T>>;
|
|
28
|
+
systemOptions: { [K_1 in { [K in keyof T]: T[K]["options"] extends readonly (infer U)[] ? U extends {
|
|
29
29
|
media: [string, string, string];
|
|
30
|
-
} ? K : never : never }[keyof T]]?: [T[K_1]["options"] extends
|
|
30
|
+
} ? K : never : never }[keyof T]]?: [T[K_1]["options"] extends readonly (infer U)[] ? U extends string ? U : U extends {
|
|
31
31
|
value: string;
|
|
32
32
|
media?: [string, string, string];
|
|
33
33
|
} ? U extends {
|
|
34
34
|
media: [string, string, string];
|
|
35
|
-
} ? never : U["value"] : never : never, T[K_1]["options"] extends
|
|
35
|
+
} ? never : U["value"] : never : never, T[K_1]["options"] extends readonly (infer U)[] ? U extends string ? U : U extends {
|
|
36
36
|
value: string;
|
|
37
37
|
media?: [string, string, string];
|
|
38
38
|
} ? U extends {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* resonare v0.0.
|
|
2
|
+
* resonare v0.0.4
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
var resonare=(function(e){var t=`resonare`;let n=({storageType:e=`localStorage`}={})=>({abortController:t})=>({getItem:t=>JSON.parse(window[e].getItem(t)||`null`),setItem:(t,n)=>{window[e].setItem(t,JSON.stringify(n))},watch:n=>{let r=new AbortController;return window.addEventListener(`storage`,t=>{t.storageArea===window[e]&&n(t.key,JSON.parse(t.newValue))},{signal:AbortSignal.any([t.signal,r.signal])}),()=>{r.abort()}}}),r=()=>({abortController:e})=>{let n=new Map,r=new BroadcastChannel(t);return{getItem:e=>n.get(e)||null,setItem:(e,t)=>{n.set(e,t)},broadcast:(e,t)=>{r.postMessage({key:e,value:t})},watch:t=>{let n=new AbortController;return r.addEventListener(`message`,e=>{t(e.data.key,e.data.value)},{signal:AbortSignal.any([e.signal,n.signal])}),()=>{n.abort()}}}}
|
|
7
|
+
var resonare=(function(e){var t=`resonare`;let n=({storageType:e=`localStorage`}={})=>({abortController:t})=>({getItem:t=>JSON.parse(window[e].getItem(t)||`null`),setItem:(t,n)=>{window[e].setItem(t,JSON.stringify(n))},watch:n=>{let r=new AbortController;return window.addEventListener(`storage`,t=>{t.storageArea===window[e]&&n(t.key,JSON.parse(t.newValue))},{signal:AbortSignal.any([t.signal,r.signal])}),()=>{r.abort()}}}),r=()=>({abortController:e})=>{let n=new Map,r=new BroadcastChannel(t);return{getItem:e=>n.get(e)||null,setItem:(e,t)=>{n.set(e,t)},broadcast:(e,t)=>{r.postMessage({key:e,value:t})},watch:t=>{let n=new AbortController;return r.addEventListener(`message`,e=>{t(e.data.key,e.data.value)},{signal:AbortSignal.any([e.signal,n.signal])}),()=>{n.abort()}}}};function i(e){return Object.entries(e).map(([e,{options:t}])=>[e,t.map(e=>typeof e==`string`?e:e.value)])}function a(e){return Object.fromEntries(Object.entries(e).map(([e,{options:t,defaultOption:n}])=>[e,n??(typeof t[0]==`string`?t[0]:t[0].value)]))}let o=typeof window<`u`&&window.document!==void 0&&window.document.createElement!==void 0;var s=class{#e;#t;#n;#r;#i;#a=new Set;#o;#s=new AbortController;constructor({key:e=t,config:r,initialState:i={},storage:o=n()}){let s={},c={...i.systemOptions};Object.entries(r).forEach(([e,{options:t}])=>{s[e]=s[e]||{},t.forEach(t=>{typeof t==`string`?s[e][t]={value:t}:(t.media&&!Object.hasOwn(c,e)&&(c[e]=[t.media[1],t.media[2]]),s[e][t.value]=t)})}),this.#n={key:e,config:s,storage:o},this.#r=c,this.#e=a(r),this.#t={...this.#e,...i.themes},this.#i=this.#n.storage({abortController:this.#s}),this.#o={}}getThemes=()=>this.#t;getResolvedThemes=()=>this.#l();setThemes=async e=>{let t=typeof e==`function`?e(this.#t):e;this.#c({...this.#t,...t});let n=this.getStateToPersist();await this.#i.setItem(this.#n.key,n),this.#i.broadcast?.(this.#n.key,n)};updateSystemOption=(e,[t,n])=>{this.#r[e]=[t,n],this.setThemes({...this.#t})};getStateToPersist=()=>({version:1,themes:this.#t,systemOptions:this.#r});restore=async()=>{let e=await this.#i.getItem(this.#n.key);if(!e){this.#c({...this.#e});return}Object.hasOwn(e,`version`)||(e={version:1,themes:e,systemOptions:this.#r}),this.#r={...this.#r,...e.systemOptions},this.#c({...this.#e,...e.themes})};subscribe=(e,{immediate:t=!1}={})=>(t&&e({themes:this.#t,resolvedThemes:this.#l()}),this.#a.add(e),()=>{this.#a.delete(e)});sync=()=>{if(!this.#i.watch){console.warn(`[${t}] No watch method was provided for storage.`);return}return this.#i.watch((e,t)=>{e===this.#n.key&&(this.#r=t.systemOptions,this.#c(t.themes))})};___destroy=()=>{this.#a.clear(),this.#s.abort()};#c=e=>{this.#t=e,this.#d()};#l=()=>Object.fromEntries(Object.entries(this.#t).map(([e,t])=>{let n=this.#n.config[e][t];return[e,this.#u({themeKey:e,option:n})]}));#u=({themeKey:e,option:n})=>{if(!n.media)return n.value;if(!o)return console.warn(`[${t}] Option with key "media" cannot be resolved in server environment.`),n.value;let[r]=n.media;if(!this.#o[r]){let t=window.matchMedia(r);this.#o[r]=t,t.addEventListener(`change`,()=>{this.#t[e]===n.value&&this.#c({...this.#t})},{signal:this.#s.signal})}let[i,a]=this.#r[e];return this.#o[r].matches?i:a};#d=()=>{for(let e of this.#a)e({themes:this.#t,resolvedThemes:this.#l()})}};let c=new class{#e=new Map;create=e=>{let n=e.key||t,r=this.#e.get(n);return r||(r=new s(e),this.#e.set(n,r)),r};get=e=>{let n=e||t;if(!this.#e.has(n))throw Error(`[${t}] Theme store with key '${n}' could not be found. Please run \`createThemeStore\` with key '${n}' first.`);return this.#e.get(n)};destroy=e=>{let n=e||t;if(!this.#e.has(n))throw Error(`[${t}] Theme store with key '${n}' could not be found. Please run \`createThemeStore\` with key '${n}' first.`);this.#e.get(n).___destroy(),this.#e.delete(n)}},l=c.create,u=c.get,d=c.destroy;return e.createThemeStore=l,e.destroyThemeStore=d,e.getDefaultThemes=a,e.getThemeStore=u,e.getThemesAndOptions=i,e.localStorageAdapter=n,e.memoryStorageAdapter=r,e})({});
|