@ryupold/vode 0.13.2 → 1.0.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 (2) hide show
  1. package/README.md +222 -6
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
- ![vode](./logo.svg)
1
+ # ![vode-logo](./logo.svg)
2
2
 
3
- Small web framework for minimal websites.
4
- Each vode app has its own state and renders a tree of HTML elements.
5
- The state is a singleton object that can be updated, and the UI will re-render when a patch object is supplied.
3
+ A small web framework for a minimalistic development flow. Zero dependencies, no build step except for typescript compilation, and a simple virtual DOM implementation that is easy to understand and use. Autocompletion out of the box due to binding to `lib.dom.d.ts`.
4
+
5
+ ## vode
6
6
 
7
7
  A `vode` is a representation of a virtual DOM node, which is a tree structure of HTML elements. It is written as tuple:
8
8
 
@@ -10,8 +10,215 @@ A `vode` is a representation of a virtual DOM node, which is a tree structure of
10
10
  [TAG, PROPS?, CHILDREN...]
11
11
  ```
12
12
 
13
+ As you can see, it is a simple array with the first element being the tag name, the second element being an optional properties object, and the rest being child-vodes.
14
+
15
+ ### Component
16
+ ```ts
17
+ type Component<S> = (s: S) => ChildVode<S>;
18
+ ```
19
+
20
+ A `Component<State>` is a function that takes a state object and returns a `Vode<State>`. It is used to render the UI based on the current state.
21
+
22
+ ```ts
23
+ // A full vode has a tag, properties, and children. props and children are optional.
24
+ const CompFooBar = (s) => [DIV, { class: "container" },
25
+
26
+ // a child vode can be a string, which results in a text node
27
+ [H1, "Hello World"],
28
+
29
+ // a vode can also be a self-closing tag
30
+ [BR],
31
+
32
+ // style object maps directly to the HTML style attribute
33
+ [P, { style: { color: "red", fontWeight: "bold" } }, "This is a paragraph."],
34
+
35
+ // class property has multiple forms
36
+ [UL,
37
+ [LI, {class: "class1 class2"}, "as string"],
38
+ [LI, {class: ["class1", "class2"]}, "as array"],
39
+ [LI, {class: {class1: true, class2: false}}, "as Record<string, boolean>"],
40
+ ],
41
+
42
+ // events get the state object as first argument
43
+ // and the HTML event object as second argument
44
+ [BUTTON, {
45
+ // all on* events accept `Patch<State>`
46
+ onclick: (state, evt) => {
47
+ // objects returned by events are patched automatically
48
+ return { counter: state.counter + 1 };
49
+ },
50
+
51
+ // you can set the patch object directly for events
52
+ onmouseenter: {pointing: true},
53
+ onmouseleave: {pointing: false},
54
+
55
+ // a patch can be an async function
56
+ onmouseup: async (state, evt) => {
57
+ state.patch(loading: true);
58
+ const result = await apiCall();
59
+ return { title: result.data.title, loading: false };
60
+ },
61
+
62
+ // you can also use a generator function that yields patches
63
+ onmousedown: async function* (state, evt) => {
64
+ yield { loading: true };
65
+ const result = await apiCall();
66
+ yield {
67
+ body: result.data.body,
68
+ loading: false
69
+ };
70
+ },
71
+
72
+ class: { bar: s.pointing }
73
+ }, "Click me!"],
74
+ ];
75
+ ```
76
+
77
+ ### app
78
+
79
+ `app` is a function that takes a HTML node, an initial state object, and a render function (`Component<State>`).
80
+ ```ts
81
+ const appNode = document.getElementById('APP-ID');
82
+ const initialState = {
83
+ counter: 0,
84
+ pointing: false,
85
+ loading: false,
86
+ title: '',
87
+ body: '',
88
+ };
89
+ const patch = app<State>(appNode, initialState, (s) => CompFooBar(s));
90
+ ```
91
+ It will render the initial state and update the DOM when patches are applied to the patch function or via events. All elements returnded by the render function are placed under `appNode`.
92
+
93
+ You can have multiple isolated `app` instances on a page, each with its own state and render function. The returned patch function from `app` can be used to synchronize the state between them.
94
+
95
+ ### memoization
96
+ To optimize performance, you can use `memo(Array, Component)` to cache the result of a component function. This is useful when the component does not depend on the state or when the state does not change frequently.
97
+
98
+ ```ts
99
+ const CompMemoFooBar = (s) => [DIV, { class: "container" },
100
+ [H1, "Hello World"],
101
+ [BR],
102
+ [P, "This is a paragraph."],
103
+
104
+ // expensive component to render
105
+ memo(
106
+ // this array is shallow compared to the previous render
107
+ [s.title, s.body],
108
+ // this is the component function that will be
109
+ // called only when the array changes
110
+ (s) => {
111
+ const list = [UL];
112
+ for (let i = 0; i < 1000; i++) {
113
+ list.push([LI, `Item ${i}`]);
114
+ }
115
+ return list;
116
+ },
117
+ )
118
+ ];
119
+ ```
120
+
121
+ ### state
122
+ The state is a singleton object that can be updated. A re-render happens when a patch object is supplied to the patch function or via event.
123
+
124
+ ```ts
125
+ // type safe way to create the state object
126
+ const s = createState({
127
+ counter: 0,
128
+ pointing: false,
129
+ loading: false,
130
+ title: 'foo',
131
+ body: '',
132
+ });
133
+
134
+ type State = typeof s;
135
+
136
+ app(appNode, s, ...); // after calling app(), the state object is bound to the appNode
137
+
138
+
139
+ s.title = 'Hello World'; // update state directly as it is a singleton (silent patch)
140
+
141
+ s.patch({}); // render patch
142
+
143
+ s.patch({ title: 'bar' }); // render patch with a change that is applied to the state
144
+
145
+ s.patch((s) => ({body: s.body + ' baz'})); // render patch with a function that receives the state
146
+
147
+ s.patch(async function*(s){
148
+ // you can also use a generator function that yields patches
149
+ yield { loading: true };
150
+ const result = await apiCall();
151
+ yield { title: result.title, body: result.body };
152
+ return { loading: false };
153
+ });
154
+
155
+ s.patch(null); // ignored, also: undefined, number, string, boolean, void
156
+
157
+ s.patch({ pointing: undefined }); // deletes the property from the state
158
+ ```
159
+
13
160
  ## Install
14
161
 
162
+ ### ESM
163
+ ```html
164
+ <!DOCTYPE html>
165
+ <html>
166
+ <head>
167
+ <title>ESM Example</title>
168
+ </head>
169
+ <body>
170
+ <div id="app"></div>
171
+ <script type="module">
172
+ import { app, createState, BR, DIV, INPUT, SPAN } from 'https://cdn.jsdelivr.net/npm/@ryupold/vode/dist/vode.min.mjs';
173
+
174
+ const appNode = document.getElementById('app');
175
+
176
+ app(appNode, { counter: 0 },
177
+ (s) => [DIV,
178
+ [INPUT, {
179
+ type: 'button',
180
+ onclick: { counter: s.counter + 1 },
181
+ value: 'Click me',
182
+ }],
183
+ [BR],
184
+ [SPAN, { style: { color: 'red' } }, `${s.counter}`],
185
+ ]
186
+ );
187
+ </script>
188
+ </body>
189
+ </html>
190
+ ```
191
+
192
+ ### Classic
193
+ ```html
194
+ <!DOCTYPE html>
195
+ <html>
196
+ <head>
197
+ <title>Classic Script Example</title>
198
+ <script src="https://cdn.jsdelivr.net/npm/@ryupold/vode/dist/vode.min.js"></script>
199
+ </head>
200
+ <body>
201
+ <div id="app"></div>
202
+ <script>
203
+ const appNode = document.getElementById('app');
204
+ V.app(appNode, { counter: 0 },
205
+ (s) => ["DIV",
206
+ ["INPUT", {
207
+ type: 'button',
208
+ onclick: { counter: s.counter + 1 },
209
+ value: 'Click me',
210
+ }
211
+ ],
212
+ ["BR"],
213
+ ["SPAN", { style: { color: 'red' } }, `${s.counter}`],
214
+ ]);
215
+ </script>
216
+ </body>
217
+ </html>
218
+ ```
219
+
220
+ ### NPM
221
+
15
222
  ```bash
16
223
  # npm
17
224
  npm install @ryupold/vode --save
@@ -23,8 +230,6 @@ yarn add @ryupold/vode
23
230
  bun add @ryupold/vode
24
231
  ```
25
232
 
26
- ## Usage
27
-
28
233
  index.html
29
234
  ```html
30
235
  <html>
@@ -63,3 +268,14 @@ app<State>(appNode, init,
63
268
  ]
64
269
  );
65
270
  ```
271
+
272
+ ## Contributing
273
+
274
+ I was delighted by the simplicity of [hyperapp](https://github.com/jorgebucaran/hyperapp), which inspired me to create this library.
275
+
276
+ Not planning to add more features, just keeping it simple and easy.
277
+
278
+ But if you find bugs or have suggestions, feel free to open an [issue](https://github.com/ryupold/vode/issues) or a pull request.
279
+
280
+ ## License
281
+ [MIT](./LICENSE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ryupold/vode",
3
- "version": "0.13.2",
3
+ "version": "1.0.0",
4
4
  "description": "Small web framework for minimal websites",
5
5
  "author": "Michael Scherbakow (ryupold)",
6
6
  "license": "MIT",