@fluixi/core 1.0.0-alpha.92 → 1.0.0-alpha.93
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cdn/core.global.js +2 -2
- package/dist/src/cdn/{chunk-MDJLC3PE.mjs → chunk-7XM7Q5VR.mjs} +271 -229
- package/dist/src/cdn/core.cjs +360 -253
- package/dist/src/cdn/core.mjs +92 -20
- package/dist/src/cdn/{router-JYFZP3EZ.mjs → router-VOY43YNM.mjs} +7 -3
- package/dist/src/index.cjs +547 -232
- package/dist/src/index.mjs +547 -223
- package/dist/src/lib/client/chunk-QT3RFKH6.mjs +53 -0
- package/dist/src/lib/client/index.cjs +310 -176
- package/dist/src/lib/client/index.d.ts.map +1 -1
- package/dist/src/lib/client/index.js +33 -18
- package/dist/src/lib/client/index.mjs +86 -15
- package/dist/src/lib/client/{router-PHLUYVQU.mjs → router-ADTS55OP.mjs} +244 -99
- package/dist/src/lib/context/context.cjs +35 -72
- package/dist/src/lib/context/context.d.ts +45 -291
- package/dist/src/lib/context/context.d.ts.map +1 -1
- package/dist/src/lib/context/context.js +65 -397
- package/dist/src/lib/context/context.mjs +38 -73
- package/dist/src/lib/context/index.cjs +35 -72
- package/dist/src/lib/context/index.d.ts +2 -2
- package/dist/src/lib/context/index.d.ts.map +1 -1
- package/dist/src/lib/context/index.js +1 -5
- package/dist/src/lib/context/index.mjs +38 -73
- package/dist/src/lib/core.cjs +546 -231
- package/dist/src/lib/core.d.ts +3 -1
- package/dist/src/lib/core.d.ts.map +1 -1
- package/dist/src/lib/core.js +5 -3
- package/dist/src/lib/core.mjs +546 -222
- package/dist/src/lib/env/index.cjs +1 -17
- package/dist/src/lib/env/index.d.ts +2 -14
- package/dist/src/lib/env/index.d.ts.map +1 -1
- package/dist/src/lib/env/index.js +2 -26
- package/dist/src/lib/env/index.mjs +1 -17
- package/dist/src/lib/i18n/index.cjs +17 -2
- package/dist/src/lib/i18n/index.d.ts +10 -0
- package/dist/src/lib/i18n/index.d.ts.map +1 -1
- package/dist/src/lib/i18n/index.js +17 -1
- package/dist/src/lib/i18n/index.mjs +17 -2
- package/dist/src/lib/index.cjs +64 -99
- package/dist/src/lib/index.mjs +43 -76
- package/dist/src/lib/isomorphic.cjs +9 -4
- package/dist/src/lib/isomorphic.d.ts +9 -2
- package/dist/src/lib/isomorphic.d.ts.map +1 -1
- package/dist/src/lib/isomorphic.js +11 -4
- package/dist/src/lib/isomorphic.mjs +9 -4
- package/dist/src/lib/jsx-runtime/index.cjs +1 -1
- package/dist/src/lib/jsx-runtime/index.mjs +1 -1
- package/dist/src/lib/jsx-runtime/jsx-dev-runtime.cjs +1 -1
- package/dist/src/lib/jsx-runtime/jsx-dev-runtime.mjs +1 -1
- package/dist/src/lib/plugins/fluixi-routes-plugin.cjs +2 -2
- package/dist/src/lib/plugins/fluixi-routes-plugin.mjs +2 -2
- package/dist/src/lib/plugins/index.cjs +3 -3
- package/dist/src/lib/plugins/index.mjs +3 -3
- package/dist/src/lib/plugins/route-codegen.cjs +2 -2
- package/dist/src/lib/plugins/route-codegen.js +1 -1
- package/dist/src/lib/plugins/route-codegen.mjs +2 -2
- package/dist/src/lib/render/index.cjs +1 -1
- package/dist/src/lib/render/index.mjs +1 -1
- package/dist/src/lib/render/versions.cjs +1 -1
- package/dist/src/lib/render/versions.mjs +1 -1
- package/dist/src/lib/router/data.cjs +113 -115
- package/dist/src/lib/router/data.d.ts +63 -10
- package/dist/src/lib/router/data.d.ts.map +1 -1
- package/dist/src/lib/router/data.js +158 -68
- package/dist/src/lib/router/data.mjs +112 -112
- package/dist/src/lib/router/index.cjs +260 -163
- package/dist/src/lib/router/index.d.ts +22 -2
- package/dist/src/lib/router/index.d.ts.map +1 -1
- package/dist/src/lib/router/index.js +165 -22
- package/dist/src/lib/router/index.mjs +244 -143
- package/dist/src/lib/server/index.cjs +1 -1
- package/dist/src/lib/server/index.mjs +1 -1
- package/dist/src/lib/storage/cache.cjs +51 -0
- package/dist/src/lib/storage/cache.d.ts +18 -0
- package/dist/src/lib/storage/cache.d.ts.map +1 -0
- package/dist/src/lib/storage/cache.js +42 -0
- package/dist/src/lib/storage/cache.mjs +30 -0
- package/dist/src/lib/storage/index.cjs +246 -0
- package/dist/src/lib/storage/index.d.ts +83 -0
- package/dist/src/lib/storage/index.d.ts.map +1 -0
- package/dist/src/lib/storage/index.js +179 -0
- package/dist/src/lib/storage/index.mjs +231 -0
- package/dist/src/lib/storage/persisted.cjs +91 -0
- package/dist/src/lib/storage/persisted.d.ts +53 -0
- package/dist/src/lib/storage/persisted.d.ts.map +1 -0
- package/dist/src/lib/storage/persisted.js +75 -0
- package/dist/src/lib/storage/persisted.mjs +70 -0
- package/dist/src/lib/theme/color-scheme.cjs +281 -0
- package/dist/src/lib/theme/color-scheme.d.ts +61 -0
- package/dist/src/lib/theme/color-scheme.d.ts.map +1 -0
- package/dist/src/lib/theme/color-scheme.js +120 -0
- package/dist/src/lib/theme/color-scheme.mjs +265 -0
- package/dist/src/lib/theme/index.cjs +194 -37
- package/dist/src/lib/theme/index.d.ts +23 -11
- package/dist/src/lib/theme/index.d.ts.map +1 -1
- package/dist/src/lib/theme/index.js +73 -32
- package/dist/src/lib/theme/index.mjs +197 -35
- package/dist/src/lib/theme/provider.cjs +346 -0
- package/dist/src/lib/theme/provider.d.ts +34 -0
- package/dist/src/lib/theme/provider.d.ts.map +1 -0
- package/dist/src/lib/theme/provider.js +60 -0
- package/dist/src/lib/theme/provider.mjs +332 -0
- package/dist/src/routes.cjs +1 -1
- package/dist/src/routes.mjs +1 -1
- package/dist/src/utils.cjs +1 -1
- package/dist/src/utils.mjs +1 -1
- package/dist/src/version.generated.cjs +1 -1
- package/dist/src/version.generated.d.ts +1 -1
- package/dist/src/version.generated.js +1 -1
- package/dist/src/version.generated.mjs +1 -1
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +18 -11
- package/dist/src/lib/client/chunk-VB567ABU.mjs +0 -133
|
@@ -4,11 +4,15 @@
|
|
|
4
4
|
* without rewriting it. Generic over an arbitrary theme list (not just light/dark),
|
|
5
5
|
* with optional async theme loading and lifecycle hooks.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* The choice lives in a cookie by default, so the server render reads it and sends
|
|
8
|
+
* `<html data-theme>` already set: no flash and no mismatch for a returning visitor.
|
|
9
9
|
*/
|
|
10
|
+
import { getActiveHead, useHead } from '@fluixi/head';
|
|
10
11
|
import { createSignal } from '@fluixi/reactive/signal';
|
|
11
|
-
import {
|
|
12
|
+
import { StorageUnavailableError } from '@fluixi/storage';
|
|
13
|
+
import { env } from '../env/index.js';
|
|
14
|
+
import { persisted } from '../storage/persisted.js';
|
|
15
|
+
import { useStorage } from '../storage/index.js';
|
|
12
16
|
function systemMap(system) {
|
|
13
17
|
if (!system)
|
|
14
18
|
return null;
|
|
@@ -16,6 +20,31 @@ function systemMap(system) {
|
|
|
16
20
|
return { dark: 'dark', light: 'light' };
|
|
17
21
|
return system;
|
|
18
22
|
}
|
|
23
|
+
const YEAR = 60 * 60 * 24 * 365;
|
|
24
|
+
/** @internal: a cookie for a year, or memory outside a request on the server. */
|
|
25
|
+
export function defaultStorage() {
|
|
26
|
+
try {
|
|
27
|
+
return useStorage({ scope: 'cookie', cookie: { maxAge: YEAR, sameSite: 'lax' } });
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (error instanceof StorageUnavailableError)
|
|
31
|
+
return null;
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** How the head script finds the stored choice before any module has loaded. */
|
|
36
|
+
/** @internal */
|
|
37
|
+
export function storedLookup(storage, key) {
|
|
38
|
+
if (!storage)
|
|
39
|
+
return null;
|
|
40
|
+
const full = storage.keyPrefix + key;
|
|
41
|
+
if (storage.scope === 'cookie') {
|
|
42
|
+
return { from: 'cookie', name: encodeURIComponent(full).replace(/[()]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`) };
|
|
43
|
+
}
|
|
44
|
+
if (storage.scope === 'local')
|
|
45
|
+
return { from: 'local', name: full };
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
19
48
|
/**
|
|
20
49
|
* A theme controller: a reactive current theme, written to an attribute on the document.
|
|
21
50
|
*
|
|
@@ -28,39 +57,42 @@ function systemMap(system) {
|
|
|
28
57
|
* ```ts
|
|
29
58
|
* const theme = createTheme({ themes: ['light', 'dark'], system: { dark: 'dark' } });
|
|
30
59
|
* theme.set('dark');
|
|
31
|
-
* theme.
|
|
60
|
+
* theme.value(); // 'dark', and the document now carries data-theme="dark"
|
|
32
61
|
* ```
|
|
33
62
|
*/
|
|
34
63
|
export function createTheme(options) {
|
|
35
64
|
const storageKey = options.storageKey ?? 'theme';
|
|
36
65
|
const attribute = options.attribute ?? 'data-theme';
|
|
37
66
|
const sys = systemMap(options.system);
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
67
|
+
const storage = options.storage === undefined ? defaultStorage() : options.storage;
|
|
68
|
+
const valid = (t) => typeof t === 'string' && options.themes.includes(t);
|
|
69
|
+
// Without a stored choice: the system preference where there is one to ask, else the default.
|
|
70
|
+
const fallback = sys && env.window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches ? sys.dark : sys ? sys.light : options.default;
|
|
71
|
+
const stored = storage ? persisted(storage, storageKey) : null;
|
|
72
|
+
// Holds a choice the storage refused (a cookie after the response went out), so the page still switches.
|
|
73
|
+
const [unsaved, setUnsaved] = createSignal(null);
|
|
74
|
+
const value = () => {
|
|
75
|
+
const local = unsaved();
|
|
76
|
+
if (local !== null)
|
|
77
|
+
return local;
|
|
78
|
+
const saved = stored?.();
|
|
79
|
+
return valid(saved) ? saved : fallback;
|
|
47
80
|
};
|
|
48
|
-
const [value, setValue] = createSignal(detectInitial());
|
|
49
81
|
const [isLoading, setLoading] = createSignal(false);
|
|
50
82
|
const apply = (t) => {
|
|
51
83
|
env.document?.documentElement.setAttribute(attribute, t);
|
|
52
|
-
try {
|
|
53
|
-
storage.setItem(storageKey, t);
|
|
54
|
-
}
|
|
55
|
-
catch {
|
|
56
|
-
/* best-effort persistence */
|
|
57
|
-
}
|
|
58
84
|
};
|
|
59
85
|
const set = (t) => {
|
|
60
86
|
if (!options.themes.includes(t))
|
|
61
87
|
return;
|
|
62
88
|
const prev = value();
|
|
63
|
-
|
|
89
|
+
try {
|
|
90
|
+
stored?.set(t);
|
|
91
|
+
setUnsaved(stored ? null : t);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
setUnsaved(() => t);
|
|
95
|
+
}
|
|
64
96
|
apply(t);
|
|
65
97
|
options.onChange?.(t, prev);
|
|
66
98
|
if (options.load) {
|
|
@@ -77,18 +109,27 @@ export function createTheme(options) {
|
|
|
77
109
|
set(options.themes[(i + 1) % options.themes.length]);
|
|
78
110
|
};
|
|
79
111
|
const init = () => apply(value());
|
|
80
|
-
const
|
|
81
|
-
const cfg = JSON.stringify({
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return (`<script>(function(){try{var c=${cfg},t=localStorage.getItem(c.k);` +
|
|
112
|
+
const scriptBody = () => {
|
|
113
|
+
const cfg = JSON.stringify({ s: storedLookup(storage, storageKey), a: attribute, d: options.default, sys, ts: options.themes }).replace(/</g, '\\u003c');
|
|
114
|
+
// Runs before paint: read the stored choice (or the system preference) and set the
|
|
115
|
+
// attribute, for pages the server did not render with it. Stored values are envelopes.
|
|
116
|
+
return (`(function(){try{var c=${cfg},t=null,s=c.s,r=null;` +
|
|
117
|
+
`if(s&&s.from==='local')r=localStorage.getItem(s.name);` +
|
|
118
|
+
`if(s&&s.from==='cookie')document.cookie.split('; ').forEach(function(p){var i=p.indexOf('=');if(p.slice(0,i)===s.name)r=decodeURIComponent(p.slice(i+1));});` +
|
|
119
|
+
`if(r){var e=JSON.parse(r);t=e&&e.v===1?e.value:e;}` +
|
|
120
|
+
`if(c.ts.indexOf(t)<0)t=null;` +
|
|
90
121
|
`if(!t&&c.sys)t=matchMedia('(prefers-color-scheme: dark)').matches?c.sys.dark:c.sys.light;` +
|
|
91
|
-
`if(!t)t=c.d;document.documentElement.setAttribute(c.a,t);}catch(e){}})()
|
|
122
|
+
`if(!t)t=c.d;document.documentElement.setAttribute(c.a,t);}catch(e){}})();`);
|
|
92
123
|
};
|
|
124
|
+
const headScript = () => `<script>${scriptBody()}</script>`;
|
|
125
|
+
// During a render the attribute goes out with the page; on the client the head keeps it current.
|
|
126
|
+
// A prerendered page, or a first visit before any choice, has nothing the server could know;
|
|
127
|
+
// the script settles it before paint, from the same configuration.
|
|
128
|
+
if (getActiveHead()) {
|
|
129
|
+
useHead({
|
|
130
|
+
htmlAttrs: { [attribute]: () => value() },
|
|
131
|
+
...(options.script === false ? {} : { script: [{ children: scriptBody() }] }),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
93
134
|
return { value, isLoading, set, toggle, init, headScript };
|
|
94
135
|
}
|
|
@@ -1,23 +1,11 @@
|
|
|
1
1
|
// src/lib/theme/index.ts
|
|
2
|
+
import { getActiveHead, useHead } from "@fluixi/head";
|
|
2
3
|
import { createSignal } from "@fluixi/reactive/signal";
|
|
4
|
+
import { StorageUnavailableError as StorageUnavailableError2 } from "@fluixi/storage";
|
|
3
5
|
|
|
4
6
|
// src/lib/env/index.ts
|
|
5
7
|
import { isServer } from "@fluixi/dom";
|
|
6
8
|
import { isServer as onServer } from "@fluixi/dom";
|
|
7
|
-
function createMemoryStorage() {
|
|
8
|
-
const mem = /* @__PURE__ */ new Map();
|
|
9
|
-
return {
|
|
10
|
-
getItem: (k) => mem.has(k) ? mem.get(k) : null,
|
|
11
|
-
setItem: (k, v) => void mem.set(k, String(v)),
|
|
12
|
-
removeItem: (k) => void mem.delete(k),
|
|
13
|
-
clear: () => mem.clear(),
|
|
14
|
-
key: (i) => Array.from(mem.keys())[i] ?? null,
|
|
15
|
-
get length() {
|
|
16
|
-
return mem.size;
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
var storage = typeof localStorage !== "undefined" ? localStorage : createMemoryStorage();
|
|
21
9
|
var env = {
|
|
22
10
|
get window() {
|
|
23
11
|
return typeof window !== "undefined" ? window : void 0;
|
|
@@ -27,36 +15,206 @@ var env = {
|
|
|
27
15
|
}
|
|
28
16
|
};
|
|
29
17
|
|
|
18
|
+
// src/lib/storage/persisted.ts
|
|
19
|
+
import { getOwner, onCleanup, signal, untrack } from "@fluixi/reactive/signal";
|
|
20
|
+
var isAsync = (storage) => "encryption" in storage;
|
|
21
|
+
function persisted(storage, key, options = {}) {
|
|
22
|
+
const fallback = options.fallback ?? null;
|
|
23
|
+
const name = options.name ?? `storage:${storage.keyPrefix}${key}`;
|
|
24
|
+
const state = signal(isAsync(storage) ? fallback : storage.get(key) ?? fallback, { name });
|
|
25
|
+
const put = (value) => state.set(() => value);
|
|
26
|
+
const resolve = (next) => typeof next === "function" ? next(untrack(state)) : next;
|
|
27
|
+
const writeOptions = (extra) => ({
|
|
28
|
+
...options.ttl !== void 0 ? { ttl: options.ttl } : {},
|
|
29
|
+
...options.encrypt ? { encrypt: true } : {},
|
|
30
|
+
...extra
|
|
31
|
+
});
|
|
32
|
+
if (!isAsync(storage)) {
|
|
33
|
+
const off2 = storage.subscribe(key, (event) => {
|
|
34
|
+
if (event.type !== "set") return put(fallback);
|
|
35
|
+
if (event.value !== void 0) put(event.value);
|
|
36
|
+
});
|
|
37
|
+
if (getOwner()) onCleanup(off2);
|
|
38
|
+
return Object.assign(() => state(), {
|
|
39
|
+
set(next, extra) {
|
|
40
|
+
const value = resolve(next);
|
|
41
|
+
storage.set(key, value, writeOptions(extra));
|
|
42
|
+
put(value);
|
|
43
|
+
},
|
|
44
|
+
remove() {
|
|
45
|
+
storage.remove(key);
|
|
46
|
+
put(fallback);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
let written = false;
|
|
51
|
+
const reread = () => storage.get(key).then(
|
|
52
|
+
(value) => put(value ?? fallback),
|
|
53
|
+
() => void 0
|
|
54
|
+
);
|
|
55
|
+
const off = storage.subscribe(key, (event) => {
|
|
56
|
+
if (event.type !== "set") return put(fallback);
|
|
57
|
+
if (event.value !== void 0) put(event.value);
|
|
58
|
+
else void reread();
|
|
59
|
+
});
|
|
60
|
+
if (getOwner()) onCleanup(off);
|
|
61
|
+
const ready = storage.get(key).then((value) => {
|
|
62
|
+
if (!written) put(value ?? fallback);
|
|
63
|
+
});
|
|
64
|
+
return Object.assign(() => state(), {
|
|
65
|
+
ready,
|
|
66
|
+
async set(next, extra) {
|
|
67
|
+
const previous = untrack(state);
|
|
68
|
+
const value = resolve(next);
|
|
69
|
+
written = true;
|
|
70
|
+
put(value);
|
|
71
|
+
try {
|
|
72
|
+
await storage.set(key, value, writeOptions(extra));
|
|
73
|
+
} catch (error) {
|
|
74
|
+
put(previous);
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
async remove() {
|
|
79
|
+
written = true;
|
|
80
|
+
await storage.remove(key);
|
|
81
|
+
put(fallback);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/lib/storage/index.ts
|
|
87
|
+
import { appendResponseHeader, getRequestContext, getServerData } from "@fluixi/dom";
|
|
88
|
+
import { createContext, useContext } from "@fluixi/reactive/signal";
|
|
89
|
+
import {
|
|
90
|
+
createAsyncStorage,
|
|
91
|
+
createSyncStorage,
|
|
92
|
+
memoryStorage,
|
|
93
|
+
StorageUnavailableError
|
|
94
|
+
} from "@fluixi/storage";
|
|
95
|
+
import { documentCookieAdapter, localStorageAdapter, sessionStorageAdapter } from "@fluixi/storage/browser";
|
|
96
|
+
import { cookieStorage, requestStorage, toStorageRequest } from "@fluixi/storage/server";
|
|
97
|
+
var StorageContext = createContext(void 0, "storage");
|
|
98
|
+
var AsyncStorageContext = createContext(void 0, "asyncStorage");
|
|
99
|
+
var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
|
|
100
|
+
var perRequest = /* @__PURE__ */ new WeakMap();
|
|
101
|
+
var perPage = /* @__PURE__ */ new Map();
|
|
102
|
+
function cached(scope, id, make) {
|
|
103
|
+
let adapter = scope.get(id);
|
|
104
|
+
if (!adapter) scope.set(id, adapter = make());
|
|
105
|
+
return adapter;
|
|
106
|
+
}
|
|
107
|
+
function requestRequired(scope) {
|
|
108
|
+
throw new StorageUnavailableError(
|
|
109
|
+
`useStorage({ scope: '${scope}' }) on the server needs a request: call it during a render, a loader or a server function, or provide a storage with <StorageProvider>.`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
function adapterFor(scope, cookie = {}) {
|
|
113
|
+
const ctx = getRequestContext();
|
|
114
|
+
const options = { httpOnly: false, ...cookie };
|
|
115
|
+
switch (scope) {
|
|
116
|
+
case "local":
|
|
117
|
+
return localStorageAdapter();
|
|
118
|
+
case "session":
|
|
119
|
+
return sessionStorageAdapter();
|
|
120
|
+
case "request":
|
|
121
|
+
if (isBrowser()) return cached(perPage, "request", memoryStorage);
|
|
122
|
+
if (!ctx) requestRequired(scope);
|
|
123
|
+
return cached(perRequest.get(ctx) ?? perRequest.set(ctx, /* @__PURE__ */ new Map()).get(ctx), "request", requestStorage);
|
|
124
|
+
case "cookie": {
|
|
125
|
+
const id = `cookie:${JSON.stringify(options)}`;
|
|
126
|
+
if (isBrowser()) {
|
|
127
|
+
const { httpOnly: _ignored, ...browser } = options;
|
|
128
|
+
return cached(perPage, id, () => documentCookieAdapter(browser));
|
|
129
|
+
}
|
|
130
|
+
if (!ctx) requestRequired(scope);
|
|
131
|
+
const raw = ctx.event.request;
|
|
132
|
+
const target = raw && typeof raw === "object" ? raw : void 0;
|
|
133
|
+
return cached(
|
|
134
|
+
perRequest.get(ctx) ?? perRequest.set(ctx, /* @__PURE__ */ new Map()).get(ctx),
|
|
135
|
+
id,
|
|
136
|
+
() => cookieStorage({
|
|
137
|
+
...options,
|
|
138
|
+
request: raw && typeof raw === "object" && ("headers" in raw || "getHeader" in raw) ? toStorageRequest(raw) : void 0,
|
|
139
|
+
response: {
|
|
140
|
+
appendHeader(name, value) {
|
|
141
|
+
if (!appendResponseHeader(name, value, target)) {
|
|
142
|
+
throw new StorageUnavailableError("The response headers were already sent, so this cookie cannot reach the browser. Write it before the page head goes out: in middleware, a loader, a server function or the render before its first chunk.");
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
})
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function useStorage(options = {}) {
|
|
152
|
+
const { scope = "auto", cookie, ...storageOptions } = options;
|
|
153
|
+
if (scope === "auto") {
|
|
154
|
+
const provided = useContext(StorageContext);
|
|
155
|
+
if (provided) return options.namespace ? provided.namespace(options.namespace) : provided;
|
|
156
|
+
}
|
|
157
|
+
return createSyncStorage({ ...storageOptions, adapter: resolveAdapter(scope, cookie) });
|
|
158
|
+
}
|
|
159
|
+
function resolveAdapter(scope, cookie) {
|
|
160
|
+
if (scope !== "auto") return adapterFor(scope, cookie);
|
|
161
|
+
if (isBrowser()) return adapterFor("local");
|
|
162
|
+
if (!getRequestContext()) requestRequired(scope);
|
|
163
|
+
return adapterFor("request");
|
|
164
|
+
}
|
|
165
|
+
|
|
30
166
|
// src/lib/theme/index.ts
|
|
31
167
|
function systemMap(system) {
|
|
32
168
|
if (!system) return null;
|
|
33
169
|
if (system === true) return { dark: "dark", light: "light" };
|
|
34
170
|
return system;
|
|
35
171
|
}
|
|
172
|
+
var YEAR = 60 * 60 * 24 * 365;
|
|
173
|
+
function defaultStorage() {
|
|
174
|
+
try {
|
|
175
|
+
return useStorage({ scope: "cookie", cookie: { maxAge: YEAR, sameSite: "lax" } });
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (error instanceof StorageUnavailableError2) return null;
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function storedLookup(storage, key) {
|
|
182
|
+
if (!storage) return null;
|
|
183
|
+
const full = storage.keyPrefix + key;
|
|
184
|
+
if (storage.scope === "cookie") {
|
|
185
|
+
return { from: "cookie", name: encodeURIComponent(full).replace(/[()]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`) };
|
|
186
|
+
}
|
|
187
|
+
if (storage.scope === "local") return { from: "local", name: full };
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
36
190
|
function createTheme(options) {
|
|
37
191
|
const storageKey = options.storageKey ?? "theme";
|
|
38
192
|
const attribute = options.attribute ?? "data-theme";
|
|
39
193
|
const sys = systemMap(options.system);
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
194
|
+
const storage = options.storage === void 0 ? defaultStorage() : options.storage;
|
|
195
|
+
const valid = (t) => typeof t === "string" && options.themes.includes(t);
|
|
196
|
+
const fallback = sys && env.window?.matchMedia?.("(prefers-color-scheme: dark)")?.matches ? sys.dark : sys ? sys.light : options.default;
|
|
197
|
+
const stored = storage ? persisted(storage, storageKey) : null;
|
|
198
|
+
const [unsaved, setUnsaved] = createSignal(null);
|
|
199
|
+
const value = () => {
|
|
200
|
+
const local = unsaved();
|
|
201
|
+
if (local !== null) return local;
|
|
202
|
+
const saved = stored?.();
|
|
203
|
+
return valid(saved) ? saved : fallback;
|
|
46
204
|
};
|
|
47
|
-
const [value, setValue] = createSignal(detectInitial());
|
|
48
205
|
const [isLoading, setLoading] = createSignal(false);
|
|
49
206
|
const apply = (t) => {
|
|
50
207
|
env.document?.documentElement.setAttribute(attribute, t);
|
|
51
|
-
try {
|
|
52
|
-
storage.setItem(storageKey, t);
|
|
53
|
-
} catch {
|
|
54
|
-
}
|
|
55
208
|
};
|
|
56
209
|
const set = (t) => {
|
|
57
210
|
if (!options.themes.includes(t)) return;
|
|
58
211
|
const prev = value();
|
|
59
|
-
|
|
212
|
+
try {
|
|
213
|
+
stored?.set(t);
|
|
214
|
+
setUnsaved(stored ? null : t);
|
|
215
|
+
} catch {
|
|
216
|
+
setUnsaved(() => t);
|
|
217
|
+
}
|
|
60
218
|
apply(t);
|
|
61
219
|
options.onChange?.(t, prev);
|
|
62
220
|
if (options.load) {
|
|
@@ -73,17 +231,21 @@ function createTheme(options) {
|
|
|
73
231
|
set(options.themes[(i + 1) % options.themes.length]);
|
|
74
232
|
};
|
|
75
233
|
const init = () => apply(value());
|
|
76
|
-
const
|
|
77
|
-
const cfg = JSON.stringify({
|
|
78
|
-
|
|
79
|
-
a: attribute,
|
|
80
|
-
d: options.default,
|
|
81
|
-
sys
|
|
82
|
-
});
|
|
83
|
-
return `<script>(function(){try{var c=${cfg},t=localStorage.getItem(c.k);if(!t&&c.sys)t=matchMedia('(prefers-color-scheme: dark)').matches?c.sys.dark:c.sys.light;if(!t)t=c.d;document.documentElement.setAttribute(c.a,t);}catch(e){}})();<\/script>`;
|
|
234
|
+
const scriptBody = () => {
|
|
235
|
+
const cfg = JSON.stringify({ s: storedLookup(storage, storageKey), a: attribute, d: options.default, sys, ts: options.themes }).replace(/</g, "\\u003c");
|
|
236
|
+
return `(function(){try{var c=${cfg},t=null,s=c.s,r=null;if(s&&s.from==='local')r=localStorage.getItem(s.name);if(s&&s.from==='cookie')document.cookie.split('; ').forEach(function(p){var i=p.indexOf('=');if(p.slice(0,i)===s.name)r=decodeURIComponent(p.slice(i+1));});if(r){var e=JSON.parse(r);t=e&&e.v===1?e.value:e;}if(c.ts.indexOf(t)<0)t=null;if(!t&&c.sys)t=matchMedia('(prefers-color-scheme: dark)').matches?c.sys.dark:c.sys.light;if(!t)t=c.d;document.documentElement.setAttribute(c.a,t);}catch(e){}})();`;
|
|
84
237
|
};
|
|
238
|
+
const headScript = () => `<script>${scriptBody()}<\/script>`;
|
|
239
|
+
if (getActiveHead()) {
|
|
240
|
+
useHead({
|
|
241
|
+
htmlAttrs: { [attribute]: () => value() },
|
|
242
|
+
...options.script === false ? {} : { script: [{ children: scriptBody() }] }
|
|
243
|
+
});
|
|
244
|
+
}
|
|
85
245
|
return { value, isLoading, set, toggle, init, headScript };
|
|
86
246
|
}
|
|
87
247
|
export {
|
|
88
|
-
createTheme
|
|
248
|
+
createTheme,
|
|
249
|
+
defaultStorage,
|
|
250
|
+
storedLookup
|
|
89
251
|
};
|