From 5bb175c706cc5295d47088147bcdb2f91e8b74d7 Mon Sep 17 00:00:00 2001 From: Matt Edmonston Date: Sat, 28 Nov 2026 06:50:51 +0000 Subject: [PATCH] Add wui.Keyed for stable row identity in the browser Keyed(key, el) gives a child an identity the patching renderer matches on, so inserting at the front of a keyed list moves the existing rows and inserts one instead of re-patching every row. List items carry their Keyed child's key onto the
  • . Keyed is transparent everywhere else: no markup, Box reads Grow and MinWidth through it, a keyed Flex still stretches, and the TUI renders the child unchanged. The TUI focus and form-value walkers now recurse through Children for container types, so wrappers like Keyed need no case of their own. Closes #21 --- README.md | 11 ++++++++++- e2e/e2e_test.go | 16 ++++++++++++++++ e2e/probe/main.go | 10 +++++++++- element.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ render_tui.go | 40 +++++++++++----------------------------- responsive_test.go | 13 +++++++++++++ vdom.go | 11 ++++++++++- vdom_test.go | 16 ++++++++++++++++ 8 files changed, 131 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 39eccb8..2152f04 100644 --- a/README.md +++ b/README.md @@ -927,6 +927,16 @@ The `element.go`, `style.go`, `msg.go`, `program.go`, `key.go`, `cmd.go`, `statu **HTML**: every state change describes the new tree as lightweight virtual nodes and **patches** `#wui-root` in place. Children are matched by identity: controls with an ID (inputs, buttons, selects, checkboxes, text areas, links) find their old node wherever it moved, and other nodes reuse the next old sibling with the same tag. A node is replaced only when nothing matches, so a banner inserted above a form does not rebuild the inputs below it. Because the DOM nodes persist, browser-owned state survives updates without any bookkeeping: keyboard focus, caret and selection, scroll offsets, in-progress typing, and an open native dropdown. `Empty()` and false `If`/`When` branches render as invisible placeholders, so toggling one never shifts its siblings onto each other's nodes. +For lists whose rows have no ID'd controls, give each row a key with `wui.Keyed`. The renderer then moves existing rows and inserts only the new one, instead of re-patching every row from the insertion point down, and per-row browser state stays with its row: + +```go +wui.List(false, wui.Map(m.todos, func(_ int, t Todo) wui.Element { + return wui.Keyed(t.ID, row(t)) +})...) +``` + +`Keyed` adds no markup or layout, and the TUI renders the child unchanged. + Event listeners are attached once per node and swap their handler on each patch, so a re-render allocates no new `js.Func`s for unchanged elements. --- @@ -983,7 +993,6 @@ The WASM binary includes none of the charmbracelet packages. The TUI binary incl ## Limitations (v1) -- **No explicit keys** — the HTML renderer matches controls by their IDs and everything else by tag order. Rows without ID'd controls are matched in order, so inserting at the front of a long list patches every row rather than moving them. - **Button focus keys derive from labels by default** — two buttons with the same label and no `WithID` in one view share TUI focus; give repeated buttons IDs. - **No theme system** — style is applied per-element inline (`Style.Merge` helps compose variants); in the browser the base stylesheet's `--wui-*` custom properties, `WithStylesheet` and `Style.Class` are the global styling hooks. - **TextArea editing is basic in the TUI** — typing, Enter, and Backspace append and delete at the end of the buffer; there is no cursor movement within the text. The browser build uses a native `