@constela/runtime 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +33 -1
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -177,6 +177,7 @@ function createComputed(getter) {
177
177
  }
178
178
 
179
179
  // src/state/store.ts
180
+ import { isCookieInitialExpr } from "@constela/core";
180
181
  function normalizePath(path) {
181
182
  if (typeof path === "string") {
182
183
  return path.split(".").map((segment) => {
@@ -213,10 +214,33 @@ function setValueAtPath(obj, path, value) {
213
214
  );
214
215
  return clone3;
215
216
  }
217
+ function getCookieValue(key2) {
218
+ if (typeof document === "undefined") return void 0;
219
+ for (const cookie of document.cookie.split(";")) {
220
+ const eqIndex = cookie.indexOf("=");
221
+ if (eqIndex === -1) continue;
222
+ const name = cookie.slice(0, eqIndex).trim();
223
+ if (name === key2) {
224
+ const value = cookie.slice(eqIndex + 1);
225
+ try {
226
+ return decodeURIComponent(value);
227
+ } catch {
228
+ return value;
229
+ }
230
+ }
231
+ }
232
+ return void 0;
233
+ }
216
234
  function createStateStore(definitions) {
217
235
  const signals = /* @__PURE__ */ new Map();
218
236
  for (const [name, def] of Object.entries(definitions)) {
219
- let initialValue = def.initial;
237
+ let initialValue;
238
+ if (isCookieInitialExpr(def.initial)) {
239
+ const cookieValue = getCookieValue(def.initial.key);
240
+ initialValue = cookieValue !== void 0 ? cookieValue : def.initial.default;
241
+ } else {
242
+ initialValue = def.initial;
243
+ }
220
244
  if (name === "theme" && typeof window !== "undefined") {
221
245
  try {
222
246
  const stored = localStorage.getItem("theme");
@@ -245,6 +269,14 @@ function createStateStore(definitions) {
245
269
  if (!signal) {
246
270
  throw new Error(`State field "${name}" does not exist`);
247
271
  }
272
+ if (name === "theme" && typeof document !== "undefined") {
273
+ try {
274
+ const valueStr = typeof value === "string" ? value : JSON.stringify(value);
275
+ const oneYear = 365 * 24 * 60 * 60;
276
+ document.cookie = `theme=${encodeURIComponent(valueStr)}; path=/; max-age=${oneYear}; SameSite=Lax`;
277
+ } catch {
278
+ }
279
+ }
248
280
  signal.set(value);
249
281
  },
250
282
  subscribe(name, fn) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constela/runtime",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "Runtime DOM renderer for Constela UI framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -18,8 +18,8 @@
18
18
  "dompurify": "^3.3.1",
19
19
  "marked": "^17.0.1",
20
20
  "shiki": "^3.20.0",
21
- "@constela/compiler": "0.11.0",
22
- "@constela/core": "0.11.0"
21
+ "@constela/compiler": "0.11.1",
22
+ "@constela/core": "0.12.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/dompurify": "^3.2.0",
@@ -29,7 +29,7 @@
29
29
  "tsup": "^8.0.0",
30
30
  "typescript": "^5.3.0",
31
31
  "vitest": "^2.0.0",
32
- "@constela/server": "7.0.0"
32
+ "@constela/server": "8.0.0"
33
33
  },
34
34
  "engines": {
35
35
  "node": ">=20.0.0"