j20 0.0.19 → 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,8 +373,8 @@ const getChildren = (propChildren) => {
373
373
  }
374
374
  return arr;
375
375
  };
376
- let count = 0;
377
- const generateId = () => (++count).toString(32);
376
+ let count$1 = 0;
377
+ const generateId = () => (++count$1).toString(32);
378
378
  const styleObjectToString = (style) => {
379
379
  let styleString = "";
380
380
  for (const key in style) {
@@ -476,6 +476,21 @@ const createDom = (tag, props, children) => {
476
476
 
477
477
  const BRAND = "j20";
478
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
+
479
494
  const registerWebComponent = (Comp) => {
480
495
  if (!Comp.customElement) {
481
496
  throw new Error("Custom element options is not provided");
@@ -565,16 +580,13 @@ const buildClass = (Comp) => {
565
580
  this.append(...elements);
566
581
  }
567
582
  }
568
- addStyleSheet(sheet) {
569
- if (this.#shadow) {
570
- this.#shadow.adoptedStyleSheets.push(sheet);
571
- }
583
+ addStyleSheet(styleSheet) {
584
+ if (this.#shadow)
585
+ addStyleSheet(this.#shadow, styleSheet);
572
586
  }
573
- removeStyleSheet(sheet) {
574
- if (this.#shadow) {
575
- this.#shadow.adoptedStyleSheets =
576
- this.#shadow.adoptedStyleSheets.filter((s) => s !== sheet);
577
- }
587
+ removeStyleSheet(styleSheet) {
588
+ if (this.#shadow)
589
+ removeStyleSheet(this.#shadow, styleSheet);
578
590
  }
579
591
  attributeChangedCallback(name, oldValue, newValue) {
580
592
  const attrs = NewElementClass.observedAttributes;
@@ -818,8 +830,6 @@ const wc = () => {
818
830
 
819
831
  const $ = (val) => val;
820
832
 
821
- const isSignal = (a) => a?.SIGNAL === true;
822
-
823
833
  const contextWeakMap = new WeakMap();
824
834
  const context = (Context) => {
825
835
  const instance = securityGetCurrentInstance();
@@ -891,16 +901,68 @@ const onMount = (callback) => {
891
901
 
892
902
  const onDestroy = (callback) => effect(() => callback);
893
903
 
894
- 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) => {
895
954
  let instance = getCurrentInstance();
896
955
  const sheet = new CSSStyleSheet();
897
956
  css && sheet.replaceSync(css);
898
957
  let root;
899
958
  while (instance) {
900
959
  if (instance.host) {
901
- instance.host.addStyleSheet(sheet);
960
+ const host = instance.host;
961
+ addReference(host, id, () => {
962
+ host.addStyleSheet(sheet);
963
+ });
902
964
  onDestroy(() => {
903
- instance.host.removeStyleSheet(sheet);
965
+ removeReference(host, id, () => host.removeStyleSheet(sheet));
904
966
  });
905
967
  break;
906
968
  }
@@ -913,33 +975,22 @@ const sheet = (css) => {
913
975
  if (root) {
914
976
  let node = root;
915
977
  do {
916
- if (node instanceof ShadowRoot) {
917
- break;
918
- }
919
- if (node === document) {
978
+ if (node instanceof ShadowRoot || node === document) {
920
979
  break;
921
980
  }
922
981
  node = node.parentNode;
923
982
  } while (node);
924
- node.adoptedStyleSheets.push(sheet);
983
+ addReference(node, id, () => {
984
+ addStyleSheet(node, sheet);
985
+ });
925
986
  onDestroy(() => {
926
- node.adoptedStyleSheets = node.adoptedStyleSheets.filter((s) => s !== sheet);
987
+ removeReference(node, id, () => {
988
+ removeStyleSheet(node, sheet);
989
+ });
927
990
  });
928
991
  }
929
992
  return sheet;
930
993
  };
931
- const css = (css) => {
932
- const styleId = id();
933
- const replacedCss = css.replace(/\.(\w[\w-]*)\s*?\{/g, (_, className) => {
934
- return `.${className}_${styleId} {`;
935
- });
936
- sheet(replacedCss);
937
- return new Proxy({}, {
938
- get: (_, prop) => {
939
- return `${prop}_${styleId}`;
940
- },
941
- });
942
- };
943
994
 
944
995
  function insertAfter(parentNode, newNode, targetNode) {
945
996
  parentNode?.insertBefore(newNode, targetNode.nextSibling);
@@ -1380,5 +1431,5 @@ const template = (template) => {
1380
1431
  };
1381
1432
  };
1382
1433
 
1383
- 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 };
1384
1435
  //# sourceMappingURL=index.js.map