# oneui docs, all pages (OneUI 1.0.0) Every page of https://recursiveai.org/oneui/docs/ as markdown, getting started first. Index: https://recursiveai.org/oneui/llms.txt # Getting started with OneUI Write a markup component, mount it on the in-memory reference document, click it and see what changed. Then build a small screen with the C# builder and print the display list it paints. This guide was run on RecursiveAI.OneUI 1.0.0 from NuGet, in a .NET 10 console app on Linux, with no window: the window and browser backends are in [OneApp](https://recursiveai.org/oneapp/). ## Install You need the .NET 10 SDK. Make a console project and add the package: ```shell dotnet new console -n Hello cd Hello dotnet add package RecursiveAI.OneUI --version 1.0.0 ``` The package carries the markup compiler. Every `.html` file in the project, and the `.css` file next to it, is a component template. ## A markup component A component is a template, an optional stylesheet and a partial class with the same name. Add three files. `Counter.html`: ```html ``` `Counter.css`: ```css .btn { padding: 4px 12px; } ``` `Counter.cs`: ```csharp using OneUI.Components; public partial class Counter { [State] public partial int Count { get; set; } private void Increment() => Count++; } ``` The compiler adds the base class, the state property and the code that creates and updates the button. A misspelled name in the template, or a CSS property the renderer can’t draw, is a build error with an `OUI` id (see [Diagnostics](https://recursiveai.org/oneui/docs/diagnostics/)). ## Run it, then build a screen in C# Replace `Program.cs` with: ```csharp using OneUI; using OneUI.Components; using OneUI.Components.Reference; using OneUI.Kit; // 1. A markup component, mounted on the in-memory reference document. var dom = new ReferenceDocument(); var ctx = new UiContext(dom); dom.Context = ctx; ctx.Mount(new Counter()); Console.WriteLine(dom.ToHtml()); dom.Click(".btn"); dom.Click(".btn"); Console.WriteLine(dom.ToHtml()); // The last click's change set: one text node rewritten. foreach (Change c in dom.Commits[^1].Changes) Console.WriteLine($"{c.Kind} node {c.Node}: \"{c.Value}\""); // 2. The C# builder: build, lay out and paint a tree. var tree = new UiTree(); tree.SetViewport(640, 360, 1); var kit = new KitHost(tree); tree.Root.Add(kit.AttachTo(Ui.Column( Ui.Label("Hello"), UiKit.Button("Count", () => Console.WriteLine("clicked"), "count")) .Padding(16).Gap(8))); DisplayList list = tree.Frame(); Console.Write(list.ToText()); tree.Invoke(tree.Find("count")!); ``` Run it: ```shell dotnet run ``` ```text SetText node 3: "Clicked 2 times" Text 'Hello' #e8eaf0 size=14 [16,16 Hello] Rect (16,40.8 72x32) #2b303a radii=4,4,4,4 Border (16,40.8 72x32) #3a4050 widths=1,1,1,1 radii=4,4,4,4 Text 'Count' #e8eaf0 size=14 [31,48.4 Count] clicked ``` What happened: - `ReferenceDocument` applies the component’s change sets to an in-memory DOM. `Click`, `Input` and `Press` drive events the way a browser does, and `ToHtml` prints the result, so the same calls work in a unit test. - The `data-oui-…` attribute scopes `Counter.css` to the component’s own elements. - After a click, the commit holds one change: the text node that reads `Count` is rewritten and nothing else is. - `UiTree.Frame()` styles and lays out the tree and returns the display list a host paints: text runs, rectangles and borders with their boxes. The kit button comes with the theme’s colors. - `tree.Invoke` clicks the button as a pointer or the confirm button would, without hit testing, and its callback prints `clicked`. ## Next - [Markup components](https://recursiveai.org/oneui/docs/markup/): bindings, events, two-way binding, conditionals, keyed lists, child components, slots, focus and SVG. - [Change sets](https://recursiveai.org/oneui/docs/change-sets/): the protocol between components and a document, and what the reference document simulates. - [CSS support](https://recursiveai.org/oneui/docs/css-support/): what a component stylesheet accepts. # RecursiveAI.OneUI A UI component model for .NET. Components are an HTML template, an optional CSS file and a C# class; a source generator that ships in the package turns each one into create and update code and checks every binding, event, element and CSS property at build time. A second front end builds the same kind of tree from C# calls and adds CSS-model styling, flexbox layout, painting to a display list, input routing, tweens and a kit of themed controls (buttons, text input, dropdown, list, table, tabs, scroll view, slider, dialog). The package depends only on .NET and [RecursiveAI.OneFont](https://www.nuget.org/packages/RecursiveAI.OneFont). It is NativeAOT- and trim-compatible and uses no reflection. ## Markup components A component is a template, an optional stylesheet and a partial class with the same name: `Counter.html` ```html ``` `Counter.cs` ```csharp using OneUI.Components; public partial class Counter { [State] public partial int Count { get; set; } private void Increment() => Count++; } ``` Clicking the button changes `Count`; the one text node that reads it is the only thing that is rewritten. Unknown names, bad bindings and CSS the engine cannot render are build errors with a stable `OUI` id: see [docs/diagnostics.md](https://recursiveai.org/oneui/docs/diagnostics/) for the list and [docs/markup.md](https://recursiveai.org/oneui/docs/markup/) for the template syntax. A component does not draw. It writes mutations (create a node, set an attribute, insert, listen) as a [change set](https://recursiveai.org/oneui/docs/change-sets/) to a document that implements `IChangeSink`. This package has the reference document, an in-memory DOM with selector queries, click and key helpers and a readable HTML dump, which is what tests use. The native window backend and the browser backend (WebAssembly, with components registered as custom elements) are provided by [RecursiveAI.OneApp](https://www.nuget.org/packages/RecursiveAI.OneApp). ## The C# builder The other front end is `OneUI.Element`, a retained tree you build and style in code. `UiTree` styles it, lays it out, handles input and focus, and produces a `DisplayList` for a host to paint. ```csharp var tree = new UiTree(); tree.SetViewport(640, 360, 1); var kit = new KitHost(tree); tree.Root.Add(kit.AttachTo(Ui.Column( Ui.Label("Hello"), UiKit.Button("Count", () => log("clicked"), "count")).Padding(16).Gap(8))); var list = tree.Frame(); // the display list: what to paint ``` ## Contents - `OneUI.Components`: components, state, the change-set protocol, the reference document (`OneUI.Components.Reference`). - `OneUI`: `Element`, `Ui`, `UiTree`, styles, layout, display list. - `OneUI.Kit`: `UiKit` controls, `KitHost`, theme tokens, text editing, contrast checks. - `OneUI.Tweens`: easing and tweens. ## Repository layout - `src/OneUI`: the library. `src/OneUI.Generators`: the markup compiler, shipped inside the package as an analyzer. - `docs/`: markup, change sets, diagnostics, CSS support. - `samples/Todo`: an app built from markup and run against the reference document. - `tests/OneUI.Tests`, `tests/OneUI.Kit.Tests`, `tests/OneUI.Components.Tests`, `tests/OneUI.Generators.Tests`: xUnit v3 tests. Layout is checked against boxes measured in headless Chrome (`testdata/ui-layout`, regenerated with `tools/ui-layout-reference/generate.py`). - `tests/OneUI.AotSmoke`: a NativeAOT app that mounts a component, builds a screen, clicks and types. - `tools/css-table`: regenerates the table of CSS properties the compiler accepts. ## Build and test ``` dotnet build OneUI.slnx dotnet test --solution OneUI.slnx dotnet test --project tests/OneUI.Components.Tests ``` The test projects use the Microsoft.Testing.Platform runner, selected in `global.json`; run `dotnet test` from inside the repository so that file is found. Set `ONEUI_ARTIFACTS` to choose where build output goes; the default is `artifacts/`. Licensed under MIT OR Apache-2.0. # Markup components A component is a template, an optional stylesheet and a C# class with the same name: ``` Counter.html Counter.css .btn { padding: 4px 12px; } Counter.cs public partial class Counter { [State] public partial int Count { get; set; } void Increment() => Count++; } ``` The package ships the markup compiler as an analyzer. Every `.html` file (and the `.css` next to it) in the project is passed to it (`RecursiveAI.OneUI.targets` adds them as `AdditionalFiles`; set `OneUIMarkupGlob` to `false` to list them yourself). The class must be a top-level, non-generic `partial class`; the compiler adds `: OneUI.Components.Component`, the state implementations and the `Build` method. Every name in the template is checked at build time (see `diagnostics.md`). ## State and parameters `[State] public partial T Name { get; set; }` is state; `[Param]` (optionally `Required = true`) is state a parent sets. Setting a different value marks it changed; the bindings that read it run before the next commit and write only what differs. At most 63 per component. `NotifyChanged()` is generated for state that is mutated in place (a list that was added to). ## Template syntax | | | |---|---| | Text | `Hello {Name}!`. `{{` and `}}` are literal braces. Values are formatted with the invariant culture; `bool` is `true`/`false`, null is empty. | | Attributes | `title="Hi {Name}"`; one `{expr}` alone for booleans (`disabled="{Busy}"`: present or absent) or to remove a null (`href="{Url}"`). Quote the attribute with `'` when the expression contains `"`. | | Classes, styles | `class:active="{IsActive}"` toggles one class; `style:color="{Color}"` sets one inline property (null removes it). A static `style="a: b; c: d"` is checked and applied property by property. | | Events | `on:click="Handler"` calls `void Handler()` or `void Handler(MouseEvent e)`; `on:click="{count++}"` runs a statement (`e` is the event). Payloads: `MouseEvent`, `KeyboardEvent`, `InputEvent` (`input`, `change`), `FocusEvent`, `DomEvent`. | | Two-way | `bind:value="Name"` on `input`, `textarea`, `select` (`input` events); `bind:checked="Agree"` on checkbox and radio inputs (`change` events). The target is any assignable expression. | | Conditionals | ``, siblings. | | Lists | ``. The key is required; rows keep their nodes while their key stays, and only rows outside the longest already-ordered run are moved. | | Components | ``: the tag is the class name (capitalised). A literal (`Count="3"`) converts to string, bool, int, long, float, double. Callbacks are `Action` parameters: `OnDone="{() => Remove(todo.Id)}"`. | | Slots | In the child: `` and `fallback`. In the parent: the tag's content fills the default slot; `` fills a named one. Slot content reads the *declaring* component's state. | | References | `` binds an `ElementRef` member (`ElementRef Search { get; } = new();`) to the element once it is created. `Search.Focus()` asks the document to focus it (see below). The reference is unbound while the element does not exist; in a list row it points at the row created last. `ref` goes on elements, not components (`OUI0070`). | | Lifecycle | `protected override void OnMounted()` runs after the commit that created the nodes, children before parents; `OnDisposed()` runs when the component is removed, parents before children. | Whitespace: text that touches a tag and spans a line break is dropped; other runs collapse to one space. ## Components from other assemblies A tag names a component class declared in the project or in any referenced assembly whose components are written with OneUI (a `PackageReference` or `ProjectReference` to the library is all a consumer needs): the compiler reads their `[Param]` properties from the assembly's metadata, and the slot names from `ComponentSlots` attributes that it wrote into the library when it compiled it, so parameters, required parameters, slots and types are checked exactly as for a component in the project. A library component's scoped stylesheet is part of its generated code, so it is added to the document the first time the component is created; the consumer does nothing. A component the project declares wins over a referenced one of the same name; two referenced components of one name are `OUI0064` where used. A component written by hand (no template) in a library takes any slot name. Only public (or `InternalsVisibleTo`) non-generic classes with a public parameterless constructor count. Nothing is looked up at run time, so it is trim- and NativeAOT-safe. A library that turns on `GenerateDocumentationFile` needs no `NoWarn`: generated members carry ``. ## Focus `ElementRef.Focus()` moves keyboard focus as the DOM's `focus()` does (a control, a link with `href`, an element with `tabindex`; nothing happens for anything else, a disabled or hidden element, or one that is not attached). Call it from `OnMounted` for initial focus, from a key handler for roving focus (give the selected tab `tabindex="0"` and the others `-1`), and on the opener when a dialog closes. It records a `Focus` change, which every backend applies after the commit's other changes; the focus events come back through `on:focus`, `on:blur`, `on:focusin` and `on:focusout`. Calling it on an unbound reference throws `InvalidOperationException`. See `change-sets.md` for the contract and for what the reference document simulates (`ActiveElement`, Tab, Enter and Space activation, label clicks). ## SVG `` and these elements inside it are created in the SVG namespace: `g`, `defs`, `symbol`, `use`, `path`, `circle`, `ellipse`, `rect`, `line`, `polyline`, `polygon`, `linearGradient`, `radialGradient`, `stop`, `clipPath`, `mask`, `title`, `desc`. Their geometry attributes (`d`, `cx`, `points`, `viewBox`, ...) and the SVG 2 presentation attributes (`fill`, `stroke`, `stroke-width`, `stroke-linecap`, `opacity`, `transform`, `clip-path`, ...) are checked per element, `class`, `id`, `style`, `data-*` and `aria-*` are always allowed, and every attribute can be bound with `{expr}` like an HTML one, as can `class:` and `style:` and `on:`. Names are case-sensitive: write `viewBox`, not `viewbox` (`OUI0011`). `xmlns` is accepted and ignored. Inside ``, `