j20 0.0.25 → 0.0.27

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
@@ -315,12 +315,11 @@ function batch(fn) {
315
315
  }
316
316
  }
317
317
 
318
- const isEvent = (eventName) => {
319
- return eventName.startsWith("on");
320
- };
321
- const getEventName = (eventName) => {
322
- return eventName.slice(2).toLowerCase();
323
- };
318
+ const exist = (val) => val !== undefined;
319
+ const isEvent = (eventName) => eventName.startsWith("on");
320
+ const isNativeEvent = (eventName) => eventName.startsWith("onNative");
321
+ const getNativeEventName = (eventName) => eventName.slice(8).toLowerCase();
322
+ const getEventName = (eventName) => eventName.slice(2).toLowerCase();
324
323
  const getChildren = (propChildren) => {
325
324
  const arr = [];
326
325
  for (let i = 0; i < propChildren.length; i++) {
@@ -363,7 +362,12 @@ const styleObjectToString = (style) => {
363
362
  return styleString.trim();
364
363
  };
365
364
  const update = (node, key, oldValue, newValue) => {
366
- if (isEvent(key)) {
365
+ if (isNativeEvent(key)) {
366
+ node.removeEventListener(getNativeEventName(key), oldValue.handleEvent);
367
+ const { handleEvent, ...restValues } = newValue;
368
+ node.addEventListener(getNativeEventName(key), handleEvent, restValues);
369
+ }
370
+ else if (isEvent(key)) {
367
371
  node.removeEventListener(getEventName(key), oldValue);
368
372
  node.addEventListener(getEventName(key), newValue);
369
373
  }
@@ -382,7 +386,10 @@ const update = (node, key, oldValue, newValue) => {
382
386
  }
383
387
  };
384
388
  const unset = (node, key, oldValue) => {
385
- if (isEvent(key)) {
389
+ if (isNativeEvent(key)) {
390
+ node.removeEventListener(getNativeEventName(key), oldValue.handleEvent);
391
+ }
392
+ else if (isEvent(key)) {
386
393
  node.removeEventListener(getEventName(key), oldValue);
387
394
  }
388
395
  else if (key === "ref") {
@@ -393,7 +400,11 @@ const unset = (node, key, oldValue) => {
393
400
  }
394
401
  };
395
402
  const add = (node, key, newValue) => {
396
- if (isEvent(key)) {
403
+ if (isNativeEvent(key)) {
404
+ const { handleEvent, ...restValues } = newValue;
405
+ node.addEventListener(getNativeEventName(key), handleEvent, restValues);
406
+ }
407
+ else if (isEvent(key)) {
397
408
  node.addEventListener(getEventName(key), newValue);
398
409
  }
399
410
  else if (key === "ref") {
@@ -422,17 +433,22 @@ const nodeAttributesEffect = (node, propsFn) => {
422
433
  let oldProps = {};
423
434
  effect(() => {
424
435
  const newProps = { ...propsFn() };
425
- const newKeys = Object.keys(newProps);
426
- const oldKeys = Object.keys(oldProps);
427
- const allKeys = new Set([...newKeys, ...oldKeys]);
436
+ const allKeys = new Set([
437
+ ...Object.keys(newProps),
438
+ ...Object.keys(oldProps),
439
+ ]);
428
440
  for (const key of allKeys) {
429
- oldKeys.includes(key)
430
- ? newKeys.includes(key)
431
- ? Object.is(oldProps[key], newProps[key])
441
+ const oldValue = oldProps[key];
442
+ const newValue = newProps[key];
443
+ exist(oldValue)
444
+ ? exist(newValue)
445
+ ? Object.is(oldValue, newValue)
432
446
  ? null
433
- : update(node, key, oldProps[key], newProps[key])
434
- : unset(node, key, oldProps[key])
435
- : add(node, key, newProps[key]);
447
+ : update(node, key, oldValue, newValue)
448
+ : unset(node, key, oldValue)
449
+ : exist(newValue)
450
+ ? add(node, key, newValue)
451
+ : null;
436
452
  }
437
453
  oldProps = newProps;
438
454
  });
@@ -880,51 +896,68 @@ const onMount = (callback) => {
880
896
 
881
897
  const onDestroy = (callback) => effect(() => callback);
882
898
 
883
- const refWeakMap = new WeakMap();
884
- const createCss = (css) => {
885
- let id = undefined;
899
+ function replaceTopLevelSelectors(css, id) {
900
+ let depth = 0;
901
+ let result = "";
902
+ let lastSlice = 0;
903
+ for (let i = 0; i < css.length; i++) {
904
+ if (css[i] === "{") {
905
+ if (depth === 0) {
906
+ result +=
907
+ css
908
+ .slice(lastSlice, i)
909
+ .replace(/\.(\w[\w-]*)/g, (_, name) => `.${name}_${id}`) + "{";
910
+ lastSlice = i + 1;
911
+ }
912
+ depth++;
913
+ }
914
+ else if (css[i] === "}") {
915
+ depth--;
916
+ if (depth === 0) {
917
+ result += css.slice(lastSlice, i + 1);
918
+ lastSlice = i + 1;
919
+ }
920
+ }
921
+ }
922
+ result += css.slice(lastSlice);
923
+ return result;
924
+ }
925
+ const createCssModule = (css) => {
926
+ let hash = undefined;
927
+ let replacedCss = undefined;
886
928
  return () => {
887
- if (!id)
888
- id = cssHash(css);
889
- const classNameId = `j_${id}`;
890
- const replacedCss = css.replace(/\.(\w[\w-]*)\s*?\{/g, (_, className) => {
891
- return `.${className}_${classNameId} {`;
892
- });
893
- styleSheet(replacedCss, id);
929
+ if (!hash)
930
+ hash = cssHash(css);
931
+ if (!replacedCss)
932
+ replacedCss = replaceTopLevelSelectors(css, hash);
933
+ styleSheet(replacedCss, hash);
894
934
  return new Proxy({}, {
895
935
  get: (_, prop) => {
896
- return `${prop}_${classNameId}`;
936
+ return `${prop}_${hash}`;
897
937
  },
898
938
  });
899
939
  };
900
940
  };
901
941
  const addReference = (host, id, css) => {
902
- if (!refWeakMap.get(host)) {
903
- refWeakMap.set(host, []);
904
- }
905
- const states = refWeakMap.get(host) ?? [];
906
- const state = states.find((state) => state.id === id);
907
- if (!state) {
908
- const newState = {
909
- id,
910
- refCount: 1,
911
- styleSheet: createStyleSheet(css),
912
- };
913
- refWeakMap.set(host, [...states, newState]);
914
- addStyleSheet(host, newState.styleSheet);
942
+ const currentSheet = host[id];
943
+ if (!currentSheet) {
944
+ const sheet = createStyleSheet(css);
945
+ sheet.refCount = 1;
946
+ host[id] = sheet;
947
+ addStyleSheet(host, sheet);
915
948
  }
916
949
  else {
917
- state.refCount++;
950
+ currentSheet.refCount++;
918
951
  }
919
952
  };
920
953
  const removeReference = (host, id) => {
921
- const states = refWeakMap.get(host) ?? [];
922
- const state = states.find((state) => state.id === id);
923
- if (state) {
924
- state.refCount--;
925
- if (state.refCount === 0) {
926
- refWeakMap.set(host, states.filter((state) => state.id !== id));
927
- removeStyleSheet(host, state.styleSheet);
954
+ const currentSheet = host[id];
955
+ if (currentSheet) {
956
+ currentSheet.refCount--;
957
+ if (currentSheet.refCount === 0) {
958
+ removeStyleSheet(host, currentSheet);
959
+ delete currentSheet.refCount;
960
+ delete host[id];
928
961
  }
929
962
  }
930
963
  };
@@ -1382,5 +1415,5 @@ const template = (template) => {
1382
1415
  };
1383
1416
  };
1384
1417
 
1385
- export { $, $useContext, Case, Computed, Default, Effect, For, Fragment, If, Replace, Signal, Some, Switch, batch, computed, createComponent, createContext, createCss, createDom, createElement, createLogicComponent, createRoot, cssHash, effect, getCurrentInstance, h2, id, instanceCreate, instanceCreateElement, instanceDestroy, instanceGetElements, instanceInit, jsx, jsxs, mounts, onDestroy, onMount, ref, registerWebComponent, securityGetCurrentInstance, signal, styleSheet, template, untrack, wc };
1418
+ export { $, $useContext, Case, Computed, Default, Effect, For, Fragment, If, Replace, Signal, Some, Switch, batch, computed, createComponent, createContext, createCssModule, createDom, createElement, createLogicComponent, createRoot, cssHash, effect, getCurrentInstance, h2, id, instanceCreate, instanceCreateElement, instanceDestroy, instanceGetElements, instanceInit, jsx, jsxs, mounts, onDestroy, onMount, ref, registerWebComponent, securityGetCurrentInstance, signal, styleSheet, template, untrack, wc };
1386
1419
  //# sourceMappingURL=index.js.map