sinwan 1.1.1 → 1.1.2

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 (38) hide show
  1. package/README.md +34 -3
  2. package/dist/cjs/index.development.js +296 -192
  3. package/dist/cjs/index.development.js.map +9 -9
  4. package/dist/cjs/index.production.min.js +2 -2
  5. package/dist/cjs/index.production.min.js.map +9 -9
  6. package/dist/cjs/renderer/index.development.js +239 -87
  7. package/dist/cjs/renderer/index.development.js.map +8 -8
  8. package/dist/cjs/renderer/index.production.min.js +2 -2
  9. package/dist/cjs/renderer/index.production.min.js.map +8 -8
  10. package/dist/cjs/server/index.development.js +132 -67
  11. package/dist/cjs/server/index.development.js.map +6 -6
  12. package/dist/cjs/server/index.production.min.js +2 -2
  13. package/dist/cjs/server/index.production.min.js.map +6 -6
  14. package/dist/component/control-flow.d.ts +6 -1
  15. package/dist/component/control-flow.d.ts.map +1 -1
  16. package/dist/component/instance.d.ts.map +1 -1
  17. package/dist/esm/index.development.js +296 -192
  18. package/dist/esm/index.development.js.map +9 -9
  19. package/dist/esm/index.production.min.js +2 -2
  20. package/dist/esm/index.production.min.js.map +9 -9
  21. package/dist/esm/renderer/index.development.js +239 -87
  22. package/dist/esm/renderer/index.development.js.map +8 -8
  23. package/dist/esm/renderer/index.production.min.js +2 -2
  24. package/dist/esm/renderer/index.production.min.js.map +8 -8
  25. package/dist/esm/server/index.development.js +132 -67
  26. package/dist/esm/server/index.development.js.map +6 -6
  27. package/dist/esm/server/index.production.min.js +2 -2
  28. package/dist/esm/server/index.production.min.js.map +6 -6
  29. package/dist/reactivity/index.d.ts +1 -1
  30. package/dist/reactivity/index.d.ts.map +1 -1
  31. package/dist/renderer/attributes.d.ts.map +1 -1
  32. package/dist/renderer/events.d.ts.map +1 -1
  33. package/dist/renderer/render-control-flow.d.ts.map +1 -1
  34. package/dist/renderer/render-element.d.ts.map +1 -1
  35. package/dist/renderer/types.d.ts +2 -0
  36. package/dist/renderer/types.d.ts.map +1 -1
  37. package/dist/server/renderer.d.ts.map +1 -1
  38. package/package.json +5 -2
@@ -462,7 +462,7 @@ function resolve(value) {
462
462
  }
463
463
  // src/renderer/events.ts
464
464
  function isEventProp(key) {
465
- return key.length > 2 && key[0] === "o" && key[1] === "n" && key[2] >= "A" && key[2] <= "Z";
465
+ return key.length > 2 && key.startsWith("on");
466
466
  }
467
467
  function toEventName(key) {
468
468
  return key.slice(2).toLowerCase();
@@ -539,19 +539,28 @@ function fireMountedHooks(instance) {
539
539
  }
540
540
  }
541
541
  function fireUnmountedHooks(instance) {
542
- for (const child of instance.children) {
542
+ const children = [...instance.children];
543
+ for (const child of children) {
543
544
  fireUnmountedHooks(child);
544
545
  }
545
- if (instance.isMounted && !instance.isUnmounted) {
546
+ if (!instance.isUnmounted) {
546
547
  instance.isUnmounted = true;
547
- instance.isMounted = false;
548
- for (const hook of instance._unmountedHooks) {
549
- hook();
548
+ if (instance.isMounted) {
549
+ instance.isMounted = false;
550
+ for (const hook of instance._unmountedHooks) {
551
+ hook();
552
+ }
550
553
  }
551
554
  for (const dispose of instance.effects) {
552
555
  dispose();
553
556
  }
554
557
  instance.effects.length = 0;
558
+ if (instance.parent) {
559
+ const idx = instance.parent.children.indexOf(instance);
560
+ if (idx !== -1) {
561
+ instance.parent.children.splice(idx, 1);
562
+ }
563
+ }
555
564
  }
556
565
  }
557
566
  function fireUpdatedHooks(instance) {
@@ -601,7 +610,9 @@ function applyAttributes(el, props) {
601
610
  for (const [key, value] of Object.entries(props)) {
602
611
  if (SKIP_PROPS.has(key) || isEventProp(key))
603
612
  continue;
604
- if (isReactive(value)) {
613
+ const attrName = resolveAttributeName(key);
614
+ const isComplex = attrName === "class" || attrName === "style";
615
+ if (isReactive(value) || isComplex && containsReactive(value)) {
605
616
  const state = { previousStyleProps: new Set };
606
617
  let initialized = false;
607
618
  const dispose = effect(() => {
@@ -663,19 +674,17 @@ function setSingleAttribute(el, key, value, state) {
663
674
  function resolveAttributeName(key) {
664
675
  return PROP_ALIASES[key] ?? key;
665
676
  }
666
- function applyStyle(el, styles, state) {
677
+ function applyStyle(el, value, state) {
678
+ const styleObj = normalizeStyle(value);
667
679
  const nextProps = new Set;
668
- for (const [prop, val] of Object.entries(styles)) {
680
+ for (const [prop, val] of Object.entries(styleObj)) {
669
681
  nextProps.add(prop);
670
682
  if (val == null) {
671
683
  removeStyleProperty(el, prop);
672
684
  continue;
673
685
  }
674
- if (prop.includes("-")) {
675
- el.style.setProperty(prop, String(val));
676
- } else {
677
- el.style[prop] = val;
678
- }
686
+ const kebabProp = prop.startsWith("--") ? prop : camelToKebab(prop);
687
+ el.style.setProperty(kebabProp, String(val));
679
688
  }
680
689
  if (!state) {
681
690
  return;
@@ -687,23 +696,75 @@ function applyStyle(el, styles, state) {
687
696
  }
688
697
  state.previousStyleProps = nextProps;
689
698
  }
699
+ function normalizeStyle(value) {
700
+ const resolved = resolve(value);
701
+ if (!resolved)
702
+ return {};
703
+ if (typeof resolved === "string") {
704
+ return parseStyleString(resolved);
705
+ }
706
+ if (Array.isArray(resolved)) {
707
+ return resolved.reduce((acc, item) => {
708
+ const normalized = normalizeStyle(item);
709
+ return Object.assign(acc, normalized);
710
+ }, {});
711
+ }
712
+ if (typeof resolved === "object") {
713
+ const result = {};
714
+ for (const [k, v] of Object.entries(resolved)) {
715
+ result[k] = resolve(v);
716
+ }
717
+ return result;
718
+ }
719
+ return {};
720
+ }
721
+ function parseStyleString(style) {
722
+ const result = {};
723
+ style.split(";").forEach((rule) => {
724
+ const i = rule.indexOf(":");
725
+ if (i > 0) {
726
+ const prop = rule.slice(0, i).trim();
727
+ const val = rule.slice(i + 1).trim();
728
+ if (prop && val) {
729
+ result[prop] = val;
730
+ }
731
+ }
732
+ });
733
+ return result;
734
+ }
690
735
  function removeStyleProperty(el, prop) {
691
- if (prop.includes("-")) {
692
- el.style.removeProperty(prop);
693
- } else {
694
- el.style[prop] = "";
695
- }
736
+ const kebabProp = prop.startsWith("--") ? prop : camelToKebab(prop);
737
+ el.style.removeProperty(kebabProp);
696
738
  }
697
739
  function applyClass(el, value) {
698
- let classStr;
699
- if (Array.isArray(value)) {
700
- classStr = value.filter(Boolean).join(" ");
701
- } else if (typeof value === "object" && value !== null) {
702
- classStr = Object.entries(value).filter(([, v]) => Boolean(v)).map(([k]) => k).join(" ");
703
- } else {
704
- classStr = String(value);
740
+ domOps.setAttribute(el, "class", normalizeClass(value));
741
+ }
742
+ function normalizeClass(value) {
743
+ const resolved = resolve(value);
744
+ if (!resolved)
745
+ return "";
746
+ if (typeof resolved === "string")
747
+ return resolved;
748
+ if (Array.isArray(resolved)) {
749
+ return resolved.map(normalizeClass).filter(Boolean).join(" ");
750
+ }
751
+ if (typeof resolved === "object") {
752
+ return Object.entries(resolved).filter(([, v]) => Boolean(resolve(v))).map(([k]) => k).join(" ");
753
+ }
754
+ return String(resolved);
755
+ }
756
+ function containsReactive(value) {
757
+ if (isReactive(value))
758
+ return true;
759
+ if (Array.isArray(value))
760
+ return value.some(containsReactive);
761
+ if (typeof value === "object" && value !== null) {
762
+ return Object.values(value).some(containsReactive);
705
763
  }
706
- domOps.setAttribute(el, "class", classStr);
764
+ return false;
765
+ }
766
+ function camelToKebab(str) {
767
+ return str.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`);
707
768
  }
708
769
 
709
770
  // src/component/control-flow.ts
@@ -799,6 +860,9 @@ function Portal(props) {
799
860
  children: []
800
861
  };
801
862
  }
863
+ function isElementLike(value) {
864
+ return value != null && typeof value === "object" && "tag" in value;
865
+ }
802
866
  function isShowElement(element) {
803
867
  return element.tag === SHOW_TYPE;
804
868
  }
@@ -823,6 +887,107 @@ function isDynamicElement(element) {
823
887
  function isPortalElement(element) {
824
888
  return element.tag === PORTAL_TYPE;
825
889
  }
890
+ function resolveSwitchContent(element) {
891
+ const props = element.props;
892
+ const children = normalizeContent(props.children ?? element.children);
893
+ const match = findTruthyMatch(children);
894
+ return match !== undefined ? match : props.fallback;
895
+ }
896
+ function findTruthyMatch(nodes) {
897
+ for (const node of nodes) {
898
+ if (node == null || typeof node === "boolean")
899
+ continue;
900
+ if (Array.isArray(node)) {
901
+ const match = findTruthyMatch(node);
902
+ if (match !== undefined)
903
+ return match;
904
+ continue;
905
+ }
906
+ if (isElementLike(node)) {
907
+ let element = node;
908
+ if (typeof element.tag === "function") {
909
+ const tag = element.tag;
910
+ if (tag === Match || tag === Show || tag === For || tag === Index || tag === Key || tag === Switch) {
911
+ element = tag(element.props);
912
+ }
913
+ }
914
+ if (isMatchElement(element)) {
915
+ const when = readReactive(element.props.when);
916
+ if (when) {
917
+ return resolveMatchChildren(element, when);
918
+ }
919
+ } else if (isShowElement(element)) {
920
+ const when = readReactive(element.props.when);
921
+ if (when) {
922
+ const content = resolveShowChildren(element, when);
923
+ const match = findTruthyMatch(normalizeContent(content));
924
+ if (match !== undefined)
925
+ return match;
926
+ } else if (element.props.fallback) {
927
+ const match = findTruthyMatch(normalizeContent(element.props.fallback));
928
+ if (match !== undefined)
929
+ return match;
930
+ }
931
+ } else if (isForElement(element)) {
932
+ const props = element.props;
933
+ const items = readReactive(props.each);
934
+ if (Array.isArray(items)) {
935
+ for (let i = 0;i < items.length; i++) {
936
+ const child = props.children(items[i], () => i);
937
+ const match = findTruthyMatch(normalizeContent(child));
938
+ if (match !== undefined)
939
+ return match;
940
+ }
941
+ }
942
+ } else if (isIndexElement(element)) {
943
+ const props = element.props;
944
+ const items = readReactive(props.each);
945
+ if (Array.isArray(items)) {
946
+ for (let i = 0;i < items.length; i++) {
947
+ const child = props.children(() => items[i], i);
948
+ const match = findTruthyMatch(normalizeContent(child));
949
+ if (match !== undefined)
950
+ return match;
951
+ }
952
+ }
953
+ } else if (isKeyElement(element)) {
954
+ const key = readReactive(element.props.when);
955
+ const child = resolveKeyChildren(element, key);
956
+ const match = findTruthyMatch(normalizeContent(child));
957
+ if (match !== undefined)
958
+ return match;
959
+ }
960
+ }
961
+ }
962
+ return;
963
+ }
964
+ function resolveMatchChildren(element, value) {
965
+ const children = element.props.children ?? element.children;
966
+ if (typeof children === "function") {
967
+ return children(value);
968
+ }
969
+ return children;
970
+ }
971
+ function resolveShowChildren(element, value) {
972
+ const children = element.props.children ?? element.children;
973
+ if (typeof children === "function") {
974
+ return children(value);
975
+ }
976
+ return children;
977
+ }
978
+ function resolveKeyChildren(element, value) {
979
+ const children = element.props.children ?? element.children;
980
+ if (typeof children === "function") {
981
+ return children(value);
982
+ }
983
+ return children;
984
+ }
985
+ function normalizeContent(content) {
986
+ if (content == null || typeof content === "boolean") {
987
+ return [];
988
+ }
989
+ return Array.isArray(content) ? content : [content];
990
+ }
826
991
  function normalizeChildren2(children) {
827
992
  if (children == null || typeof children === "boolean") {
828
993
  return [];
@@ -962,8 +1127,10 @@ function renderShowBlock(element, block, parent, namespace, owner) {
962
1127
  return effect(() => {
963
1128
  clearChildren(block);
964
1129
  const when = readReactive2(element.props.when);
965
- const content = withOptionalInstance(owner, () => when ? resolveShowChildren(element, when) : element.props.fallback);
966
- block.children = renderBlockContent(content, parent, block.endAnchor, namespace, owner);
1130
+ block.children = withOptionalInstance(owner, () => {
1131
+ const content = when ? resolveShowChildren(element, when) : element.props.fallback;
1132
+ return renderBlockContent(content, parent, block.endAnchor, namespace, owner);
1133
+ });
967
1134
  if (initialized) {
968
1135
  fireMountedAndQueueUpdated(owner);
969
1136
  }
@@ -1150,18 +1317,31 @@ function renderPortal(element, parent, anchor, namespace) {
1150
1317
  insertNode(parent, placeholder, anchor);
1151
1318
  const owner = getCurrentInstance();
1152
1319
  let disposeEffect = () => {};
1320
+ const targetAnchor = domOps.createComment("Sinwan-pa");
1321
+ let lastTarget = null;
1153
1322
  const portal = {
1154
1323
  type: "portal",
1155
1324
  anchor: placeholder,
1156
1325
  children: [],
1157
- dispose: () => disposeEffect()
1326
+ dispose: () => disposeEffect(),
1327
+ targetAnchor
1158
1328
  };
1159
1329
  let initialized = false;
1160
1330
  disposeEffect = effect(() => {
1161
- clearPortalChildren(portal);
1162
1331
  const target = resolvePortalTarget(element.props.mount);
1332
+ if (target !== lastTarget) {
1333
+ if (lastTarget) {
1334
+ domOps.remove(targetAnchor);
1335
+ }
1336
+ if (target) {
1337
+ domOps.appendChild(target, targetAnchor);
1338
+ }
1339
+ lastTarget = target;
1340
+ portal.target = target;
1341
+ }
1342
+ clearPortalChildren(portal);
1163
1343
  if (target) {
1164
- portal.children = renderBlockContent(element.props.children ?? element.children, target, null, namespace, owner);
1344
+ portal.children = renderBlockContent(element.props.children ?? element.children, target, targetAnchor, namespace, owner);
1165
1345
  }
1166
1346
  if (initialized) {
1167
1347
  fireMountedAndQueueUpdated(owner);
@@ -1170,48 +1350,12 @@ function renderPortal(element, parent, anchor, namespace) {
1170
1350
  });
1171
1351
  return portal;
1172
1352
  }
1173
- function resolveShowChildren(element, value) {
1174
- const children = element.props.children ?? element.children;
1175
- if (typeof children === "function") {
1176
- return children(value);
1177
- }
1178
- return children;
1179
- }
1180
- function resolveSwitchContent(element) {
1181
- const props = element.props;
1182
- const children = normalizeContent(props.children ?? element.children);
1183
- for (const child of children) {
1184
- const match = getMatchElement(child);
1185
- if (!match) {
1186
- continue;
1187
- }
1188
- const when = readReactive2(match.props.when);
1189
- if (when) {
1190
- return resolveMatchChildren(match, when);
1191
- }
1192
- }
1193
- return props.fallback;
1194
- }
1195
- function resolveMatchChildren(element, value) {
1196
- const children = element.props.children ?? element.children;
1197
- if (typeof children === "function") {
1198
- return children(value);
1199
- }
1200
- return children;
1201
- }
1202
- function resolveKeyChildren(element, value) {
1203
- const children = element.props.children ?? element.children;
1204
- if (typeof children === "function") {
1205
- return children(value);
1206
- }
1207
- return children;
1208
- }
1209
1353
  function createDynamicElement(element, tag) {
1210
1354
  if (typeof tag !== "string" && typeof tag !== "function") {
1211
1355
  return null;
1212
1356
  }
1213
1357
  const { component, ...props } = element.props;
1214
- const children = normalizeContent(props.children ?? element.children);
1358
+ const children = normalizeContent2(props.children ?? element.children);
1215
1359
  return {
1216
1360
  tag,
1217
1361
  props,
@@ -1219,11 +1363,14 @@ function createDynamicElement(element, tag) {
1219
1363
  };
1220
1364
  }
1221
1365
  function renderBlockContent(content, parent, anchor, namespace, owner) {
1222
- if (content == null || typeof content === "boolean") {
1366
+ if (content == null || typeof content === "boolean")
1223
1367
  return [];
1224
- }
1225
- const nodes = Array.isArray(content) ? content : [content];
1226
- return nodes.map((node) => withOptionalInstance(owner, () => renderNodeToDOM(node, parent, anchor, namespace)));
1368
+ return withOptionalInstance(owner, () => {
1369
+ if (Array.isArray(content)) {
1370
+ return content.map((child) => renderNodeToDOM(child, parent, anchor, namespace));
1371
+ }
1372
+ return [renderNodeToDOM(content, parent, anchor, namespace)];
1373
+ });
1227
1374
  }
1228
1375
  function clearChildren(block) {
1229
1376
  for (const child of block.children) {
@@ -1241,6 +1388,23 @@ function moveBeforeEnd(parent, mounted, endAnchor) {
1241
1388
  for (const node of getMountedDomNodes(mounted)) {
1242
1389
  domOps.insertBefore(parent, node, endAnchor);
1243
1390
  }
1391
+ syncPortalOrder(mounted);
1392
+ }
1393
+ function syncPortalOrder(mounted) {
1394
+ if (mounted.type === "portal") {
1395
+ if (mounted.target && mounted.targetAnchor) {
1396
+ for (const child of mounted.children) {
1397
+ for (const node of getMountedDomNodes(child)) {
1398
+ domOps.appendChild(mounted.target, node);
1399
+ }
1400
+ }
1401
+ domOps.appendChild(mounted.target, mounted.targetAnchor);
1402
+ }
1403
+ } else if ("children" in mounted && Array.isArray(mounted.children)) {
1404
+ for (const child of mounted.children) {
1405
+ syncPortalOrder(child);
1406
+ }
1407
+ }
1244
1408
  }
1245
1409
  function fireMountedAndQueueUpdated(owner) {
1246
1410
  if (owner) {
@@ -1254,24 +1418,12 @@ function withOptionalInstance(owner, fn) {
1254
1418
  function readReactive2(value) {
1255
1419
  return resolve(value);
1256
1420
  }
1257
- function normalizeContent(content) {
1421
+ function normalizeContent2(content) {
1258
1422
  if (content == null || typeof content === "boolean") {
1259
1423
  return [];
1260
1424
  }
1261
1425
  return Array.isArray(content) ? content : [content];
1262
1426
  }
1263
- function isElementLike(value) {
1264
- return value != null && typeof value === "object" && "tag" in value;
1265
- }
1266
- function getMatchElement(value) {
1267
- if (!isElementLike(value)) {
1268
- return null;
1269
- }
1270
- if (isMatchElement(value)) {
1271
- return value;
1272
- }
1273
- return value.tag === Match ? Match(value.props) : null;
1274
- }
1275
1427
  function resolvePortalTarget(value) {
1276
1428
  const target = readReactive2(value);
1277
1429
  if (target == null) {
@@ -1609,5 +1761,5 @@ function render(node, container) {
1609
1761
  };
1610
1762
  }
1611
1763
 
1612
- //# debugId=2D5A4F08E5DE065564756E2164756E21
1764
+ //# debugId=74A1711C6AA3EA8F64756E2164756E21
1613
1765
  //# sourceMappingURL=index.development.js.map