Welcome to the Deleight Tutorials repository! 🚀
This repo contains step-by-step guides and practical examples to help you understand and master Deleight.
This example demonstrates reactive state management using Deleight.
import { hh } from "deleight/dom/builder";
import { sets } from "deleight/object/shared";
const counterState = { count: 0 };
const counterElement = hh.div(counterState.count).build();
const reactiveObject = {
count: [counterState],
textContent: [counterElement]
};
const incrementButton = hh.button('Increment').assign({
onclick() { sets(reactiveObject, counterState.count + 1) }
}).build();
const decrementButton = hh.button('Decrement').assign({
onclick() { sets(reactiveObject, counterState.count - 1) }
}).build();
document.body.append(decrementButton, counterElement, incrementButton);
counterState
stores the count value.reactiveObject
binds the count
property to both state and DOM.sets
updates both the state and the DOM automatically.incrementButton
and decrementButton
modify the state when clicked.import { hh } from "deleight/dom/builder";
import { ElementList } from "deleight/lists";
const todos: [];
const listElement = hh.ul().build();
const todoElementList = new ElementList(listElement, 1, item => hh.li(item).build());
const reactiveObject = [todos, todoElementList]
const addTodo = (text) => {
calls({ push: reactiveObject })
};
const input = hh.input().build();
const button = hh.button("Add Todo").assign({
onclick() { addTodo(input.value); input.value = ''; }
}).build();
document.body.append(input, button, listElement);
state.todos
array stores tasks.sets
updates the DOM when items are added.import { hh } from "deleight/dom/builder";
import { sets } from "deleight/object/shared";
const state = { name: '' };
const input = hh.input().assign({
oninput(event) { state.name = event.target.value; }
}).build();
const display = hh.div(state.name).build();
const reactiveState = { textContent: [display], name: [state] };
document.body.append(input, display);
sets(state, value)
updates both state and DOM.import { hh } from "deleight/dom/builder";
import { sets } from "deleight/object/shared";
const state = { visible: true };
const message = hh.div("Hello, Deleight!").build();
const toggleButton = hh.button("Toggle Message").assign({
onclick() { sets({
visible: [state, (value) => message.classList.toggle('visible') ]
}, !state.visible);
}
}).build();
// This pattern is not normal in deleight. I recommend going directly and just toggling a class in the event handler instead of matching the visibility state with the value of a property.
document.body.append(toggleButton, message);
hidden
property.import { hh } from "deleight/dom/builder";
import { sets } from "deleight/object/shared";
import { ElementList } from "deleight/lists/element";
const state = { users: [] };
const listElement = hh.ul().build();
const apiElementList = new ElementList(listElement, 1, (user) => hh.li(user.name).build());
fetch("https://jsonplaceholder.typicode.com/users")
.then(res => res.json())
.then(data => apiElementList.push(...data));
document.body.append(listElement);
import { hh } from "deleight/dom/builder";
const content = hh.div("Hello from the server!");
console.log(content.render());
This outputs:
<div>Hello from the server!</div>
import { hh } from "deleight/dom/builder";
import { sets } from "deleight/object/shared";
const routes = {
"#home": "Welcome to Home!",
"#about": "About Us",
"#contact": "Contact Us"
};
const state = { page: routes[location.hash] || "404 Not Found" };
const content = hh.div(state.page).build();
window.onhashchange = () => content.textContent = routes[location.hash] || "404 Not Found";
document.body.append(content);
onhashchange
).