j20 0.0.17 → 0.0.20

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/index.js CHANGED
@@ -373,15 +373,8 @@ const getChildren = (propChildren) => {
373
373
  }
374
374
  return arr;
375
375
  };
376
- let overflow = "";
377
- let count = 0;
378
- const generateId = () => {
379
- if (count === Number.MAX_SAFE_INTEGER) {
380
- overflow += count.toString(32);
381
- count = 0;
382
- }
383
- return `${overflow}${(count++).toString(32)}`;
384
- };
376
+ let count$1 = 0;
377
+ const generateId = () => (++count$1).toString(32);
385
378
  const styleObjectToString = (style) => {
386
379
  let styleString = "";
387
380
  for (const key in style) {
@@ -483,6 +476,21 @@ const createDom = (tag, props, children) => {
483
476
 
484
477
  const BRAND = "j20";
485
478
 
479
+ const isSignal = (a) => a?.SIGNAL === true;
480
+ const addStyleSheet = (node, styleSheet) => {
481
+ if (node.adoptedStyleSheets &&
482
+ !node.adoptedStyleSheets.includes(styleSheet)) {
483
+ node.adoptedStyleSheets = [...node.adoptedStyleSheets, styleSheet];
484
+ }
485
+ };
486
+ const removeStyleSheet = (node, styleSheet) => {
487
+ if (node.adoptedStyleSheets && node.adoptedStyleSheets.includes(styleSheet)) {
488
+ const newSet = new Set(node.adoptedStyleSheets);
489
+ newSet.delete(styleSheet);
490
+ node.adoptedStyleSheets = Array.from(newSet);
491
+ }
492
+ };
493
+
486
494
  const registerWebComponent = (Comp) => {
487
495
  if (!Comp.customElement) {
488
496
  throw new Error("Custom element options is not provided");
@@ -572,16 +580,13 @@ const buildClass = (Comp) => {
572
580
  this.append(...elements);
573
581
  }
574
582
  }
575
- addStyleSheet(sheet) {
576
- if (this.#shadow) {
577
- this.#shadow.adoptedStyleSheets.push(sheet);
578
- }
583
+ addStyleSheet(styleSheet) {
584
+ if (this.#shadow)
585
+ addStyleSheet(this.#shadow, styleSheet);
579
586
  }
580
- removeStyleSheet(sheet) {
581
- if (this.#shadow) {
582
- this.#shadow.adoptedStyleSheets =
583
- this.#shadow.adoptedStyleSheets.filter((s) => s !== sheet);
584
- }
587
+ removeStyleSheet(styleSheet) {
588
+ if (this.#shadow)
589
+ removeStyleSheet(this.#shadow, styleSheet);
585
590
  }
586
591
  attributeChangedCallback(name, oldValue, newValue) {
587
592
  const attrs = NewElementClass.observedAttributes;
@@ -825,8 +830,6 @@ const wc = () => {
825
830
 
826
831
  const $ = (val) => val;
827
832
 
828
- const isSignal = (a) => a?.SIGNAL === true;
829
-
830
833
  const contextWeakMap = new WeakMap();
831
834
  const context = (Context) => {
832
835
  const instance = securityGetCurrentInstance();
@@ -898,40 +901,96 @@ const onMount = (callback) => {
898
901
 
899
902
  const onDestroy = (callback) => effect(() => callback);
900
903
 
901
- const sheet = (css) => {
904
+ let count = BigInt(0);
905
+ const getId = () => (++count).toString(32);
906
+ const refWeakMap = new WeakMap();
907
+ const createCss = (css) => {
908
+ const id = getId();
909
+ return () => {
910
+ const styleId = id;
911
+ const replacedCss = css.replace(/\.(\w[\w-]*)\s*?\{/g, (_, className) => {
912
+ return `.${className}_${styleId} {`;
913
+ });
914
+ styleSheet(id, replacedCss);
915
+ return new Proxy({}, {
916
+ get: (_, prop) => {
917
+ return `${prop}_${styleId}`;
918
+ },
919
+ });
920
+ };
921
+ };
922
+ const addReference = (host, id, add) => {
923
+ if (!refWeakMap.get(host)) {
924
+ refWeakMap.set(host, []);
925
+ }
926
+ const states = refWeakMap.get(host) ?? [];
927
+ const state = states.find((state) => state.id === id);
928
+ if (!state) {
929
+ refWeakMap.set(host, [
930
+ ...states,
931
+ {
932
+ id,
933
+ refCount: 1,
934
+ },
935
+ ]);
936
+ add();
937
+ }
938
+ else {
939
+ state.refCount++;
940
+ }
941
+ };
942
+ const removeReference = (host, id, remove) => {
943
+ const states = refWeakMap.get(host) ?? [];
944
+ const state = states.find((state) => state.id === id);
945
+ if (state) {
946
+ state.refCount--;
947
+ if (state.refCount === 0) {
948
+ remove();
949
+ refWeakMap.set(host, states.filter((state) => state.id !== id));
950
+ }
951
+ }
952
+ };
953
+ const styleSheet = (id, css) => {
902
954
  let instance = getCurrentInstance();
903
955
  const sheet = new CSSStyleSheet();
904
956
  css && sheet.replaceSync(css);
957
+ let root;
905
958
  while (instance) {
906
959
  if (instance.host) {
907
- instance.host.addStyleSheet(sheet);
960
+ const host = instance.host;
961
+ addReference(host, id, () => {
962
+ host.addStyleSheet(sheet);
963
+ });
908
964
  onDestroy(() => {
909
- instance.host.removeStyleSheet(sheet);
965
+ removeReference(host, id, () => host.removeStyleSheet(sheet));
910
966
  });
911
967
  break;
912
968
  }
969
+ if (instance.root) {
970
+ root = instance.root;
971
+ break;
972
+ }
913
973
  instance = instance?.parent;
914
974
  }
915
- if (!instance) {
916
- document.adoptedStyleSheets.push(sheet);
975
+ if (root) {
976
+ let node = root;
977
+ do {
978
+ if (node instanceof ShadowRoot || node === document) {
979
+ break;
980
+ }
981
+ node = node.parentNode;
982
+ } while (node);
983
+ addReference(node, id, () => {
984
+ addStyleSheet(node, sheet);
985
+ });
917
986
  onDestroy(() => {
918
- document.adoptedStyleSheets = document.adoptedStyleSheets.filter((s) => s !== sheet);
987
+ removeReference(node, id, () => {
988
+ removeStyleSheet(node, sheet);
989
+ });
919
990
  });
920
991
  }
921
992
  return sheet;
922
993
  };
923
- const css = (css) => {
924
- const styleId = id();
925
- const replacedCss = css.replace(/\.(\w[\w-]*)\s*?\{/g, (_, className) => {
926
- return `.${className}_${styleId} {`;
927
- });
928
- sheet(replacedCss);
929
- return new Proxy({}, {
930
- get: (_, prop) => {
931
- return `${prop}_${styleId}`;
932
- },
933
- });
934
- };
935
994
 
936
995
  function insertAfter(parentNode, newNode, targetNode) {
937
996
  parentNode?.insertBefore(newNode, targetNode.nextSibling);
@@ -1339,12 +1398,15 @@ const createElement = (tag, props, children) => {
1339
1398
  : createComponent(tag, props);
1340
1399
  };
1341
1400
 
1342
- const createRoot = (boot) => {
1343
- let [instance, fragment] = instanceCreate(() => boot());
1344
- return {
1345
- element: fragment,
1346
- instance,
1401
+ const createRoot = (boot, rootElement) => {
1402
+ const rootInstance = {
1403
+ root: rootElement,
1404
+ id: generateId(),
1405
+ range: [document.createTextNode(""), document.createTextNode("")],
1347
1406
  };
1407
+ let [, fragment] = instanceCreate(() => createElement(boot, null, null), rootInstance);
1408
+ rootElement.append(rootInstance.range[0], fragment, rootInstance.range[1]);
1409
+ return rootInstance;
1348
1410
  };
1349
1411
 
1350
1412
  const h2 = (tag, props, children) => {
@@ -1369,5 +1431,5 @@ const template = (template) => {
1369
1431
  };
1370
1432
  };
1371
1433
 
1372
- export { $, $useContext, Case, Computed, Default, Effect, For, Fragment, If, Replace, Signal, Some, Switch, batch, computed, createComponent, createContext, createDom, createElement, createLogicComponent, createRoot, css, effect, getCurrentInstance, h2, id, instanceCreate, instanceCreateElement, instanceDestroy, instanceGetElements, instanceInit, jsx, jsxs, mounts, onDestroy, onMount, ref, registerWebComponent, securityGetCurrentInstance, sheet, signal, template, untrack, wc };
1434
+ export { $, $useContext, Case, Computed, Default, Effect, For, Fragment, If, Replace, Signal, Some, Switch, batch, computed, createComponent, createContext, createCss, createDom, createElement, createLogicComponent, createRoot, effect, getCurrentInstance, h2, id, instanceCreate, instanceCreateElement, instanceDestroy, instanceGetElements, instanceInit, jsx, jsxs, mounts, onDestroy, onMount, ref, registerWebComponent, securityGetCurrentInstance, signal, styleSheet, template, untrack, wc };
1373
1435
  //# sourceMappingURL=index.js.map