ziko 1.1.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ziko.js CHANGED
@@ -2,7 +2,7 @@
2
2
  /*
3
3
  Project: ziko.js
4
4
  Author: Zakaria Elalaoui
5
- Date : Tue Jul 28 2026 17:04:51 GMT+0100 (UTC+01:00)
5
+ Date : Thu Jul 30 2026 19:50:38 GMT+0100 (UTC+01:00)
6
6
  Git-Repo : https://github.com/zakarialaoui10/ziko.js
7
7
  Git-Wiki : https://github.com/zakarialaoui10/ziko.js/wiki
8
8
  Released under MIT License
@@ -1880,115 +1880,134 @@
1880
1880
  const useIPC = (name) => new UseIPC(name);
1881
1881
 
1882
1882
  class UseStorage {
1883
- constructor(storage, globalKey, initialValue, use_channel = true) {
1884
- this.cache = {
1885
- storage,
1886
- globalKey,
1887
- channel: use_channel ? useIPC(`Ziko:useStorage-${globalKey}`) : null,
1888
- oldItemKeys: new Set()
1889
- };
1883
+ static RESERVED_KEYS = new Set([
1884
+ "cache", "items", "set", "add", "remove", "get", "clear", "onStorageUpdated"
1885
+ ]);
1890
1886
 
1891
- this.#init(initialValue, use_channel);
1892
- }
1887
+ constructor(storage, globalKey, initialValue, use_channel = true) {
1888
+ this.cache = {
1889
+ storage,
1890
+ globalKey,
1891
+ channel: use_channel ? useIPC(`Ziko:useStorage-${globalKey}`) : null,
1892
+ oldItemKeys: new Set()
1893
+ };
1893
1894
 
1894
- get items() {
1895
- const raw = this.cache.storage.getItem(this.cache.globalKey);
1896
- if (!raw) return {};
1897
- try {
1898
- return JSON.parse(raw);
1899
- } catch {
1900
- return {};
1901
- }
1902
- }
1895
+ this.#init(initialValue, use_channel);
1896
+ }
1903
1897
 
1904
- #maintain() {
1905
- const items = this.items;
1906
- this.cache.oldItemKeys.forEach(k => delete this[k]);
1907
- for (const key in items) {
1908
- this[key] = items[key];
1909
- this.cache.oldItemKeys.add(key);
1910
- }
1911
- }
1912
- #init(initialValue, use_channel) {
1913
- if (use_channel && this.cache.channel) this.cache.channel.on("Ziko-Storage-Updated", () => this.#maintain());
1914
- if (!initialValue) {
1915
- this.#maintain();
1916
- return;
1917
- }
1918
- if (this.cache.storage.getItem(this.cache.globalKey)) {
1919
- const existing = this.items;
1920
- Object.keys(existing).forEach(k => this.cache.oldItemKeys.add(k));
1921
- this.#maintain();
1922
- }
1923
- else this.set(initialValue);
1898
+ get items() {
1899
+ const raw = this.cache.storage.getItem(this.cache.globalKey);
1900
+ if (!raw) return {};
1901
+ try {
1902
+ return JSON.parse(raw);
1903
+ } catch {
1904
+ return {};
1924
1905
  }
1906
+ }
1925
1907
 
1926
- set(data) {
1927
- this.cache.storage.setItem(this.cache.globalKey, JSON.stringify(data));
1928
- if (this.cache.channel) this.cache.channel.emit("Ziko-Storage-Updated", data);
1929
- this.#maintain();
1930
- return this;
1931
- }
1908
+ #maintain() {
1909
+ const currentItems = this.items;
1910
+ const currentKeys = new Set(Object.keys(currentItems));
1932
1911
 
1933
- add(data) {
1934
- this.set({
1935
- ...this.items,
1936
- ...data
1937
- });
1938
- return this;
1912
+ // Cleanup keys that were removed
1913
+ this.cache.oldItemKeys.forEach(key => {
1914
+ if (!currentKeys.has(key)) {
1915
+ delete this[key];
1916
+ this.cache.oldItemKeys.delete(key);
1917
+ }
1918
+ });
1919
+
1920
+ // Populate keys from storage onto `this`
1921
+ for (const key in currentItems) {
1922
+ if (!UseStorage.RESERVED_KEYS.has(key)) {
1923
+ this[key] = currentItems[key];
1924
+ this.cache.oldItemKeys.add(key);
1925
+ }
1939
1926
  }
1940
- remove(...keys) {
1941
- const items = { ...this.items };
1942
- keys.forEach(key => {
1943
- delete items[key];
1944
- delete this[key];
1945
- this.cache.oldItemKeys.delete(key);
1946
- });
1947
- this.set(items);
1948
- return this;
1927
+ }
1928
+
1929
+ #init(initialValue, use_channel) {
1930
+ if (use_channel && this.cache.channel) {
1931
+ this.cache.channel.on("Ziko-Storage-Updated", () => this.#maintain());
1949
1932
  }
1950
- get(key) {
1951
- return this.items[key];
1933
+
1934
+ const hasStoredData = this.cache.storage.getItem(this.cache.globalKey) !== null;
1935
+
1936
+ if (hasStoredData) {
1937
+ // Key already exists in storage -> Restore data to instance
1938
+ this.#maintain();
1939
+ } else if (initialValue !== undefined) {
1940
+ // Key doesn't exist -> Seed with initialValue
1941
+ this.set(initialValue);
1942
+ } else {
1943
+ // Key doesn't exist and no initialValue provided -> Default to empty
1944
+ this.#maintain();
1952
1945
  }
1953
- clear() {
1954
- this.cache.storage.removeItem(this.cache.globalKey);
1955
- this.cache.oldItemKeys.forEach(k => delete this[k]);
1956
- this.cache.oldItemKeys.clear();
1957
- this.#maintain();
1958
- return this;
1946
+ }
1947
+
1948
+ set(data) {
1949
+ this.cache.storage.setItem(this.cache.globalKey, JSON.stringify(data));
1950
+ if (this.cache.channel) {
1951
+ this.cache.channel.emit("Ziko-Storage-Updated", data);
1959
1952
  }
1960
- onStorageUpdated(callback) {
1961
- if (this.cache.channel) {
1962
- this.cache.channel.on("Ziko-Storage-Updated", callback);
1963
- }
1964
- return this;
1953
+ this.#maintain();
1954
+ return this;
1955
+ }
1956
+
1957
+ add(data) {
1958
+ return this.set({
1959
+ ...this.items,
1960
+ ...data
1961
+ });
1962
+ }
1963
+
1964
+ remove(...keys) {
1965
+ const items = { ...this.items };
1966
+ keys.forEach(key => delete items[key]);
1967
+ return this.set(items);
1968
+ }
1969
+
1970
+ get(key) {
1971
+ return this.items[key];
1972
+ }
1973
+
1974
+ clear() {
1975
+ this.cache.storage.removeItem(this.cache.globalKey);
1976
+ this.#maintain();
1977
+ return this;
1978
+ }
1979
+
1980
+ onStorageUpdated(callback) {
1981
+ if (this.cache.channel) {
1982
+ this.cache.channel.on("Ziko-Storage-Updated", callback);
1965
1983
  }
1984
+ return this;
1985
+ }
1966
1986
  }
1967
1987
 
1968
- // factory functions
1969
- const useLocaleStorage = (key, initialValue, use_channel = true) =>
1970
- new UseStorage(localStorage, key, initialValue, use_channel);
1988
+ const useLocalStorage = (key, initialValue, use_channel = true) =>
1989
+ new UseStorage(localStorage, key, initialValue, use_channel);
1971
1990
 
1972
1991
  const useSessionStorage = (key, initialValue, use_channel = true) =>
1973
- new UseStorage(sessionStorage, key, initialValue, use_channel);
1992
+ new UseStorage(sessionStorage, key, initialValue, use_channel);
1974
1993
 
1975
1994
  var __State__ = {
1976
1995
  store : new Map(),
1977
1996
  index : 0,
1978
1997
  session_storage : null,
1979
1998
  register: function(state){
1980
- if(!undefined.SSR && undefined.DEV){
1981
- if(!this.session) this.session_storage = useSessionStorage('ziko-state', {});
1982
- const savedValue = this.session_storage.get(this.index);
1983
- if(!savedValue) this.session_storage.add({[this.index] : state.value});
1984
- else state.value = savedValue;
1985
- }
1986
- this.store.set(this.index++, state);
1999
+ // if(!import.meta?.env?.SSR && import.meta?.env?.DEV){
2000
+ // if(!this.session) this.session_storage = useSessionStorage('ziko-state', {})
2001
+ // const savedValue = this.session_storage.get(this.index)
2002
+ // if(!savedValue) this.session_storage.add({[this.index] : state.value});
2003
+ // else state.value = savedValue
2004
+ // }
2005
+ // this.store.set(this.index++, state)
1987
2006
  },
1988
2007
  update: function(index, value){
1989
- if(!undefined.SSR && undefined.DEV){
1990
- this.session_storage.add({[index] : value});
1991
- }
2008
+ // if(!import.meta?.env?.SSR && import.meta?.env?.DEV){
2009
+ // this.session_storage.add({[index] : value})
2010
+ // }
1992
2011
  },
1993
2012
 
1994
2013
  };
@@ -2165,57 +2184,61 @@
2165
2184
  unmount: unmount
2166
2185
  });
2167
2186
 
2168
- if(!globalThis.__Ziko__) __init__global__();
2187
+ // import { __init__global__ } from "../__ziko__/index.js";
2188
+
2189
+ // if (!globalThis.__Ziko__) __init__global__();
2169
2190
 
2170
2191
  function useState(initialValue) {
2171
2192
 
2172
- const {store, index} = __Ziko__.__State__;
2173
- __Ziko__.__State__.register({
2174
- value : initialValue,
2175
- subscribers : new Set(),
2176
- paused : false
2177
- });
2178
-
2179
- let current = store.get(index);
2193
+ const state = {
2194
+ value: initialValue,
2195
+ subscribers: new Set(),
2196
+ paused: false,
2197
+ };
2180
2198
 
2181
2199
  function getValue() {
2182
2200
  return {
2183
- value: current.value,
2201
+ value: state.value,
2184
2202
  isStateGetter: () => true,
2185
- _subscribe: (fn) => current.subscribers.add(fn),
2203
+ _subscribe: (fn) => {
2204
+ state.subscribers.add(fn);
2205
+ return () => state.subscribers.delete(fn);
2206
+ },
2186
2207
  };
2187
2208
  }
2188
2209
 
2189
2210
  function setValue(newValue) {
2190
- if (current.paused) return;
2211
+ if (state.paused) return;
2212
+
2191
2213
  if (typeof newValue === "function") {
2192
- newValue = newValue(current.value);
2214
+ newValue = newValue(state.value);
2193
2215
  }
2194
- if (newValue !== current.value) {
2195
- current.value = newValue;
2196
- current.subscribers.forEach(fn => fn(current.value));
2197
- __Ziko__.__State__.update(index, newValue);
2216
+
2217
+ if (!Object.is(newValue, state.value)) {
2218
+ state.value = newValue;
2219
+ state.subscribers.forEach((fn) => fn(state.value));
2198
2220
  }
2199
2221
  }
2200
2222
 
2201
2223
  const controller = {
2202
- pause: () => { current.paused = true; },
2203
- resume: () => { current.paused = false; },
2204
- clear: () => { current.subscribers.clear(); },
2224
+ pause: () => { state.paused = true; },
2225
+ resume: () => { state.paused = false; },
2226
+ clear: () => { state.subscribers.clear(); },
2205
2227
  force: (newValue) => {
2206
- if (typeof newValue === "function") newValue = newValue(current.value);
2207
- current.value = newValue;
2208
- current.subscribers.forEach(fn => fn(current.value));
2228
+ if (typeof newValue === "function") {
2229
+ newValue = newValue(state.value);
2230
+ }
2231
+ state.value = newValue;
2232
+ state.subscribers.forEach((fn) => fn(state.value));
2209
2233
  },
2210
- getSubscribers: () => new Set(current.subscribers),
2234
+ getSubscribers: () => new Set(state.subscribers),
2211
2235
  };
2212
2236
 
2213
2237
  return [getValue, setValue, controller];
2214
2238
  }
2215
2239
 
2216
-
2217
2240
  const isStateGetter = (arg) => {
2218
- return typeof arg === 'function' && arg?.()?.isStateGetter?.();
2241
+ return typeof arg === "function" && arg?.()?.isStateGetter?.() === true;
2219
2242
  };
2220
2243
 
2221
2244
  const camel2hyphencase = (text = '') => text.replace(/[A-Z]/g, match => '-' + match.toLowerCase());
@@ -3485,6 +3508,63 @@
3485
3508
  // return this.cases[matched]()
3486
3509
  // }
3487
3510
 
3511
+ class UISwap extends UIElement$1 {
3512
+ #DISPLAYS_MAP = new WeakMap()
3513
+ constructor({ activeIndex = 0 } = {}, ...items) {
3514
+ super({ element: 'div' });
3515
+ this.style({ display: 'contents' });
3516
+
3517
+ this.states = {
3518
+ activeIndex
3519
+ };
3520
+
3521
+ this.append(...items);
3522
+ this.#setup();
3523
+ }
3524
+ get activeItem(){
3525
+ return this.items[this.states.activeIndex]
3526
+ }
3527
+ #setup() {
3528
+ this.items.forEach((n, i) => {
3529
+ // Store initial display style (fallback to empty string or default)
3530
+ const initialDisplay = n.element?.style?.display || '';
3531
+ this.#DISPLAYS_MAP.set(n, initialDisplay);
3532
+
3533
+ if (i !== this.states.activeIndex) {
3534
+ n.style({ display: 'none' });
3535
+ }
3536
+ });
3537
+ }
3538
+ next(n = 1) {
3539
+ return this.activate(this.states.activeIndex + n);
3540
+ }
3541
+ previous(n = 1) {
3542
+ return this.activate(this.states.activeIndex - n);
3543
+ }
3544
+ activate(index) {
3545
+ if (!this.items.length) return this;
3546
+
3547
+ const currentItem = this.items.at(this.states.activeIndex);
3548
+
3549
+ if (currentItem) {
3550
+ currentItem.style({ display: 'none' });
3551
+ }
3552
+
3553
+ const len = this.items.length;
3554
+ this.states.activeIndex = ((index % len) + len) % len;
3555
+
3556
+ const activeItem = this.items.at(this.states.activeIndex);
3557
+ if (activeItem) {
3558
+ const restoredDisplay = this.#DISPLAYS_MAP.get(activeItem) || 'block';
3559
+ activeItem.style({ display: restoredDisplay === 'none' ? 'block' : restoredDisplay });
3560
+ }
3561
+
3562
+ return this;
3563
+ }
3564
+ }
3565
+
3566
+ const Swap = call_with_optional_props(UISwap);
3567
+
3488
3568
  const { PI, sqrt: sqrt$1, cos: cos$2, sin: sin$2, acos, pow } = Math;
3489
3569
 
3490
3570
  const linear = t => t;
@@ -4911,6 +4991,7 @@
4911
4991
  exports.SVGWrapper = SVGWrapper;
4912
4992
  exports.Scheduler = Scheduler;
4913
4993
  exports.Suspense = Suspense;
4994
+ exports.Swap = Swap;
4914
4995
  exports.SwipeEvent = SwipeEvent;
4915
4996
  exports.Switch = Switch;
4916
4997
  exports.Tick = Tick;
@@ -4922,6 +5003,7 @@
4922
5003
  exports.UIHTMLWrapper = UIHTMLWrapper;
4923
5004
  exports.UINode = UINode;
4924
5005
  exports.UISVGWrapper = UISVGWrapper;
5006
+ exports.UISwap = UISwap;
4925
5007
  exports.UISwitch = UISwitch;
4926
5008
  exports.UIView = UIView;
4927
5009
  exports.UseRoot = UseRoot;
@@ -5081,7 +5163,7 @@
5081
5163
  exports.useDerived = useDerived;
5082
5164
  exports.useEventEmitter = useEventEmitter;
5083
5165
  exports.useIPC = useIPC;
5084
- exports.useLocaleStorage = useLocaleStorage;
5166
+ exports.useLocalStorage = useLocalStorage;
5085
5167
  exports.useMediaQuery = useMediaQuery;
5086
5168
  exports.useQueryParams = useQueryParams;
5087
5169
  exports.useReactive = useReactive;