lume-js 0.1.1 → 0.2.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.
- package/package.json +1 -1
- package/src/core/bindDom.js +50 -0
- package/src/core/state.js +32 -27
- package/src/core/utils.js +14 -0
- package/src/index.js +13 -1
package/package.json
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lume-JS Zero-runtime DOM binding
|
|
3
|
+
*
|
|
4
|
+
* Binds reactive state to DOM elements using [data-bind].
|
|
5
|
+
* Supports two-way binding for INPUT/TEXTAREA.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { bindDom } from "lume-js";
|
|
9
|
+
* bindDom(document.body, store);
|
|
10
|
+
*
|
|
11
|
+
* HTML:
|
|
12
|
+
* <span data-bind="count"></span>
|
|
13
|
+
* <input data-bind="user.name">
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { resolvePath } from "./utils.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Zero-runtime DOM binding for a reactive store
|
|
20
|
+
*
|
|
21
|
+
* @param {HTMLElement} root - root element to scan for [data-bind]
|
|
22
|
+
* @param {object} store - reactive state object
|
|
23
|
+
*/
|
|
24
|
+
export function bindDom(root, store) {
|
|
25
|
+
const nodes = root.querySelectorAll("[data-bind]");
|
|
26
|
+
|
|
27
|
+
nodes.forEach(el => {
|
|
28
|
+
const pathArr = el.getAttribute("data-bind").split(".");
|
|
29
|
+
const lastKey = pathArr.pop();
|
|
30
|
+
|
|
31
|
+
let target;
|
|
32
|
+
try {
|
|
33
|
+
target = resolvePath(store, pathArr); // must be wrapped with state() if nested
|
|
34
|
+
} catch (err) {
|
|
35
|
+
console.warn(`Skipping binding for ${el}: ${err.message}`);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Subscribe once
|
|
40
|
+
target.$subscribe(lastKey, val => {
|
|
41
|
+
if (el.tagName === "INPUT" || el.tagName === "TEXTAREA") el.value = val;
|
|
42
|
+
else el.textContent = val;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// 2-way binding for inputs
|
|
46
|
+
if (el.tagName === "INPUT" || el.tagName === "TEXTAREA") {
|
|
47
|
+
el.addEventListener("input", e => target[lastKey] = e.target.value);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
package/src/core/state.js
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lume-JS Reactive State Core
|
|
3
|
+
*
|
|
4
|
+
* Provides minimal, zero-runtime reactive state.
|
|
5
|
+
*
|
|
6
|
+
* Features:
|
|
7
|
+
* - Lightweight and Go-style
|
|
8
|
+
* - Explicit nested states
|
|
9
|
+
* - $subscribe for listening to key changes
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* import { state } from "lume-js";
|
|
13
|
+
* const store = state({ count: 0 });
|
|
14
|
+
* store.$subscribe("count", val => console.log(val));
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Creates a reactive state object.
|
|
20
|
+
*
|
|
21
|
+
* @param {Object} obj - Initial state object
|
|
22
|
+
* @returns {Proxy} Reactive proxy with $subscribe method
|
|
23
|
+
*/
|
|
1
24
|
export function state(obj) {
|
|
2
25
|
const listeners = {};
|
|
3
26
|
|
|
27
|
+
// Notify subscribers of a key
|
|
4
28
|
function notify(key, val) {
|
|
5
29
|
if (listeners[key]) listeners[key].forEach(fn => fn(val));
|
|
6
30
|
}
|
|
@@ -16,37 +40,18 @@ export function state(obj) {
|
|
|
16
40
|
}
|
|
17
41
|
});
|
|
18
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Subscribe to changes for a specific key.
|
|
45
|
+
* Calls the callback immediately with the current value.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} key
|
|
48
|
+
* @param {function} fn
|
|
49
|
+
*/
|
|
19
50
|
proxy.$subscribe = (key, fn) => {
|
|
20
51
|
if (!listeners[key]) listeners[key] = [];
|
|
21
52
|
listeners[key].push(fn);
|
|
22
|
-
// initialize
|
|
23
|
-
fn(proxy[key]);
|
|
53
|
+
fn(proxy[key]); // initialize
|
|
24
54
|
};
|
|
25
55
|
|
|
26
56
|
return proxy;
|
|
27
57
|
}
|
|
28
|
-
|
|
29
|
-
// Zero-runtime data-bind init
|
|
30
|
-
export function bindDom(root, store) {
|
|
31
|
-
const nodes = root.querySelectorAll("[data-bind]");
|
|
32
|
-
nodes.forEach(el => {
|
|
33
|
-
const path = el.getAttribute("data-bind").split(".");
|
|
34
|
-
const lastKey = path.pop();
|
|
35
|
-
|
|
36
|
-
let target = store;
|
|
37
|
-
for (const key of path) {
|
|
38
|
-
target = target[key]; // must be wrapped with state()
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// subscribe once
|
|
42
|
-
target.$subscribe(lastKey, val => {
|
|
43
|
-
if (el.tagName === "INPUT" || el.tagName === "TEXTAREA") el.value = val;
|
|
44
|
-
else el.textContent = val;
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// 2-way binding for inputs
|
|
48
|
-
if (el.tagName === "INPUT" || el.tagName === "TEXTAREA") {
|
|
49
|
-
el.addEventListener("input", e => target[lastKey] = e.target.value);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve a nested path in an object.
|
|
3
|
+
* Example: path "user.name" returns obj.user.name
|
|
4
|
+
*
|
|
5
|
+
* @param {object} obj - The root object
|
|
6
|
+
* @param {string[]} pathArr - Array of keys
|
|
7
|
+
* @returns {object} Last object in the path
|
|
8
|
+
*/
|
|
9
|
+
export function resolvePath(obj, pathArr) {
|
|
10
|
+
return pathArr.reduce((acc, key) => {
|
|
11
|
+
if (!acc) throw new Error(`Invalid path: ${pathArr.join(".")}`);
|
|
12
|
+
return acc[key];
|
|
13
|
+
}, obj);
|
|
14
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Lume-JS Library Core API
|
|
3
|
+
*
|
|
4
|
+
* Exposes:
|
|
5
|
+
* - state(): create reactive state
|
|
6
|
+
* - bindDom(): zero-runtime DOM binding
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* import { state, bindDom } from "lume-js";
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export { state } from "./core/state.js";
|
|
13
|
+
export { bindDom } from "./core/bindDom.js";
|