A todo list with uiSession and a keyed list
A todo list whose keyed rows update in place: unchanged rows keep their DOM nodes.
Apache-2.0Runs on this page
Package: @awacloud/fw
Add, toggle and remove todos. The items live in one signal; a keyed
uiSession list reconciles the rows from it, so unchanged rows keep their
DOM nodes (focus, selection and transitions survive an update).
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
Any list that changes over time: logs, search results, carts, editors. The pattern is "state in a signal, DOM derived by one effect", with the list doing the minimal DOM work per change.
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.html,main.jsandstyle.css, then openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link. - Replace the two block templates and
dataFnwith your row shape.
What it shows
template.init(name, { to, main: true }), thenuiSession(name):uiSessionresolves to a factory that takes the context name.ui.parseblocks with#{var}bindings and a${rows}slot;ui.add([{ id, block, data }])for the layout.ui.list(blockId, slot, { keyFn, block, dataFn, onUpdate: 'patch' })andlist.sync(items), which returns the added / kept / updated / removed keys.ui.text(blockId, localId, value)for the counter,ui.on(blockId, localId, type, fn)for one delegated click listener.signal.create/update/effect: the state changes, the effect re-syncs.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { uiSession } from '@awacloud/fw/dom/rendering/uiSession.js';
import { template } from '@awacloud/fw/dom/rendering/template.js';
import { signal } from '@awacloud/fw/io/utils/signal.js';
fw.runtime.registerAllDeep([uiSession, template, signal]);
const SIGNAL = fw.runtime.resolve('signal');
// A template context, then a session on it. `uiSession` resolves to a factory.
fw.runtime.resolve('template').init('todo', { to: '#app', main: true });
const ui = fw.runtime.resolve('uiSession')('todo');
/** @typedef {{ id: string, text: string, done: boolean }} Todo */
/** The state: one signal holding the whole array. @type {import('@awacloud/fw/io/utils/signal.js').Signal<Todo[]>} */
export const todos = SIGNAL.create([
{ id: 't1', text: 'Read the contract', done: true },
{ id: 't2', text: 'Copy an example', done: false },
]);
// The layout block declares a `${rows}` slot; the row block is one `<li>`.
const layout = ui.parse('<section><ul id="items">${rows}</ul><p id="count">#{count}</p></section>');
const row = ui.parse('<li class="#{cls}" data-key="#{id}"><button data-action="toggle">#{action}</button>'
+ '<span>#{text}</span><button data-action="remove">Remove</button></li>');
ui.add([{ id: 'todo', block: layout, data: { count: '' } }]);
/** The keyed list: `sync(items)` adds, patches and removes rows by key. */
export const list = ui.list('todo', 'rows', {
keyFn: (/** @type {Todo} */ t) => t.id,
block: row,
dataFn: (/** @type {Todo} */ t) => ({
id: t.id, text: t.text, cls: t.done ? 'done' : '', action: t.done ? 'Undo' : 'Done',
}),
onUpdate: 'patch',
});
let seq = 0;
/** @param {string} text */
export function add(text) {
const trimmed = text.trim();
if (!trimmed) return;
todos.update((items) => [...items, { id: `n${++seq}`, text: trimmed, done: false }]);
}
/** @param {string} id */
export function toggle(id) {
todos.update((items) => items.map((t) => (t.id === id ? { ...t, done: !t.done } : t)));
}
/** @param {string} id */
export function remove(id) {
todos.update((items) => items.filter((t) => t.id !== id));
}
// One effect re-syncs the rows and the counter whenever the signal changes.
SIGNAL.effect(() => {
const items = todos.get();
list.sync(items);
ui.text('todo', 'count', `${items.filter((t) => !t.done).length} of ${items.length} left`);
});
// One delegated listener for every row, present or future.
ui.on('todo', 'items', 'click', (/** @type {Event} */ event) => {
const button = /** @type {HTMLElement} */ (event.target).closest('button');
const key = button?.closest('li')?.getAttribute('data-key');
if (!key) return;
if (button?.getAttribute('data-action') === 'remove') remove(key); else toggle(key);
});
const form = /** @type {HTMLFormElement} */ (document.getElementById('form'));
const input = /** @type {HTMLInputElement} */ (document.getElementById('new'));
form.addEventListener('submit', (event) => {
event.preventDefault();
add(input.value);
input.value = '';
});index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · A todo list with uiSession and a keyed list</title>
<link rel="stylesheet" href="../../_style/examples.css">
<link rel="stylesheet" href="./style.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>A todo list with uiSession and a keyed list</h1>
<form id="form">
<label for="new">New todo</label>
<input id="new" type="text" autocomplete="off" required>
<button type="submit">Add</button>
</form>
<div id="app"></div>
</main>
</body>
</html>style.css
/* SPDX-License-Identifier: Apache-2.0 */
li.done span { text-decoration: line-through; opacity: 0.6; }
li { display: flex; gap: 0.5rem; align-items: center; margin-block: 0.25rem; }