-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
# Introduction
ef.js is a JavaScript library that provides state management functionality and declarative DOM functions based on that.
This library is designed and built only for my personal needs.
Unlike the majority of UI library, ef.js does not abstract DOM.
It is designed to be simple and thin so it can be applied to anywhere standard DOM is expected and can cooperate with other code and libraries.
Therefore, this is not for whom just trying to build a web app without understanding the web platform.
## State management
The state management feature is designed based on Signals, a design approach for reactivity, popularised by [S.js](https://github.com/adamhaile/S) and [Solid.js](https://www.solidjs.com/).
The API surface is kept bare minimum, so it is recommended to define helper function for each project if needed.
```ts
import { derived, effect, signal } from "@pocka/ef";
// Create a reactive value
const $number = signal(1);
// Define a derived value
const $isEven = derived(() => {
return $number.get() % 2 === 0;
});
// Run a function and re-run when `$isEven` was changed
effect(() => {
console.log($isEven.get());
});
// [LOG]: false
// Update a reactive value
$number.set(2);
// [LOG]: true
```
## Declarative DOM
The DOM part is quite simple ― it just creates DOM Nodes.
"Declarative" kicks in only when it takes Signals value, as it subscribes to value changes then update necessary part on changes.
This is very similar to what Solid.js or similar libraries do, but without a special compiler or Virtual DOM.
Of course, lack of those also means inferior DX and/or missing optimisation opportunities.
Be advised to choose with consideration.
```ts
import { el, prop, signal } from "@pocka/ef";
const $sent = signal(false);
// `myButton` is HTMLButtonElement
const myButton = el("button", [prop("disabled", $sent)], ["Send data"]);
document.appendChild(myButton);
$sent.set(true);
// myButton is now disabled
// ...internal of `prop` is something like this
function prop(name, value) {
return (element) => {
if (isReactive(value)) {
effect(() => {
element[name] = value.get();
});
return;
}
element[name] = value;
};
}
```