resonare 0.0.10 → 0.0.12
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 +96 -55
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -6
- package/dist/index.js.map +1 -1
- package/dist/inline-script.ts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/resonare.iife.min.js +2 -2
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
# Resonare [](https://www.npmjs.com/package/resonare)
|
|
2
2
|
|
|
3
|
-
A configuration-based theme store for building deeply personal user
|
|
3
|
+
A configuration-based theme store for building deeply personal user interfaces.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- Define and manage multi-dimensional themes,
|
|
7
|
+
- Define and manage multi-dimensional themes, e.g.:
|
|
8
8
|
- Color scheme: system, light, dark
|
|
9
9
|
- Contrast preference: standard, high
|
|
10
10
|
- Spacing: compact, comfortable, spacious
|
|
11
|
-
- etc
|
|
11
|
+
- etc.
|
|
12
12
|
- Framework-agnostic
|
|
13
13
|
- Prevent theme flicker on page load
|
|
14
14
|
- Honor system preferences
|
|
15
15
|
- Create sections with independent theming
|
|
16
16
|
- Sync theme selection across tabs and windows
|
|
17
|
-
- Flexible persistence options, defaulting to localStorage
|
|
18
|
-
-
|
|
17
|
+
- Flexible client-side persistence options, defaulting to localStorage
|
|
18
|
+
- Support server-side persistence
|
|
19
19
|
|
|
20
20
|
## Demo
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
[Check it out](https://resonare.universse.workers.dev).
|
|
23
23
|
|
|
24
24
|
## Installation
|
|
25
25
|
|
|
@@ -35,7 +35,7 @@ pnpm add resonare
|
|
|
35
35
|
|
|
36
36
|
## Basic Usage
|
|
37
37
|
|
|
38
|
-
It's recommended to initialize Resonare in a synchronous script to avoid theme flicker on page load.
|
|
38
|
+
It's recommended to initialize Resonare in a synchronous script to avoid theme flicker on page load. Load via CDN or inline the stringified version to reduce the number of HTTP requests.
|
|
39
39
|
|
|
40
40
|
```html
|
|
41
41
|
<script src="https://unpkg.com/resonare"></script>
|
|
@@ -54,14 +54,14 @@ It's recommended to initialize Resonare in a synchronous script to avoid theme f
|
|
|
54
54
|
},
|
|
55
55
|
'light',
|
|
56
56
|
'dark',
|
|
57
|
-
]
|
|
57
|
+
],
|
|
58
58
|
},
|
|
59
59
|
},
|
|
60
60
|
})
|
|
61
61
|
|
|
62
62
|
themeStore.subscribe(({ resolvedThemes }) => {
|
|
63
|
-
for (const [theme,
|
|
64
|
-
document.documentElement.dataset[theme] =
|
|
63
|
+
for (const [theme, option] of Object.entries(resolvedThemes)) {
|
|
64
|
+
document.documentElement.dataset[theme] = option
|
|
65
65
|
}
|
|
66
66
|
})
|
|
67
67
|
|
|
@@ -71,15 +71,50 @@ It's recommended to initialize Resonare in a synchronous script to avoid theme f
|
|
|
71
71
|
</script>
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
```ts
|
|
75
|
+
import { type ThemeConfig, type ThemeStore } from 'resonare'
|
|
76
|
+
import { resonareInlineScript } from 'resonare/inline-script'
|
|
77
|
+
|
|
78
|
+
const themeConfig = {
|
|
79
|
+
colorScheme: {
|
|
80
|
+
options: [
|
|
81
|
+
{
|
|
82
|
+
value: 'system',
|
|
83
|
+
media: ['(prefers-color-scheme: dark)', 'dark', 'light'],
|
|
84
|
+
},
|
|
85
|
+
'light',
|
|
86
|
+
'dark',
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
} as const satisfies ThemeConfig
|
|
75
90
|
|
|
76
|
-
|
|
77
|
-
{
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
91
|
+
declare module 'resonare' {
|
|
92
|
+
interface ThemeStoreRegistry {
|
|
93
|
+
resonare: ThemeStore<typeof themeConfig>
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function initTheme({ config }: { config: ThemeConfig }) {
|
|
98
|
+
const themeStore = window.resonare.createThemeStore({ config })
|
|
99
|
+
|
|
100
|
+
themeStore.subscribe(({ resolvedThemes }) => {
|
|
101
|
+
Object.entries(resolvedThemes).forEach(([theme, option]) => {
|
|
102
|
+
document.documentElement.dataset[theme] = option
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
themeStore.restore()
|
|
107
|
+
themeStore.sync()
|
|
82
108
|
}
|
|
109
|
+
|
|
110
|
+
export const themeScript = `${resonareInlineScript};
|
|
111
|
+
(${initTheme.toString()})(${JSON.stringify({ config: themeConfig })})`
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Add a triple-slash directive to any `.d.ts` file in your project (e.g. `env.d.ts`):
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
/// <reference types="resonare/global" />
|
|
83
118
|
```
|
|
84
119
|
|
|
85
120
|
## API
|
|
@@ -89,7 +124,7 @@ If you are using TypeScript, add `node_modules/resonare/global.d.ts` to `include
|
|
|
89
124
|
```ts
|
|
90
125
|
import { createThemeStore, type ThemeConfig, type ThemeStore } from 'resonare'
|
|
91
126
|
|
|
92
|
-
const
|
|
127
|
+
const themeConfig = {
|
|
93
128
|
colorScheme: {
|
|
94
129
|
options: [
|
|
95
130
|
{
|
|
@@ -98,7 +133,7 @@ const config = {
|
|
|
98
133
|
},
|
|
99
134
|
'light',
|
|
100
135
|
'dark',
|
|
101
|
-
]
|
|
136
|
+
],
|
|
102
137
|
},
|
|
103
138
|
contrast: {
|
|
104
139
|
options: [
|
|
@@ -114,31 +149,25 @@ const config = {
|
|
|
114
149
|
'high',
|
|
115
150
|
],
|
|
116
151
|
initialValue: 'standard',
|
|
117
|
-
}
|
|
152
|
+
},
|
|
118
153
|
} as const satisfies ThemeConfig
|
|
119
154
|
|
|
120
155
|
declare module 'resonare' {
|
|
121
156
|
interface ThemeStoreRegistry {
|
|
122
|
-
resonare: ThemeStore<typeof
|
|
157
|
+
resonare: ThemeStore<typeof themeConfig>
|
|
123
158
|
}
|
|
124
159
|
}
|
|
125
160
|
|
|
126
|
-
|
|
127
161
|
const themeStore = createThemeStore({
|
|
128
162
|
// optional, default 'resonare'
|
|
129
163
|
// should be unique, also used as client storage key
|
|
130
164
|
key: 'resonare',
|
|
131
165
|
|
|
132
166
|
// required, specify theme and options
|
|
133
|
-
config,
|
|
167
|
+
config: themeConfig,
|
|
134
168
|
|
|
135
|
-
// optional, useful for
|
|
136
|
-
initialState:
|
|
137
|
-
themes: {
|
|
138
|
-
colorScheme: 'dark',
|
|
139
|
-
contrast: 'high',
|
|
140
|
-
},
|
|
141
|
-
},
|
|
169
|
+
// optional, useful for server-side persistence
|
|
170
|
+
initialState: persistedStateFromDb, // persisted state returned by themeStore.toPersist()
|
|
142
171
|
|
|
143
172
|
// optional, specify your own client storage
|
|
144
173
|
// localStorage is used by default
|
|
@@ -157,7 +186,7 @@ const themeStore = createThemeStore({
|
|
|
157
186
|
window.addEventListener(
|
|
158
187
|
'storage',
|
|
159
188
|
(e) => {
|
|
160
|
-
if (e.storageArea !== localStorage) return
|
|
189
|
+
if (e.storageArea !== window.localStorage) return
|
|
161
190
|
|
|
162
191
|
cb(e.key, JSON.parse(e.newValue!))
|
|
163
192
|
},
|
|
@@ -182,8 +211,8 @@ const themeStore = createThemeStore({
|
|
|
182
211
|
```ts
|
|
183
212
|
import { getThemeStore } from 'resonare'
|
|
184
213
|
|
|
185
|
-
//
|
|
186
|
-
const themeStore = getThemeStore('resonare')
|
|
214
|
+
// get an existing theme store by key
|
|
215
|
+
const themeStore = getThemeStore('resonare')
|
|
187
216
|
```
|
|
188
217
|
|
|
189
218
|
### `destroyThemeStore`
|
|
@@ -191,8 +220,8 @@ const themeStore = getThemeStore('resonare')
|
|
|
191
220
|
```ts
|
|
192
221
|
import { destroyThemeStore } from 'resonare'
|
|
193
222
|
|
|
194
|
-
//
|
|
195
|
-
|
|
223
|
+
// destroy an existing theme store by key
|
|
224
|
+
destroyThemeStore('resonare')
|
|
196
225
|
```
|
|
197
226
|
|
|
198
227
|
### ThemeStore Methods
|
|
@@ -206,14 +235,14 @@ interface ThemeStore<T> {
|
|
|
206
235
|
getResolvedThemes(): Record<string, string>
|
|
207
236
|
|
|
208
237
|
// update theme
|
|
209
|
-
setThemes(themes: Partial<Record<string, string>>):
|
|
238
|
+
setThemes(themes: Partial<Record<string, string>>): void
|
|
210
239
|
|
|
211
240
|
// get state to persist, useful for server-side persistence
|
|
212
|
-
// to restore, pass the returned object to initialState
|
|
241
|
+
// to restore, pass the returned object to createThemeStore's initialState
|
|
213
242
|
toPersist(): object
|
|
214
243
|
|
|
215
|
-
// restore persisted
|
|
216
|
-
restore():
|
|
244
|
+
// restore persisted state from client-side storage
|
|
245
|
+
restore(): void
|
|
217
246
|
|
|
218
247
|
// sync theme selection across tabs/windows
|
|
219
248
|
sync(): () => void
|
|
@@ -232,21 +261,36 @@ interface ThemeStore<T> {
|
|
|
232
261
|
}
|
|
233
262
|
```
|
|
234
263
|
|
|
235
|
-
|
|
264
|
+
## Framework Integrations
|
|
265
|
+
|
|
266
|
+
### React
|
|
236
267
|
|
|
237
268
|
Ensure that you have initialized Resonare as per instructions under [Basic Usage](#basic-usage).
|
|
238
269
|
|
|
239
270
|
```tsx
|
|
240
271
|
import * as React from 'react'
|
|
241
|
-
import { getThemesAndOptions } from 'resonare'
|
|
272
|
+
import { getThemesAndOptions, type ThemeConfig } from 'resonare'
|
|
242
273
|
import { useResonare } from 'resonare/react'
|
|
243
274
|
|
|
275
|
+
const themeConfig = {
|
|
276
|
+
colorScheme: {
|
|
277
|
+
options: [
|
|
278
|
+
{
|
|
279
|
+
value: 'system',
|
|
280
|
+
media: ['(prefers-color-scheme: dark)', 'dark', 'light'],
|
|
281
|
+
},
|
|
282
|
+
'light',
|
|
283
|
+
'dark',
|
|
284
|
+
],
|
|
285
|
+
},
|
|
286
|
+
} as const satisfies ThemeConfig
|
|
287
|
+
|
|
244
288
|
function ThemeSelect() {
|
|
245
289
|
const { themes, setThemes } = useResonare(() =>
|
|
246
290
|
window.resonare.getThemeStore(),
|
|
247
291
|
)
|
|
248
292
|
|
|
249
|
-
return getThemesAndOptions(
|
|
293
|
+
return getThemesAndOptions(themeConfig).map(([theme, options]) => (
|
|
250
294
|
<div key={theme}>
|
|
251
295
|
<label htmlFor={theme}>{theme}</label>
|
|
252
296
|
<select
|
|
@@ -268,9 +312,9 @@ function ThemeSelect() {
|
|
|
268
312
|
}
|
|
269
313
|
```
|
|
270
314
|
|
|
271
|
-
|
|
315
|
+
## Server-side Persistence
|
|
272
316
|
|
|
273
|
-
|
|
317
|
+
Use `memoryStorageAdapter` to avoid storing any data client-side. The synchronous script is not required.
|
|
274
318
|
|
|
275
319
|
```tsx
|
|
276
320
|
import {
|
|
@@ -278,12 +322,11 @@ import {
|
|
|
278
322
|
getThemesAndOptions,
|
|
279
323
|
memoryStorageAdapter,
|
|
280
324
|
type ThemeConfig,
|
|
281
|
-
type Themes,
|
|
282
325
|
} from 'resonare'
|
|
283
326
|
import { useResonare } from 'resonare/react'
|
|
284
327
|
import * as React from 'react'
|
|
285
328
|
|
|
286
|
-
const
|
|
329
|
+
const themeConfig = {
|
|
287
330
|
colorScheme: {
|
|
288
331
|
options: ['light', 'dark'],
|
|
289
332
|
},
|
|
@@ -292,15 +335,11 @@ const config = {
|
|
|
292
335
|
},
|
|
293
336
|
} as const satisfies ThemeConfig
|
|
294
337
|
|
|
295
|
-
export function ThemeSelect({
|
|
296
|
-
persistedServerThemes,
|
|
297
|
-
}: { persistedServerThemes: Themes<typeof config> }) {
|
|
338
|
+
export function ThemeSelect({ persistedStateFromDb }) {
|
|
298
339
|
const [themeStore] = React.useState(() =>
|
|
299
340
|
createThemeStore({
|
|
300
|
-
config,
|
|
301
|
-
initialState:
|
|
302
|
-
themes: persistedServerThemes,
|
|
303
|
-
},
|
|
341
|
+
config: themeConfig,
|
|
342
|
+
initialState: persistedStateFromDb,
|
|
304
343
|
storage: memoryStorageAdapter(),
|
|
305
344
|
}),
|
|
306
345
|
)
|
|
@@ -309,14 +348,16 @@ export function ThemeSelect({
|
|
|
309
348
|
initOnMount: true,
|
|
310
349
|
})
|
|
311
350
|
|
|
312
|
-
return getThemesAndOptions(
|
|
351
|
+
return getThemesAndOptions(themeConfig).map(([theme, options]) => (
|
|
313
352
|
<div key={theme}>
|
|
314
353
|
<label htmlFor={theme}>{theme}</label>
|
|
315
354
|
<select
|
|
316
355
|
id={theme}
|
|
317
356
|
name={theme}
|
|
318
|
-
onChange={(e) => {
|
|
357
|
+
onChange={async (e) => {
|
|
319
358
|
setThemes({ [theme]: e.target.value })
|
|
359
|
+
|
|
360
|
+
await saveToDb(themeStore.toPersist())
|
|
320
361
|
}}
|
|
321
362
|
value={themes[theme]}
|
|
322
363
|
>
|
package/dist/index.d.ts
CHANGED
|
@@ -75,7 +75,7 @@ declare class ThemeStore<T extends ThemeConfig> {
|
|
|
75
75
|
}: ThemeStoreOptions<T>);
|
|
76
76
|
getThemes: () => Themes<T>;
|
|
77
77
|
getResolvedThemes: () => Themes<T>;
|
|
78
|
-
setThemes: (themes: Partial<Themes<T>> | ((currentThemes: Themes<T>) => Partial<Themes<T>>)) =>
|
|
78
|
+
setThemes: (themes: Partial<Themes<T>> | ((currentThemes: Themes<T>) => Partial<Themes<T>>)) => void;
|
|
79
79
|
updateSystemOption: <K extends ThemeKeysWithSystemOption<T>>(themeKey: K, [ifMatch, ifNotMatch]: [NonSystemOptionValues<T, K>, NonSystemOptionValues<T, K>]) => void;
|
|
80
80
|
toPersist: () => PersistedState<T>;
|
|
81
81
|
restore: () => void;
|
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":";KAEY,cAAA;EACX,GAAA,GAAM,GAAA;EACN,GAAA,GAAM,GAAA,UAAa,KAAA;EAEnB,SAAA,IAAa,GA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A,MAAA;IACA,YAAA;IACA;EAAA,GACE,iBAAA,CAAkB,CAAA;EA4CrB,SAAA,QAAgB,MAAA,CAAO,CAAA;EAIvB,iBAAA,QAAwB,MAAA,CAAO,CAAA;EAI/B,SAAA,GACC,MAAA,EACG,OAAA,CAAQ,MAAA,CAAO,CAAA,OACb,aAAA,EAAe,MAAA,CAAO,CAAA,MAAO,OAAA,CAAQ,MAAA,CAAO,CAAA;EAelD,kBAAA,aAAgC,yBAAA,CAA0B,CAAA,GACzD,QAAA,EAAU,CAAA,GACV,OAAA,EAAA,UAAA,IACC,qBAAA,CAAsB,CAAA,EAAG,CAAA,GACzB,qBAAA,CAAsB,CAAA,EAAG,CAAA;EAQ3B,SAAA,QAAgB,cAAA,CAAe,CAAA;EAQ/B,OAAA;EA4BA,SAAA,GACC,QAAA,EAAU,QAAA,CAAS,CAAA;IACnB;EAAA;IAAyB,SAAA;EAAA;EAgB1B,IAAA;EAsBA,UAAA;AAAA;AAAA,cA8HY,gBAAA,aA1CQ,WAAA,EAAW,OAAA,EAAA,iBAAA,CAAA,CAAA,MAAA,UAAA,CAAA,CAAA;AAAA,cA2CnB,aAAA,mBA5BW,kBAAA,EAAkB,GAAA,GAAA,CAAA,iBAAA,kBAAA,CAAA,CAAA;AAAA,cA6B7B,iBAAA,mBAde,kBAAA,EAAkB,GAAA,GAAA,CAAA;AAAA,UAkB7B,kBAAA"}
|
package/dist/index.js
CHANGED
|
@@ -107,7 +107,7 @@ var ThemeStore = class {
|
|
|
107
107
|
getResolvedThemes = () => {
|
|
108
108
|
return this.#resolveThemes();
|
|
109
109
|
};
|
|
110
|
-
setThemes =
|
|
110
|
+
setThemes = (themes) => {
|
|
111
111
|
const updatedThemes = typeof themes === "function" ? themes(this.#currentThemes) : themes;
|
|
112
112
|
this.#setThemesAndNotify({
|
|
113
113
|
...this.#currentThemes,
|
|
@@ -161,11 +161,7 @@ var ThemeStore = class {
|
|
|
161
161
|
};
|
|
162
162
|
};
|
|
163
163
|
sync = () => {
|
|
164
|
-
if (!this.#storage?.watch)
|
|
165
|
-
if (!(process.env.NODE_ENV === "production")) if (this.#storage) console.warn(`[${name}] No watch method was provided for storage.`);
|
|
166
|
-
else console.warn(`[${name}] No storage was provided.`);
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
164
|
+
if (!this.#storage?.watch) return;
|
|
169
165
|
return this.#storage.watch((key, persistedState) => {
|
|
170
166
|
if (key !== this.#options.key) return;
|
|
171
167
|
this.#systemOptions = persistedState.systemOptions;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["name","PACKAGE_NAME","type","StorageAdapter","get","key","set","value","broadcast","watch","cb","StorageAdapterCreate","abortController","AbortController","StorageAdapterCreator","options","Options","localStorageAdapter","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","ThemeOptionValue","ThemeOption","value","T","media","ThemeOptions","options","ReadonlyArray","initialValue","ThemeConfig","Record","KeyedThemeConfig","Themes","K","U","Listener","themes","resolvedThemes","ThemeKeysWithSystemOption","NonSystemOptionValues","SystemOptions","PersistedState","version","Partial","systemOptions","ThemeStoreOptions","key","config","initialState","storage","ThemeAndOptions","Array","getThemesAndOptions","Object","entries","map","themeKey","themeOptions","option","getDefaultThemes","fromEntries","defaultValue","ThemeStore","defaultThemes","currentThemes","listeners","Set","mediaQueryCache","MediaQueryList","abortController","AbortController","constructor","keyedConfig","forEach","hasOwn","String","getThemes","getResolvedThemes","resolveThemes","setThemes","Promise","updatedThemes","setThemesAndNotify","stateToPersist","toPersist","set","broadcast","updateSystemOption","ifMatch","ifNotMatch","restore","persistedState","get","subscribe","callback","immediate","add","delete","sync","watch","PROD","console","warn","___destroy","clear","abort","#setThemesAndNotify","notify","#resolveThemes","optionKey","resolveThemeOption","#resolveThemeOption","IIFE","window","document","createElement","mediaQuery","mediaQueryList","matchMedia","addEventListener","signal","matches","#notify","listener","Registry","registry","Map","create","storeKey","themeStore","ThemeStoreRegistry","has","Error","destroy","createThemeStore","getThemeStore","destroyThemeStore"],"sources":["../package.json","../src/storage.ts","../src/index.ts"],"sourcesContent":["","import { name as PACKAGE_NAME } from '../package.json' with { type: 'json' }\n\nexport type StorageAdapter = {\n\tget: (key: string) => object | null\n\tset: (key: string, value: object) => void\n\t// del: (key: string) => void\n\tbroadcast?: (key: string, value: object) => void\n\twatch?: (cb: (key: string | null, 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\nexport const localStorageAdapter: StorageAdapterCreator<{\n\ttype?: 'localStorage' | 'sessionStorage'\n}> = ({ type = 'localStorage' } = {}) => {\n\treturn ({ abortController }) => {\n\t\treturn {\n\t\t\tget: (key: string) => {\n\t\t\t\treturn JSON.parse(window[type].getItem(key) || 'null')\n\t\t\t},\n\n\t\t\tset: (key: string, value: object) => {\n\t\t\t\twindow[type].setItem(key, JSON.stringify(value))\n\t\t\t},\n\n\t\t\t// del: (key: string) => {\n\t\t\t// \twindow[type].removeItem(key)\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\tcb(e.key, 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\nexport const memoryStorageAdapter: StorageAdapterCreator<never> = () => {\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: (key: string) => {\n\t\t\t\treturn storage.get(key) || null\n\t\t\t},\n\n\t\t\tset: (key: string, value: object) => {\n\t\t\t\tstorage.set(key, value)\n\t\t\t},\n\n\t\t\t// del: (key: string) => {\n\t\t\t// \tstorage.delete(key)\n\t\t\t// },\n\n\t\t\tbroadcast: (key: string, 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\tcb(e.data.key, 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\ntype ThemeOptionValue = string | number | boolean\n\ntype ThemeOption<T extends ThemeOptionValue = string> = {\n\tvalue: T\n\tmedia?: [string, T, T]\n}\n\ntype ThemeOptions<T extends ThemeOptionValue = string> =\n\t| {\n\t\t\toptions: [\n\t\t\t\tT | ThemeOption<T>,\n\t\t\t\tT | ThemeOption<T>,\n\t\t\t\t...ReadonlyArray<T | ThemeOption<T>>,\n\t\t\t]\n\t\t\tinitialValue?: T\n\t }\n\t| {\n\t\t\tinitialValue: T\n\t }\n\nexport type ThemeConfig = Record<\n\tstring,\n\tThemeOptions<string> | ThemeOptions<number> | ThemeOptions<boolean>\n>\n\n// { [themeKey]: { [optionKey]: ThemeOption } }\ntype KeyedThemeConfig<T extends ThemeConfig> = {\n\t[K in keyof T]: Record<string, ThemeOption>\n}\n\nexport type Themes<T extends ThemeConfig> = {\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 ThemeConfig> = (value: {\n\tthemes: Themes<T>\n\tresolvedThemes: Themes<T>\n}) => void\n\ntype ThemeKeysWithSystemOption<T extends ThemeConfig> = {\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 ThemeConfig,\n\tK extends keyof T,\n> = T[K] extends { options: ReadonlyArray<infer U> }\n\t? U extends ThemeOptionValue\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 ThemeConfig> = {\n\t[K in ThemeKeysWithSystemOption<T>]?: [\n\t\tNonSystemOptionValues<T, K>,\n\t\tNonSystemOptionValues<T, K>,\n\t]\n}\n\ntype PersistedState<T extends ThemeConfig> = {\n\tversion: 1\n\tthemes: Partial<Themes<T>>\n\tsystemOptions: SystemOptions<T>\n}\n\nexport type ThemeStoreOptions<T extends ThemeConfig> = {\n\tkey?: string\n\tconfig: T\n\tinitialState?: Partial<PersistedState<T>>\n\tstorage?: StorageAdapterCreate | null\n}\n\nexport type ThemeAndOptions<T extends ThemeConfig> = 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 ThemeConfig>(config: T) {\n\treturn Object.entries(config).map(([themeKey, themeOptions]) => {\n\t\treturn [\n\t\t\tthemeKey,\n\t\t\t'options' in themeOptions\n\t\t\t\t? themeOptions.options.map((option) =>\n\t\t\t\t\t\ttypeof option === 'string' ? option : option.value,\n\t\t\t\t\t)\n\t\t\t\t: [],\n\t\t]\n\t}) as ThemeAndOptions<T>\n}\n\nexport function getDefaultThemes<T extends ThemeConfig>(config: T) {\n\treturn Object.fromEntries(\n\t\tObject.entries(config).map(([themeKey, themeOptions]) => {\n\t\t\tconst defaultValue =\n\t\t\t\tthemeOptions.initialValue ??\n\t\t\t\tthemeOptions.options[0].value ??\n\t\t\t\tthemeOptions.options[0]\n\n\t\t\treturn [themeKey, defaultValue]\n\t\t}),\n\t) as Themes<T>\n}\n\nexport class ThemeStore<T extends ThemeConfig> {\n\t#defaultThemes: Themes<T>\n\t#currentThemes: Themes<T>\n\n\t#options: {\n\t\tkey: string\n\t\tconfig: KeyedThemeConfig<T>\n\t}\n\n\t#systemOptions: SystemOptions<T>\n\n\t#storage: StorageAdapter | null\n\n\t#listeners: Set<Listener<T>> = new Set<Listener<T>>()\n\n\t#mediaQueryCache: Record<string, MediaQueryList>\n\n\t#abortController = new AbortController()\n\n\tconstructor({\n\t\tkey = PACKAGE_NAME,\n\t\tconfig,\n\t\tinitialState = {},\n\t\tstorage = localStorageAdapter(),\n\t}: ThemeStoreOptions<T>) {\n\t\tconst keyedConfig: Record<string, Record<string, ThemeOption<any>>> = {}\n\t\tconst systemOptions: Record<string, [ThemeOptionValue, ThemeOptionValue]> =\n\t\t\t{\n\t\t\t\t...initialState.systemOptions,\n\t\t\t}\n\n\t\tObject.entries(config).forEach(([themeKey, themeOptions]) => {\n\t\t\tkeyedConfig[themeKey] = {}\n\n\t\t\tif ('options' in themeOptions) {\n\t\t\t\tthemeOptions.options.forEach((option) => {\n\t\t\t\t\tif (typeof option === 'object') {\n\t\t\t\t\t\tif (option.media && !Object.hasOwn(systemOptions, themeKey)) {\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\tkeyedConfig[themeKey]![String(option.value)] = option\n\t\t\t\t\t} else {\n\t\t\t\t\t\tkeyedConfig[themeKey]![String(option)] = { value: option }\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t}\n\t\t})\n\n\t\tthis.#options = {\n\t\t\tkey,\n\t\t\tconfig: keyedConfig as KeyedThemeConfig<T>,\n\t\t}\n\n\t\tthis.#systemOptions = systemOptions as SystemOptions<T>\n\n\t\tthis.#defaultThemes = getDefaultThemes(config)\n\n\t\tthis.#currentThemes = { ...this.#defaultThemes, ...initialState.themes }\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.#mediaQueryCache = {}\n\t}\n\n\tgetThemes = (): Themes<T> => {\n\t\treturn this.#currentThemes\n\t}\n\n\tgetResolvedThemes = (): Themes<T> => {\n\t\treturn this.#resolveThemes()\n\t}\n\n\tsetThemes = async (\n\t\tthemes:\n\t\t\t| Partial<Themes<T>>\n\t\t\t| ((currentThemes: Themes<T>) => Partial<Themes<T>>),\n\t): Promise<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(this.#options.key, stateToPersist)\n\t\t\tthis.#storage.broadcast?.(this.#options.key, 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) => {\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\tversion: 1,\n\t\t\tthemes: this.#currentThemes,\n\t\t\tsystemOptions: this.#systemOptions,\n\t\t}\n\t}\n\n\trestore = () => {\n\t\tlet persistedState = this.#storage?.get(this.#options.key)\n\n\t\tif (!persistedState) {\n\t\t\tthis.#setThemesAndNotify({ ...this.#defaultThemes })\n\t\t\treturn\n\t\t}\n\n\t\t// for backward compatibility\n\t\tif (!Object.hasOwn(persistedState, 'version')) {\n\t\t\tpersistedState = {\n\t\t\t\tversion: 1,\n\t\t\t\tthemes: persistedState,\n\t\t\t\tsystemOptions: this.#systemOptions,\n\t\t\t}\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 = (\n\t\tcallback: Listener<T>,\n\t\t{ immediate = false }: { immediate?: boolean } = {},\n\t): (() => void) => {\n\t\tif (immediate) {\n\t\t\tcallback({\n\t\t\t\tthemes: this.#currentThemes,\n\t\t\t\tresolvedThemes: this.#resolveThemes(),\n\t\t\t})\n\t\t}\n\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) {\n\t\t\tif (!PROD) {\n\t\t\t\tif (this.#storage) {\n\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t`[${PACKAGE_NAME}] No watch method was provided for storage.`,\n\t\t\t\t\t)\n\t\t\t\t} else {\n\t\t\t\t\tconsole.warn(`[${PACKAGE_NAME}] No storage was provided.`)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn\n\t\t}\n\n\t\treturn this.#storage.watch((key, persistedState) => {\n\t\t\tif (key !== this.#options.key) return\n\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___destroy = (): 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\t\tthis.#notify()\n\t}\n\n\t#resolveThemes = (): Themes<T> => {\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries(this.#currentThemes).map(([themeKey, optionKey]) => {\n\t\t\t\tconst option = this.#options.config[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\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 (!IIFE) {\n\t\t\tif (\n\t\t\t\t!(\n\t\t\t\t\ttypeof window !== 'undefined' &&\n\t\t\t\t\ttypeof window.document !== 'undefined' &&\n\t\t\t\t\ttypeof window.document.createElement !== 'undefined'\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`[${PACKAGE_NAME}] Option with key \"media\" cannot be resolved in server environment.`,\n\t\t\t\t)\n\n\t\t\t\treturn option.value\n\t\t\t}\n\t\t}\n\n\t\tconst [mediaQuery] = option.media\n\n\t\tif (!this.#mediaQueryCache[mediaQuery]) {\n\t\t\tconst mediaQueryList = window.matchMedia(mediaQuery)\n\n\t\t\tthis.#mediaQueryCache[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 this.#mediaQueryCache[mediaQuery].matches ? ifMatch : ifNotMatch\n\t}\n\n\t#notify = (): void => {\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.#resolveThemes(),\n\t\t\t})\n\t\t}\n\t}\n}\n\nclass Registry {\n\t#registry = new Map<string, ThemeStore<ThemeConfig>>()\n\n\tcreate = <T extends ThemeConfig>(\n\t\toptions: ThemeStoreOptions<T>,\n\t): ThemeStore<T> => {\n\t\tconst storeKey = options.key || PACKAGE_NAME\n\n\t\tlet themeStore = this.#registry.get(storeKey) as ThemeStore<T>\n\n\t\tif (!themeStore) {\n\t\t\tthemeStore = new ThemeStore<T>(options)\n\t\t\tthis.#registry.set(storeKey, themeStore as ThemeStore<ThemeConfig>)\n\t\t}\n\n\t\treturn themeStore\n\t}\n\n\tget = <T extends keyof ThemeStoreRegistry>(key?: T) => {\n\t\tconst storeKey = key || PACKAGE_NAME\n\n\t\tif (!this.#registry.has(storeKey)) {\n\t\t\tif (IIFE) {\n\t\t\t\tthrow new Error(`Theme store '${storeKey}' not found.`)\n\t\t\t}\n\t\t\tthrow new Error(\n\t\t\t\t`[${PACKAGE_NAME}] Theme store with key '${storeKey}' could not be found. Please run \\`createThemeStore\\` with key '${storeKey}' first.`,\n\t\t\t)\n\t\t}\n\n\t\treturn this.#registry.get(storeKey)! as ThemeStoreRegistry[T]\n\t}\n\n\tdestroy = <T extends keyof ThemeStoreRegistry>(key?: T) => {\n\t\tconst storeKey = key || PACKAGE_NAME\n\n\t\tif (!this.#registry.has(storeKey)) return\n\n\t\tthis.#registry.get(storeKey)!.___destroy()\n\t\tthis.#registry.delete(storeKey)\n\t}\n}\n\nconst registry = new Registry()\n\nexport const createThemeStore = registry.create\nexport const getThemeStore = registry.get\nexport const destroyThemeStore = registry.destroy\n\nexport * from './storage'\n\nexport interface ThemeStoreRegistry {}\n"],"mappings":";;;;;ACoBA,MAAaiB,uBAEP,EAAEf,OAAO,mBAAmB,EAAE,KAAK;AACxC,SAAQ,EAAEU,sBAAsB;AAC/B,SAAO;GACNR,MAAMC,QAAgB;AACrB,WAAOa,KAAKC,MAAMC,OAAOlB,MAAMmB,QAAQhB,IAAI,IAAI,OAAO;;GAGvDC,MAAMD,KAAaE,UAAkB;AACpCa,WAAOlB,MAAMoB,QAAQjB,KAAKa,KAAKK,UAAUhB,MAAM,CAAC;;GAOjDE,QAAQC,OAAO;IACd,MAAMc,aAAa,IAAIX,iBAAiB;AAExCO,WAAOK,iBACN,YACCC,MAAM;AACN,SAAIA,EAAEC,gBAAgBP,OAAOlB,MAAO;AAEpCQ,QAAGgB,EAAErB,KAAKa,KAAKC,MAAMO,EAAEE,SAAU,CAAC;OAEnC,EACCC,QAAQC,YAAYC,IAAI,CACvBnB,gBAAgBiB,QAChBL,WAAWK,OACX,CAAA,EAEH,CAAC;AAED,iBAAa;AACZL,gBAAWQ,OAAO;;;GAGpB;;;AAIH,MAAaC,6BAA2D;AACvE,SAAQ,EAAErB,sBAAsB;EAC/B,MAAMsB,0BAAU,IAAIC,KAAqB;EACzC,MAAMC,UAAU,IAAIC,iBAAiBpC,KAA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,aAAa,MAAK,SAAUrC,IAAIoC,SAAS;AAE7C,MAAI,CAACC,YAAY;AAChBA,gBAAa,IAAInE,WAAcpC,QAAQ;AACvC,SAAK,SAAU2D,IAAI2C,UAAUC,WAAsC;;AAGpE,SAAOA;;CAGRrC,OAA2C9C,QAAY;EACtD,MAAMkF,WAAWlF,OAAO/B;AAExB,MAAI,CAAC,MAAK,SAAUoH,IAAIH,SAAS,CAIhC,OAAM,IAAII,MACT,IAAIrH,KAAY,0BAA2BiH,SAAQ,kEAAmEA,SAAQ,UAC9H;AAGF,SAAO,MAAK,SAAUpC,IAAIoC,SAAS;;CAGpCK,WAA+CvF,QAAY;EAC1D,MAAMkF,WAAWlF,OAAO/B;AAExB,MAAI,CAAC,MAAK,SAAUoH,IAAIH,SAAS,CAAE;AAEnC,QAAK,SAAUpC,IAAIoC,SAAS,CAAEzB,YAAY;AAC1C,QAAK,SAAUN,OAAO+B,SAAS;;;AAIjC,MAAMH,WAAW,IAAID,UAAU;AAE/B,MAAaU,mBAAmBT,SAASE;AACzC,MAAaQ,gBAAgBV,SAASjC;AACtC,MAAa4C,oBAAoBX,SAASQ"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["name","PACKAGE_NAME","type","StorageAdapter","get","key","set","value","broadcast","watch","cb","StorageAdapterCreate","abortController","AbortController","StorageAdapterCreator","options","Options","localStorageAdapter","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","ThemeOptionValue","ThemeOption","value","T","media","ThemeOptions","options","ReadonlyArray","initialValue","ThemeConfig","Record","KeyedThemeConfig","Themes","K","U","Listener","themes","resolvedThemes","ThemeKeysWithSystemOption","NonSystemOptionValues","SystemOptions","PersistedState","version","Partial","systemOptions","ThemeStoreOptions","key","config","initialState","storage","ThemeAndOptions","Array","getThemesAndOptions","Object","entries","map","themeKey","themeOptions","option","getDefaultThemes","fromEntries","defaultValue","ThemeStore","defaultThemes","currentThemes","listeners","Set","mediaQueryCache","MediaQueryList","abortController","AbortController","constructor","keyedConfig","forEach","hasOwn","String","getThemes","getResolvedThemes","resolveThemes","setThemes","updatedThemes","setThemesAndNotify","stateToPersist","toPersist","set","broadcast","updateSystemOption","ifMatch","ifNotMatch","restore","persistedState","get","subscribe","callback","immediate","add","delete","sync","watch","___destroy","clear","abort","#setThemesAndNotify","notify","#resolveThemes","optionKey","resolveThemeOption","#resolveThemeOption","IIFE","window","document","createElement","console","warn","mediaQuery","mediaQueryList","matchMedia","addEventListener","signal","matches","#notify","listener","Registry","registry","Map","create","storeKey","themeStore","ThemeStoreRegistry","has","Error","destroy","createThemeStore","getThemeStore","destroyThemeStore"],"sources":["../package.json","../src/storage.ts","../src/index.ts"],"sourcesContent":["","import { name as PACKAGE_NAME } from '../package.json' with { type: 'json' }\n\nexport type StorageAdapter = {\n\tget: (key: string) => object | null\n\tset: (key: string, value: object) => void\n\t// del: (key: string) => void\n\tbroadcast?: (key: string, value: object) => void\n\twatch?: (cb: (key: string | null, 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\nexport const localStorageAdapter: StorageAdapterCreator<{\n\ttype?: 'localStorage' | 'sessionStorage'\n}> = ({ type = 'localStorage' } = {}) => {\n\treturn ({ abortController }) => {\n\t\treturn {\n\t\t\tget: (key: string) => {\n\t\t\t\treturn JSON.parse(window[type].getItem(key) || 'null')\n\t\t\t},\n\n\t\t\tset: (key: string, value: object) => {\n\t\t\t\twindow[type].setItem(key, JSON.stringify(value))\n\t\t\t},\n\n\t\t\t// del: (key: string) => {\n\t\t\t// \twindow[type].removeItem(key)\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\tcb(e.key, 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\nexport const memoryStorageAdapter: StorageAdapterCreator<never> = () => {\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: (key: string) => {\n\t\t\t\treturn storage.get(key) || null\n\t\t\t},\n\n\t\t\tset: (key: string, value: object) => {\n\t\t\t\tstorage.set(key, value)\n\t\t\t},\n\n\t\t\t// del: (key: string) => {\n\t\t\t// \tstorage.delete(key)\n\t\t\t// },\n\n\t\t\tbroadcast: (key: string, 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\tcb(e.data.key, 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\ntype ThemeOptionValue = string | number | boolean\n\ntype ThemeOption<T extends ThemeOptionValue = string> = {\n\tvalue: T\n\tmedia?: [string, T, T]\n}\n\ntype ThemeOptions<T extends ThemeOptionValue = string> =\n\t| {\n\t\t\toptions: [\n\t\t\t\tT | ThemeOption<T>,\n\t\t\t\tT | ThemeOption<T>,\n\t\t\t\t...ReadonlyArray<T | ThemeOption<T>>,\n\t\t\t]\n\t\t\tinitialValue?: T\n\t }\n\t| {\n\t\t\tinitialValue: T\n\t }\n\nexport type ThemeConfig = Record<\n\tstring,\n\tThemeOptions<string> | ThemeOptions<number> | ThemeOptions<boolean>\n>\n\n// { [themeKey]: { [optionKey]: ThemeOption } }\ntype KeyedThemeConfig<T extends ThemeConfig> = {\n\t[K in keyof T]: Record<string, ThemeOption>\n}\n\nexport type Themes<T extends ThemeConfig> = {\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 ThemeConfig> = (value: {\n\tthemes: Themes<T>\n\tresolvedThemes: Themes<T>\n}) => void\n\ntype ThemeKeysWithSystemOption<T extends ThemeConfig> = {\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 ThemeConfig,\n\tK extends keyof T,\n> = T[K] extends { options: ReadonlyArray<infer U> }\n\t? U extends ThemeOptionValue\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 ThemeConfig> = {\n\t[K in ThemeKeysWithSystemOption<T>]?: [\n\t\tNonSystemOptionValues<T, K>,\n\t\tNonSystemOptionValues<T, K>,\n\t]\n}\n\ntype PersistedState<T extends ThemeConfig> = {\n\tversion: 1\n\tthemes: Partial<Themes<T>>\n\tsystemOptions: SystemOptions<T>\n}\n\nexport type ThemeStoreOptions<T extends ThemeConfig> = {\n\tkey?: string\n\tconfig: T\n\tinitialState?: Partial<PersistedState<T>>\n\tstorage?: StorageAdapterCreate | null\n}\n\nexport type ThemeAndOptions<T extends ThemeConfig> = 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 ThemeConfig>(config: T) {\n\treturn Object.entries(config).map(([themeKey, themeOptions]) => {\n\t\treturn [\n\t\t\tthemeKey,\n\t\t\t'options' in themeOptions\n\t\t\t\t? themeOptions.options.map((option) =>\n\t\t\t\t\t\ttypeof option === 'string' ? option : option.value,\n\t\t\t\t\t)\n\t\t\t\t: [],\n\t\t]\n\t}) as ThemeAndOptions<T>\n}\n\nexport function getDefaultThemes<T extends ThemeConfig>(config: T) {\n\treturn Object.fromEntries(\n\t\tObject.entries(config).map(([themeKey, themeOptions]) => {\n\t\t\tconst defaultValue =\n\t\t\t\tthemeOptions.initialValue ??\n\t\t\t\tthemeOptions.options[0].value ??\n\t\t\t\tthemeOptions.options[0]\n\n\t\t\treturn [themeKey, defaultValue]\n\t\t}),\n\t) as Themes<T>\n}\n\nexport class ThemeStore<T extends ThemeConfig> {\n\t#defaultThemes: Themes<T>\n\t#currentThemes: Themes<T>\n\n\t#options: {\n\t\tkey: string\n\t\tconfig: KeyedThemeConfig<T>\n\t}\n\n\t#systemOptions: SystemOptions<T>\n\n\t#storage: StorageAdapter | null\n\n\t#listeners: Set<Listener<T>> = new Set<Listener<T>>()\n\n\t#mediaQueryCache: Record<string, MediaQueryList>\n\n\t#abortController = new AbortController()\n\n\tconstructor({\n\t\tkey = PACKAGE_NAME,\n\t\tconfig,\n\t\tinitialState = {},\n\t\tstorage = localStorageAdapter(),\n\t}: ThemeStoreOptions<T>) {\n\t\tconst keyedConfig: Record<string, Record<string, ThemeOption<any>>> = {}\n\t\tconst systemOptions: Record<string, [ThemeOptionValue, ThemeOptionValue]> =\n\t\t\t{\n\t\t\t\t...initialState.systemOptions,\n\t\t\t}\n\n\t\tObject.entries(config).forEach(([themeKey, themeOptions]) => {\n\t\t\tkeyedConfig[themeKey] = {}\n\n\t\t\tif ('options' in themeOptions) {\n\t\t\t\tthemeOptions.options.forEach((option) => {\n\t\t\t\t\tif (typeof option === 'object') {\n\t\t\t\t\t\tif (option.media && !Object.hasOwn(systemOptions, themeKey)) {\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\tkeyedConfig[themeKey]![String(option.value)] = option\n\t\t\t\t\t} else {\n\t\t\t\t\t\tkeyedConfig[themeKey]![String(option)] = { value: option }\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t}\n\t\t})\n\n\t\tthis.#options = {\n\t\t\tkey,\n\t\t\tconfig: keyedConfig as KeyedThemeConfig<T>,\n\t\t}\n\n\t\tthis.#systemOptions = systemOptions as SystemOptions<T>\n\n\t\tthis.#defaultThemes = getDefaultThemes(config)\n\n\t\tthis.#currentThemes = { ...this.#defaultThemes, ...initialState.themes }\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.#mediaQueryCache = {}\n\t}\n\n\tgetThemes = (): Themes<T> => {\n\t\treturn this.#currentThemes\n\t}\n\n\tgetResolvedThemes = (): Themes<T> => {\n\t\treturn this.#resolveThemes()\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(this.#options.key, stateToPersist)\n\t\t\tthis.#storage.broadcast?.(this.#options.key, 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) => {\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\tversion: 1,\n\t\t\tthemes: this.#currentThemes,\n\t\t\tsystemOptions: this.#systemOptions,\n\t\t}\n\t}\n\n\trestore = (): void => {\n\t\tlet persistedState = this.#storage?.get(this.#options.key)\n\n\t\tif (!persistedState) {\n\t\t\tthis.#setThemesAndNotify({ ...this.#defaultThemes })\n\t\t\treturn\n\t\t}\n\n\t\t// for backward compatibility\n\t\tif (!Object.hasOwn(persistedState, 'version')) {\n\t\t\tpersistedState = {\n\t\t\t\tversion: 1,\n\t\t\t\tthemes: persistedState,\n\t\t\t\tsystemOptions: this.#systemOptions,\n\t\t\t}\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 = (\n\t\tcallback: Listener<T>,\n\t\t{ immediate = false }: { immediate?: boolean } = {},\n\t): (() => void) => {\n\t\tif (immediate) {\n\t\t\tcallback({\n\t\t\t\tthemes: this.#currentThemes,\n\t\t\t\tresolvedThemes: this.#resolveThemes(),\n\t\t\t})\n\t\t}\n\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) {\n\t\t\t// if (this.#storage) {\n\t\t\t// \tconsole.warn(\n\t\t\t// \t\t`[${PACKAGE_NAME}] No watch method was provided for storage.`,\n\t\t\t// \t)\n\t\t\t// } else {\n\t\t\t// \tconsole.warn(`[${PACKAGE_NAME}] No storage was provided.`)\n\t\t\t// }\n\n\t\t\treturn\n\t\t}\n\n\t\treturn this.#storage.watch((key, persistedState) => {\n\t\t\tif (key !== this.#options.key) return\n\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___destroy = (): 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\t\tthis.#notify()\n\t}\n\n\t#resolveThemes = (): Themes<T> => {\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries(this.#currentThemes).map(([themeKey, optionKey]) => {\n\t\t\t\tconst option = this.#options.config[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\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 (!IIFE) {\n\t\t\tif (\n\t\t\t\t!(\n\t\t\t\t\ttypeof window !== 'undefined' &&\n\t\t\t\t\ttypeof window.document !== 'undefined' &&\n\t\t\t\t\ttypeof window.document.createElement !== 'undefined'\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`[${PACKAGE_NAME}] Option with key \"media\" cannot be resolved in server environment.`,\n\t\t\t\t)\n\n\t\t\t\treturn option.value\n\t\t\t}\n\t\t}\n\n\t\tconst [mediaQuery] = option.media\n\n\t\tif (!this.#mediaQueryCache[mediaQuery]) {\n\t\t\tconst mediaQueryList = window.matchMedia(mediaQuery)\n\n\t\t\tthis.#mediaQueryCache[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 this.#mediaQueryCache[mediaQuery].matches ? ifMatch : ifNotMatch\n\t}\n\n\t#notify = (): void => {\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.#resolveThemes(),\n\t\t\t})\n\t\t}\n\t}\n}\n\nclass Registry {\n\t#registry = new Map<string, ThemeStore<ThemeConfig>>()\n\n\tcreate = <T extends ThemeConfig>(\n\t\toptions: ThemeStoreOptions<T>,\n\t): ThemeStore<T> => {\n\t\tconst storeKey = options.key || PACKAGE_NAME\n\n\t\tlet themeStore = this.#registry.get(storeKey) as ThemeStore<T>\n\n\t\tif (!themeStore) {\n\t\t\tthemeStore = new ThemeStore<T>(options)\n\t\t\tthis.#registry.set(storeKey, themeStore as ThemeStore<ThemeConfig>)\n\t\t}\n\n\t\treturn themeStore\n\t}\n\n\tget = <T extends keyof ThemeStoreRegistry>(key?: T) => {\n\t\tconst storeKey = key || PACKAGE_NAME\n\n\t\tif (!this.#registry.has(storeKey)) {\n\t\t\tif (IIFE) {\n\t\t\t\tthrow new Error(`Theme store '${storeKey}' not found.`)\n\t\t\t}\n\t\t\tthrow new Error(\n\t\t\t\t`[${PACKAGE_NAME}] Theme store with key '${storeKey}' could not be found. Please run \\`createThemeStore\\` with key '${storeKey}' first.`,\n\t\t\t)\n\t\t}\n\n\t\treturn this.#registry.get(storeKey)! as ThemeStoreRegistry[T]\n\t}\n\n\tdestroy = <T extends keyof ThemeStoreRegistry>(key?: T) => {\n\t\tconst storeKey = key || PACKAGE_NAME\n\n\t\tif (!this.#registry.has(storeKey)) return\n\n\t\tthis.#registry.get(storeKey)!.___destroy()\n\t\tthis.#registry.delete(storeKey)\n\t}\n}\n\nconst registry = new Registry()\n\nexport const createThemeStore = registry.create\nexport const getThemeStore = registry.get\nexport const destroyThemeStore = registry.destroy\n\nexport * from './storage'\n\nexport interface ThemeStoreRegistry {}\n"],"mappings":";;;;;ACoBA,MAAaiB,uBAEP,EAAEf,OAAO,mBAAmB,EAAE,KAAK;AACxC,SAAQ,EAAEU,sBAAsB;AAC/B,SAAO;GACNR,MAAMC,QAAgB;AACrB,WAAOa,KAAKC,MAAMC,OAAOlB,MAAMmB,QAAQhB,IAAI,IAAI,OAAO;;GAGvDC,MAAMD,KAAaE,UAAkB;AACpCa,WAAOlB,MAAMoB,QAAQjB,KAAKa,KAAKK,UAAUhB,MAAM,CAAC;;GAOjDE,QAAQC,OAAO;IACd,MAAMc,aAAa,IAAIX,iBAAiB;AAExCO,WAAOK,iBACN,YACCC,MAAM;AACN,SAAIA,EAAEC,gBAAgBP,OAAOlB,MAAO;AAEpCQ,QAAGgB,EAAErB,KAAKa,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ACbC,gBAAgB,MAAK,eAAe;GACpC,CAAC;;;AAKL,IAAMqF,WAAN,MAAe;CACd,4BAAY,IAAIE,KAAsC;CAEtDC,UACCnG,YACmB;EACnB,MAAMoG,WAAWpG,QAAQoB,OAAO/B;EAEhC,IAAIgH,aAAa,MAAK,SAAUpC,IAAImC,SAAS;AAE7C,MAAI,CAACC,YAAY;AAChBA,gBAAa,IAAIjE,WAAcpC,QAAQ;AACvC,SAAK,SAAU0D,IAAI0C,UAAUC,WAAsC;;AAGpE,SAAOA;;CAGRpC,OAA2C7C,QAAY;EACtD,MAAMgF,WAAWhF,OAAO/B;AAExB,MAAI,CAAC,MAAK,SAAUkH,IAAIH,SAAS,CAIhC,OAAM,IAAII,MACT,IAAInH,KAAY,0BAA2B+G,SAAQ,kEAAmEA,SAAQ,UAC9H;AAGF,SAAO,MAAK,SAAUnC,IAAImC,SAAS;;CAGpCK,WAA+CrF,QAAY;EAC1D,MAAMgF,WAAWhF,OAAO/B;AAExB,MAAI,CAAC,MAAK,SAAUkH,IAAIH,SAAS,CAAE;AAEnC,QAAK,SAAUnC,IAAImC,SAAS,CAAE3B,YAAY;AAC1C,QAAK,SAAUH,OAAO8B,SAAS;;;AAIjC,MAAMH,WAAW,IAAID,UAAU;AAE/B,MAAaU,mBAAmBT,SAASE;AACzC,MAAaQ,gBAAgBV,SAAShC;AACtC,MAAa2C,oBAAoBX,SAASQ"}
|
package/dist/inline-script.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const resonareInlineScript = "/**\n* resonare v0.0.
|
|
1
|
+
export const resonareInlineScript = "/**\n* resonare v0.0.12\n*\n* This source code is licensed under the MIT license found in the\n* LICENSE file in the root directory of this source tree.\n*/\nvar resonare=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=`resonare`;let n=({type:e=`localStorage`}={})=>({abortController:t})=>({get:t=>JSON.parse(window[e].getItem(t)||`null`),set:(t,n)=>{window[e].setItem(t,JSON.stringify(n))},watch:n=>{let r=new AbortController;return window.addEventListener(`storage`,t=>{t.storageArea===window[e]&&n(t.key,JSON.parse(t.newValue))},{signal:AbortSignal.any([t.signal,r.signal])}),()=>{r.abort()}}});function r(e){return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,t.initialValue??t.options[0].value??t.options[0]]))}var i=class{#e;#t;#n;#r;#i;#a=new Set;#o;#s=new AbortController;constructor({key:e=t,config:i,initialState:a={},storage:o=n()}){let s={},c={...a.systemOptions};Object.entries(i).forEach(([e,t])=>{s[e]={},`options`in t&&t.options.forEach(t=>{typeof t==`object`?(t.media&&!Object.hasOwn(c,e)&&(c[e]=[t.media[1],t.media[2]]),s[e][String(t.value)]=t):s[e][String(t)]={value:t}})}),this.#n={key:e,config:s},this.#r=c,this.#e=r(i),this.#t={...this.#e,...a.themes},this.#i=o?.({abortController:this.#s})??null,this.#o={}}getThemes=()=>this.#t;getResolvedThemes=()=>this.#l();setThemes=e=>{let t=typeof e==`function`?e(this.#t):e;this.#c({...this.#t,...t});let n=this.toPersist();this.#i&&(this.#i.set(this.#n.key,n),this.#i.broadcast?.(this.#n.key,n))};updateSystemOption=(e,[t,n])=>{this.#r[e]=[t,n],this.setThemes({...this.#t})};toPersist=()=>({version:1,themes:this.#t,systemOptions:this.#r});restore=()=>{let e=this.#i?.get(this.#n.key);if(!e){this.#c({...this.#e});return}Object.hasOwn(e,`version`)||(e={version:1,themes:e,systemOptions:this.#r}),this.#r={...this.#r,...e.systemOptions},this.#c({...this.#e,...e.themes})};subscribe=(e,{immediate:t=!1}={})=>(t&&e({themes:this.#t,resolvedThemes:this.#l()}),this.#a.add(e),()=>{this.#a.delete(e)});sync=()=>{if(this.#i?.watch)return this.#i.watch((e,t)=>{e===this.#n.key&&(this.#r=t.systemOptions,this.#c(t.themes))})};___destroy=()=>{this.#a.clear(),this.#s.abort()};#c=e=>{this.#t=e,this.#d()};#l=()=>Object.fromEntries(Object.entries(this.#t).map(([e,t])=>{let n=this.#n.config[e]?.[t];return[e,n?this.#u({themeKey:e,option:n}):t]}));#u=({themeKey:e,option:t})=>{if(!t.media)return t.value;let[n]=t.media;if(!this.#o[n]){let r=window.matchMedia(n);this.#o[n]=r,r.addEventListener(`change`,()=>{this.#t[e]===t.value&&this.#c({...this.#t})},{signal:this.#s.signal})}let[r,i]=this.#r[e];return this.#o[n].matches?r:i};#d=()=>{for(let e of this.#a)e({themes:this.#t,resolvedThemes:this.#l()})}};let a=new class{#e=new Map;create=e=>{let n=e.key||t,r=this.#e.get(n);return r||(r=new i(e),this.#e.set(n,r)),r};get=e=>{let n=e||t;if(!this.#e.has(n))throw Error(`Theme store '${n}' not found.`);return this.#e.get(n)};destroy=e=>{let n=e||t;this.#e.has(n)&&(this.#e.get(n).___destroy(),this.#e.delete(n))}},o=a.create,s=a.get,c=a.destroy;return e.createThemeStore=o,e.destroyThemeStore=c,e.getThemeStore=s,e})({});"
|
package/dist/react.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ declare function useResonare<T extends ThemeConfig>(getStore: () => ThemeStore<T
|
|
|
8
8
|
}): {
|
|
9
9
|
themes: Themes<T>;
|
|
10
10
|
resolvedThemes: Themes<T>;
|
|
11
|
-
setThemes: (themes: Partial<Themes<T>> | ((currentThemes: Themes<T>) => Partial<Themes<T>>)) =>
|
|
11
|
+
setThemes: (themes: Partial<Themes<T>> | ((currentThemes: Themes<T>) => Partial<Themes<T>>)) => void;
|
|
12
12
|
updateSystemOption: <K extends { [K_1 in keyof T]: T[K_1] extends {
|
|
13
13
|
options: ReadonlyArray<infer U>;
|
|
14
14
|
} ? U extends {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* resonare v0.0.
|
|
2
|
+
* resonare v0.0.12
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
var resonare=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=`resonare`;let n=({type:e=`localStorage`}={})=>({abortController:t})=>({get:t=>JSON.parse(window[e].getItem(t)||`null`),set:(t,n)=>{window[e].setItem(t,JSON.stringify(n))},watch:n=>{let r=new AbortController;return window.addEventListener(`storage`,t=>{t.storageArea===window[e]&&n(t.key,JSON.parse(t.newValue))},{signal:AbortSignal.any([t.signal,r.signal])}),()=>{r.abort()}}});function r(e){return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,t.initialValue??t.options[0].value??t.options[0]]))}var i=class{#e;#t;#n;#r;#i;#a=new Set;#o;#s=new AbortController;constructor({key:e=t,config:i,initialState:a={},storage:o=n()}){let s={},c={...a.systemOptions};Object.entries(i).forEach(([e,t])=>{s[e]={},`options`in t&&t.options.forEach(t=>{typeof t==`object`?(t.media&&!Object.hasOwn(c,e)&&(c[e]=[t.media[1],t.media[2]]),s[e][String(t.value)]=t):s[e][String(t)]={value:t}})}),this.#n={key:e,config:s},this.#r=c,this.#e=r(i),this.#t={...this.#e,...a.themes},this.#i=o?.({abortController:this.#s})??null,this.#o={}}getThemes=()=>this.#t;getResolvedThemes=()=>this.#l();setThemes=
|
|
7
|
+
var resonare=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=`resonare`;let n=({type:e=`localStorage`}={})=>({abortController:t})=>({get:t=>JSON.parse(window[e].getItem(t)||`null`),set:(t,n)=>{window[e].setItem(t,JSON.stringify(n))},watch:n=>{let r=new AbortController;return window.addEventListener(`storage`,t=>{t.storageArea===window[e]&&n(t.key,JSON.parse(t.newValue))},{signal:AbortSignal.any([t.signal,r.signal])}),()=>{r.abort()}}});function r(e){return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,t.initialValue??t.options[0].value??t.options[0]]))}var i=class{#e;#t;#n;#r;#i;#a=new Set;#o;#s=new AbortController;constructor({key:e=t,config:i,initialState:a={},storage:o=n()}){let s={},c={...a.systemOptions};Object.entries(i).forEach(([e,t])=>{s[e]={},`options`in t&&t.options.forEach(t=>{typeof t==`object`?(t.media&&!Object.hasOwn(c,e)&&(c[e]=[t.media[1],t.media[2]]),s[e][String(t.value)]=t):s[e][String(t)]={value:t}})}),this.#n={key:e,config:s},this.#r=c,this.#e=r(i),this.#t={...this.#e,...a.themes},this.#i=o?.({abortController:this.#s})??null,this.#o={}}getThemes=()=>this.#t;getResolvedThemes=()=>this.#l();setThemes=e=>{let t=typeof e==`function`?e(this.#t):e;this.#c({...this.#t,...t});let n=this.toPersist();this.#i&&(this.#i.set(this.#n.key,n),this.#i.broadcast?.(this.#n.key,n))};updateSystemOption=(e,[t,n])=>{this.#r[e]=[t,n],this.setThemes({...this.#t})};toPersist=()=>({version:1,themes:this.#t,systemOptions:this.#r});restore=()=>{let e=this.#i?.get(this.#n.key);if(!e){this.#c({...this.#e});return}Object.hasOwn(e,`version`)||(e={version:1,themes:e,systemOptions:this.#r}),this.#r={...this.#r,...e.systemOptions},this.#c({...this.#e,...e.themes})};subscribe=(e,{immediate:t=!1}={})=>(t&&e({themes:this.#t,resolvedThemes:this.#l()}),this.#a.add(e),()=>{this.#a.delete(e)});sync=()=>{if(this.#i?.watch)return this.#i.watch((e,t)=>{e===this.#n.key&&(this.#r=t.systemOptions,this.#c(t.themes))})};___destroy=()=>{this.#a.clear(),this.#s.abort()};#c=e=>{this.#t=e,this.#d()};#l=()=>Object.fromEntries(Object.entries(this.#t).map(([e,t])=>{let n=this.#n.config[e]?.[t];return[e,n?this.#u({themeKey:e,option:n}):t]}));#u=({themeKey:e,option:t})=>{if(!t.media)return t.value;let[n]=t.media;if(!this.#o[n]){let r=window.matchMedia(n);this.#o[n]=r,r.addEventListener(`change`,()=>{this.#t[e]===t.value&&this.#c({...this.#t})},{signal:this.#s.signal})}let[r,i]=this.#r[e];return this.#o[n].matches?r:i};#d=()=>{for(let e of this.#a)e({themes:this.#t,resolvedThemes:this.#l()})}};let a=new class{#e=new Map;create=e=>{let n=e.key||t,r=this.#e.get(n);return r||(r=new i(e),this.#e.set(n,r)),r};get=e=>{let n=e||t;if(!this.#e.has(n))throw Error(`Theme store '${n}' not found.`);return this.#e.get(n)};destroy=e=>{let n=e||t;this.#e.has(n)&&(this.#e.get(n).___destroy(),this.#e.delete(n))}},o=a.create,s=a.get,c=a.destroy;return e.createThemeStore=o,e.destroyThemeStore=c,e.getThemeStore=s,e})({});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resonare",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"description": "Resonare",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"theming"
|
|
@@ -19,6 +19,9 @@
|
|
|
19
19
|
"types": "./dist/index.d.ts",
|
|
20
20
|
"import": "./dist/index.js"
|
|
21
21
|
},
|
|
22
|
+
"./global": {
|
|
23
|
+
"types": "./global.d.ts"
|
|
24
|
+
},
|
|
22
25
|
"./inline-script": {
|
|
23
26
|
"import": "./dist/inline-script.ts"
|
|
24
27
|
},
|