resonare 0.1.6 → 0.1.7
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 +5 -1
- package/dist/index.d.ts +14 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/react.d.ts +26 -18
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +1 -1
- package/dist/react.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Resonare [](https://www.npmjs.com/package/resonare)
|
|
2
2
|
|
|
3
|
-
A state store for multi-dimensional themes and user preferences. [Check out the demo](https://resonare.
|
|
3
|
+
A state store for multi-dimensional themes and user preferences. [Check out the demo](https://resonare.phuoccss.workers.dev).
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
@@ -143,6 +143,10 @@ themeStore.getThemes()
|
|
|
143
143
|
// e.g.: { colorScheme: 'dark', contrast: 'standard', sidebarWidth: 240 }
|
|
144
144
|
themeStore.getResolvedThemes()
|
|
145
145
|
|
|
146
|
+
// get resolved system options
|
|
147
|
+
// e.g.: { colorScheme: 'dark' }
|
|
148
|
+
themeStore.getResolvedSystemThemes()
|
|
149
|
+
|
|
146
150
|
// update theme
|
|
147
151
|
themeStore.setThemes({ colorScheme: 'light', sidebarWidth: 280 })
|
|
148
152
|
|
package/dist/index.d.ts
CHANGED
|
@@ -66,10 +66,6 @@ type Themes<T extends ThemeStoreConfig> = { [K in keyof T]: T[K] extends {
|
|
|
66
66
|
} ? U extends ThemeOption ? U['value'] : U : T[K] extends {
|
|
67
67
|
initialValue: infer U;
|
|
68
68
|
} ? U extends string ? string : U extends number ? number : boolean : never };
|
|
69
|
-
type Listener<T extends ThemeStoreConfig> = (value: {
|
|
70
|
-
themes: Themes<T>;
|
|
71
|
-
resolvedThemes: Themes<T>;
|
|
72
|
-
}) => void;
|
|
73
69
|
type ThemeKeysWithSystemOption<T extends ThemeStoreConfig> = { [K in keyof T]: T[K] extends {
|
|
74
70
|
options: ReadonlyArray<infer U>;
|
|
75
71
|
} ? U extends {
|
|
@@ -80,10 +76,19 @@ type NonSystemOptionValues<T extends ThemeStoreConfig, K extends keyof T> = T[K]
|
|
|
80
76
|
} ? U extends ThemeValue ? U : U extends ThemeOption ? U extends {
|
|
81
77
|
media: [string, string, string];
|
|
82
78
|
} ? never : U['value'] : never : never;
|
|
83
|
-
type
|
|
79
|
+
type ResolvedSystemThemes<T extends ThemeStoreConfig> = { [K in ThemeKeysWithSystemOption<T>]: NonSystemOptionValues<T, K> };
|
|
80
|
+
type Listener<T extends ThemeStoreConfig, O extends boolean = false> = (value: O extends true ? {
|
|
81
|
+
themes: Themes<T>;
|
|
82
|
+
resolvedThemes: Themes<T>;
|
|
83
|
+
} : {
|
|
84
|
+
themes: Themes<T>;
|
|
85
|
+
resolvedThemes: Themes<T>;
|
|
86
|
+
resolvedSystemThemes: ResolvedSystemThemes<T>;
|
|
87
|
+
}) => void;
|
|
88
|
+
type PersistedSystemOptionPairs = Record<string, [string, string]>;
|
|
84
89
|
type PersistedState<T extends ThemeStoreConfig> = {
|
|
85
90
|
themes: Partial<Themes<T>>;
|
|
86
|
-
systemOptions:
|
|
91
|
+
systemOptions: PersistedSystemOptionPairs;
|
|
87
92
|
};
|
|
88
93
|
type ThemeStoreOptions<T extends ThemeStoreConfig> = {
|
|
89
94
|
initialState?: Partial<PersistedState<T>>;
|
|
@@ -102,8 +107,9 @@ declare class ThemeStore<T extends ThemeStoreConfig> {
|
|
|
102
107
|
}?: ThemeStoreOptions<T>);
|
|
103
108
|
getThemes: () => Themes<T>;
|
|
104
109
|
getResolvedThemes: () => Themes<T>;
|
|
110
|
+
getResolvedSystemThemes: () => ResolvedSystemThemes<T>;
|
|
105
111
|
setThemes: (themes: Partial<Themes<T>> | ((currentThemes: Themes<T>) => Partial<Themes<T>>)) => void;
|
|
106
|
-
updateSystemOption: <K extends ThemeKeysWithSystemOption<T>>(themeKey: K,
|
|
112
|
+
updateSystemOption: <K extends ThemeKeysWithSystemOption<T>>(themeKey: K, pair: [NonSystemOptionValues<T, K>, NonSystemOptionValues<T, K>]) => void;
|
|
107
113
|
toPersist: () => PersistedState<T>;
|
|
108
114
|
restore: () => void;
|
|
109
115
|
subscribe: (callback: Listener<T>) => (() => void);
|
|
@@ -115,7 +121,7 @@ declare function createThemeStore<T extends ThemeStoreConfig>(config: T, options
|
|
|
115
121
|
type ThemeScriptParameter = {
|
|
116
122
|
/** `localStorage` key; defaults to the 'resonare'. */key?: string;
|
|
117
123
|
config: ThemeStoreConfig;
|
|
118
|
-
handler: Listener<any>;
|
|
124
|
+
handler: Listener<any, true>;
|
|
119
125
|
};
|
|
120
126
|
/**
|
|
121
127
|
* Creates an IIFE script string that reads persisted themes from `localStorage` and runs your handlers immediately.
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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,
|
|
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,OA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elD,kBAAA,aAAgC,yBAAA,CAA0B,CAAA,GACzD,QAAA,EAAU,CAAA,EACV,IAAA,GAAO,qBAAA,CAAsB,CAAA,EAAG,CAAA,GAAI,qBAAA,CAAsB,CAAA,EAAG,CAAA;EAO9D,SAAA,QAAgB,cAAA,CAAe,CAAA;EAY/B,OAAA;EAgBA,SAAA,GAAa,QAAA,EAAU,QAAA,CAAS,CAAA;EAMhC,IAAA;EAvRiB;EAoSjB,OAAA;AAAA;AAAA,iBA0De,gBAAA,WAA2B,gBAAA,EAC1C,MAAA,EAAQ,CAAA,EACR,OAAA,GAAS,iBAAA,CAAkB,CAAA,IACzB,UAAA,CAAW,CAAA;AAAA,KAuDF,oBAAA;EArZI,sDAuZf,GAAA;EACA,MAAA,EAAQ,gBAAA;EACR,OAAA,EAAS,QAAQ;AAAA;;;;;;;;;;;;;;;;;;;AApZW;AAE7B;;;;iBA4agB,uBAAA,CACf,qBAAA,EAAuB,oBAAA,GAAuB,KAAA,CAAM,oBAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
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
|
|
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=new Set;#o=new AbortController;constructor(n,{initialState:r={},storage:a=t({key:e})}={}){this.#e=i(n),this.#t={...this.#e,...r.themes},this.#n=Object.fromEntries(Object.entries(n).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}getThemes=()=>this.#t;getResolvedThemes=()=>Object.fromEntries(Object.entries(this.#t).map(([e,t])=>{let n=this.#n[e];return[e,n&&t===n.value?this.#l(e):t]}));getResolvedSystemThemes=()=>Object.fromEntries(Object.keys(this.#n).map(e=>[e,this.#l(e)]));setThemes=e=>{let t=typeof e==`function`?e(this.#t):e;this.#c({...this.#t,...t});let n=this.toPersist();this.#r&&(this.#r.set(n),this.#r.broadcast?.(n))};updateSystemOption=(e,t)=>{this.#n[e].pair=t,this.setThemes({...this.#t})};toPersist=()=>({themes:this.#t,systemOptions:Object.fromEntries(Object.entries(this.#n).map(([e,{pair:t}])=>[e,t]))});restore=()=>{let e=this.#r?.get();if(!e){this.#c({...this.#e});return}this.#s(e.systemOptions),this.#c({...this.#e,...e.themes})};subscribe=e=>(this.#a.add(e),()=>this.#a.delete(e));sync=()=>{if(this.#r?.watch)return this.#r.watch(e=>{this.#s(e.systemOptions),this.#c(e.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=e=>{this.#t=e;for(let e of this.#a)e({themes:this.#t,resolvedThemes:this.getResolvedThemes(),resolvedSystemThemes:this.getResolvedSystemThemes()})};#l=e=>{let{value:t,mediaQuery:n,pair:r}=this.#n[e];return typeof window<`u`&&window.document!==void 0&&window.document.createElement!==void 0?(this.#i[n]||(this.#i[n]=window.matchMedia(n),this.#i[n].addEventListener(`change`,()=>{this.#c({...this.#t})},{signal:this.#o.signal})),this.#i[n].matches?r[0]:r[1]):t}};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,c===i){let n=r.systemOptions?.[t]??[o,s];e.resolvedThemes[t]=matchMedia(a).matches?n[0]:n[1]}return 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}=a;return[e,i,t,...n]}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};
|
|
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","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,QA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,EACfC,EACC,CA0CD,MAAO,IAAIjB,EAAa,MAxCvBhE,MAAMmF,QAAQF,EAAsB,CACjCA,EACA,CAACA,EAAsB,EACzB5E,KAAK,CAAEiB,MAAMvD,EAAcmC,SAAQkE,aAAc,CAClD,IAAMD,EAAkBhE,OAAOC,QAAQF,EAAO,CAACG,KAC7C,CAACC,EAAU,CAAE5B,UAASE,mBAAoB,CAC1C,IAAMwG,EAAc1G,IAAU,GAExB2G,EACLzG,IACC,OAAOwG,GAAgB,SAAWA,EAAY9G,MAAQ8G,GAElDE,EAAe5G,GAAS6G,KAC5B/E,GACA,OAAOA,GAAW,UAAY,CAAC,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"}
|
|
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,UAC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,SAASiB,IAAayE,EAIrD,GAFAD,EAAIxF,eAAegB,GAAYwE,EAAIzF,OAAOiB,GAAY2E,EAElDA,IAAiBD,EAAmB,CACvC,IAAM9F,EAAOsF,EAAU7E,gBAAgBW,IAAa,CACnDoB,EACAC,CAAoB,EAGrBmD,EAAIxF,eAAegB,GAAYyD,WAAW9E,CAAW,CAAC,CAACiF,QACpDhF,EAAK,GACLA,EAAK,EACT,CAEA,OAAO4F,CACR,EACA,CAAEzF,OAAQ,CAAC,EAAGC,eAAgB,CAAC,CAAE,CAClC,CACD,CACD,CAAC,CACF,EAgCA,SAAgB6F,EACfC,EACC,CAgCD,MAAO,IAAIhB,EAAa,MA9BvBpE,MAAMsF,QAAQF,CAAqB,EAChCA,EACA,CAACA,CAAqB,EAAA,CACxB/E,KAAK,CAAEgB,MAAM3D,EAAcwC,SAAQqE,aAAc,CAClD,IAAMD,EAAkBnE,OAAOC,QAAQF,CAAM,CAAC,CAACG,KAC7C,CAACC,EAAU,CAAEjC,UAASE,mBAAoB,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"}
|
package/dist/react.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { ThemeStore, ThemeStoreConfig, Themes } from "./index.js";
|
|
|
6
6
|
* @example
|
|
7
7
|
* ```tsx
|
|
8
8
|
* export function ThemeToggle() {
|
|
9
|
-
* const { themes, resolvedThemes, setThemes } = useResonare(store)
|
|
9
|
+
* const { themes, resolvedThemes, resolvedSystemThemes, setThemes } = useResonare(store)
|
|
10
10
|
*
|
|
11
11
|
* // ...
|
|
12
12
|
* }
|
|
@@ -15,48 +15,56 @@ import { ThemeStore, ThemeStoreConfig, Themes } from "./index.js";
|
|
|
15
15
|
declare function useResonare<T extends ThemeStoreConfig>(store: ThemeStore<T>): {
|
|
16
16
|
themes: Themes<T>;
|
|
17
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 };
|
|
18
30
|
destroy: () => void;
|
|
19
31
|
restore: () => void;
|
|
20
32
|
setThemes: (themes: Partial<Themes<T>> | ((currentThemes: Themes<T>) => Partial<Themes<T>>)) => void;
|
|
21
33
|
subscribe: (callback: (value: {
|
|
22
34
|
themes: Themes<T>;
|
|
23
35
|
resolvedThemes: Themes<T>;
|
|
24
|
-
|
|
25
|
-
sync: () => (() => void) | undefined;
|
|
26
|
-
toPersist: () => {
|
|
27
|
-
themes: Partial<Themes<T>>;
|
|
28
|
-
systemOptions: { [K_1 in { [K in keyof T]: T[K] extends {
|
|
36
|
+
resolvedSystemThemes: { [K_1 in { [K in keyof T]: T[K] extends {
|
|
29
37
|
options: ReadonlyArray<infer U>;
|
|
30
38
|
} ? U extends {
|
|
31
39
|
media: ReadonlyArray<unknown>;
|
|
32
|
-
} ? K : never : never }[keyof T]]
|
|
40
|
+
} ? K : never : never }[keyof T]]: T[K_1] extends {
|
|
33
41
|
options: ReadonlyArray<infer U>;
|
|
34
42
|
} ? U extends string | number | boolean ? U : U extends {
|
|
35
43
|
value: string;
|
|
36
44
|
media?: [string, string, string];
|
|
37
45
|
} ? U extends {
|
|
38
46
|
media: [string, string, string];
|
|
39
|
-
} ? never : U["value"] : never : never
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
+
} ? never : U["value"] : never : never };
|
|
48
|
+
}) => void) => (() => void);
|
|
49
|
+
sync: () => (() => void) | undefined;
|
|
50
|
+
toPersist: () => {
|
|
51
|
+
themes: Partial<Themes<T>>;
|
|
52
|
+
systemOptions: {
|
|
53
|
+
[x: string]: [string, string];
|
|
54
|
+
};
|
|
47
55
|
};
|
|
48
|
-
updateSystemOption: <
|
|
56
|
+
updateSystemOption: <K_2 extends { [K in keyof T]: T[K] extends {
|
|
49
57
|
options: ReadonlyArray<infer U>;
|
|
50
58
|
} ? U extends {
|
|
51
59
|
media: ReadonlyArray<unknown>;
|
|
52
|
-
} ?
|
|
60
|
+
} ? K : never : never }[keyof T]>(themeKey: K_2, pair: [T[K_2] extends {
|
|
53
61
|
options: ReadonlyArray<infer U>;
|
|
54
62
|
} ? U extends string | number | boolean ? U : U extends {
|
|
55
63
|
value: string;
|
|
56
64
|
media?: [string, string, string];
|
|
57
65
|
} ? U extends {
|
|
58
66
|
media: [string, string, string];
|
|
59
|
-
} ? never : U["value"] : never : never, T[
|
|
67
|
+
} ? never : U["value"] : never : never, T[K_2] extends {
|
|
60
68
|
options: ReadonlyArray<infer U>;
|
|
61
69
|
} ? U extends string | number | boolean ? U : U extends {
|
|
62
70
|
value: string;
|
package/dist/react.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","names":[],"sources":["../src/react.ts"],"mappings":";;;;;AAcA;;;;;;;;;iBAAgB,WAAA,WAAsB,gBAAA,
|
|
1
|
+
{"version":3,"file":"react.d.ts","names":[],"sources":["../src/react.ts"],"mappings":";;;;;AAcA;;;;;;;;;iBAAgB,WAAA,WAAsB,gBAAA,EAAkB,KAAA,EAAO,UAAA,CAAW,CAAA"}
|
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(17),{destroy:i,getResolvedThemes:a,getResolvedSystemThemes:o,getThemes:s,restore:c,setThemes:l,subscribe:u,sync:d,toPersist:f,updateSystemOption:p}=n,m=t.useSyncExternalStore(u,s,s),h;r[0]!==a||r[1]!==m?(h=a(m),r[0]=a,r[1]=m,r[2]=h):h=r[2];let g;r[3]!==o||r[4]!==m?(g=o(m),r[3]=o,r[4]=m,r[5]=g):g=r[5];let _;return r[6]!==i||r[7]!==c||r[8]!==l||r[9]!==u||r[10]!==d||r[11]!==h||r[12]!==g||r[13]!==m||r[14]!==f||r[15]!==p?(_={themes:m,resolvedThemes:h,resolvedSystemThemes:g,destroy:i,restore:c,setThemes:l,subscribe:u,sync:d,toPersist:f,updateSystemOption:p},r[6]=i,r[7]=c,r[8]=l,r[9]=u,r[10]=d,r[11]=h,r[12]=g,r[13]=m,r[14]=f,r[15]=p,r[16]=_):_=r[16],_}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","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,
|
|
1
|
+
{"version":3,"file":"react.js","names":["React","ThemeStore","ThemeStoreConfig","useResonare","store","$","_c","destroy","getResolvedThemes","getResolvedSystemThemes","getThemes","restore","setThemes","subscribe","sync","toPersist","updateSystemOption","themes","useSyncExternalStore","t0","t1","t2","resolvedThemes","resolvedSystemThemes"],"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\tgetResolvedThemes,\n\t\tgetResolvedSystemThemes,\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\t// @ts-expect-error - workaround for React compiler as getResolvedSystemThemes is not called again without 'themes' dependency\n\t\tresolvedSystemThemes: getResolvedSystemThemes(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,EAAA,EACN,CAAAC,UAAAC,oBAAAC,0BAAAC,YAAAC,UAAAC,YAAAC,YAAAC,OAAAC,YAAAC,sBAWIZ,EAEJa,EAAejB,EAAKkB,qBAAsBL,EAAWH,EAAWA,CAAS,EAACS,EAAAd,EAAA,KAAAG,GAAAH,EAAA,KAAAY,GAKzDE,EAAAX,EAAkBS,CAAM,EAACZ,EAAA,GAAAG,EAAAH,EAAA,GAAAY,EAAAZ,EAAA,GAAAc,GAAAA,EAAAd,EAAA,GAAA,IAAAe,EAAAf,EAAA,KAAAI,GAAAJ,EAAA,KAAAY,GAEnBG,EAAAX,EAAwBQ,CAAM,EAACZ,EAAA,GAAAI,EAAAJ,EAAA,GAAAY,EAAAZ,EAAA,GAAAe,GAAAA,EAAAf,EAAA,GAAA,IAAAgB,EAQrD,OARqDhB,EAAA,KAAAE,GAAAF,EAAA,KAAAM,GAAAN,EAAA,KAAAO,GAAAP,EAAA,KAAAQ,GAAAR,EAAA,MAAAS,GAAAT,EAAA,MAAAc,GAAAd,EAAA,MAAAe,GAAAf,EAAA,MAAAY,GAAAZ,EAAA,MAAAU,GAAAV,EAAA,MAAAW,GAL/CK,EAAA,CAAAJ,SAAAK,eAGUH,EAAyBI,qBAEnBH,EAA+Bb,UAAAI,UAAAC,YAAAC,YAAAC,OAAAC,YAAAC,oBAQtD,EAACX,EAAA,GAAAE,EAAAF,EAAA,GAAAM,EAAAN,EAAA,GAAAO,EAAAP,EAAA,GAAAQ,EAAAR,EAAA,IAAAS,EAAAT,EAAA,IAAAc,EAAAd,EAAA,IAAAe,EAAAf,EAAA,IAAAY,EAAAZ,EAAA,IAAAU,EAAAV,EAAA,IAAAW,EAAAX,EAAA,IAAAgB,GAAAA,EAAAhB,EAAA,IAbMgB,CAaN"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resonare",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Resonare",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"theming",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
],
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/
|
|
12
|
+
"url": "https://github.com/phuoccss/resonare.git",
|
|
13
13
|
"directory": "resonare"
|
|
14
14
|
},
|
|
15
15
|
"license": "MIT",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"react": "19.2.5",
|
|
46
46
|
"react-dom": "19.2.5",
|
|
47
47
|
"size-limit": "12.1.0",
|
|
48
|
-
"tsdown": "0.
|
|
48
|
+
"tsdown": "0.22.1",
|
|
49
49
|
"typescript": "6.0.3",
|
|
50
50
|
"vite": "8.0.10",
|
|
51
51
|
"vitest": "4.1.5"
|