sinho 0.1.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.
Files changed (117) hide show
  1. package/.github/workflows/ci.yml +24 -0
  2. package/.github/workflows/deploy-docs.yml +47 -0
  3. package/.prettierrc +3 -0
  4. package/LICENSE.md +21 -0
  5. package/README.md +33 -0
  6. package/ci/check-size.js +8 -0
  7. package/dist/array_mutation.d.ts +16 -0
  8. package/dist/array_mutation.js +75 -0
  9. package/dist/array_mutation.js.map +1 -0
  10. package/dist/bundle.d.ts +1126 -0
  11. package/dist/bundle.js +1074 -0
  12. package/dist/bundle.min.js +1 -0
  13. package/dist/component.d.ts +253 -0
  14. package/dist/component.js +256 -0
  15. package/dist/component.js.map +1 -0
  16. package/dist/context.d.ts +21 -0
  17. package/dist/context.js +34 -0
  18. package/dist/context.js.map +1 -0
  19. package/dist/create_element.d.ts +43 -0
  20. package/dist/create_element.js +43 -0
  21. package/dist/create_element.js.map +1 -0
  22. package/dist/dom.d.ts +602 -0
  23. package/dist/dom.js +97 -0
  24. package/dist/dom.js.map +1 -0
  25. package/dist/intrinsic/ClassComponent.d.ts +2 -0
  26. package/dist/intrinsic/ClassComponent.js +10 -0
  27. package/dist/intrinsic/ClassComponent.js.map +1 -0
  28. package/dist/intrinsic/Dynamic.d.ts +33 -0
  29. package/dist/intrinsic/Dynamic.js +53 -0
  30. package/dist/intrinsic/Dynamic.js.map +1 -0
  31. package/dist/intrinsic/ErrorBoundary.d.ts +14 -0
  32. package/dist/intrinsic/ErrorBoundary.js +36 -0
  33. package/dist/intrinsic/ErrorBoundary.js.map +1 -0
  34. package/dist/intrinsic/For.d.ts +10 -0
  35. package/dist/intrinsic/For.js +81 -0
  36. package/dist/intrinsic/For.js.map +1 -0
  37. package/dist/intrinsic/Fragment.d.ts +23 -0
  38. package/dist/intrinsic/Fragment.js +28 -0
  39. package/dist/intrinsic/Fragment.js.map +1 -0
  40. package/dist/intrinsic/If.d.ts +24 -0
  41. package/dist/intrinsic/If.js +47 -0
  42. package/dist/intrinsic/If.js.map +1 -0
  43. package/dist/intrinsic/Portal.d.ts +6 -0
  44. package/dist/intrinsic/Portal.js +15 -0
  45. package/dist/intrinsic/Portal.js.map +1 -0
  46. package/dist/intrinsic/Style.d.ts +7 -0
  47. package/dist/intrinsic/Style.js +70 -0
  48. package/dist/intrinsic/Style.js.map +1 -0
  49. package/dist/intrinsic/TagComponent.d.ts +4 -0
  50. package/dist/intrinsic/TagComponent.js +67 -0
  51. package/dist/intrinsic/TagComponent.js.map +1 -0
  52. package/dist/intrinsic/Text.d.ts +6 -0
  53. package/dist/intrinsic/Text.js +16 -0
  54. package/dist/intrinsic/Text.js.map +1 -0
  55. package/dist/intrinsic/mod.d.ts +5 -0
  56. package/dist/intrinsic/mod.js +6 -0
  57. package/dist/intrinsic/mod.js.map +1 -0
  58. package/dist/jsx-runtime/mod.d.ts +23 -0
  59. package/dist/jsx-runtime/mod.js +11 -0
  60. package/dist/jsx-runtime/mod.js.map +1 -0
  61. package/dist/mod.d.ts +8 -0
  62. package/dist/mod.js +7 -0
  63. package/dist/mod.js.map +1 -0
  64. package/dist/renderer.d.ts +13 -0
  65. package/dist/renderer.js +25 -0
  66. package/dist/renderer.js.map +1 -0
  67. package/dist/scope.d.ts +138 -0
  68. package/dist/scope.js +228 -0
  69. package/dist/scope.js.map +1 -0
  70. package/dist/template.d.ts +10 -0
  71. package/dist/template.js +7 -0
  72. package/dist/template.js.map +1 -0
  73. package/dist/utils.d.ts +6 -0
  74. package/dist/utils.js +13 -0
  75. package/dist/utils.js.map +1 -0
  76. package/package.json +71 -0
  77. package/src/array_mutation.ts +118 -0
  78. package/src/component.ts +624 -0
  79. package/src/context.ts +70 -0
  80. package/src/create_element.ts +89 -0
  81. package/src/dom.ts +819 -0
  82. package/src/intrinsic/ClassComponent.ts +17 -0
  83. package/src/intrinsic/For.ts +122 -0
  84. package/src/intrinsic/Fragment.ts +38 -0
  85. package/src/intrinsic/If.ts +73 -0
  86. package/src/intrinsic/Portal.ts +25 -0
  87. package/src/intrinsic/Style.ts +120 -0
  88. package/src/intrinsic/TagComponent.ts +102 -0
  89. package/src/intrinsic/Text.ts +24 -0
  90. package/src/intrinsic/mod.ts +5 -0
  91. package/src/jsx-runtime/mod.ts +41 -0
  92. package/src/mod.ts +37 -0
  93. package/src/renderer.ts +45 -0
  94. package/src/scope.ts +404 -0
  95. package/src/template.ts +16 -0
  96. package/src/utils.ts +29 -0
  97. package/terser.config.json +16 -0
  98. package/tsconfig.json +18 -0
  99. package/web/README.md +41 -0
  100. package/web/babel.config.js +3 -0
  101. package/web/dist/shingo.min.d.ts +1131 -0
  102. package/web/dist/shingo.min.js +1 -0
  103. package/web/docusaurus.config.ts +151 -0
  104. package/web/package-lock.json +14850 -0
  105. package/web/package.json +54 -0
  106. package/web/sidebars.ts +31 -0
  107. package/web/src/components/monacoEditor.tsx +72 -0
  108. package/web/src/components/playground.tsx +89 -0
  109. package/web/src/components/playgroundComponent.tsx +168 -0
  110. package/web/src/css/custom.css +37 -0
  111. package/web/src/pages/index.module.css +31 -0
  112. package/web/src/pages/index.tsx +73 -0
  113. package/web/src/pages/playground.tsx +64 -0
  114. package/web/static/.nojekyll +0 -0
  115. package/web/static/dist/bundle.d.ts +1126 -0
  116. package/web/static/dist/bundle.min.js +1 -0
  117. package/web/tsconfig.json +8 -0
@@ -0,0 +1,24 @@
1
+ # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3
+
4
+ name: CI
5
+
6
+ on:
7
+ push:
8
+ branches: [ "main" ]
9
+ pull_request:
10
+ branches: [ "main" ]
11
+
12
+ jobs:
13
+ build:
14
+ runs-on: ubuntu-latest
15
+
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+ - name: Use Node.js
19
+ uses: actions/setup-node@v3
20
+ with:
21
+ cache: 'npm'
22
+ - run: npm ci
23
+ - run: npm run build
24
+ - run: npm test
@@ -0,0 +1,47 @@
1
+ # Simple workflow for deploying static content to GitHub Pages
2
+ name: Deploy Website
3
+
4
+ on:
5
+ # Runs on pushes targeting the default branch
6
+ push:
7
+ branches: ["main"]
8
+ paths: ["web/**"]
9
+
10
+ # Allows you to run this workflow manually from the Actions tab
11
+ workflow_dispatch:
12
+
13
+ # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
14
+ permissions:
15
+ contents: read
16
+ pages: write
17
+ id-token: write
18
+
19
+ # Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
20
+ # However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
21
+ concurrency:
22
+ group: "pages"
23
+ cancel-in-progress: false
24
+
25
+ jobs:
26
+ # Single deploy job since we're just deploying
27
+ deploy:
28
+ environment:
29
+ name: github-pages
30
+ url: ${{ steps.deployment.outputs.page_url }}
31
+ runs-on: ubuntu-latest
32
+ steps:
33
+ - name: Checkout
34
+ uses: actions/checkout@v4
35
+ - name: Install Dependencies
36
+ run: npm install && cd ./web && npm install
37
+ - name: Build
38
+ run: npm run build && cd web && npm run build
39
+ - name: Setup Pages
40
+ uses: actions/configure-pages@v5
41
+ - name: Upload Artifact
42
+ uses: actions/upload-pages-artifact@v3
43
+ with:
44
+ path: ./web/build
45
+ - name: Deploy to GitHub Pages
46
+ id: deployment
47
+ uses: actions/deploy-pages@v4
package/.prettierrc ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "proseWrap": "always"
3
+ }
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Yichuan Shen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # đŸšĨ Sinho
2
+
3
+ A lightweight signal-based library for building web components with a React-like
4
+ API.
5
+
6
+ - 🌌 Web standards with custom HTML elements
7
+ - âš›ī¸ React-like API
8
+ - âœ’ī¸ Declarative templating with JSX (no additional parsing)
9
+ - đŸšĨ Fine-grained reactivity with signals
10
+ - 🛟 Type-safe components with TypeScript
11
+ - đŸĒļ Lightweight (~4KB minified and compressed)
12
+
13
+ ```tsx
14
+ import { Component, useSignal, defineComponents } from "sinho";
15
+
16
+ class Counter extends Component("x-counter") {
17
+ render() {
18
+ const [value, setValue] = useSignal(0);
19
+
20
+ return (
21
+ <>
22
+ <p>Counter: {value}</p>
23
+ <p>
24
+ <button onclick={() => setValue((n) => n + 1)}>Increment</button>{" "}
25
+ <button onclick={() => setValue((n) => n - 1)}>Decrement</button>
26
+ </p>
27
+ </>
28
+ );
29
+ }
30
+ }
31
+
32
+ defineComponents(Counter);
33
+ ```
@@ -0,0 +1,8 @@
1
+ import zlib from "node:zlib";
2
+ import fs from "node:fs";
3
+
4
+ const content = fs.readFileSync("./dist/bundle.min.js");
5
+ const gzip = zlib.gzipSync(content);
6
+
7
+ console.log(gzip.byteLength, "bytes");
8
+ console.log(gzip.byteLength / 1024, "KB");
@@ -0,0 +1,16 @@
1
+ import { Signal, SignalLike } from "./scope.js";
2
+ export type ArrayMutation = {
3
+ _type: "a" | "r";
4
+ _key: unknown;
5
+ _index: number;
6
+ } | {
7
+ _type: "m";
8
+ _key: unknown;
9
+ _from: number;
10
+ _to: number;
11
+ };
12
+ export interface ArrayMutationResult {
13
+ _mutations: ArrayMutation[];
14
+ _map: Map<unknown, number>;
15
+ }
16
+ export declare const useArrayMutation: <T extends unknown>(array: SignalLike<readonly T[]>, keyFn: (entry: T, index: number) => unknown) => Signal<ArrayMutationResult>;
@@ -0,0 +1,75 @@
1
+ import { useEffect, useSignal } from "./scope.js";
2
+ const getIndexMap = (array, keyFn) => {
3
+ const keyMap = new Map();
4
+ for (let i = 0; i < array.length; i++) {
5
+ const key = keyFn(array[i], i);
6
+ if (keyMap.has(key)) {
7
+ throw new Error(`Duplicate key '${key}'`);
8
+ }
9
+ keyMap.set(key, i);
10
+ }
11
+ return keyMap;
12
+ };
13
+ export const useArrayMutation = (array, keyFn) => {
14
+ const [result, setResult] = useSignal({
15
+ _mutations: [],
16
+ _map: new Map(),
17
+ });
18
+ let indexMap = new Map();
19
+ useEffect(() => {
20
+ const mutations = [];
21
+ const oldIndexMap = indexMap;
22
+ const newIndexMap = getIndexMap(array(), keyFn);
23
+ const transformToOldIndex = (i = NaN) => mutations
24
+ .map((mutation) => mutation._type == "r"
25
+ ? (j) => j < mutation._index ? j : j == mutation._index ? NaN : j - 1
26
+ : mutation._type == "a"
27
+ ? (j) => (j < mutation._index ? j : j + 1)
28
+ : mutation._type == "m"
29
+ ? (j) => mutation._to <= j && j < mutation._from
30
+ ? j + 1
31
+ : j == mutation._from
32
+ ? mutation._to
33
+ : j
34
+ : (j) => j)
35
+ .reduce((i, fn) => fn(i), i);
36
+ for (const key of oldIndexMap.keys()) {
37
+ const i = transformToOldIndex(oldIndexMap.get(key));
38
+ if (!newIndexMap.has(key)) {
39
+ mutations.push({
40
+ _type: "r",
41
+ _key: key,
42
+ _index: i,
43
+ });
44
+ }
45
+ }
46
+ for (let i = 0; i < array().length; i++) {
47
+ const key = keyFn(array()[i], i);
48
+ const oldIndex = transformToOldIndex(oldIndexMap.get(key));
49
+ if (isNaN(oldIndex)) {
50
+ mutations.push({
51
+ _type: "a",
52
+ _key: key,
53
+ _index: i,
54
+ });
55
+ }
56
+ else if (oldIndex != i) {
57
+ mutations.push({
58
+ _type: "m",
59
+ _key: key,
60
+ _from: oldIndex,
61
+ _to: i,
62
+ });
63
+ }
64
+ }
65
+ if (mutations.length > 0) {
66
+ setResult({
67
+ _mutations: mutations,
68
+ _map: newIndexMap,
69
+ });
70
+ }
71
+ indexMap = newIndexMap;
72
+ });
73
+ return result;
74
+ };
75
+ //# sourceMappingURL=array_mutation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"array_mutation.js","sourceRoot":"","sources":["../src/array_mutation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEtE,MAAM,WAAW,GAAG,CAClB,KAAmB,EACnB,KAA2C,EACrB,EAAE;IACxB,MAAM,MAAM,GAAG,IAAI,GAAG,EAAmB,CAAC;IAE1C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAE/B,IAAI,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,kBAAkB,GAAG,GAAG,CAAC,CAAC;QAC5C,CAAC;QAED,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;IACrB,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAoBF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B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