@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
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/lib/theme/provider.ts
|
|
21
|
+
var provider_exports = {};
|
|
22
|
+
__export(provider_exports, {
|
|
23
|
+
ColorSchemeProvider: () => ColorSchemeProvider,
|
|
24
|
+
ThemeProvider: () => ThemeProvider,
|
|
25
|
+
useColorScheme: () => useColorScheme,
|
|
26
|
+
useTheme: () => useTheme
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(provider_exports);
|
|
29
|
+
|
|
30
|
+
// src/lib/context/context.ts
|
|
31
|
+
var import_dom = require("@fluixi/dom");
|
|
32
|
+
var import_signal = require("@fluixi/reactive/signal");
|
|
33
|
+
function createContext(defaultValue, name) {
|
|
34
|
+
const context = (0, import_signal.createContext)(defaultValue, name);
|
|
35
|
+
const Provider = (props) => (
|
|
36
|
+
// Children stay a getter: the reactive Provider reads them after stamping the value on
|
|
37
|
+
// its owner. Reading `props.children` here would render them, and any <Outlet/> in them,
|
|
38
|
+
// before the context exists.
|
|
39
|
+
context.Provider({
|
|
40
|
+
value: props.value,
|
|
41
|
+
get children() {
|
|
42
|
+
return props.children;
|
|
43
|
+
},
|
|
44
|
+
debug: props.debug
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
return {
|
|
48
|
+
id: context.id,
|
|
49
|
+
Provider: (0, import_dom.asProvider)(Provider),
|
|
50
|
+
defaultValue
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function useContext(context) {
|
|
54
|
+
const value = (0, import_signal.useContext)(context);
|
|
55
|
+
return value !== void 0 ? value : context.defaultValue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/lib/theme/color-scheme.ts
|
|
59
|
+
var import_head2 = require("@fluixi/head");
|
|
60
|
+
var import_signal5 = require("@fluixi/reactive/signal");
|
|
61
|
+
|
|
62
|
+
// src/lib/env/index.ts
|
|
63
|
+
var import_dom2 = require("@fluixi/dom");
|
|
64
|
+
var import_dom3 = require("@fluixi/dom");
|
|
65
|
+
var env = {
|
|
66
|
+
get window() {
|
|
67
|
+
return typeof window !== "undefined" ? window : void 0;
|
|
68
|
+
},
|
|
69
|
+
get document() {
|
|
70
|
+
return typeof document !== "undefined" ? document : void 0;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/lib/storage/persisted.ts
|
|
75
|
+
var import_signal2 = require("@fluixi/reactive/signal");
|
|
76
|
+
var isAsync = (storage) => "encryption" in storage;
|
|
77
|
+
function persisted(storage, key, options = {}) {
|
|
78
|
+
const fallback = options.fallback ?? null;
|
|
79
|
+
const name = options.name ?? `storage:${storage.keyPrefix}${key}`;
|
|
80
|
+
const state = (0, import_signal2.signal)(isAsync(storage) ? fallback : storage.get(key) ?? fallback, { name });
|
|
81
|
+
const put = (value) => state.set(() => value);
|
|
82
|
+
const resolve = (next) => typeof next === "function" ? next((0, import_signal2.untrack)(state)) : next;
|
|
83
|
+
const writeOptions = (extra) => ({
|
|
84
|
+
...options.ttl !== void 0 ? { ttl: options.ttl } : {},
|
|
85
|
+
...options.encrypt ? { encrypt: true } : {},
|
|
86
|
+
...extra
|
|
87
|
+
});
|
|
88
|
+
if (!isAsync(storage)) {
|
|
89
|
+
const off2 = storage.subscribe(key, (event) => {
|
|
90
|
+
if (event.type !== "set") return put(fallback);
|
|
91
|
+
if (event.value !== void 0) put(event.value);
|
|
92
|
+
});
|
|
93
|
+
if ((0, import_signal2.getOwner)()) (0, import_signal2.onCleanup)(off2);
|
|
94
|
+
return Object.assign(() => state(), {
|
|
95
|
+
set(next, extra) {
|
|
96
|
+
const value = resolve(next);
|
|
97
|
+
storage.set(key, value, writeOptions(extra));
|
|
98
|
+
put(value);
|
|
99
|
+
},
|
|
100
|
+
remove() {
|
|
101
|
+
storage.remove(key);
|
|
102
|
+
put(fallback);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
let written = false;
|
|
107
|
+
const reread = () => storage.get(key).then(
|
|
108
|
+
(value) => put(value ?? fallback),
|
|
109
|
+
() => void 0
|
|
110
|
+
);
|
|
111
|
+
const off = storage.subscribe(key, (event) => {
|
|
112
|
+
if (event.type !== "set") return put(fallback);
|
|
113
|
+
if (event.value !== void 0) put(event.value);
|
|
114
|
+
else void reread();
|
|
115
|
+
});
|
|
116
|
+
if ((0, import_signal2.getOwner)()) (0, import_signal2.onCleanup)(off);
|
|
117
|
+
const ready = storage.get(key).then((value) => {
|
|
118
|
+
if (!written) put(value ?? fallback);
|
|
119
|
+
});
|
|
120
|
+
return Object.assign(() => state(), {
|
|
121
|
+
ready,
|
|
122
|
+
async set(next, extra) {
|
|
123
|
+
const previous = (0, import_signal2.untrack)(state);
|
|
124
|
+
const value = resolve(next);
|
|
125
|
+
written = true;
|
|
126
|
+
put(value);
|
|
127
|
+
try {
|
|
128
|
+
await storage.set(key, value, writeOptions(extra));
|
|
129
|
+
} catch (error) {
|
|
130
|
+
put(previous);
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
async remove() {
|
|
135
|
+
written = true;
|
|
136
|
+
await storage.remove(key);
|
|
137
|
+
put(fallback);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/lib/theme/index.ts
|
|
143
|
+
var import_head = require("@fluixi/head");
|
|
144
|
+
var import_signal4 = require("@fluixi/reactive/signal");
|
|
145
|
+
var import_storage2 = require("@fluixi/storage");
|
|
146
|
+
|
|
147
|
+
// src/lib/storage/index.ts
|
|
148
|
+
var import_dom4 = require("@fluixi/dom");
|
|
149
|
+
var import_signal3 = require("@fluixi/reactive/signal");
|
|
150
|
+
var import_storage = require("@fluixi/storage");
|
|
151
|
+
var import_browser = require("@fluixi/storage/browser");
|
|
152
|
+
var import_server = require("@fluixi/storage/server");
|
|
153
|
+
var StorageContext = (0, import_signal3.createContext)(void 0, "storage");
|
|
154
|
+
var AsyncStorageContext = (0, import_signal3.createContext)(void 0, "asyncStorage");
|
|
155
|
+
var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
|
|
156
|
+
var perRequest = /* @__PURE__ */ new WeakMap();
|
|
157
|
+
var perPage = /* @__PURE__ */ new Map();
|
|
158
|
+
function cached(scope, id, make) {
|
|
159
|
+
let adapter = scope.get(id);
|
|
160
|
+
if (!adapter) scope.set(id, adapter = make());
|
|
161
|
+
return adapter;
|
|
162
|
+
}
|
|
163
|
+
function requestRequired(scope) {
|
|
164
|
+
throw new import_storage.StorageUnavailableError(
|
|
165
|
+
`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>.`
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
function adapterFor(scope, cookie = {}) {
|
|
169
|
+
const ctx = (0, import_dom4.getRequestContext)();
|
|
170
|
+
const options = { httpOnly: false, ...cookie };
|
|
171
|
+
switch (scope) {
|
|
172
|
+
case "local":
|
|
173
|
+
return (0, import_browser.localStorageAdapter)();
|
|
174
|
+
case "session":
|
|
175
|
+
return (0, import_browser.sessionStorageAdapter)();
|
|
176
|
+
case "request":
|
|
177
|
+
if (isBrowser()) return cached(perPage, "request", import_storage.memoryStorage);
|
|
178
|
+
if (!ctx) requestRequired(scope);
|
|
179
|
+
return cached(perRequest.get(ctx) ?? perRequest.set(ctx, /* @__PURE__ */ new Map()).get(ctx), "request", import_server.requestStorage);
|
|
180
|
+
case "cookie": {
|
|
181
|
+
const id = `cookie:${JSON.stringify(options)}`;
|
|
182
|
+
if (isBrowser()) {
|
|
183
|
+
const { httpOnly: _ignored, ...browser } = options;
|
|
184
|
+
return cached(perPage, id, () => (0, import_browser.documentCookieAdapter)(browser));
|
|
185
|
+
}
|
|
186
|
+
if (!ctx) requestRequired(scope);
|
|
187
|
+
const raw = ctx.event.request;
|
|
188
|
+
const target = raw && typeof raw === "object" ? raw : void 0;
|
|
189
|
+
return cached(
|
|
190
|
+
perRequest.get(ctx) ?? perRequest.set(ctx, /* @__PURE__ */ new Map()).get(ctx),
|
|
191
|
+
id,
|
|
192
|
+
() => (0, import_server.cookieStorage)({
|
|
193
|
+
...options,
|
|
194
|
+
request: raw && typeof raw === "object" && ("headers" in raw || "getHeader" in raw) ? (0, import_server.toStorageRequest)(raw) : void 0,
|
|
195
|
+
response: {
|
|
196
|
+
appendHeader(name, value) {
|
|
197
|
+
if (!(0, import_dom4.appendResponseHeader)(name, value, target)) {
|
|
198
|
+
throw new import_storage.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.");
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function useStorage(options = {}) {
|
|
208
|
+
const { scope = "auto", cookie, ...storageOptions } = options;
|
|
209
|
+
if (scope === "auto") {
|
|
210
|
+
const provided = (0, import_signal3.useContext)(StorageContext);
|
|
211
|
+
if (provided) return options.namespace ? provided.namespace(options.namespace) : provided;
|
|
212
|
+
}
|
|
213
|
+
return (0, import_storage.createSyncStorage)({ ...storageOptions, adapter: resolveAdapter(scope, cookie) });
|
|
214
|
+
}
|
|
215
|
+
function resolveAdapter(scope, cookie) {
|
|
216
|
+
if (scope !== "auto") return adapterFor(scope, cookie);
|
|
217
|
+
if (isBrowser()) return adapterFor("local");
|
|
218
|
+
if (!(0, import_dom4.getRequestContext)()) requestRequired(scope);
|
|
219
|
+
return adapterFor("request");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// src/lib/theme/index.ts
|
|
223
|
+
var YEAR = 60 * 60 * 24 * 365;
|
|
224
|
+
function defaultStorage() {
|
|
225
|
+
try {
|
|
226
|
+
return useStorage({ scope: "cookie", cookie: { maxAge: YEAR, sameSite: "lax" } });
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (error instanceof import_storage2.StorageUnavailableError) return null;
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function storedLookup(storage, key) {
|
|
233
|
+
if (!storage) return null;
|
|
234
|
+
const full = storage.keyPrefix + key;
|
|
235
|
+
if (storage.scope === "cookie") {
|
|
236
|
+
return { from: "cookie", name: encodeURIComponent(full).replace(/[()]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`) };
|
|
237
|
+
}
|
|
238
|
+
if (storage.scope === "local") return { from: "local", name: full };
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// src/lib/theme/color-scheme.ts
|
|
243
|
+
var CHOICES = ["system", "light", "dark"];
|
|
244
|
+
var DARK = "(prefers-color-scheme: dark)";
|
|
245
|
+
function createColorScheme(options = {}) {
|
|
246
|
+
const storageKey = options.storageKey ?? "color-scheme";
|
|
247
|
+
const attribute = options.attribute ?? "data-theme";
|
|
248
|
+
const systemAs = options.systemAs ?? "resolved";
|
|
249
|
+
const storage = options.storage === void 0 ? defaultStorage() : options.storage;
|
|
250
|
+
const saved = storage ? persisted(storage, storageKey) : null;
|
|
251
|
+
const [unsaved, setUnsaved] = (0, import_signal5.createSignal)(null);
|
|
252
|
+
const value = () => {
|
|
253
|
+
const local = unsaved();
|
|
254
|
+
if (local) return local;
|
|
255
|
+
const stored = saved?.();
|
|
256
|
+
return CHOICES.includes(stored) ? stored : "system";
|
|
257
|
+
};
|
|
258
|
+
const media = env.window?.matchMedia?.(DARK);
|
|
259
|
+
const [osDark, setOsDark] = (0, import_signal5.createSignal)(media?.matches ?? false);
|
|
260
|
+
if (media) {
|
|
261
|
+
const onChange = () => setOsDark(media.matches);
|
|
262
|
+
media.addEventListener?.("change", onChange);
|
|
263
|
+
if ((0, import_signal5.getOwner)()) (0, import_signal5.onCleanup)(() => media.removeEventListener?.("change", onChange));
|
|
264
|
+
}
|
|
265
|
+
const resolved = () => {
|
|
266
|
+
const choice = value();
|
|
267
|
+
return choice === "system" ? osDark() ? "dark" : "light" : choice;
|
|
268
|
+
};
|
|
269
|
+
const shown = () => systemAs === "system" && value() === "system" ? "system" : resolved();
|
|
270
|
+
const apply = () => {
|
|
271
|
+
env.document?.documentElement.setAttribute(attribute, shown());
|
|
272
|
+
};
|
|
273
|
+
const set = (choice) => {
|
|
274
|
+
if (!CHOICES.includes(choice)) return;
|
|
275
|
+
if (!saved) setUnsaved(choice);
|
|
276
|
+
else {
|
|
277
|
+
try {
|
|
278
|
+
saved.set(choice);
|
|
279
|
+
setUnsaved(null);
|
|
280
|
+
} catch {
|
|
281
|
+
setUnsaved(choice);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
apply();
|
|
285
|
+
options.onChange?.(choice);
|
|
286
|
+
};
|
|
287
|
+
const scriptBody = () => {
|
|
288
|
+
const cfg = JSON.stringify({ s: storedLookup(storage, storageKey), a: attribute, sys: systemAs === "system", tc: options.themeColor ?? null, cs: options.colorSchemeMeta !== false }).replace(/</g, "\\u003c");
|
|
289
|
+
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;}var chosen=t==='light'||t==='dark'?t:null;if(!chosen)t=c.sys?'system':(matchMedia('${DARK}').matches?'dark':'light');document.documentElement.setAttribute(c.a,t);if(chosen&&c.tc)document.querySelectorAll('meta[name="theme-color"]').forEach(function(m){m.setAttribute('content',c.tc[chosen]);m.removeAttribute('media');});if(chosen&&c.cs){var m=document.querySelector('meta[name="color-scheme"]');if(m)m.setAttribute('content',chosen);}}catch(e){}})();`;
|
|
290
|
+
};
|
|
291
|
+
if ((0, import_head2.getActiveHead)()) {
|
|
292
|
+
const colors = options.themeColor;
|
|
293
|
+
(0, import_head2.useHead)({
|
|
294
|
+
htmlAttrs: { [attribute]: shown },
|
|
295
|
+
...colors ? {
|
|
296
|
+
themeColor: () => value() === "system" ? [
|
|
297
|
+
{ color: colors.light, media: "(prefers-color-scheme: light)" },
|
|
298
|
+
{ color: colors.dark, media: DARK }
|
|
299
|
+
] : colors[resolved()]
|
|
300
|
+
} : {},
|
|
301
|
+
...options.colorSchemeMeta === false ? {} : { colorScheme: () => value() === "system" ? "light dark" : resolved() },
|
|
302
|
+
...options.script === false ? {} : { script: [{ children: scriptBody() }] }
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
if (env.document && (0, import_signal5.getOwner)()) (0, import_signal5.createEffect)(apply);
|
|
306
|
+
return {
|
|
307
|
+
value,
|
|
308
|
+
resolved,
|
|
309
|
+
set,
|
|
310
|
+
toggle: () => set(resolved() === "dark" ? "light" : "dark"),
|
|
311
|
+
init: apply,
|
|
312
|
+
headScript: () => `<script>${scriptBody()}<\/script>`
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// src/lib/theme/provider.ts
|
|
317
|
+
var ThemesContext = createContext(void 0, "themes");
|
|
318
|
+
function ThemeProvider(props) {
|
|
319
|
+
const outer = useContext(ThemesContext);
|
|
320
|
+
return ThemesContext.Provider({
|
|
321
|
+
// A nested provider adds to the axes above it rather than hiding them.
|
|
322
|
+
value: outer ? { ...outer, ...props.themes } : props.themes,
|
|
323
|
+
get children() {
|
|
324
|
+
return props.children;
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
function useTheme(name) {
|
|
329
|
+
const themes = useContext(ThemesContext);
|
|
330
|
+
if (!themes || !(name in themes)) {
|
|
331
|
+
throw new Error(`[fluixi] No theme named "${name}" above this component: provide it with <ThemeProvider> or <ColorSchemeProvider>.`);
|
|
332
|
+
}
|
|
333
|
+
return themes[name];
|
|
334
|
+
}
|
|
335
|
+
function ColorSchemeProvider(props) {
|
|
336
|
+
const scheme = createColorScheme(props);
|
|
337
|
+
return ThemeProvider({
|
|
338
|
+
themes: { scheme },
|
|
339
|
+
get children() {
|
|
340
|
+
return props.children;
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
function useColorScheme() {
|
|
345
|
+
return useTheme("scheme");
|
|
346
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type ColorScheme, type ColorSchemeOptions } from './color-scheme.js';
|
|
2
|
+
/**
|
|
3
|
+
* Several theme axes at once, by name: a scheme, a palette, a density.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```tsx
|
|
7
|
+
* const scheme = createColorScheme({ storageKey: 'app-scheme' });
|
|
8
|
+
* const palette = createTheme({ themes: ['ink', 'saffron'], default: 'ink', attribute: 'data-accent' });
|
|
9
|
+
*
|
|
10
|
+
* <ThemeProvider themes={{ scheme, palette }}>{props.children}</ThemeProvider>
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare function ThemeProvider(props: {
|
|
14
|
+
themes: object;
|
|
15
|
+
children?: unknown;
|
|
16
|
+
}): any;
|
|
17
|
+
/** The theme axis `name` from the nearest provider. Throws when none provides it. */
|
|
18
|
+
export declare function useTheme<T = unknown>(name: string): T;
|
|
19
|
+
/**
|
|
20
|
+
* A color scheme made here, during the render, and provided as the `scheme` axis.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* <ColorSchemeProvider storageKey="app-scheme">
|
|
25
|
+
* <App />
|
|
26
|
+
* </ColorSchemeProvider>
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function ColorSchemeProvider(props: ColorSchemeOptions & {
|
|
30
|
+
children?: unknown;
|
|
31
|
+
}): any;
|
|
32
|
+
/** The `scheme` axis: from `<ColorSchemeProvider>`, or a `ThemeProvider` that names one `scheme`. */
|
|
33
|
+
export declare function useColorScheme(): ColorScheme;
|
|
34
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../../src/lib/theme/provider.ts"],"names":[],"mappings":"AAKA,OAAO,EAAqB,KAAK,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAIjG;;;;;;;;;;GAUG;AAEH,wBAAgB,aAAa,CAAC,KAAK,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,GAAG,CAShF;AAED,qFAAqF;AACrF,wBAAgB,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,CAMrD;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,kBAAkB,GAAG;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,GAAG,CAQ3F;AAED,qGAAqG;AACrG,wBAAgB,cAAc,IAAI,WAAW,CAE5C"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Themes for the components below: the app root makes them during the render, the provider
|
|
3
|
+
* hands them down, and any component, or a custom element, reads them.
|
|
4
|
+
*/
|
|
5
|
+
import { createContext, useContext } from '../context/context.js';
|
|
6
|
+
import { createColorScheme } from './color-scheme.js';
|
|
7
|
+
const ThemesContext = createContext(undefined, 'themes');
|
|
8
|
+
/**
|
|
9
|
+
* Several theme axes at once, by name: a scheme, a palette, a density.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* const scheme = createColorScheme({ storageKey: 'app-scheme' });
|
|
14
|
+
* const palette = createTheme({ themes: ['ink', 'saffron'], default: 'ink', attribute: 'data-accent' });
|
|
15
|
+
*
|
|
16
|
+
* <ThemeProvider themes={{ scheme, palette }}>{props.children}</ThemeProvider>
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
// `object`, not a Record: an app's own interface of axes has no index signature to match one.
|
|
20
|
+
export function ThemeProvider(props) {
|
|
21
|
+
const outer = useContext(ThemesContext);
|
|
22
|
+
return ThemesContext.Provider({
|
|
23
|
+
// A nested provider adds to the axes above it rather than hiding them.
|
|
24
|
+
value: (outer ? { ...outer, ...props.themes } : props.themes),
|
|
25
|
+
get children() {
|
|
26
|
+
return props.children;
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/** The theme axis `name` from the nearest provider. Throws when none provides it. */
|
|
31
|
+
export function useTheme(name) {
|
|
32
|
+
const themes = useContext(ThemesContext);
|
|
33
|
+
if (!themes || !(name in themes)) {
|
|
34
|
+
throw new Error(`[fluixi] No theme named "${name}" above this component: provide it with <ThemeProvider> or <ColorSchemeProvider>.`);
|
|
35
|
+
}
|
|
36
|
+
return themes[name];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A color scheme made here, during the render, and provided as the `scheme` axis.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```tsx
|
|
43
|
+
* <ColorSchemeProvider storageKey="app-scheme">
|
|
44
|
+
* <App />
|
|
45
|
+
* </ColorSchemeProvider>
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export function ColorSchemeProvider(props) {
|
|
49
|
+
const scheme = createColorScheme(props);
|
|
50
|
+
return ThemeProvider({
|
|
51
|
+
themes: { scheme },
|
|
52
|
+
get children() {
|
|
53
|
+
return props.children;
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/** The `scheme` axis: from `<ColorSchemeProvider>`, or a `ThemeProvider` that names one `scheme`. */
|
|
58
|
+
export function useColorScheme() {
|
|
59
|
+
return useTheme('scheme');
|
|
60
|
+
}
|