Update sigpro2.js
This commit is contained in:
@@ -41,6 +41,13 @@ export const $e = (fn) => {
|
|||||||
return effect;
|
return effect;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const _storage = (key, initial, storage = localStorage) => {
|
||||||
|
const saved = storage.getItem(key);
|
||||||
|
const signal = $((saved !== null) ? JSON.parse(saved) : initial);
|
||||||
|
$e(() => storage.setItem(key, JSON.stringify(signal())));
|
||||||
|
return signal;
|
||||||
|
};
|
||||||
|
|
||||||
const h = (tag, props = {}, children = []) => {
|
const h = (tag, props = {}, children = []) => {
|
||||||
const el = document.createElement(tag);
|
const el = document.createElement(tag);
|
||||||
|
|
||||||
@@ -50,12 +57,11 @@ const h = (tag, props = {}, children = []) => {
|
|||||||
}
|
}
|
||||||
else if (key.startsWith('$')) {
|
else if (key.startsWith('$')) {
|
||||||
const attr = key.slice(1);
|
const attr = key.slice(1);
|
||||||
|
// Two-way binding
|
||||||
if ((attr === 'value' || attr === 'checked') && typeof val === 'function') {
|
if ((attr === 'value' || attr === 'checked') && typeof val === 'function') {
|
||||||
const ev = attr === 'checked' ? 'change' : 'input';
|
const ev = attr === 'checked' ? 'change' : 'input';
|
||||||
el.addEventListener(ev, e => val(attr === 'checked' ? e.target.checked : e.target.value));
|
el.addEventListener(ev, e => val(attr === 'checked' ? e.target.checked : e.target.value));
|
||||||
}
|
}
|
||||||
|
|
||||||
$e(() => {
|
$e(() => {
|
||||||
const v = typeof val === 'function' ? val() : val;
|
const v = typeof val === 'function' ? val() : val;
|
||||||
if (attr === 'value' || attr === 'checked') el[attr] = v;
|
if (attr === 'value' || attr === 'checked') el[attr] = v;
|
||||||
@@ -88,7 +94,27 @@ const h = (tag, props = {}, children = []) => {
|
|||||||
return el;
|
return el;
|
||||||
};
|
};
|
||||||
|
|
||||||
const tags = ['div', 'span', 'p', 'button', 'input', 'h1', 'h2', 'label', 'section', 'ul', 'li', 'a', 'header', 'footer', 'main', 'nav'];
|
export const _router = (routes) => {
|
||||||
|
const path = $(window.location.hash.replace(/^#/, "") || "/");
|
||||||
|
window.addEventListener("hashchange", () => path(window.location.hash.replace(/^#/, "") || "/"));
|
||||||
|
|
||||||
|
return _div({ class: "router-container" }, [
|
||||||
|
() => {
|
||||||
|
const current = path();
|
||||||
|
const route = routes.find(r => r.path === current) || routes.find(r => r.path === "*");
|
||||||
|
return route ? route.component() : _h1("404");
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const _render = (node, target = document.body) => {
|
||||||
|
target.innerHTML = '';
|
||||||
|
const element = typeof node === 'function' ? node() : node;
|
||||||
|
target.appendChild(element);
|
||||||
|
return element;
|
||||||
|
};
|
||||||
|
|
||||||
|
const tags = ['div', 'span', 'p', 'button', 'input', 'h1', 'h2', 'label', 'section', 'ul', 'li', 'a', 'header', 'footer'];
|
||||||
tags.forEach(tag => {
|
tags.forEach(tag => {
|
||||||
window[`_${tag}`] = (props, children) => {
|
window[`_${tag}`] = (props, children) => {
|
||||||
if (typeof props !== 'object' || props instanceof Node || Array.isArray(props)) {
|
if (typeof props !== 'object' || props instanceof Node || Array.isArray(props)) {
|
||||||
@@ -107,3 +133,7 @@ window.Col = (props, children) => _div({
|
|||||||
...((typeof props === 'object' && !Array.isArray(props)) ? props : {}),
|
...((typeof props === 'object' && !Array.isArray(props)) ? props : {}),
|
||||||
style: `display:flex; flex-direction:column; gap:10px; ${props?.style || ''}`
|
style: `display:flex; flex-direction:column; gap:10px; ${props?.style || ''}`
|
||||||
}, (Array.isArray(props) ? props : children));
|
}, (Array.isArray(props) ? props : children));
|
||||||
|
|
||||||
|
window._storage = _storage;
|
||||||
|
window._router = _router;
|
||||||
|
window._render = _render;
|
||||||
|
|||||||
Reference in New Issue
Block a user