@nuvo-js/core 1.1.16 → 1.1.17
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/README.md +6 -548
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,552 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
**Nuvo** is a lightweight, TypeScript-first UI library built around native DOM APIs.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
<p >
|
|
6
|
+
<a href="https://datewise-khaki.vercel.app">Live Demo</a>
|
|
7
|
+
</p>
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
- **TypeScript support** — Generic element types and public type definitions.
|
|
11
|
-
- **HTML elements** — Create, configure, and update native HTML elements.
|
|
12
|
-
- **SVG elements** — Build SVG graphics using the same class-based approach.
|
|
13
|
-
- **Event management** — Register and clean up native DOM event listeners.
|
|
14
|
-
- **Lifecycle callbacks** — Respond to element mount and unmount events.
|
|
15
|
-
- **Reactive Store** — Manage application state with subscriptions and effects.
|
|
16
|
-
- **DOM diffing** — Efficiently update existing child nodes.
|
|
17
|
-
- **Keyed diffing** — Reuse, insert, remove, and reorder elements using keys.
|
|
18
|
-
- **No Virtual DOM** — Reconcile actual DOM nodes.
|
|
19
|
-
- **No JSX required** — Build interfaces with standard JavaScript and TypeScript.
|
|
20
|
-
|
|
21
|
-
## Demo
|
|
22
|
-
|
|
23
|
-
🌐 Live demo: https://example-ten-opal.vercel.app
|
|
24
|
-
|
|
25
|
-
## Installation
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
npm install @nuvo-js/core
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Usage
|
|
32
|
-
|
|
33
|
-
### ECMAScript modules
|
|
34
|
-
|
|
35
|
-
Import `createRoot` from the package and mount your application to a DOM element.
|
|
36
|
-
|
|
37
|
-
**main.ts**
|
|
38
|
-
|
|
39
|
-
```typescript
|
|
40
|
-
import { createRoot } from '@nuvo-js/core';
|
|
41
|
-
import App from './App';
|
|
42
|
-
|
|
43
|
-
const root = document.getElementById('app');
|
|
44
|
-
|
|
45
|
-
if (root) {
|
|
46
|
-
createRoot(root, App());
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
**index.html**
|
|
51
|
-
|
|
52
|
-
```html
|
|
53
|
-
<div id="app"></div>
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
### Browser global
|
|
57
|
-
|
|
58
|
-
Nuvo is also accessible through the browser global namespace:
|
|
59
|
-
|
|
60
|
-
```javascript
|
|
61
|
-
const nuvojs = window['nuvo-js'];
|
|
62
|
-
|
|
63
|
-
console.log(nuvojs);
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## HTML Elements
|
|
67
|
-
|
|
68
|
-
Create native HTML elements using the `Element` class. The constructor accepts the HTML tag name, a props object, and an optional parent element.
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
import { Element } from '@nuvo-js/core';
|
|
72
|
-
|
|
73
|
-
const element = new Element<HTMLDivElement>(
|
|
74
|
-
'div',
|
|
75
|
-
{
|
|
76
|
-
className: 'row',
|
|
77
|
-
children: [
|
|
78
|
-
'I am a div element.'
|
|
79
|
-
]
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
The `.dom` property provides access to the underlying native DOM element.
|
|
85
|
-
|
|
86
|
-
You can also pass a parent as the third constructor argument to append the element immediately:
|
|
87
|
-
|
|
88
|
-
```typescript
|
|
89
|
-
const element = new Element<HTMLDivElement>(
|
|
90
|
-
'div',
|
|
91
|
-
{
|
|
92
|
-
className: 'container',
|
|
93
|
-
children: ['Hello Nuvo']
|
|
94
|
-
},
|
|
95
|
-
document.body
|
|
96
|
-
);
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
### TypeScript generics
|
|
100
|
-
|
|
101
|
-
Specify the native element type with a generic to get the corresponding DOM API and type information:
|
|
102
|
-
|
|
103
|
-
```typescript
|
|
104
|
-
const button = new Element<HTMLButtonElement>(
|
|
105
|
-
'button',
|
|
106
|
-
{
|
|
107
|
-
children: ['Click me']
|
|
108
|
-
}
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
const input = new Element<HTMLInputElement>(
|
|
112
|
-
'input',
|
|
113
|
-
{
|
|
114
|
-
type: 'text',
|
|
115
|
-
placeholder: 'Enter your name'
|
|
116
|
-
}
|
|
117
|
-
);
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
### Children
|
|
121
|
-
|
|
122
|
-
The `children` prop must be an array. It can contain text and DOM nodes created by Nuvo or other native DOM APIs.
|
|
123
|
-
|
|
124
|
-
```typescript
|
|
125
|
-
const button = new Element<HTMLButtonElement>(
|
|
126
|
-
'button',
|
|
127
|
-
{
|
|
128
|
-
children: ['Click me']
|
|
129
|
-
}
|
|
130
|
-
);
|
|
131
|
-
|
|
132
|
-
const container = new Element<HTMLDivElement>(
|
|
133
|
-
'div',
|
|
134
|
-
{
|
|
135
|
-
className: 'container',
|
|
136
|
-
children: [
|
|
137
|
-
'A button: ',
|
|
138
|
-
button.dom
|
|
139
|
-
]
|
|
140
|
-
}
|
|
141
|
-
);
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
### Updating properties
|
|
145
|
-
|
|
146
|
-
Use `SetProps()` to update an existing element:
|
|
147
|
-
|
|
148
|
-
```typescript
|
|
149
|
-
const div = new Element<HTMLDivElement>(
|
|
150
|
-
'div',
|
|
151
|
-
{
|
|
152
|
-
className: 'container'
|
|
153
|
-
}
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
div.SetProps({
|
|
157
|
-
className: 'container active',
|
|
158
|
-
title: 'Updated container'
|
|
159
|
-
});
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
`SetProps()` updates the existing DOM element rather than creating a new instance.
|
|
163
|
-
|
|
164
|
-
You can also update children. Nuvo reconciles the new child array with the existing DOM children:
|
|
165
|
-
|
|
166
|
-
```typescript
|
|
167
|
-
div.SetProps({
|
|
168
|
-
children: [
|
|
169
|
-
'Updated content'
|
|
170
|
-
]
|
|
171
|
-
});
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
Pass `true` as the second argument to force the child update:
|
|
175
|
-
|
|
176
|
-
```typescript
|
|
177
|
-
div.SetProps(
|
|
178
|
-
{
|
|
179
|
-
children: ['Fresh content']
|
|
180
|
-
},
|
|
181
|
-
true
|
|
182
|
-
);
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
### Events
|
|
186
|
-
|
|
187
|
-
Pass native event listeners through the `events` prop. Event names use the `on` prefix:
|
|
188
|
-
|
|
189
|
-
```typescript
|
|
190
|
-
const button = new Element<HTMLButtonElement>(
|
|
191
|
-
'button',
|
|
192
|
-
{
|
|
193
|
-
children: ['Click me'],
|
|
194
|
-
events: {
|
|
195
|
-
onClick: () => {
|
|
196
|
-
console.log('Button clicked');
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
);
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
Use `SetEvents()` to replace the element's current event listeners:
|
|
204
|
-
|
|
205
|
-
```typescript
|
|
206
|
-
button.SetEvents({
|
|
207
|
-
onClick: () => {
|
|
208
|
-
console.log('Updated click handler');
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
Nuvo manages listeners with an `AbortController`. Calling `SetEvents()` aborts the previous listeners before registering the new ones. Calling `Destroy()` also aborts the element's listeners.
|
|
214
|
-
|
|
215
|
-
### Mount and unmount lifecycle
|
|
216
|
-
|
|
217
|
-
Use `OnMount()` to run a callback when the element is connected to the document:
|
|
218
|
-
|
|
219
|
-
```typescript
|
|
220
|
-
div.OnMount((element) => {
|
|
221
|
-
console.log('Mounted:', element.dom);
|
|
222
|
-
});
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
Use `OnUnMount()` to respond when the element is disconnected. The callback receives the Nuvo element instance; after the callback, Nuvo destroys the element and its managed event listeners.
|
|
226
|
-
|
|
227
|
-
```typescript
|
|
228
|
-
div.OnUnMount((element) => {
|
|
229
|
-
console.log('Unmounted:', element.dom);
|
|
230
|
-
});
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
### Removing and destroying elements
|
|
234
|
-
|
|
235
|
-
`Remove()` removes the DOM node from its parent.
|
|
236
|
-
|
|
237
|
-
```typescript
|
|
238
|
-
div.Remove();
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
`Destroy()` aborts the element's managed event listeners and removes the DOM node.
|
|
242
|
-
|
|
243
|
-
```typescript
|
|
244
|
-
div.Destroy();
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
## SVG Elements
|
|
248
|
-
|
|
249
|
-
Nuvo provides an `SVGElement` class for creating native SVG elements. It uses the SVG namespace and exposes the underlying SVG node through `.dom`.
|
|
250
|
-
|
|
251
|
-
```typescript
|
|
252
|
-
import { SVGElement } from '@nuvo-js/core';
|
|
253
|
-
|
|
254
|
-
const circle = new SVGElement<SVGCircleElement>(
|
|
255
|
-
'circle',
|
|
256
|
-
{
|
|
257
|
-
cx: 12,
|
|
258
|
-
cy: 12,
|
|
259
|
-
r: 10,
|
|
260
|
-
fill: 'none',
|
|
261
|
-
stroke: 'currentColor',
|
|
262
|
-
strokeWidth: 2
|
|
263
|
-
},
|
|
264
|
-
document.body
|
|
265
|
-
);
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
### Example: reusable trash icon
|
|
269
|
-
|
|
270
|
-
You can compose an SVG element with child SVG DOM nodes:
|
|
271
|
-
|
|
272
|
-
```typescript
|
|
273
|
-
import { SVGElement } from '@nuvo-js/core';
|
|
274
|
-
import type { SVGElementPropsType } from '@nuvo-js/core/types';
|
|
275
|
-
|
|
276
|
-
function Trash(props: SVGElementPropsType) {
|
|
277
|
-
return (
|
|
278
|
-
new SVGElement('svg', {
|
|
279
|
-
xmlns: 'http://www.w3.org/2000/svg',
|
|
280
|
-
width: 24,
|
|
281
|
-
height: 24,
|
|
282
|
-
viewBox: '0 0 24 24',
|
|
283
|
-
fill: 'none',
|
|
284
|
-
stroke: 'currentColor',
|
|
285
|
-
strokeWidth: 4,
|
|
286
|
-
strokeLinecap: 'round',
|
|
287
|
-
strokeLinejoin: 'round',
|
|
288
|
-
...props,
|
|
289
|
-
children: [
|
|
290
|
-
new SVGElement<SVGPolylineElement>('polyline',
|
|
291
|
-
{
|
|
292
|
-
points: '3 6 5 6 21 6' as SVGPointList & string,
|
|
293
|
-
strokeWidth: 4,
|
|
294
|
-
}).dom,
|
|
295
|
-
|
|
296
|
-
new SVGElement<SVGPathElement>('path',
|
|
297
|
-
{
|
|
298
|
-
strokeWidth: 4,
|
|
299
|
-
d: 'M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2',
|
|
300
|
-
}).dom
|
|
301
|
-
],
|
|
302
|
-
}).dom
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
export default Trash;
|
|
307
|
-
|
|
308
|
-
```
|
|
309
|
-
|
|
310
|
-
The `SVGElement` constructor accepts the SVG tag name, a props object, and a parent element. SVG attributes are applied to the native SVG node, and `.dom` exposes that node.
|
|
311
|
-
|
|
312
|
-
## Fragment
|
|
313
|
-
|
|
314
|
-
Nuvo provides `Fragment` for grouping multiple elements without introducing an additional wrapper element into the DOM.
|
|
315
|
-
|
|
316
|
-
```typescript
|
|
317
|
-
import { Element, Fragment } from '@nuvo-js/core';
|
|
318
|
-
|
|
319
|
-
function App() {
|
|
320
|
-
return Fragment([
|
|
321
|
-
new Element<HTMLDivElement>(
|
|
322
|
-
'div',
|
|
323
|
-
{
|
|
324
|
-
children: ['First element']
|
|
325
|
-
}
|
|
326
|
-
).dom,
|
|
327
|
-
|
|
328
|
-
new Element<HTMLDivElement>(
|
|
329
|
-
'div',
|
|
330
|
-
{
|
|
331
|
-
children: ['Second element']
|
|
332
|
-
}
|
|
333
|
-
).dom
|
|
334
|
-
]);
|
|
335
|
-
}
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
Fragments are useful for composing UI structures while keeping the resulting DOM hierarchy clean.
|
|
339
|
-
|
|
340
|
-
## Store
|
|
341
|
-
|
|
342
|
-
Nuvo includes a reactive `Store` class for application state management.
|
|
343
|
-
|
|
344
|
-
### Creating a store
|
|
345
|
-
|
|
346
|
-
```typescript
|
|
347
|
-
import { Store } from '@nuvo-js/core';
|
|
348
|
-
|
|
349
|
-
const store = new Store<number[]>([]);
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
### Get state
|
|
353
|
-
|
|
354
|
-
Retrieve the current state with `GetState()`:
|
|
355
|
-
|
|
356
|
-
```typescript
|
|
357
|
-
const state = store.GetState();
|
|
358
|
-
|
|
359
|
-
console.log(state);
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
### Set state
|
|
363
|
-
|
|
364
|
-
Update the state by providing a new value:
|
|
365
|
-
|
|
366
|
-
```typescript
|
|
367
|
-
store.SetState([1, 2, 3]);
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
You can also use a callback to update the previous state:
|
|
371
|
-
|
|
372
|
-
```typescript
|
|
373
|
-
store.SetState(prev => [
|
|
374
|
-
...prev,
|
|
375
|
-
4
|
|
376
|
-
]);
|
|
377
|
-
```
|
|
378
|
-
|
|
379
|
-
### Subscribe
|
|
380
|
-
|
|
381
|
-
Use `Subscribe()` to listen for state changes:
|
|
382
|
-
|
|
383
|
-
```typescript
|
|
384
|
-
store.Subscribe(() => {
|
|
385
|
-
console.log('Store updated!');
|
|
386
|
-
});
|
|
387
|
-
```
|
|
388
|
-
|
|
389
|
-
### Effect
|
|
390
|
-
|
|
391
|
-
`Effect()` executes its callback immediately when registered and again whenever the store state changes:
|
|
392
|
-
|
|
393
|
-
```typescript
|
|
394
|
-
store.Effect((state) => {
|
|
395
|
-
console.log('Current state:', state);
|
|
396
|
-
});
|
|
397
|
-
```
|
|
398
|
-
|
|
399
|
-
### Store API reference
|
|
400
|
-
|
|
401
|
-
| Method | Description |
|
|
402
|
-
|---|---|
|
|
403
|
-
| `GetState()` | Returns the current state. |
|
|
404
|
-
| `SetState(value)` | Updates the state with a new value. |
|
|
405
|
-
| `SetState(callback)` | Updates the state using the previous value. |
|
|
406
|
-
| `Subscribe(callback)` | Listens for state changes. |
|
|
407
|
-
| `Effect(callback)` | Executes immediately and listens for subsequent state changes. |
|
|
408
|
-
|
|
409
|
-
## DOM Diffing
|
|
410
|
-
|
|
411
|
-
Nuvo updates existing child nodes rather than unnecessarily replacing the entire DOM structure.
|
|
412
|
-
|
|
413
|
-
When you update an element's `children` through `SetProps()`, Nuvo compares the existing children with the new children.
|
|
414
|
-
|
|
415
|
-
Nuvo supports two reconciliation strategies:
|
|
416
|
-
|
|
417
|
-
- **Unkeyed diffing** — Compares children by their position in the DOM.
|
|
418
|
-
- **Keyed diffing** — Identifies children using unique keys and reconciles them independently of their position.
|
|
419
|
-
|
|
420
|
-
### Unkeyed diffing
|
|
421
|
-
|
|
422
|
-
For collections without keys, Nuvo compares elements by their position:
|
|
423
|
-
|
|
424
|
-
```typescript
|
|
425
|
-
const list = new Element<HTMLUListElement>(
|
|
426
|
-
'ul',
|
|
427
|
-
{
|
|
428
|
-
children: [
|
|
429
|
-
new Element<HTMLLIElement>(
|
|
430
|
-
'li',
|
|
431
|
-
{
|
|
432
|
-
children: ['Item 1']
|
|
433
|
-
}
|
|
434
|
-
).dom,
|
|
435
|
-
|
|
436
|
-
new Element<HTMLLIElement>(
|
|
437
|
-
'li',
|
|
438
|
-
{
|
|
439
|
-
children: ['Item 2']
|
|
440
|
-
}
|
|
441
|
-
).dom
|
|
442
|
-
]
|
|
443
|
-
}
|
|
444
|
-
);
|
|
445
|
-
```
|
|
446
|
-
|
|
447
|
-
### Keyed diffing
|
|
448
|
-
|
|
449
|
-
When rendering dynamic collections, assign a unique `key` to each element:
|
|
450
|
-
|
|
451
|
-
```typescript
|
|
452
|
-
const item = new Element<HTMLLIElement>(
|
|
453
|
-
'li',
|
|
454
|
-
{
|
|
455
|
-
key: 'todo-1',
|
|
456
|
-
children: ['Learn Nuvo']
|
|
457
|
-
}
|
|
458
|
-
);
|
|
459
|
-
```
|
|
460
|
-
|
|
461
|
-
Nuvo uses keys to identify and reconcile existing DOM elements.
|
|
462
|
-
|
|
463
|
-
The keyed diffing process:
|
|
464
|
-
|
|
465
|
-
- **Reuse** — Reuses existing DOM elements with matching keys.
|
|
466
|
-
- **Remove** — Removes elements whose keys no longer exist.
|
|
467
|
-
- **Insert** — Inserts newly created elements.
|
|
468
|
-
- **Reorder** — Moves existing DOM elements when their order changes.
|
|
469
|
-
|
|
470
|
-
### Example: reactive todo list
|
|
471
|
-
|
|
472
|
-
```typescript
|
|
473
|
-
import { Element, Store } from '@nuvo-js/core';
|
|
474
|
-
|
|
475
|
-
interface Todo {
|
|
476
|
-
id: string;
|
|
477
|
-
title: string;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
const todos = new Store<Todo[]>([]);
|
|
481
|
-
|
|
482
|
-
const list = new Element<HTMLOListElement>(
|
|
483
|
-
'ol',
|
|
484
|
-
{
|
|
485
|
-
children: []
|
|
486
|
-
}
|
|
487
|
-
);
|
|
488
|
-
|
|
489
|
-
todos.Effect((state) => {
|
|
490
|
-
list.SetProps({
|
|
491
|
-
children: state.map(todo =>
|
|
492
|
-
new Element<HTMLLIElement>(
|
|
493
|
-
'li',
|
|
494
|
-
{
|
|
495
|
-
key: todo.id,
|
|
496
|
-
children: [todo.title]
|
|
497
|
-
}
|
|
498
|
-
).dom
|
|
499
|
-
)
|
|
500
|
-
});
|
|
501
|
-
});
|
|
502
|
-
|
|
503
|
-
// Add a todo
|
|
504
|
-
todos.SetState(prev => [
|
|
505
|
-
...prev,
|
|
506
|
-
{
|
|
507
|
-
id: crypto.randomUUID(),
|
|
508
|
-
title: 'Learn Nuvo'
|
|
509
|
-
}
|
|
510
|
-
]);
|
|
511
|
-
|
|
512
|
-
// Remove a todo
|
|
513
|
-
todos.SetState(prev =>
|
|
514
|
-
prev.filter(todo => todo.id !== 'some-id')
|
|
515
|
-
);
|
|
516
|
-
```
|
|
517
|
-
|
|
518
|
-
### Key best practices
|
|
519
|
-
|
|
520
|
-
- Always use unique keys among sibling elements.
|
|
521
|
-
- Prefer stable identifiers such as database IDs or UUIDs.
|
|
522
|
-
- Avoid array indexes as keys when elements can be reordered or removed.
|
|
523
|
-
- Use consistent keys when rendering dynamic collections.
|
|
524
|
-
- When using keyed diffing, provide keys for all sibling elements.
|
|
525
|
-
|
|
526
|
-
## TypeScript Support
|
|
527
|
-
|
|
528
|
-
Nuvo Core is written in TypeScript and provides public type definitions.
|
|
529
|
-
|
|
530
|
-
Import types directly from the package:
|
|
531
|
-
|
|
532
|
-
```typescript
|
|
533
|
-
import type {
|
|
534
|
-
ElementPropsType,
|
|
535
|
-
SVGElementPropsType
|
|
536
|
-
} from '@nuvo-js/core/types';
|
|
537
|
-
```
|
|
538
|
-
|
|
539
|
-
## Philosophy
|
|
540
|
-
|
|
541
|
-
Nuvo focuses on simplicity, control, and native browser capabilities.
|
|
542
|
-
|
|
543
|
-
Rather than introducing another abstraction layer between developers and the DOM, Nuvo provides tools to work directly with the platform.
|
|
544
|
-
|
|
545
|
-
Inspired by the Italian word *nuovo* and the French word *nouveau*, both meaning new, fresh, or recently developed, the name reflects Nuvo's vision of bringing a fresh, simple, and modern approach to building web interfaces.
|
|
546
|
-
|
|
547
|
-
Build with JavaScript. Control the DOM. Keep things simple.
|
|
548
|
-
|
|
549
|
-
**Nuvo — A fresh approach to the DOM.**
|
|
550
|
-
|
|
551
|
-
## License
|
|
552
|
-
|
|
553
|
-
MIT
|
|
9
|
+
<p>
|
|
10
|
+
<a href="https://github.com/Gagik-arch/nuvo-js/docs/README.md">Read documentation</a>
|
|
11
|
+
</p>
|