# oneui Go docs, all pages (oneui-go 1.0.1)
Every page of https://recursiveai.org/oneui/go/docs/ as markdown, getting started first. Index: https://recursiveai.org/oneui/go/llms.txt
# Getting started with oneui for Go
Install oneui for Go, write a component, compile it with `go generate`, render it to HTML, test it without a browser, build a list with a two-way binding, read a template diagnostic, and run the component in a web page.
oneui for Go is a component model: a component is an HTML template, optional scoped CSS and a Go struct, and `oneui generate` compiles it into plain Go that updates only the nodes bound to state that changed. Every sample on this page was compiled and run with Go 1.26.8 against `recursiveai.org/go/oneui` v1.0.1 from the Go module proxy; the output under each one is what it printed.
## Install
oneui needs Go 1.24 or newer.
```sh
mkdir hello && cd hello
go mod init example.com/hello
go get recursiveai.org/go/oneui@v1.0.1
```
The module has no dependencies: its `go.mod` requires nothing. The license is MIT or Apache-2.0, at your choice. The generator runs through `go run`, so there is nothing else to install.
## A component
A component is a template next to a Go struct. `Hello.html`:
```html
```
`main.go`:
```go
package main
//go:generate go run recursiveai.org/go/oneui/cmd/oneui generate
import (
"os"
"recursiveai.org/go/oneui"
)
type Hello struct {
oneui.Base
Count int `oneui:"state"`
}
func (h *Hello) Increment() { h.SetCount(h.Count + 1) }
func main() {
h := &Hello{}
h.Increment()
oneui.RenderHTML(os.Stdout, h)
}
```
```sh
go generate ./...
go run .
```
```text
```
`go generate` checks every name in the template against the struct and writes three files: `Hello_oneui.go` (the `SetCount` setter and the component's shape), `Hello_build_oneui.go` (the code that creates the button once and rewrites only its text when `Count` changes) and `reload_oneui.go` (used only by builds with the `oneui_reload` tag, for template reload). Run it again after editing a template or upgrading oneui; a generated file built against another runtime version stops at start-up with a message that says so.
A field tagged `oneui:"state"` gets `SetCount`, which updates the bound nodes when the value changes, and `NotifyCountChanged`, for a value changed in place. `oneui.RenderHTML` writes the markup for the current state without creating nodes, for first paint or server-side rendering. Listeners and stylesheets are not in that markup, and mounting over it is not supported (no hydration).
## Test it
`oneuitest.Mount` mounts a component in the reference document, an in-memory DOM with selector and accessibility queries. `Click` and `Type` act the way a user does, apply what the component did and fail the test on any error. `hello_test.go`:
```go
package main
import (
"testing"
"recursiveai.org/go/oneui/oneuitest"
)
func TestHello(t *testing.T) {
page := oneuitest.Mount(t, &Hello{})
page.Click("button")
page.Click("button")
if got := page.Text("button"); got != "Clicked 2 times" {
t.Fatal(got)
}
}
```
```sh
go test -v .
```
```text
=== RUN TestHello
--- PASS: TestHello (0.00s)
PASS
ok example.com/hello 0.002s
```
## A list with a two-way binding
A component in its own package, `todo/`: an input bound to `Draft` with `bind:value`, an `if`/`else` block, and a keyed `each` block whose rows toggle a class on click. `Todos.css` is scoped to the component. `todo/Todos.html`:
```html
Nothing to do.
{t.Title}
```
`todo/Todos.css`:
```css
li.done { text-decoration: line-through; }
```
`todo/todos.go`:
```go
// Package todo is a task list: an input, a keyed list and a two-way binding.
package todo
import "recursiveai.org/go/oneui"
//go:generate go run recursiveai.org/go/oneui/cmd/oneui generate
type Item struct {
ID int
Title string
Done bool
}
type Todos struct {
oneui.Base
Draft string `oneui:"state"`
Items []Item `oneui:"state"`
}
func (c *Todos) Add() {
if c.Draft == "" {
return
}
c.SetItems(append(c.Items, Item{ID: len(c.Items) + 1, Title: c.Draft}))
c.SetDraft("")
}
func (c *Todos) Toggle(id int) {
for i := range c.Items {
if c.Items[i].ID == id {
c.Items[i].Done = !c.Items[i].Done
}
}
c.NotifyItemsChanged()
}
```
`Toggle` changes an item in place, so it calls `NotifyItemsChanged` instead of the setter. `todo/todos_test.go` types into the input, adds the task and toggles it:
```go
package todo
import (
"testing"
"recursiveai.org/go/oneui/oneuitest"
)
func TestAddAndToggle(t *testing.T) {
page := oneuitest.Mount(t, &Todos{})
if got := page.Text("p"); got != "Nothing to do." {
t.Fatal(got)
}
page.Type("input", "Write docs")
page.Click("button")
page.Click("li")
if got := page.Text("li.done"); got != "Write docs" {
t.Fatal(got)
}
}
```
```sh
go generate ./...
go test ./todo
```
```text
ok example.com/hello/todo 0.002s
```
Every generated component also has its own `RenderHTML` method. `cmd/list/main.go` renders the list with two items:
```go
package main
import (
"log"
"os"
"example.com/hello/todo"
)
func main() {
c := &todo.Todos{Items: []todo.Item{
{ID: 1, Title: "Write docs", Done: true},
{ID: 2, Title: "Tag v1.0.1"},
}}
if err := c.RenderHTML(os.Stdout); err != nil {
log.Fatal(err)
}
}
```
```text
Write docs
Tag v1.0.1
```
The `data-oui-…` attribute is what scopes `Todos.css` to this component. The bound `Draft` is written as the `value` attribute.
## Template diagnostics
A name the struct doesn’t have stops `go generate` with its position in the template, and nothing is written. With `{t.Title}` changed to `{t.Name}` in `Todos.html`:
```text
Todos.html:7:62: OUI0031: Item has no field Name
exit status 1
todo/todos.go:6: running "go": exit status 1
```
The command reports every problem in one run and exits 1; `go doc recursiveai.org/go/oneui/cmd/oneui` lists its flags.
## In the browser
Package `recursiveai.org/go/oneui/browser`, built with `GOOS=js GOARCH=wasm`, applies change sets to the page’s real DOM, routes DOM events back to your handlers and registers components as custom elements. `web/main.go`:
```go
//go:build js && wasm
package main
import (
"log"
"example.com/hello/todo"
"recursiveai.org/go/oneui"
"recursiveai.org/go/oneui/browser"
)
func main() {
if err := browser.Define("x-todos", func() oneui.Component { return &todo.Todos{} }); err != nil {
log.Fatal(err)
}
select {} // the page drives the program from here on
}
```
Build it and copy the loader from the same Go release:
```sh
GOOS=js GOARCH=wasm go build -o web/app.wasm ./web
cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" web/
```
`web/index.html`:
```html
Todos
```
Serve `web/` with any static file server. In Chrome, the page’s `` element holds:
```html
Nothing to do.
```
The browser package installs its bundled script itself, which needs the page to allow `Function` construction. A page whose content security policy forbids that should load `oneui-browser.js` (`browser.Glue()` returns it) with a script tag.
## Next
- [Overview](https://recursiveai.org/oneui/go/docs/overview/): the README, with state, testing, HTML rendering and the browser backend.
- [Changelog](https://recursiveai.org/oneui/go/docs/changelog/): the 1.0.1 form submission fixes, every package in 1.0.0 and the names and shapes of the API.
- [API reference on pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/oneui), including `ui` and `ui/kit`, the retained UI tree and its widgets.
# oneui
A pure-Go component model. A component is an HTML template, optional scoped CSS and a Go struct; `oneui generate` compiles it into plain Go that updates only the nodes bound to changed state, and emits a backend-neutral change-set stream. Includes a reference DOM with selector and accessibility queries, an engine-free retained UI tree, and a browser backend for `GOOS=js GOARCH=wasm`.
Licence: MIT OR Apache-2.0.
## Quick start
A component is a template next to a Go struct. `Hello.html`:
```html
```
`main.go`:
```go
package main
//go:generate go run recursiveai.org/go/oneui/cmd/oneui generate
import (
"os"
"recursiveai.org/go/oneui"
)
type Hello struct {
oneui.Base
Count int `oneui:"state"`
}
func (h *Hello) Increment() { h.SetCount(h.Count + 1) }
func main() {
h := &Hello{}
h.Increment()
oneui.RenderHTML(os.Stdout, h)
}
```
```sh
go mod init example.com/hello
go get recursiveai.org/go/oneui@v1.0.0
go generate ./...
go run .
#
```
`go generate` writes `Hello_oneui.go` with `SetCount` and the code that updates only the button's text when `Count` changes. The same component runs in a test (below), in the browser and in any backend that applies change sets.
## State and generated code
A field tagged `oneui:"state"` gets two generated methods: `SetX` replaces the value (and updates the bound nodes only when it changed, for types Go can compare; for slices, maps and structs holding them it always updates) and `NotifyXChanged` tells the component the value was changed in place. Generated files call the support package `rt`, which is versioned with the generator and is outside the compatibility promise: regenerate after upgrading. A generated file built against another runtime version stops at start-up with a message that says so.
## Testing a component
`oneui/oneuitest` mounts a component in the reference document, with a manual clock:
```go
page := oneuitest.Mount(t, &counter.Counter{})
page.Click("button")
if got := page.Text("button"); got != "Clicked 1 times" {
t.Fatal(got)
}
```
## Rendering to HTML
Every generated component has `RenderHTML(w io.Writer) error`, and `oneui.RenderHTML(w, c)` does the same for any `oneui.Component`: the markup for the current state, written without creating nodes, for first paint or server-side rendering. Read "RenderHTML or mount?" on `oneui.HTMLRenderer` first: a child shows its default state, nothing a user typed is in the markup, no lifecycle callbacks run, stylesheets and listeners are not emitted, and mounting over the markup is not supported (no hydration). Bound values are written as the HTML attribute with the same meaning (`value`, `checked`).
## In the browser
`recursiveai.org/go/oneui/browser` (`GOOS=js GOARCH=wasm`) applies change sets to the page's real DOM, routes DOM events back to your handlers and registers components as custom elements:
```go
func main() {
browser.Define("x-counter", func() oneui.Component { return &counter.Counter{} })
select {} // the page drives the program from here on
}
```
`examples/browser` builds and runs it (`./build.sh`, then serve the folder). The browser package's tests (`browser/host_js_test.go`) run in a real page: build the test binary with `GOOS=js GOARCH=wasm go test -c ./browser` and host it, with Go's `wasm_exec.js`, in a headless browser.
## Speed
A 1,000-row list against templ and go-app, each checked to produce the same markup first (`bench/METHOD.md`, `bench/RESULTS.md`; AMD Ryzen 9 7950X3D, Go 1.26, six counts):
| | oneui | templ | go-app |
|---|---:|---:|---:|
| Render to HTML | 28.3 µs | 537 µs | 1.11 ms |
| Update one row, live (change set) | 13.9 µs | — | — |
| Update one row, server re-render | 29.1 µs | 471 µs | 1.08 ms |
| Update all rows, server re-render | 29.5 µs | 500 µs | 1.18 ms |
# Changelog
## 1.0.1
### Fixed
- Form submission in the reference document. A click on a submit button (a `button` of type `submit` or with no type or an unknown one, an `input` of type `submit` or `image`) now submits its form owner: the form's `submit` event is raised, bubbles, and can be cancelled, as in the HTML Standard. The form owner follows the `form` attribute; disabled controls and fieldsets, `type=button` and `type=reset` never submit; a `required` control without a value blocks submission (its `invalid` event is raised and it takes focus) unless the submitter has `formnovalidate` or the form has `novalidate`. Enter in a text-like field submits implicitly: through a click on the form's first submit button, or directly when the form has at most one such field and no submit button. Enter or Space on a focused submit button, and a click on a label for one, submit too. The click and the submit run as one user action, so they commit as one change set. `invalid` is now reported as an event that does not bubble (`changeset.BubblesNot`).
- Browser backend: forms submit natively, so handlers see the browser's own `submit` event once; the bridge cancels the submission's navigation for a form with no `action`, so a form with `on:submit` no longer reloads the page.
### Added
- `oneuitest.Page.Press(selector, key)`.
## 1.0.0 — 2026-12-05
The API as it stands for 1.0.
### HTML rendering
- `oneui generate` writes a `RenderHTML(w io.Writer) error` method on every component with a template, and `oneui.RenderHTML(w, c)` calls it for any `oneui.Component` (`oneui.HTMLRenderer` is the interface, `oneui.ErrNotRenderable` the error, tested with `errors.Is`). It writes the markup for the component's current state without nodes or change sets, for first paint and server-side rendering. The output is the reference document's serialisation after a mount, with a two-way binding's DOM property written as the HTML attribute with the same meaning (`value`, `checked`; a textarea's value as content), and U+00A0 written as the character. `oneui.HTMLRenderer` documents what it contains and when to mount instead.
### Performance
- Mounting a list roughly halves its allocations and time, and updating one row of a long keyed list no longer visits the others: a row whose item (a value type) and index are unchanged skips its row bindings, and a list whose keys did not change skips the reconcile. Rows and bindings take one allocation each, the context swaps its two change buffers instead of copying, and the reference document keeps nodes in a dense table. No API or protocol change.
### Packages
- `oneui`: the component runtime. `Component` is sealed on purpose: embed `Base` and implement `Build` and `Update`.
- `oneui/oneuitest`: `Mount(t, component)` mounts a component in the reference document with a manual clock and returns a `Page` with `Click`, `Type`, `Text`, `HTML` and `Advance`. Each action applies what the component did and fails the test on an error.
- `oneui/changeset`: change-set protocol 3, JSON and binary encodings, a constructor for each change kind (`NewSetText`, `NewInsert`, ...), bounded decoders (`Limits`).
- `oneui/dom`: the reference document with selector and accessibility queries. `dom.New(dom.WithMaxCommits(n))`; `Query` returns an error wrapping `ErrNotFound` when nothing matches; a `*Node` has `Click`, `Input`, `Dispatch` and `SetBox`.
- `oneui/template`: the template and stylesheet parser. `Parse`, `ParseCSS` and `ParseInline` return `error`; `*template.Error` wraps `ErrSyntax`, `ErrorList` holds several, `Errors(err)` lists them.
- `oneui/gen` and the `oneui` command: `gen` is `Generate`, `Input`, `Result`, `Diagnostic` and `GeneratedMark`. `Diagnostic` is an error (`File`, `Line`, `Col`, `Msg`, `ID`); `Result.Err()` joins them and wraps `ErrDiagnostics`; a Go syntax error in a source file is returned as a `*Diagnostic` with an empty ID. The command prints every problem as `file:line:col: ...` and exits 0, 1 or 2 (see `go doc ./cmd/oneui`).
- `oneui/reload`: `Template`, `Result`, `Status`, `Cause` and `Available`, for template reload in programs built with the `oneui_reload` tag.
- `oneui/rt` and `oneui/rt/reload`: support code that generated files call. Outside the compatibility promise and versioned with the generator: generated files check `rt.RuntimeVersion` at start-up and stop with a message to run `go generate` again when it differs.
- `oneui/ui` and `oneui/ui/kit`: the engine-free retained UI tree and the widget kit.
- `oneui/browser` (`GOOS=js GOARCH=wasm`): the browser backend. `browser.Glue()` returns the bundled script.
### Names and shapes
- `oneui.NodeID` names a node; `oneui.Append` (0) as the "before" argument places a node last. Arguments are `(parent, child, before)` everywhere, including `Base.Mount` and `Context.MountAt`.
- `oneui.ElementRef` is the type of `ref="{Name}"` fields.
- Handlers take `*oneui.Event`; there are no per-kind event types.
- `Rect` and `Size` fields are `X, Y, W, H` and `W, H` in `changeset` and `ui`.
- `oneui.WithErrorHandler` receives problems the context cannot return to a caller (`browser.WithErrorHandler` likewise).
- `Scheduler.Schedule` returns a `*Timer`; `Timer.Stop` reports whether it stopped the call, like `time.Timer`. `Loop.Post` returns false once the loop is closed.
- `oneui.New` connects a sink that has `SetDispatcher` (the reference document, the browser host) to the context.
- `Context.Batch(fn)` is the only way to batch. `Base.Runtime` and `Base.SetRuntime` hold generated code's update scope.
- `oneui.Format` is `rt.Format`; `ScrollAlign` is the scroll alignment of `ScrollIntoView`.
- `ui`: `Element.SetAcceptsText` and `AcceptsText`, `IDSelector`, `Element.TextContent`, `BoxSizingBorder` and `BoxSizingContent`, `Tree.SetValue` and `Tree.Value`.
- `ui/kit`: constructors take required arguments positionally and the rest as options (`kit.ID`, `kit.Kind`, `kit.Placeholder`, `kit.Horizontal`, `kit.ShowValue`, `kit.Danger`, `kit.OnCancel`, `kit.ConfirmText`, `kit.CancelText`); lists and tables take `ListOptions` and `TableOptions`. Clipboards belong to a `Host` (`kit.WithClipboard`, `Host.SetTreeClipboard`); there is no package-level clipboard.
### Generated code
The generated files use named state-bit constants, role-named parameters (`parent`, `before`), `oneui.Append`, and no empty import. Setters of types Go cannot compare say they always update.