Examples
Practical examples. Live demos.
Pick an example to open it full screen, with the live preview and its source side by side.
Pick an example to open it full screen, with the live preview and its source side by side.
Use list() for repeated items. Call update() after each mutation.
import 'nuclo' interface Todo { id: number text: string done: boolean } let todos: Todo[] = [] let filter: "all" | "active" | "done" = "all" let text = "" let nextId = 1 function visible() { if (filter === "active") return todos.filter((t) => !t.done) if (filter === "done") return todos.filter((t) => t.done) return todos } function addTodo() { if (!text.trim()) return todos.push({ id: nextId++, text, done: false }) text = "" update() } // A filter button: pressed while its filter is the active one function FilterButton(label: string, next: typeof filter) { return button(label, { "aria-pressed": () => String(filter === next), style: () => ({ fontWeight: filter === next ? "700" : "400" }), onClick: () => { filter = next; update() }, }) } export function TodoList() { return div( input( // value: () => text keeps the input in sync, so Add can clear it { placeholder: "Add a task...", value: () => text }, { onInput: (event) => { text = (event.target as HTMLInputElement).value }, onKeyDown: (event) => { if (event.key === "Enter") addTodo() }, }, ), button("Add", { onClick: addTodo }), div( FilterButton("All", "all"), FilterButton("Active", "active"), FilterButton("Done", "done"), ), ul( list( () => visible(), (todo) => li( input( { type: "checkbox", checked: () => todo.done }, { onChange: () => { todo.done = !todo.done; update() } }, ), span(todo.text, { style: () => ({ textDecoration: todo.done ? "line-through" : "none", opacity: todo.done ? "0.6" : "1", }), }), button("✕", { "aria-label": "Delete", onClick: () => { todos = todos.filter((item) => item.id !== todo.id) update() }, }), ), ), when(() => visible().length === 0, div("No tasks yet.")), ), p(() => { const left = todos.filter((t) => !t.done).length return `${left} of ${todos.length} remaining` }), ) }