resonare 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +329 -270
- package/dist/index.d.ts +219 -29
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lite-react.d.ts +27 -22
- package/dist/lite-react.d.ts.map +1 -1
- package/dist/lite-react.js +1 -1
- package/dist/lite-react.js.map +1 -1
- package/dist/lite-solid.d.ts +42 -0
- package/dist/lite-solid.d.ts.map +1 -0
- package/dist/lite-solid.js +2 -0
- package/dist/lite-solid.js.map +1 -0
- package/dist/lite-svelte.d.ts +41 -0
- package/dist/lite-svelte.d.ts.map +1 -0
- package/dist/lite-svelte.js +2 -0
- package/dist/lite-svelte.js.map +1 -0
- package/dist/lite-vue.d.ts +49 -0
- package/dist/lite-vue.d.ts.map +1 -0
- package/dist/lite-vue.js +2 -0
- package/dist/lite-vue.js.map +1 -0
- package/dist/lite.d.ts +103 -22
- package/dist/lite.d.ts.map +1 -1
- package/dist/lite.js +1 -1
- package/dist/lite.js.map +1 -1
- package/dist/react.d.ts +47 -38
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +1 -1
- package/dist/react.js.map +1 -1
- package/dist/solid.d.ts +84 -0
- package/dist/solid.d.ts.map +1 -0
- package/dist/solid.js +2 -0
- package/dist/solid.js.map +1 -0
- package/dist/storage-BqWA8jeP.js +2 -0
- package/dist/storage-BqWA8jeP.js.map +1 -0
- package/dist/storage-CsXjZKyt.d.ts +65 -0
- package/dist/storage-CsXjZKyt.d.ts.map +1 -0
- package/dist/svelte.d.ts +86 -0
- package/dist/svelte.d.ts.map +1 -0
- package/dist/svelte.js +2 -0
- package/dist/svelte.js.map +1 -0
- package/dist/vue.d.ts +82 -0
- package/dist/vue.d.ts.map +1 -0
- package/dist/vue.js +2 -0
- package/dist/vue.js.map +1 -0
- package/package.json +67 -32
- package/dist/storage-B5xjLRee.js +0 -2
- package/dist/storage-B5xjLRee.js.map +0 -1
- package/dist/storage-DGYV332J.d.ts +0 -23
- package/dist/storage-DGYV332J.d.ts.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { a as memoryStorageAdapter, i as localStorageAdapter, n as StorageAdapterCreate, r as StorageAdapterCreator, t as StorageAdapter } from "./storage-
|
|
2
|
-
|
|
3
|
-
//#region src/index.d.ts
|
|
1
|
+
import { a as memoryStorageAdapter, i as localStorageAdapter, n as StorageAdapterCreate, r as StorageAdapterCreator, t as StorageAdapter } from "./storage-CsXjZKyt.js";
|
|
4
2
|
type ThemeValue = string | number | boolean;
|
|
5
3
|
type ThemeOption<T extends ThemeValue = string> = {
|
|
6
4
|
value: T;
|
|
7
|
-
media?: [string, T, T];
|
|
5
|
+
media?: readonly [string, T, T];
|
|
8
6
|
};
|
|
9
7
|
type ThemeConfig<T extends ThemeValue = string> = {
|
|
10
8
|
options: ReadonlyArray<T | ThemeOption<T>>;
|
|
@@ -13,23 +11,62 @@ type ThemeConfig<T extends ThemeValue = string> = {
|
|
|
13
11
|
defaultValue: T;
|
|
14
12
|
options?: never;
|
|
15
13
|
};
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Config for `createThemeStore`. Each key is a theme setting (`colorMode`,
|
|
16
|
+
* `contrast`, …) with either:
|
|
17
|
+
*
|
|
18
|
+
* - `options`: the allowed values, plus an optional `defaultValue` (otherwise the
|
|
19
|
+
* first option). One option may be a `{ value, media }` object: when it is
|
|
20
|
+
* selected, the setting follows a media query, using `media[1]` while the
|
|
21
|
+
* query matches and `media[2]` while it doesn't.
|
|
22
|
+
* - `defaultValue` alone: a free value of that type, such as a number.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const config = {
|
|
26
|
+
* colorMode: {
|
|
27
|
+
* options: [
|
|
28
|
+
* 'light',
|
|
29
|
+
* 'dark',
|
|
30
|
+
* {
|
|
31
|
+
* value: 'system',
|
|
32
|
+
* media: ['(prefers-color-scheme: dark)', 'dark', 'light'],
|
|
33
|
+
* },
|
|
34
|
+
* ],
|
|
35
|
+
* defaultValue: 'system',
|
|
36
|
+
* },
|
|
37
|
+
* contrast: {
|
|
38
|
+
* options: ['normal', 'more'],
|
|
39
|
+
* },
|
|
40
|
+
* sidebarWidth: {
|
|
41
|
+
* defaultValue: 240,
|
|
42
|
+
* },
|
|
43
|
+
* } as const satisfies ThemeStoreConfig
|
|
44
|
+
*/
|
|
45
|
+
export type ThemeStoreConfig = Record<string, ThemeConfig<string> | ThemeConfig<number> | ThemeConfig<boolean>>;
|
|
46
|
+
/**
|
|
47
|
+
* The selected value of each setting, as passed to `setThemes` (e.g.
|
|
48
|
+
* `colorMode: 'system'`). Inferred from your `ThemeStoreConfig`.
|
|
49
|
+
*/
|
|
50
|
+
export type Themes<T extends ThemeStoreConfig> = { [K in keyof T]: T[K] extends {
|
|
18
51
|
options: ReadonlyArray<infer U>;
|
|
19
52
|
} ? U extends ThemeOption ? U['value'] : U : T[K] extends {
|
|
20
53
|
defaultValue: infer U;
|
|
21
|
-
} ? U extends string ? string : U extends number ? number : boolean : never };
|
|
54
|
+
} ? U extends string ? string : U extends number ? number : boolean : never; };
|
|
22
55
|
type ThemeKeysWithSystemOption<T extends ThemeStoreConfig> = { [K in keyof T]: T[K] extends {
|
|
23
56
|
options: ReadonlyArray<infer U>;
|
|
24
57
|
} ? U extends {
|
|
25
58
|
media: ReadonlyArray<unknown>;
|
|
26
|
-
} ? K : never : never }[keyof T];
|
|
59
|
+
} ? K : never : never; }[keyof T];
|
|
27
60
|
type NonSystemOptionValues<T extends ThemeStoreConfig, K extends keyof T> = T[K] extends {
|
|
28
61
|
options: ReadonlyArray<infer U>;
|
|
29
62
|
} ? U extends ThemeValue ? U : U extends ThemeOption ? U extends {
|
|
30
|
-
media:
|
|
63
|
+
media: ReadonlyArray<unknown>;
|
|
31
64
|
} ? never : U['value'] : never : never;
|
|
32
|
-
|
|
65
|
+
/**
|
|
66
|
+
* For each setting with a `media` option, the value that option currently
|
|
67
|
+
* resolves to, whether or not it is selected.
|
|
68
|
+
*/
|
|
69
|
+
type ResolvedSystemThemes<T extends ThemeStoreConfig> = { [K in ThemeKeysWithSystemOption<T>]: NonSystemOptionValues<T, K>; };
|
|
33
70
|
type Snapshot<T extends ThemeStoreConfig> = {
|
|
34
71
|
themes: Themes<T>;
|
|
35
72
|
resolvedThemes: Themes<T>;
|
|
@@ -47,34 +84,187 @@ type ThemeStoreOptions<T extends ThemeStoreConfig> = {
|
|
|
47
84
|
};
|
|
48
85
|
type ThemeAndOptions<T extends ThemeStoreConfig> = Array<{ [K in keyof T]: [K, Array<T[K] extends {
|
|
49
86
|
options: ReadonlyArray<infer U>;
|
|
50
|
-
} ? U extends ThemeOption ? U['value'] : U : never>] }[keyof T]>;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
87
|
+
} ? U extends ThemeOption ? U['value'] : U : never>]; }[keyof T]>;
|
|
88
|
+
/**
|
|
89
|
+
* Lists each setting with its option values (`{ value, media }` options become
|
|
90
|
+
* their `value`). Useful for rendering a picker for every setting.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* import { getThemesAndOptions } from 'resonare'
|
|
94
|
+
*
|
|
95
|
+
* getThemesAndOptions({
|
|
96
|
+
* colorMode: {
|
|
97
|
+
* options: [
|
|
98
|
+
* 'light',
|
|
99
|
+
* 'dark',
|
|
100
|
+
* {
|
|
101
|
+
* value: 'system',
|
|
102
|
+
* media: ['(prefers-color-scheme: dark)', 'dark', 'light'],
|
|
103
|
+
* },
|
|
104
|
+
* ],
|
|
105
|
+
* },
|
|
106
|
+
* })
|
|
107
|
+
* // [['colorMode', ['light', 'dark', 'system']]]
|
|
108
|
+
*/
|
|
109
|
+
export declare function getThemesAndOptions<T extends ThemeStoreConfig>(config: T): ThemeAndOptions<T>;
|
|
110
|
+
/**
|
|
111
|
+
* Returns each setting's default: its `defaultValue`, or else its first
|
|
112
|
+
* option.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* import { getDefaultThemes } from 'resonare'
|
|
116
|
+
*
|
|
117
|
+
* getDefaultThemes({
|
|
118
|
+
* colorMode: { options: ['light', 'dark'], defaultValue: 'dark' },
|
|
119
|
+
* }) // { colorMode: 'dark' }
|
|
120
|
+
*/
|
|
121
|
+
export declare function getDefaultThemes<T extends ThemeStoreConfig>(config: T): Themes<T>;
|
|
122
|
+
/**
|
|
123
|
+
* Creates a store for the settings in `config`. It saves changes through
|
|
124
|
+
* `storage` (`localStorage` under the key `'resonare'` by default; `null`
|
|
125
|
+
* disables saving), resolves `media` options in the browser, and notifies
|
|
126
|
+
* subscribers.
|
|
127
|
+
*
|
|
128
|
+
* Read state with `getSnapshot()` or `subscribe`.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* import { createThemeStore, localStorageAdapter } from 'resonare'
|
|
132
|
+
*
|
|
133
|
+
* const store = createThemeStore(
|
|
134
|
+
* {
|
|
135
|
+
* colorMode: {
|
|
136
|
+
* options: [
|
|
137
|
+
* 'light',
|
|
138
|
+
* 'dark',
|
|
139
|
+
* {
|
|
140
|
+
* value: 'system',
|
|
141
|
+
* media: ['(prefers-color-scheme: dark)', 'dark', 'light'],
|
|
142
|
+
* },
|
|
143
|
+
* ],
|
|
144
|
+
* defaultValue: 'system',
|
|
145
|
+
* },
|
|
146
|
+
* },
|
|
147
|
+
* { storage: localStorageAdapter({ key: 'my-app' }) }
|
|
148
|
+
* )
|
|
149
|
+
*
|
|
150
|
+
* // In the browser: load the saved choice and follow other tabs.
|
|
151
|
+
* store.restore()
|
|
152
|
+
* store.sync()
|
|
153
|
+
*
|
|
154
|
+
* store.subscribe(({ resolvedThemes }) => {
|
|
155
|
+
* document.documentElement.dataset.colorMode = resolvedThemes.colorMode
|
|
156
|
+
* })
|
|
157
|
+
*
|
|
158
|
+
* store.setThemes({ colorMode: 'dark' })
|
|
159
|
+
*/
|
|
160
|
+
export declare function createThemeStore<T extends ThemeStoreConfig>(config: T, { persisted, storage: createStorage }?: ThemeStoreOptions<T>): {
|
|
161
|
+
/**
|
|
162
|
+
* Current state:
|
|
163
|
+
*
|
|
164
|
+
* - `themes`: the selected value of each setting (e.g. `'system'`).
|
|
165
|
+
* - `resolvedThemes`: the same with `media` options resolved (e.g. `'dark'`).
|
|
166
|
+
* - `resolvedSystemThemes`: what each `media` option resolves to, even when
|
|
167
|
+
* it isn't selected.
|
|
168
|
+
*
|
|
169
|
+
* Returns the same object until the next change.
|
|
170
|
+
*/
|
|
62
171
|
getSnapshot: () => Snapshot<T>;
|
|
172
|
+
/**
|
|
173
|
+
* The state as rendered on the server: defaults plus `persisted`, with
|
|
174
|
+
* `media` options resolved to their "not matched" value. It never changes,
|
|
175
|
+
* so hydration can match the server HTML even after `restore()`.
|
|
176
|
+
*/
|
|
177
|
+
getServerSnapshot: () => Snapshot<T>;
|
|
178
|
+
/**
|
|
179
|
+
* Updates the given settings (others keep their value), saves to storage,
|
|
180
|
+
* and notifies subscribers.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* store.setThemes({ colorMode: 'dark' })
|
|
184
|
+
*
|
|
185
|
+
* store.setThemes((current) => ({
|
|
186
|
+
* colorMode: current.colorMode === 'dark' ? 'light' : 'dark',
|
|
187
|
+
* }))
|
|
188
|
+
*/
|
|
63
189
|
setThemes: (themes: Partial<Themes<T>> | ((currentThemes: Themes<T>) => Partial<Themes<T>>)) => void;
|
|
190
|
+
/**
|
|
191
|
+
* Changes which values a `media` option resolves to: `[whenMatched,
|
|
192
|
+
* whenNotMatched]`, replacing `media[1]` and `media[2]` from the config.
|
|
193
|
+
* Saved like `setThemes`.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* // system now picks 'dark-dimmed' when the OS prefers dark
|
|
197
|
+
* store.updateSystemOption('colorMode', ['dark-dimmed', 'light'])
|
|
198
|
+
*/
|
|
64
199
|
updateSystemOption: <K extends ThemeKeysWithSystemOption<T>>(themeKey: K, pair: [NonSystemOptionValues<T, K>, NonSystemOptionValues<T, K>]) => void;
|
|
200
|
+
/**
|
|
201
|
+
* The state to save, e.g. to a database: pass it back as `persisted` when
|
|
202
|
+
* creating the store.
|
|
203
|
+
*/
|
|
65
204
|
toPersist: () => PersistedState<T>;
|
|
205
|
+
/**
|
|
206
|
+
* Loads the saved state from storage. Does nothing when nothing is saved.
|
|
207
|
+
* Call it in the browser, e.g. after hydration.
|
|
208
|
+
*/
|
|
66
209
|
restore: () => void;
|
|
210
|
+
/**
|
|
211
|
+
* Calls `callback` with the new state (see `getSnapshot`) after every
|
|
212
|
+
* change, including OS preference changes. Returns an unsubscribe
|
|
213
|
+
* function.
|
|
214
|
+
*/
|
|
67
215
|
subscribe: (callback: Listener<T>) => (() => void);
|
|
216
|
+
/**
|
|
217
|
+
* Applies changes saved by other tabs (if the storage adapter supports
|
|
218
|
+
* it); when another tab clears the saved state, resets to the config
|
|
219
|
+
* defaults. Returns a function that stops syncing, or `undefined` when the
|
|
220
|
+
* adapter can't sync.
|
|
221
|
+
*/
|
|
68
222
|
sync: () => (() => void) | undefined;
|
|
223
|
+
/**
|
|
224
|
+
* Removes all subscribers and stops listening to media queries and
|
|
225
|
+
* storage. The store doesn't update after this.
|
|
226
|
+
*/
|
|
69
227
|
destroy: () => void;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
228
|
+
};
|
|
229
|
+
export type ThemeStore<T extends ThemeStoreConfig> = ReturnType<typeof createThemeStore<T>>;
|
|
230
|
+
/** One store for `createInlineThemeScript`. */
|
|
231
|
+
export type ThemeScriptParameter = {
|
|
232
|
+
/** The store's `localStorage` key; defaults to `'resonare'`. */
|
|
233
|
+
key?: string;
|
|
234
|
+
/** The same config passed to `createThemeStore`. */
|
|
74
235
|
config: ThemeStoreConfig;
|
|
236
|
+
/**
|
|
237
|
+
* Applies the state to the page; receives the same value as `subscribe`.
|
|
238
|
+
* It is copied into the script as source code, so it can't use variables
|
|
239
|
+
* from outside the function.
|
|
240
|
+
*/
|
|
75
241
|
handler: Listener<any>;
|
|
76
242
|
};
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
243
|
+
/**
|
|
244
|
+
* Returns the source of a small script that reads the saved state from
|
|
245
|
+
* `localStorage` and runs `handler` with it. Put it in an inline `<script>` in
|
|
246
|
+
* the `<head>` so the page has the right theme before first paint. Pass an
|
|
247
|
+
* array to set up several stores.
|
|
248
|
+
*
|
|
249
|
+
* Use the same handler with `store.subscribe` to keep the page updated after
|
|
250
|
+
* that.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* import { createInlineThemeScript } from 'resonare'
|
|
254
|
+
*
|
|
255
|
+
* // config: the same object passed to createThemeStore
|
|
256
|
+
* const themeScript = createInlineThemeScript({
|
|
257
|
+
* key: 'my-app',
|
|
258
|
+
* config,
|
|
259
|
+
* handler: ({ resolvedThemes }) => {
|
|
260
|
+
* const root = document.documentElement
|
|
261
|
+
* root.dataset.colorMode = resolvedThemes.colorMode
|
|
262
|
+
* },
|
|
263
|
+
* })
|
|
264
|
+
*
|
|
265
|
+
* // In <head>:
|
|
266
|
+
* // <script dangerouslySetInnerHTML={{ __html: themeScript }} />
|
|
267
|
+
*/
|
|
268
|
+
export declare function createInlineThemeScript(themeScriptParameters: ThemeScriptParameter | Array<ThemeScriptParameter>): string;
|
|
269
|
+
export { StorageAdapter, StorageAdapterCreate, StorageAdapterCreator, localStorageAdapter, memoryStorageAdapter };
|
|
80
270
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[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U,kBACzC,QAAQ,KAEN,WACA,SAAS,kBACR,kBAAkB;;;;;;;;;;;EAoKF,mBAAA,SAAS;;;;;;EASH,yBAAA,SAAS;;;;;;;;;;;;EApC5B,YAAA,QAAA,QAAQ,OAAO,QACb,eAAe,OAAO,OAAO,QAAQ,OAAO;;;;;;;;;;EA6D7B,qBAAA,UAAU,0BAA0B,IAAE,UAC/C,GAAC,OACJ,sBAAsB,GAAG,IAAI,sBAAsB,GAAG;;;;;EA5E3C,iBAAA,eAAe;;;;;EA6FtB;;;;;;EASS,YAAA,UAAA,SAAS;;;;;;;EAcrB;;;;;EAqBG;;YAQL,WAAW,UAAU,oBAAoB,kBAC5C,iBAAiB;;YA+Dd;;EAEV;;EAEA,QAAQ;;;;;;EAMR,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA4BK,wBACd,uBAAuB,uBAAuB,MAAM"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{a as e,i as t,n,r,t as i}from"./storage-BqWA8jeP.js";function a(e){return Object.entries(e).map(([e,t])=>[e,(t.options||[]).map(e=>typeof e==`object`?e.value:e)])}function o(e){return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,t.defaultValue??(typeof t.options[0]==`object`?t.options[0].value:t.options[0])]))}function s(t,{persisted:n={},storage:r=i({key:e})}={}){let a=o(t),s={...a,...n.themes},c={};for(let e in t){let r=(t[e].options||[]).find(e=>typeof e==`object`&&!!e.media);if(r){let{value:t,media:[i,a,o]}=r,s=[a,o];c[e]={value:t,mediaQuery:i,pair:[...n.systemOptions?.[e]??s],defaultPair:s}}}let l={},u=/* @__PURE__ */ new Set,d=new AbortController,f=r?.({abortController:d}),p=(e,t)=>{let{mediaQuery:n,pair:r}=c[e];if(t||typeof matchMedia>`u`)return r[1];let i=l[n];return i||(i=l[n]=matchMedia(n),i.addEventListener(`change`,()=>{_()},d)),r[+!i.matches]},m=e=>{let t={},n={...s};for(let r in c)t[r]=p(r,e),n[r]===c[r].value&&(n[r]=t[r]);return{themes:s,resolvedThemes:n,resolvedSystemThemes:t}},h=m(!0),g=m(),_=e=>{s={...s,...e},g=m();for(let e of u)e(g)},v=e=>{let t;try{t=e?JSON.parse(e):void 0}catch{}if(t?.themes&&typeof t.themes==`object`){for(let e in t.systemOptions){let n=t.systemOptions[e];n&&c[e]&&(c[e].pair=n)}_(t.themes)}},y=()=>{let e={};for(let t in c)e[t]=[...c[t].pair];return{themes:{...s},systemOptions:e}},b=e=>{if(_(typeof e==`function`?e(s):e),f){let e=JSON.stringify(y());f.set(e),f.broadcast?.(e)}};return{getSnapshot:()=>g,getServerSnapshot:()=>h,setThemes:b,updateSystemOption:(e,t)=>{c[e].pair=[...t],b({})},toPersist:y,restore:()=>{v(f?.get())},subscribe:e=>(u.add(e),()=>{u.delete(e)}),sync:()=>f?.watch?.(e=>{if(e===null){for(let e in c){let t=c[e];t.pair=t.defaultPair}_(a)}else v(e)}),destroy:()=>{u.clear(),d.abort()}}}const c=e=>{e.forEach(([e,t,n])=>{let r;try{r=JSON.parse(localStorage.getItem(e))}catch{}n(t.reduce((e,[t,n,i,a,o,s])=>{let c=r?.themes?.[t]??n;if(e.resolvedThemes[t]=e.themes[t]=c,a!=null){let n=r?.systemOptions?.[t]??[o,s],l=matchMedia(a).matches?n[0]:n[1];e.resolvedSystemThemes[t]=l,c===i&&(e.resolvedThemes[t]=l)}return e},{themes:{},resolvedThemes:{},resolvedSystemThemes:{}}))})};function l(n){let i=(Array.isArray(n)?n:[n]).map(({key:n=e,config:i,handler:a})=>{let o=Object.entries(i).map(([e,{options:t,defaultValue:n}])=>{let r=t?.[0],i=n??(typeof r==`object`?r.value:r),a=t?.find(e=>typeof e==`object`&&!!e.media);if(a){let{value:t,media:n}=a;return[e,i,t,...n]}return[e,i]});return`[${t(n)},${t(o)},${r(a)}]`});return`(${c})([${i}])`}export{l as createInlineThemeScript,s as createThemeStore,o as getDefaultThemes,a as getThemesAndOptions,i 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":["DEFAULT_KEY","localStorageAdapter","StorageAdapter","StorageAdapterCreate","ThemeValue","ThemeOption","value","T","media","ThemeConfig","options","ReadonlyArray","defaultValue","ThemeStoreConfig","Record","Themes","K","U","ThemeKeysWithSystemOption","NonSystemOptionValues","ResolvedSystemThemes","SystemOptions","mediaQuery","pair","Snapshot","themes","resolvedThemes","resolvedSystemThemes","Listener","PersistedSystemOptionPairs","PersistedState","Partial","systemOptions","ThemeStoreOptions","persisted","storage","ThemeAndOptions","Array","getThemesAndOptions","config","Object","entries","map","themeKey","themeConfig","option","getDefaultThemes","fromEntries","ThemeStore","currentThemes","snapshot","mediaQueryCache","MediaQueryList","listeners","Set","abortController","AbortController","constructor","key","flatMap","systemOption","find","Required","optionWhenMatched","optionWhenNotMatched","createSnapshot","getThemes","getResolvedThemes","optionKey","resolveSystemOption","getResolvedSystemThemes","keys","getSnapshot","setThemes","updatedThemes","setThemesAndNotify","stored","JSON","stringify","toPersist","set","broadcast","updateSystemOption","restore","get","parse","applyPersistedSystemOptions","subscribe","callback","add","delete","sync","watch","destroy","clear","abort","#applyPersistedSystemOptions","forEach","#createSnapshot","#setThemesAndNotify","listener","#resolveSystemOption","matchMedia","window","addEventListener","signal","matches","createThemeStore","restoreScript","params","flattenedConfig","handler","localStorage","getItem","reduce","acc","initial","systemOptionValue","currentValue","systemTheme","ThemeScriptParameter","createInlineThemeScript","themeScriptParameters","serializedArgs","isArray","firstOption","resolvedInitialValue"],"sources":["../src/index.ts"],"sourcesContent":["import { DEFAULT_KEY } from './constants'\nimport {\n\tlocalStorageAdapter,\n\ttype StorageAdapter,\n\ttype StorageAdapterCreate,\n} from './storage'\n\nexport * from './storage'\n\ntype ThemeValue = string | number | boolean\n\ntype ThemeOption<T extends ThemeValue = string> = {\n\tvalue: T\n\tmedia?: [string, T, T]\n}\n\ntype ThemeConfig<T extends ThemeValue = string> =\n\t| {\n\t\t\toptions: ReadonlyArray<T | ThemeOption<T>>\n\t\t\tdefaultValue?: T\n\t }\n\t| { defaultValue: T; options?: never }\n\nexport type ThemeStoreConfig = Record<\n\tstring,\n\tThemeConfig<string> | ThemeConfig<number> | ThemeConfig<boolean>\n>\n\nexport type Themes<T extends ThemeStoreConfig> = {\n\t[K in keyof T]: T[K] extends { options: ReadonlyArray<infer U> }\n\t\t? U extends ThemeOption\n\t\t\t? U['value']\n\t\t\t: U\n\t\t: T[K] extends { defaultValue: infer U }\n\t\t\t? U extends string\n\t\t\t\t? string\n\t\t\t\t: U extends number\n\t\t\t\t\t? number\n\t\t\t\t\t: boolean\n\t\t\t: never\n}\n\ntype ThemeKeysWithSystemOption<T extends ThemeStoreConfig> = {\n\t[K in keyof T]: T[K] extends { options: ReadonlyArray<infer U> }\n\t\t? U extends { media: ReadonlyArray<unknown> }\n\t\t\t? K\n\t\t\t: never\n\t\t: never\n}[keyof T]\n\ntype NonSystemOptionValues<\n\tT extends ThemeStoreConfig,\n\tK extends keyof T,\n> = T[K] extends { options: ReadonlyArray<infer U> }\n\t? U extends ThemeValue\n\t\t? U\n\t\t: U extends ThemeOption\n\t\t\t? U extends { media: [string, string, string] }\n\t\t\t\t? never\n\t\t\t\t: U['value']\n\t\t\t: never\n\t: never\n\ntype ResolvedSystemThemes<T extends ThemeStoreConfig> = {\n\t[K in ThemeKeysWithSystemOption<T>]: NonSystemOptionValues<T, K>\n}\n\ntype SystemOptions = Record<\n\tstring,\n\t{\n\t\tvalue: string\n\t\tmediaQuery: string\n\t\tpair: [string, string]\n\t}\n>\n\ntype Snapshot<T extends ThemeStoreConfig> = {\n\tthemes: Themes<T>\n\tresolvedThemes: Themes<T>\n\tresolvedSystemThemes: ResolvedSystemThemes<T>\n}\n\ntype Listener<T extends ThemeStoreConfig> = (value: Snapshot<T>) => void\n\ntype PersistedSystemOptionPairs = Record<string, [string, string]>\n\ntype PersistedState<T extends ThemeStoreConfig> = {\n\tthemes: Partial<Themes<T>>\n\tsystemOptions: PersistedSystemOptionPairs\n}\n\ntype ThemeStoreOptions<T extends ThemeStoreConfig> = {\n\tpersisted?: Partial<PersistedState<T>>\n\tstorage?: StorageAdapterCreate | null\n}\n\ntype ThemeAndOptions<T extends ThemeStoreConfig> = Array<\n\t{\n\t\t[K in keyof T]: [\n\t\t\tK,\n\t\t\tArray<\n\t\t\t\tT[K] extends { options: ReadonlyArray<infer U> }\n\t\t\t\t\t? U extends ThemeOption\n\t\t\t\t\t\t? U['value']\n\t\t\t\t\t\t: U\n\t\t\t\t\t: never\n\t\t\t>,\n\t\t]\n\t}[keyof T]\n>\n\nexport function getThemesAndOptions<T extends ThemeStoreConfig>(config: T) {\n\treturn Object.entries(config).map(([themeKey, themeConfig]) => {\n\t\treturn [\n\t\t\tthemeKey,\n\t\t\t(themeConfig.options || []).map((option) =>\n\t\t\t\ttypeof option === 'object' ? option.value : option,\n\t\t\t),\n\t\t]\n\t}) as ThemeAndOptions<T>\n}\n\nexport function getDefaultThemes<T extends ThemeStoreConfig>(config: T) {\n\treturn Object.fromEntries(\n\t\tObject.entries(config).map(([themeKey, themeConfig]) => {\n\t\t\treturn [\n\t\t\t\tthemeKey,\n\t\t\t\tthemeConfig.defaultValue ??\n\t\t\t\t\t(typeof themeConfig.options[0] === 'object'\n\t\t\t\t\t\t? themeConfig.options[0].value\n\t\t\t\t\t\t: themeConfig.options[0]),\n\t\t\t]\n\t\t}),\n\t) as Themes<T>\n}\n\nclass ThemeStore<T extends ThemeStoreConfig> {\n\t#currentThemes: Themes<T>\n\n\t#snapshot: Snapshot<T>\n\n\t#systemOptions: SystemOptions = {}\n\n\t#storage: StorageAdapter | null\n\n\t#mediaQueryCache: Record<string, MediaQueryList> = {}\n\n\t#listeners: Set<Listener<T>> = new Set<Listener<T>>()\n\n\t#abortController = new AbortController()\n\n\tconstructor(\n\t\tconfig: T,\n\t\t{\n\t\t\tpersisted = {},\n\t\t\tstorage = localStorageAdapter({ key: DEFAULT_KEY }),\n\t\t}: ThemeStoreOptions<T> = {},\n\t) {\n\t\tthis.#currentThemes = { ...getDefaultThemes(config), ...persisted.themes }\n\n\t\tthis.#systemOptions = Object.fromEntries(\n\t\t\tObject.entries(config).flatMap(([themeKey, themeConfig]) => {\n\t\t\t\tconst systemOption = (themeConfig.options || []).find(\n\t\t\t\t\t(option): option is Required<ThemeOption> =>\n\t\t\t\t\t\ttypeof option === 'object' && !!option.media,\n\t\t\t\t)\n\n\t\t\t\tif (!systemOption) return []\n\n\t\t\t\tconst {\n\t\t\t\t\tvalue,\n\t\t\t\t\tmedia: [mediaQuery, optionWhenMatched, optionWhenNotMatched],\n\t\t\t\t} = systemOption\n\n\t\t\t\treturn [\n\t\t\t\t\t[\n\t\t\t\t\t\tthemeKey,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\tmediaQuery,\n\t\t\t\t\t\t\tpair: persisted.systemOptions?.[themeKey] ?? [\n\t\t\t\t\t\t\t\toptionWhenMatched,\n\t\t\t\t\t\t\t\toptionWhenNotMatched,\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t]\n\t\t\t}),\n\t\t)\n\n\t\tthis.#storage =\n\t\t\tstorage?.({\n\t\t\t\tabortController: this.#abortController,\n\t\t\t}) ?? null\n\n\t\tthis.#snapshot = this.#createSnapshot()\n\t}\n\n\tgetThemes = (): Themes<T> => {\n\t\treturn this.#currentThemes\n\t}\n\n\tgetResolvedThemes = (): Themes<T> => {\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries(this.#currentThemes).map(([themeKey, optionKey]) => {\n\t\t\t\tconst systemOption = this.#systemOptions[themeKey]\n\n\t\t\t\treturn [\n\t\t\t\t\tthemeKey,\n\t\t\t\t\tsystemOption && optionKey === systemOption.value\n\t\t\t\t\t\t? this.#resolveSystemOption(themeKey)\n\t\t\t\t\t\t: optionKey,\n\t\t\t\t]\n\t\t\t}),\n\t\t) as Themes<T>\n\t}\n\n\tgetResolvedSystemThemes = (): ResolvedSystemThemes<T> => {\n\t\treturn Object.fromEntries(\n\t\t\tObject.keys(this.#systemOptions).map((themeKey) => [\n\t\t\t\tthemeKey,\n\t\t\t\tthis.#resolveSystemOption(themeKey),\n\t\t\t]),\n\t\t) as ResolvedSystemThemes<T>\n\t}\n\n\tgetSnapshot = (): Snapshot<T> => {\n\t\treturn this.#snapshot\n\t}\n\n\tsetThemes = (\n\t\tthemes:\n\t\t\t| Partial<Themes<T>>\n\t\t\t| ((currentThemes: Themes<T>) => Partial<Themes<T>>),\n\t): void => {\n\t\tconst updatedThemes =\n\t\t\ttypeof themes === 'function' ? themes(this.#currentThemes) : themes\n\n\t\tthis.#setThemesAndNotify(updatedThemes)\n\n\t\tif (this.#storage) {\n\t\t\tconst stored = JSON.stringify(this.toPersist())\n\n\t\t\tthis.#storage.set(stored)\n\n\t\t\tthis.#storage.broadcast?.(stored)\n\t\t}\n\t}\n\n\tupdateSystemOption = <K extends ThemeKeysWithSystemOption<T>>(\n\t\tthemeKey: K,\n\t\tpair: [NonSystemOptionValues<T, K>, NonSystemOptionValues<T, K>],\n\t): void => {\n\t\tthis.#systemOptions[themeKey]!.pair = pair\n\n\t\tthis.setThemes({})\n\t}\n\n\ttoPersist = (): PersistedState<T> => {\n\t\treturn {\n\t\t\tthemes: this.#currentThemes,\n\t\t\tsystemOptions: Object.fromEntries(\n\t\t\t\tObject.entries(this.#systemOptions).map(([themeKey, { pair }]) => [\n\t\t\t\t\tthemeKey,\n\t\t\t\t\tpair,\n\t\t\t\t]),\n\t\t\t),\n\t\t}\n\t}\n\n\trestore = (): void => {\n\t\tconst stored = this.#storage?.get()\n\n\t\tif (!stored) {\n\t\t\tthis.#setThemesAndNotify()\n\t\t\treturn\n\t\t}\n\n\t\tconst persisted = JSON.parse(stored) as PersistedState<T>\n\n\t\tthis.#applyPersistedSystemOptions(persisted.systemOptions)\n\n\t\tthis.#setThemesAndNotify(persisted.themes)\n\t}\n\n\tsubscribe = (callback: Listener<T>): (() => void) => {\n\t\tthis.#listeners.add(callback)\n\n\t\treturn () => this.#listeners.delete(callback)\n\t}\n\n\tsync = (): (() => void) | undefined => {\n\t\tif (!this.#storage?.watch) return\n\n\t\treturn this.#storage.watch((stored) => {\n\t\t\tif (!stored) return\n\n\t\t\tconst persisted = JSON.parse(stored) as PersistedState<T>\n\n\t\t\tthis.#applyPersistedSystemOptions(persisted.systemOptions)\n\n\t\t\tthis.#setThemesAndNotify(persisted.themes)\n\t\t})\n\t}\n\n\tdestroy = (): void => {\n\t\tthis.#listeners.clear()\n\t\tthis.#abortController.abort()\n\t}\n\n\t#applyPersistedSystemOptions = (\n\t\tsystemOptions: PersistedSystemOptionPairs = {},\n\t): void => {\n\t\tObject.entries(systemOptions).forEach(([themeKey, pair]) => {\n\t\t\tif (!pair || !this.#systemOptions[themeKey]) return\n\n\t\t\tthis.#systemOptions[themeKey].pair = pair\n\t\t})\n\t}\n\n\t#createSnapshot = (): Snapshot<T> => ({\n\t\tthemes: this.#currentThemes,\n\t\tresolvedThemes: this.getResolvedThemes(),\n\t\tresolvedSystemThemes: this.getResolvedSystemThemes(),\n\t})\n\n\t#setThemesAndNotify = (themes?: Partial<Themes<T>>): void => {\n\t\tthis.#currentThemes = { ...this.#currentThemes, ...themes }\n\n\t\tthis.#snapshot = this.#createSnapshot()\n\n\t\tfor (const listener of this.#listeners) {\n\t\t\tlistener(this.#snapshot)\n\t\t}\n\t}\n\n\t#resolveSystemOption = (themeKey: string): string => {\n\t\tconst { value, mediaQuery, pair } = this.#systemOptions[themeKey]!\n\n\t\tif (typeof matchMedia === 'undefined') return value\n\n\t\tif (!this.#mediaQueryCache[mediaQuery]) {\n\t\t\tthis.#mediaQueryCache[mediaQuery] = window.matchMedia(mediaQuery)\n\n\t\t\tthis.#mediaQueryCache[mediaQuery].addEventListener(\n\t\t\t\t'change',\n\t\t\t\t() => {\n\t\t\t\t\tthis.#setThemesAndNotify()\n\t\t\t\t},\n\t\t\t\t{ signal: this.#abortController.signal },\n\t\t\t)\n\t\t}\n\n\t\treturn this.#mediaQueryCache[mediaQuery].matches ? pair[0] : pair[1]\n\t}\n}\n\nexport type { ThemeStore }\n\nexport function createThemeStore<T extends ThemeStoreConfig>(\n\tconfig: T,\n\toptions: ThemeStoreOptions<T> = {},\n): ThemeStore<T> {\n\treturn new ThemeStore<T>(config, options)\n}\n\nconst restoreScript = <T extends ThemeValue>(\n\tparams: Array<\n\t\t[string, Array<[string, T] | [string, T, T, string, T, T]>, Listener<any>]\n\t>,\n) => {\n\tparams.forEach(([key, flattenedConfig, handler]) => {\n\t\tconst persisted = JSON.parse(localStorage.getItem(key) || '{}')\n\n\t\thandler(\n\t\t\tflattenedConfig.reduce<{\n\t\t\t\tthemes: Record<string, ThemeValue>\n\t\t\t\tresolvedThemes: Record<string, ThemeValue>\n\t\t\t\tresolvedSystemThemes: Record<string, ThemeValue>\n\t\t\t}>(\n\t\t\t\t(\n\t\t\t\t\tacc,\n\t\t\t\t\t[\n\t\t\t\t\t\tthemeKey,\n\t\t\t\t\t\tinitial,\n\t\t\t\t\t\tsystemOptionValue,\n\t\t\t\t\t\tmediaQuery,\n\t\t\t\t\t\toptionWhenMatched,\n\t\t\t\t\t\toptionWhenNotMatched,\n\t\t\t\t\t],\n\t\t\t\t) => {\n\t\t\t\t\tconst currentValue = persisted.themes?.[themeKey] ?? initial\n\n\t\t\t\t\tacc.resolvedThemes[themeKey] = acc.themes[themeKey] = currentValue\n\n\t\t\t\t\tif (mediaQuery != null) {\n\t\t\t\t\t\tconst pair = persisted.systemOptions?.[themeKey] ?? [\n\t\t\t\t\t\t\toptionWhenMatched!,\n\t\t\t\t\t\t\toptionWhenNotMatched!,\n\t\t\t\t\t\t]\n\n\t\t\t\t\t\tconst systemTheme = matchMedia(mediaQuery).matches\n\t\t\t\t\t\t\t? pair[0]\n\t\t\t\t\t\t\t: pair[1]\n\n\t\t\t\t\t\tacc.resolvedSystemThemes[themeKey] = systemTheme\n\n\t\t\t\t\t\tif (currentValue === systemOptionValue) {\n\t\t\t\t\t\t\tacc.resolvedThemes[themeKey] = systemTheme\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn acc\n\t\t\t\t},\n\t\t\t\t{ themes: {}, resolvedThemes: {}, resolvedSystemThemes: {} },\n\t\t\t),\n\t\t)\n\t})\n}\n\nexport type ThemeScriptParameter = {\n\t/** `localStorage` key; defaults to the 'resonare'. */\n\tkey?: string\n\tconfig: ThemeStoreConfig\n\thandler: Listener<any>\n}\n\nexport function createInlineThemeScript(\n\tthemeScriptParameters: ThemeScriptParameter | Array<ThemeScriptParameter>,\n) {\n\tconst serializedArgs = (\n\t\tArray.isArray(themeScriptParameters)\n\t\t\t? themeScriptParameters\n\t\t\t: [themeScriptParameters]\n\t).map(({ key = DEFAULT_KEY, config, handler }) => {\n\t\tconst flattenedConfig = Object.entries(config).map(\n\t\t\t([themeKey, { options, defaultValue }]) => {\n\t\t\t\tconst firstOption = options?.[0]\n\n\t\t\t\tconst resolvedInitialValue =\n\t\t\t\t\tdefaultValue ??\n\t\t\t\t\t(typeof firstOption === 'object' ? firstOption.value : firstOption!)\n\n\t\t\t\tconst systemOption = options?.find(\n\t\t\t\t\t(option): option is Required<ThemeOption> =>\n\t\t\t\t\t\ttypeof option === 'object' && !!option.media,\n\t\t\t\t)\n\n\t\t\t\tif (systemOption) {\n\t\t\t\t\tconst { value, media } = systemOption\n\n\t\t\t\t\treturn [themeKey, resolvedInitialValue, value, ...media]\n\t\t\t\t}\n\n\t\t\t\treturn [themeKey, resolvedInitialValue]\n\t\t\t},\n\t\t)\n\n\t\treturn `['${key}',${JSON.stringify(flattenedConfig)},${handler}]`\n\t})\n\n\treturn `(${restoreScript})([${serializedArgs}])`\n}\n"],"mappings":"wDA+GA,SAAgBsC,EAAgDC,EAAW,CAC1E,OAAOC,OAAOC,QAAQF,CAAM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OAAOA,GAAW,UAAY,CAAC,CAACA,EAAOrC,KACzC,EAEA,GAAIoD,EAAc,CACjB,GAAM,CAAEtD,QAAOE,SAAUoD,EAEzB,MAAO,CAACjB,EAAUmF,EAAsBxH,EAAO,GAAGE,CAAK,CACxD,CAEA,MAAO,CAACmC,EAAUmF,CAAoB,CACvC,CACD,EAEA,MAAO,KAAKpE,EAAG,IAAKmB,KAAKC,UAAUgC,CAAe,EAAC,GAAIC,EAAO,EAC/D,CAE8BY,EAAc,GAC7C"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["DEFAULT_KEY","toScriptJson","localStorageAdapter","getThemesAndOptions","config","themeKey","themeConfig","option","getDefaultThemes","createThemeStore","createStorage","defaultThemes","currentThemes","persisted","systemOptions","systemOption","mediaQuery","optionWhenMatched","optionWhenNotMatched","defaultPair","mediaQueryLists","listeners","abortController","storage","resolveSystemOption","server","pair","mqList","setThemesAndNotify","createSnapshot","resolvedSystemThemes","resolvedThemes","serverSnapshot","snapshot","themes","listener","applyStored","parsed","stored","toPersist","pairs","setThemes","callback","restoreScript","params","key","flattenedConfig","handler","initial","systemOptionValue","currentValue","systemTheme","acc","createInlineThemeScript","serializedArgs","themeScriptParameters","firstOption","options","resolvedInitialValue","defaultValue","value","media","toScriptFunction","memoryStorageAdapter"],"sources":["../src/index.ts"],"sourcesContent":["import { DEFAULT_KEY } from './constants'\nimport { toScriptFunction, toScriptJson } from './script'\nimport { type StorageAdapterCreate, localStorageAdapter } from './storage'\n\nexport * from './storage'\n\ntype ThemeValue = string | number | boolean\n\ntype ThemeOption<T extends ThemeValue = string> = {\n value: T\n media?: readonly [string, T, T]\n}\n\ntype ThemeConfig<T extends ThemeValue = string> =\n | {\n options: ReadonlyArray<T | ThemeOption<T>>\n defaultValue?: T\n }\n | { defaultValue: T; options?: never }\n\n/**\n * Config for `createThemeStore`. Each key is a theme setting (`colorMode`,\n * `contrast`, …) with either:\n *\n * - `options`: the allowed values, plus an optional `defaultValue` (otherwise the\n * first option). One option may be a `{ value, media }` object: when it is\n * selected, the setting follows a media query, using `media[1]` while the\n * query matches and `media[2]` while it doesn't.\n * - `defaultValue` alone: a free value of that type, such as a number.\n *\n * @example\n * const config = {\n * colorMode: {\n * options: [\n * 'light',\n * 'dark',\n * {\n * value: 'system',\n * media: ['(prefers-color-scheme: dark)', 'dark', 'light'],\n * },\n * ],\n * defaultValue: 'system',\n * },\n * contrast: {\n * options: ['normal', 'more'],\n * },\n * sidebarWidth: {\n * defaultValue: 240,\n * },\n * } as const satisfies ThemeStoreConfig\n */\nexport type ThemeStoreConfig = Record<\n string,\n ThemeConfig<string> | ThemeConfig<number> | ThemeConfig<boolean>\n>\n\n/**\n * The selected value of each setting, as passed to `setThemes` (e.g.\n * `colorMode: 'system'`). Inferred from your `ThemeStoreConfig`.\n */\nexport type Themes<T extends ThemeStoreConfig> = {\n [K in keyof T]: T[K] extends { options: ReadonlyArray<infer U> }\n ? U extends ThemeOption\n ? U['value']\n : U\n : T[K] extends { defaultValue: infer U }\n ? U extends string\n ? string\n : U extends number\n ? number\n : boolean\n : never\n}\n\ntype ThemeKeysWithSystemOption<T extends ThemeStoreConfig> = {\n [K in keyof T]: T[K] extends { options: ReadonlyArray<infer U> }\n ? U extends { media: ReadonlyArray<unknown> }\n ? K\n : never\n : never\n}[keyof T]\n\ntype NonSystemOptionValues<\n T extends ThemeStoreConfig,\n K extends keyof T,\n> = T[K] extends { options: ReadonlyArray<infer U> }\n ? U extends ThemeValue\n ? U\n : U extends ThemeOption\n ? U extends { media: ReadonlyArray<unknown> }\n ? never\n : U['value']\n : never\n : never\n\n/**\n * For each setting with a `media` option, the value that option currently\n * resolves to, whether or not it is selected.\n */\ntype ResolvedSystemThemes<T extends ThemeStoreConfig> = {\n [K in ThemeKeysWithSystemOption<T>]: NonSystemOptionValues<T, K>\n}\n\n/** Runtime state for each dimension that defines a `media` option. */\ntype SystemOptions = Record<\n string,\n {\n value: string\n mediaQuery: string\n pair: [string, string]\n defaultPair: [string, string]\n }\n>\n\ntype Snapshot<T extends ThemeStoreConfig> = {\n themes: Themes<T>\n resolvedThemes: Themes<T>\n resolvedSystemThemes: ResolvedSystemThemes<T>\n}\n\ntype Listener<T extends ThemeStoreConfig> = (value: Snapshot<T>) => void\n\ntype PersistedSystemOptionPairs = Record<string, [string, string]>\n\ntype PersistedState<T extends ThemeStoreConfig> = {\n themes: Partial<Themes<T>>\n systemOptions: PersistedSystemOptionPairs\n}\n\ntype ThemeStoreOptions<T extends ThemeStoreConfig> = {\n persisted?: Partial<PersistedState<T>>\n storage?: StorageAdapterCreate | null\n}\n\ntype ThemeAndOptions<T extends ThemeStoreConfig> = Array<\n {\n [K in keyof T]: [\n K,\n Array<\n T[K] extends { options: ReadonlyArray<infer U> }\n ? U extends ThemeOption\n ? U['value']\n : U\n : never\n >,\n ]\n }[keyof T]\n>\n\n/**\n * Lists each setting with its option values (`{ value, media }` options become\n * their `value`). Useful for rendering a picker for every setting.\n *\n * @example\n * import { getThemesAndOptions } from 'resonare'\n *\n * getThemesAndOptions({\n * colorMode: {\n * options: [\n * 'light',\n * 'dark',\n * {\n * value: 'system',\n * media: ['(prefers-color-scheme: dark)', 'dark', 'light'],\n * },\n * ],\n * },\n * })\n * // [['colorMode', ['light', 'dark', 'system']]]\n */\nexport function getThemesAndOptions<T extends ThemeStoreConfig>(config: T) {\n return Object.entries(config).map(([themeKey, themeConfig]) => {\n return [\n themeKey,\n (themeConfig.options || []).map((option) => {\n return typeof option === 'object' ? option.value : option\n }),\n ]\n }) as ThemeAndOptions<T>\n}\n\n/**\n * Returns each setting's default: its `defaultValue`, or else its first\n * option.\n *\n * @example\n * import { getDefaultThemes } from 'resonare'\n *\n * getDefaultThemes({\n * colorMode: { options: ['light', 'dark'], defaultValue: 'dark' },\n * }) // { colorMode: 'dark' }\n */\nexport function getDefaultThemes<T extends ThemeStoreConfig>(config: T) {\n return Object.fromEntries(\n Object.entries(config).map(([themeKey, themeConfig]) => {\n return [\n themeKey,\n themeConfig.defaultValue ??\n (typeof themeConfig.options[0] === 'object'\n ? themeConfig.options[0].value\n : themeConfig.options[0]),\n ]\n })\n ) as Themes<T>\n}\n\n/**\n * Creates a store for the settings in `config`. It saves changes through\n * `storage` (`localStorage` under the key `'resonare'` by default; `null`\n * disables saving), resolves `media` options in the browser, and notifies\n * subscribers.\n *\n * Read state with `getSnapshot()` or `subscribe`.\n *\n * @example\n * import { createThemeStore, localStorageAdapter } from 'resonare'\n *\n * const store = createThemeStore(\n * {\n * colorMode: {\n * options: [\n * 'light',\n * 'dark',\n * {\n * value: 'system',\n * media: ['(prefers-color-scheme: dark)', 'dark', 'light'],\n * },\n * ],\n * defaultValue: 'system',\n * },\n * },\n * { storage: localStorageAdapter({ key: 'my-app' }) }\n * )\n *\n * // In the browser: load the saved choice and follow other tabs.\n * store.restore()\n * store.sync()\n *\n * store.subscribe(({ resolvedThemes }) => {\n * document.documentElement.dataset.colorMode = resolvedThemes.colorMode\n * })\n *\n * store.setThemes({ colorMode: 'dark' })\n */\nexport function createThemeStore<T extends ThemeStoreConfig>(\n config: T,\n {\n persisted = {},\n storage: createStorage = localStorageAdapter({ key: DEFAULT_KEY }),\n }: ThemeStoreOptions<T> = {}\n) {\n const defaultThemes = getDefaultThemes(config)\n\n let currentThemes: Themes<T> = { ...defaultThemes, ...persisted.themes }\n\n const systemOptions: SystemOptions = {}\n\n for (const themeKey in config) {\n const systemOption = (config[themeKey]!.options || []).find(\n (option): option is Required<ThemeOption> => {\n return typeof option === 'object' && !!option.media\n }\n )\n\n if (systemOption) {\n const {\n value,\n media: [mediaQuery, optionWhenMatched, optionWhenNotMatched],\n } = systemOption\n\n const defaultPair: [string, string] = [\n optionWhenMatched,\n optionWhenNotMatched,\n ]\n\n systemOptions[themeKey] = {\n value,\n mediaQuery,\n pair: [...(persisted.systemOptions?.[themeKey] ?? defaultPair)],\n defaultPair,\n }\n }\n }\n\n const mediaQueryLists: Record<string, MediaQueryList> = {}\n\n const listeners = new Set<Listener<T>>()\n\n const abortController = new AbortController()\n\n const storage = createStorage?.({ abortController })\n\n const resolveSystemOption = (themeKey: string, server?: boolean): string => {\n const { mediaQuery, pair } = systemOptions[themeKey]!\n\n // No media support (e.g. on the server): use the \"not matched\" value.\n if (server || typeof matchMedia === 'undefined') {\n return pair[1]\n }\n\n let mqList = mediaQueryLists[mediaQuery]\n\n if (!mqList) {\n mqList = mediaQueryLists[mediaQuery] = matchMedia(mediaQuery)\n\n mqList.addEventListener(\n 'change',\n () => {\n setThemesAndNotify()\n },\n abortController\n )\n }\n\n return pair[mqList.matches ? 0 : 1]\n }\n\n const createSnapshot = (server?: boolean): Snapshot<T> => {\n const resolvedSystemThemes: Record<string, string> = {}\n\n const resolvedThemes = { ...currentThemes } as Record<string, ThemeValue>\n\n for (const themeKey in systemOptions) {\n resolvedSystemThemes[themeKey] = resolveSystemOption(themeKey, server)\n\n if (resolvedThemes[themeKey] === systemOptions[themeKey]!.value) {\n resolvedThemes[themeKey] = resolvedSystemThemes[themeKey]\n }\n }\n\n return {\n themes: currentThemes,\n resolvedThemes: resolvedThemes as Themes<T>,\n resolvedSystemThemes: resolvedSystemThemes as ResolvedSystemThemes<T>,\n }\n }\n\n const serverSnapshot = createSnapshot(true)\n\n let snapshot = createSnapshot()\n\n const setThemesAndNotify = (themes?: Partial<Themes<T>>): void => {\n currentThemes = { ...currentThemes, ...themes }\n\n snapshot = createSnapshot()\n\n for (const listener of listeners) {\n listener(snapshot)\n }\n }\n\n /** Applies saved state; ignores empty or malformed input. */\n const applyStored = (stored: string | null | undefined): void => {\n let parsed: PersistedState<T> | undefined\n\n try {\n parsed = stored ? JSON.parse(stored) : undefined\n } catch {}\n\n if (!parsed?.themes || typeof parsed.themes !== 'object') {\n return\n }\n\n for (const themeKey in parsed.systemOptions) {\n const pair = parsed.systemOptions[themeKey]\n\n if (pair && systemOptions[themeKey]) {\n systemOptions[themeKey].pair = pair\n }\n }\n\n setThemesAndNotify(parsed.themes)\n }\n\n const toPersist = (): PersistedState<T> => {\n const pairs: PersistedSystemOptionPairs = {}\n\n for (const themeKey in systemOptions) {\n pairs[themeKey] = [...systemOptions[themeKey]!.pair]\n }\n\n return { themes: { ...currentThemes }, systemOptions: pairs }\n }\n\n const setThemes = (\n themes:\n | Partial<Themes<T>>\n | ((currentThemes: Themes<T>) => Partial<Themes<T>>)\n ): void => {\n setThemesAndNotify(\n typeof themes === 'function' ? themes(currentThemes) : themes\n )\n\n if (storage) {\n const stored = JSON.stringify(toPersist())\n\n storage.set(stored)\n\n storage.broadcast?.(stored)\n }\n }\n\n return {\n /**\n * Current state:\n *\n * - `themes`: the selected value of each setting (e.g. `'system'`).\n * - `resolvedThemes`: the same with `media` options resolved (e.g. `'dark'`).\n * - `resolvedSystemThemes`: what each `media` option resolves to, even when\n * it isn't selected.\n *\n * Returns the same object until the next change.\n */\n getSnapshot: (): Snapshot<T> => {\n return snapshot\n },\n\n /**\n * The state as rendered on the server: defaults plus `persisted`, with\n * `media` options resolved to their \"not matched\" value. It never changes,\n * so hydration can match the server HTML even after `restore()`.\n */\n getServerSnapshot: (): Snapshot<T> => {\n return serverSnapshot\n },\n\n /**\n * Updates the given settings (others keep their value), saves to storage,\n * and notifies subscribers.\n *\n * @example\n * store.setThemes({ colorMode: 'dark' })\n *\n * store.setThemes((current) => ({\n * colorMode: current.colorMode === 'dark' ? 'light' : 'dark',\n * }))\n */\n setThemes,\n\n /**\n * Changes which values a `media` option resolves to: `[whenMatched,\n * whenNotMatched]`, replacing `media[1]` and `media[2]` from the config.\n * Saved like `setThemes`.\n *\n * @example\n * // system now picks 'dark-dimmed' when the OS prefers dark\n * store.updateSystemOption('colorMode', ['dark-dimmed', 'light'])\n */\n updateSystemOption: <K extends ThemeKeysWithSystemOption<T>>(\n themeKey: K,\n pair: [NonSystemOptionValues<T, K>, NonSystemOptionValues<T, K>]\n ): void => {\n systemOptions[themeKey as string]!.pair = [...pair]\n\n setThemes({})\n },\n\n /**\n * The state to save, e.g. to a database: pass it back as `persisted` when\n * creating the store.\n */\n toPersist,\n\n /**\n * Loads the saved state from storage. Does nothing when nothing is saved.\n * Call it in the browser, e.g. after hydration.\n */\n restore: (): void => {\n applyStored(storage?.get())\n },\n\n /**\n * Calls `callback` with the new state (see `getSnapshot`) after every\n * change, including OS preference changes. Returns an unsubscribe\n * function.\n */\n subscribe: (callback: Listener<T>): (() => void) => {\n listeners.add(callback)\n\n return () => {\n listeners.delete(callback)\n }\n },\n\n /**\n * Applies changes saved by other tabs (if the storage adapter supports\n * it); when another tab clears the saved state, resets to the config\n * defaults. Returns a function that stops syncing, or `undefined` when the\n * adapter can't sync.\n */\n sync: (): (() => void) | undefined => {\n return storage?.watch?.((stored) => {\n // `null` means another tab cleared the stored preferences.\n if (stored === null) {\n for (const themeKey in systemOptions) {\n const systemOption = systemOptions[themeKey]!\n\n systemOption.pair = systemOption.defaultPair\n }\n\n setThemesAndNotify(defaultThemes)\n } else {\n applyStored(stored)\n }\n })\n },\n\n /**\n * Removes all subscribers and stops listening to media queries and\n * storage. The store doesn't update after this.\n */\n destroy: (): void => {\n listeners.clear()\n\n abortController.abort()\n },\n }\n}\n\nexport type ThemeStore<T extends ThemeStoreConfig> = ReturnType<\n typeof createThemeStore<T>\n>\n\nconst restoreScript = <T extends ThemeValue>(\n params: Array<\n [string, Array<[string, T] | [string, T, T, string, T, T]>, Listener<any>]\n >\n) => {\n params.forEach(([key, flattenedConfig, handler]) => {\n let persisted: any\n\n // Blocked storage or bad JSON must not stop the handler from running.\n try {\n persisted = JSON.parse(localStorage.getItem(key)!)\n } catch {}\n\n handler(\n flattenedConfig.reduce<{\n themes: Record<string, ThemeValue>\n resolvedThemes: Record<string, ThemeValue>\n resolvedSystemThemes: Record<string, ThemeValue>\n }>(\n (\n acc,\n [\n themeKey,\n initial,\n systemOptionValue,\n mediaQuery,\n optionWhenMatched,\n optionWhenNotMatched,\n ]\n ) => {\n const currentValue = persisted?.themes?.[themeKey] ?? initial\n\n acc.resolvedThemes[themeKey] = acc.themes[themeKey] = currentValue\n\n if (mediaQuery != null) {\n const pair = persisted?.systemOptions?.[themeKey] ?? [\n optionWhenMatched!,\n optionWhenNotMatched!,\n ]\n\n const systemTheme = matchMedia(mediaQuery).matches\n ? pair[0]\n : pair[1]\n\n acc.resolvedSystemThemes[themeKey] = systemTheme\n\n if (currentValue === systemOptionValue) {\n acc.resolvedThemes[themeKey] = systemTheme\n }\n }\n\n return acc\n },\n { themes: {}, resolvedThemes: {}, resolvedSystemThemes: {} }\n )\n )\n })\n}\n\n/** One store for `createInlineThemeScript`. */\nexport type ThemeScriptParameter = {\n /** The store's `localStorage` key; defaults to `'resonare'`. */\n key?: string\n /** The same config passed to `createThemeStore`. */\n config: ThemeStoreConfig\n /**\n * Applies the state to the page; receives the same value as `subscribe`.\n * It is copied into the script as source code, so it can't use variables\n * from outside the function.\n */\n handler: Listener<any>\n}\n\n/**\n * Returns the source of a small script that reads the saved state from\n * `localStorage` and runs `handler` with it. Put it in an inline `<script>` in\n * the `<head>` so the page has the right theme before first paint. Pass an\n * array to set up several stores.\n *\n * Use the same handler with `store.subscribe` to keep the page updated after\n * that.\n *\n * @example\n * import { createInlineThemeScript } from 'resonare'\n *\n * // config: the same object passed to createThemeStore\n * const themeScript = createInlineThemeScript({\n * key: 'my-app',\n * config,\n * handler: ({ resolvedThemes }) => {\n * const root = document.documentElement\n * root.dataset.colorMode = resolvedThemes.colorMode\n * },\n * })\n *\n * // In <head>:\n * // <script dangerouslySetInnerHTML={{ __html: themeScript }} />\n */\nexport function createInlineThemeScript(\n themeScriptParameters: ThemeScriptParameter | Array<ThemeScriptParameter>\n) {\n const serializedArgs = (\n Array.isArray(themeScriptParameters)\n ? themeScriptParameters\n : [themeScriptParameters]\n ).map(({ key = DEFAULT_KEY, config, handler }) => {\n const flattenedConfig = Object.entries(config).map(\n ([themeKey, { options, defaultValue }]) => {\n const firstOption = options?.[0]\n\n const resolvedInitialValue =\n defaultValue ??\n (typeof firstOption === 'object' ? firstOption.value : firstOption!)\n\n const systemOption = options?.find(\n (option): option is Required<ThemeOption> => {\n return typeof option === 'object' && !!option.media\n }\n )\n\n if (systemOption) {\n const { value, media } = systemOption\n\n return [themeKey, resolvedInitialValue, value, ...media]\n }\n\n return [themeKey, resolvedInitialValue]\n }\n )\n\n return `[${toScriptJson(key)},${toScriptJson(flattenedConfig)},${toScriptFunction(handler)}]`\n })\n\n return `(${restoreScript})([${serializedArgs}])`\n}\n"],"mappings":"A,4DA0KA,SAAgBG,EAAgD,EAAW,CACzE,OAAO,OAAO,QAAQC,CAAM,CAAC,CAAC,KAAK,CAACC,EAAUC,KACrC,CACLD,GACCC,EAAY,SAAW,CAAC,EAAA,CAAG,IAAK,GACxB,OAAOC,GAAW,SAAWA,EAAO,MAAQA,CACpD,CACH,CACD,CACH,CAaA,SAAgBC,EAA6C,EAAW,CACtE,OAAO,OAAO,YACZ,OAAO,QAAQJ,CAAM,CAAC,CAAC,KAAK,CAACC,EAAUC,KAC9B,CACLD,EACAC,EAAY,eACT,OAAOA,EAAY,QAAQ,IAAO,SAC/BA,EAAY,QAAQ,EAAE,CAAC,MACvBA,EAAY,QAAQ,GAC5B,CACD,CACH,CACF,CAwCA,SAAgBG,EACd,EACA,CACE,YAAY,CAAC,EACb,QAASC,EAAgBR,EAAoB,CAAE,IAAKF,CAAY,CAAC,GACzC,CAAC,EAC3B,CACA,IAAMW,EAAgBH,EAAiBJ,CAAM,EAEzCQ,EAA2B,CAAE,GAAGD,EAAe,GAAGE,EAAU,MAAO,EAEjEC,EAA+B,CAAC,EAEtC,IAAK,IAAMT,KAAYD,EAAQ,CAC7B,IAAMW,GAAgBX,EAAOC,EAAS,CAAE,SAAW,CAAC,EAAA,CAAG,KACpD,GACQ,OAAOE,GAAW,UAAY,CAAC,CAACA,EAAO,KAElD,EAEA,GAAIQ,EAAc,CAChB,GAAM,CACJ,QACA,MAAO,CAACC,EAAYC,EAAmBC,IACrCH,EAEEI,EAAgC,CACpCF,EACAC,CACF,EAEA,EAAcb,GAAY,CACxB,QACA,a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-react.d.ts
CHANGED
|
@@ -1,36 +1,41 @@
|
|
|
1
1
|
import { ThemeStore } from "./lite.js";
|
|
2
|
-
|
|
3
|
-
//#region src/lite-react.d.ts
|
|
4
2
|
/**
|
|
5
|
-
*
|
|
3
|
+
* Returns the store's current state (see `store.getSnapshot()`) and its
|
|
4
|
+
* methods, and re-renders the component when the state changes. Server
|
|
5
|
+
* rendering and hydration are handled automatically.
|
|
6
|
+
*
|
|
6
7
|
* @example
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* import { useResonare } from 'resonare/lite/react'
|
|
8
|
+
* import { createThemeStore } from 'resonare/lite'
|
|
9
|
+
* import { useResonare } from 'resonare/lite/react'
|
|
10
10
|
*
|
|
11
|
-
*
|
|
11
|
+
* const store = createThemeStore()
|
|
12
12
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
13
|
+
* function ThemeToggle() {
|
|
14
|
+
* const { resolvedTheme, setTheme } = useResonare(store)
|
|
15
15
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* return (
|
|
17
|
+
* <button
|
|
18
|
+
* onClick={() => {
|
|
19
|
+
* setTheme(resolvedTheme === 'dark' ? 'light' : 'dark')
|
|
20
|
+
* }}
|
|
21
|
+
* >
|
|
22
|
+
* {resolvedTheme}
|
|
23
|
+
* </button>
|
|
24
|
+
* )
|
|
25
|
+
* }
|
|
19
26
|
*/
|
|
20
|
-
declare function useResonare(store: ThemeStore): {
|
|
27
|
+
export declare function useResonare(store: ThemeStore): {
|
|
28
|
+
theme: "dark" | "light" | "system";
|
|
29
|
+
resolvedTheme: "dark" | "light";
|
|
30
|
+
systemTheme: "dark" | "light";
|
|
21
31
|
destroy: () => void;
|
|
22
32
|
restore: () => void;
|
|
23
|
-
setTheme: (
|
|
33
|
+
setTheme: (next: ("dark" | "light" | "system") | ((current: "dark" | "light" | "system") => "dark" | "light" | "system")) => void;
|
|
24
34
|
subscribe: (callback: (value: {
|
|
25
|
-
theme: "
|
|
26
|
-
resolvedTheme: "
|
|
27
|
-
systemTheme: "
|
|
35
|
+
theme: "dark" | "light" | "system";
|
|
36
|
+
resolvedTheme: "dark" | "light";
|
|
37
|
+
systemTheme: "dark" | "light";
|
|
28
38
|
}) => void) => (() => void);
|
|
29
39
|
sync: () => (() => void) | undefined;
|
|
30
|
-
theme: "system" | "light" | "dark";
|
|
31
|
-
resolvedTheme: "light" | "dark";
|
|
32
|
-
systemTheme: "light" | "dark";
|
|
33
40
|
};
|
|
34
|
-
//#endregion
|
|
35
|
-
export { useResonare };
|
|
36
41
|
//# sourceMappingURL=lite-react.d.ts.map
|
package/dist/lite-react.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lite-react.d.ts","names":[],"sources":["../src/lite-react.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"lite-react.d.ts","names":[],"sources":["../src/lite-react.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;wBA6BgB,YAAY,OAAO"}
|
package/dist/lite-react.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{c as e}from"react
|
|
1
|
+
import{c as e}from"react/compiler-runtime";import*as t from"react";function n(n){let r=e(7),{destroy:i,getServerSnapshot:a,getSnapshot:o,restore:s,setTheme:c,subscribe:l,sync:u}=n,d=t.useSyncExternalStore(l,o,a),f;return r[0]!==i||r[1]!==s||r[2]!==c||r[3]!==d||r[4]!==l||r[5]!==u?(f={...d,destroy:i,restore:s,setTheme:c,subscribe:l,sync:u},r[0]=i,r[1]=s,r[2]=c,r[3]=d,r[4]=l,r[5]=u,r[6]=f):f=r[6],f}export{n as useResonare};
|
|
2
2
|
//# sourceMappingURL=lite-react.js.map
|
package/dist/lite-react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lite-react.js","names":["
|
|
1
|
+
{"version":3,"file":"lite-react.js","names":["{ ...snapshot, destroy, restore, setTheme, subscribe, sync }","_c","React","useResonare","$","c","store","snapshot","subscribe","getSnapshot","getServerSnapshot","t0","destroy","restore","setTheme","sync"],"sources":["../src/lite-react.ts"],"sourcesContent":["import * as React from 'react'\n\nimport type { ThemeStore } from './lite'\n\n/**\n * Returns the store's current state (see `store.getSnapshot()`) and its\n * methods, and re-renders the component when the state changes. Server\n * rendering and hydration are handled automatically.\n *\n * @example\n * import { createThemeStore } from 'resonare/lite'\n * import { useResonare } from 'resonare/lite/react'\n *\n * const store = createThemeStore()\n *\n * function ThemeToggle() {\n * const { resolvedTheme, setTheme } = useResonare(store)\n *\n * return (\n * <button\n * onClick={() => {\n * setTheme(resolvedTheme === 'dark' ? 'light' : 'dark')\n * }}\n * >\n * {resolvedTheme}\n * </button>\n * )\n * }\n */\nexport function useResonare(store: ThemeStore) {\n const {\n destroy,\n getServerSnapshot,\n getSnapshot,\n restore,\n setTheme,\n subscribe,\n sync,\n } = store\n\n const snapshot = React.useSyncExternalStore(\n subscribe,\n getSnapshot,\n getServerSnapshot\n )\n\n return { ...snapshot, destroy, restore, setTheme, subscribe, sync }\n}\n"],"mappings":"A,mEA6BA,SAAgBG,EAAY,EAAmB,C,WACvC,CACJ,UACA,oBACA,cACA,UACA,WACA,YACA,QACEG,EAEEC,EAAWL,EAAM,qBACrBM,EACAC,EACAC,CACF,EAEOV,EAAP,OAAsBI,EAAA,KAAAQ,GAAAR,EAAA,KAASS,GAAAT,EAAA,KAASU,GAAAV,EAAA,KAA5BG,GAAAH,EAAA,KAAsCI,GAAAJ,EAAA,KAAWW,GAAtD,EAAA,CAAE,GAAGR,EAAU,UAAS,UAAS,WAAU,YAAW,MAAK,EAA5C,EAAA,GAAAK,EAAS,EAAA,GAAAC,EAAS,EAAA,GAAAC,EAA5B,EAAA,GAAAP,EAAsC,EAAA,GAAAC,EAAW,EAAA,GAAAO,E,eAAtDf,CACT,C"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ThemeStore } from "./lite.js";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the store's state as signals (`theme()`, `resolvedTheme()`,
|
|
4
|
+
* `systemTheme()`) and its methods. Call it inside a component (or
|
|
5
|
+
* `createRoot`); the subscription ends when that owner is cleaned up. Server
|
|
6
|
+
* rendering and hydration are handled automatically.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import { createThemeStore } from 'resonare/lite'
|
|
10
|
+
* import { useResonare } from 'resonare/lite/solid'
|
|
11
|
+
*
|
|
12
|
+
* const store = createThemeStore()
|
|
13
|
+
*
|
|
14
|
+
* function ThemeToggle() {
|
|
15
|
+
* const { resolvedTheme, setTheme } = useResonare(store)
|
|
16
|
+
*
|
|
17
|
+
* return (
|
|
18
|
+
* <button
|
|
19
|
+
* onClick={() => {
|
|
20
|
+
* setTheme(resolvedTheme() === 'dark' ? 'light' : 'dark')
|
|
21
|
+
* }}
|
|
22
|
+
* >
|
|
23
|
+
* {resolvedTheme()}
|
|
24
|
+
* </button>
|
|
25
|
+
* )
|
|
26
|
+
* }
|
|
27
|
+
*/
|
|
28
|
+
export declare function useResonare(store: ThemeStore): {
|
|
29
|
+
theme: () => "dark" | "light" | "system";
|
|
30
|
+
resolvedTheme: () => "dark" | "light";
|
|
31
|
+
systemTheme: () => "dark" | "light";
|
|
32
|
+
destroy: () => void;
|
|
33
|
+
restore: () => void;
|
|
34
|
+
setTheme: (next: ("dark" | "light" | "system") | ((current: "dark" | "light" | "system") => "dark" | "light" | "system")) => void;
|
|
35
|
+
subscribe: (callback: (value: {
|
|
36
|
+
theme: "dark" | "light" | "system";
|
|
37
|
+
resolvedTheme: "dark" | "light";
|
|
38
|
+
systemTheme: "dark" | "light";
|
|
39
|
+
}) => void) => (() => void);
|
|
40
|
+
sync: () => (() => void) | undefined;
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=lite-solid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lite-solid.d.ts","names":[],"sources":["../src/lite-solid.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;wBA8BgB,YAAY,OAAO"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{createSignal as e,onCleanup as t,onMount as n,sharedConfig as r}from"solid-js";function i(i){let{destroy:a,getServerSnapshot:o,getSnapshot:s,restore:c,setTheme:l,subscribe:u,sync:d}=i,f=!!r.context,[p,m]=e(f?o():s()),h=u(e=>{m(()=>e)});return t(h),f&&n(()=>{m(()=>s())}),{theme:()=>p().theme,resolvedTheme:()=>p().resolvedTheme,systemTheme:()=>p().systemTheme,destroy:a,restore:c,setTheme:l,subscribe:u,sync:d}}export{i as useResonare};
|
|
2
|
+
//# sourceMappingURL=lite-solid.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lite-solid.js","names":["useResonare","store","hydrating","sharedConfig","snapshot","setSnapshot","createSignal","getServerSnapshot","getSnapshot","unsubscribe","subscribe","value","onCleanup","onMount"],"sources":["../src/lite-solid.ts"],"sourcesContent":["import { createSignal, onCleanup, onMount, sharedConfig } from 'solid-js'\n\nimport type { ThemeStore } from './lite'\n\n/**\n * Returns the store's state as signals (`theme()`, `resolvedTheme()`,\n * `systemTheme()`) and its methods. Call it inside a component (or\n * `createRoot`); the subscription ends when that owner is cleaned up. Server\n * rendering and hydration are handled automatically.\n *\n * @example\n * import { createThemeStore } from 'resonare/lite'\n * import { useResonare } from 'resonare/lite/solid'\n *\n * const store = createThemeStore()\n *\n * function ThemeToggle() {\n * const { resolvedTheme, setTheme } = useResonare(store)\n *\n * return (\n * <button\n * onClick={() => {\n * setTheme(resolvedTheme() === 'dark' ? 'light' : 'dark')\n * }}\n * >\n * {resolvedTheme()}\n * </button>\n * )\n * }\n */\nexport function useResonare(store: ThemeStore) {\n const {\n destroy,\n getServerSnapshot,\n getSnapshot,\n restore,\n setTheme,\n subscribe,\n sync,\n } = store\n\n // Match the server HTML while rendering on the server or hydrating, then\n // switch to the live snapshot once mounted.\n const hydrating = !!sharedConfig.context\n\n const [snapshot, setSnapshot] = createSignal(\n hydrating ? getServerSnapshot() : getSnapshot()\n )\n\n const unsubscribe = subscribe((value) => {\n setSnapshot(() => {\n return value\n })\n })\n\n onCleanup(unsubscribe)\n\n if (hydrating) {\n onMount(() => {\n setSnapshot(() => {\n return getSnapshot()\n })\n })\n }\n\n return {\n theme: () => {\n return snapshot().theme\n },\n resolvedTheme: () => {\n return snapshot().resolvedTheme\n },\n systemTheme: () => {\n return snapshot().systemTheme\n },\n destroy,\n restore,\n setTheme,\n subscribe,\n sync,\n }\n}\n"],"mappings":"A,sFA8BA,SAAgBA,EAAY,EAAmB,CAC7C,GAAM,CACJ,UACA,oBACA,cACA,UACA,WACA,YACA,QACEC,EAIEC,EAAY,CAAC,CAACC,EAAa,QAE3B,CAACC,EAAUC,GAAeC,EAC9BJ,EAAYK,EAAkB,EAAIC,EAAY,CAChD,EAEMC,EAAcC,EAAW,GAAU,CACvCL,MACSM,CACR,CACH,CAAC,EAYD,OAVAC,EAAUH,CAAW,EAEjBP,GACFW,MAAc,CACZR,MACSG,EAAY,CACpB,CACH,CAAC,EAGI,CACL,UACSJ,EAAS,CAAC,CAAC,MAEpB,kBACSA,EAAS,CAAC,CAAC,cAEpB,gBACSA,EAAS,CAAC,CAAC,YAEpB,UACA,UACA,WACA,YACA,MACF,CACF,C"}
|