Osy#betaRun it on Osyrin: osyrin.com

Reference / UI

[Section] — a part that keeps its place across the pages that share it

[Section(SectionKey)] component Head(...) { string SectionKey() => …; render { … } }

Mark a part that draws a page's head — its title, its breadcrumb, its tabs — `[Section(SectionKey)]`, naming a parameterless `string` method of the part. When the app moves from one page to another and both render the part with the same non-empty key, the second page does not build its own: the one on screen is kept, the same DOM and the same instance, and from then on shows the second page's arguments and slot content. A tab switch redraws only the body. An empty key means "not a section" — the part is built per page, as any component is. An object's tabbed pages draw their whole view as one section keyed by the object's address, so a tab move reads nothing again.

stable2 examples compiled by CIuiroutingcompositionattribute

Summary#

An object's tabs are usually routes, and each tab is its own page. When each page draws the object's head itself — ConsolePageHead(trail, "Alerts", tabs: AlertTabs(), current: "/alerts"), as a page using a look kit does — every tab switch used to build that head again to show what it already showed. [Section(F)] on the part says which pages share it: those whose calls give the same non-empty F(). The router keeps ONE instance of the part for them.

Signature#

[Composable] [Section(SectionKey)]
component Head(string title, List<TabItem> tabs, string current) {
  string SectionKey() => string.Join("|", tabs.Select(t => t.To));   // the tabs' addresses: same tabs, same section
  render { … }
}

Description#

  • What is kept. The part's instance and every element it rendered. When page B is shown, the part is MOVED into B's page at the place B renders it, and bound to B: its parameters read B's arguments (it patches in place — the current tab, a count, the title), and its slots show B's content (slot primary { … } is the page's own). Its live reads stay subscribed while B is shown, idle while B is hidden, and are asked again when B is refreshed; a read whose arguments come from B asks again with B's values.
  • Its own state carries over from page to page: it is one instance, so what the reader set in the head itself stays as it was.
  • Nothing of it is read again for the arriving page. A move asks the server for the next page's data and names the parts the browser keeps; the server reads none of a part that page draws under a key it names — not its queries, not its on mount reads, not the parts it draws — and sends only what the arriving page needs. What the page puts in the part's slots is the page's, and is read. A part named that the page draws under another key, or that the browser then builds after all, asks for its own reads, so a page always ends up with its data.
  • Who shares it. The same part, the same non-empty key, under the same outermost layout. Which slots a page fills does not matter: they are the page's content, and a wrapper the part draws only around a slot (a row holding Slot("primary")) stands there while the page shown fills it. A page reached by a full load, a page under another layout, a page whose key differs, or a second copy of the part on one page builds its own — as it always did. A recompile that changes the part builds it again.
  • The key is a method of the part itself, parameterless, returning string, read with the call's arguments. Key it by what makes two heads "the same head": for a tabbed head, its tabs' addresses; for a head without tabs over a family of pages reached from a rail (an object's sections), the links of its breadcrumb — the look kit's ConsolePageHead and PageBand do both. An empty key when there is nothing to share by. [Section(...)] on anything else is a compile error that says so.
  • Retained pages. Under Outlet(retain: N) the part stands in the page on screen; going back to a retained page moves it back there. Disposing or evicting a page never disposes the part while another mounted page renders it.
  • Focus stays where it was: the tab link just clicked keeps it after the move. Under a plain Outlet; the head stays on screen while the arriving page is on its way.
  • The first paint is unchanged: the server paints each page with its own head, and the browser keeps the head of the first page it builds as the shared one.
  • A layout or a part. A head that is the app's own markup, built without such a part, can live in a [[ui-routing#section-layouts|section layout]] instead — a [Layout] inside the shell. A kit part marked [Section] needs neither: pages keep using the part exactly as before.
  • Compile errors: [Section(Missing)] naming no method of the part; [Section(F)] where F takes parameters or returns anything but string; [Section("F")] (a literal, not an identifier) — each says what a section key is for.

A page that draws route tabs itself — warning ROUTE_TABS_OUTSIDE_A_SECTION

A page ([Route]) that draws a Tab(…, to: "/b") whose address is ANOTHER page of the app — in its own render or in a component it calls — not inside a [Section] part and not under a section layout is warned about (osy validate prints it): each tab is its own page, so every tab switch takes the strip down with the page and builds it again, the head around it too. Draw the head in a [Section] part (the look kit's ConsolePageHead and PageBand with tabs: are), or the tabs in a [[ui-routing#section-layouts|section layout]] the tabs' pages render inside. Not warned: a tab whose address is the page's own (/orders/{view} drawing /orders/lines) or no page at all — but a changed route PARAMETER is still another page, built again with everything it reads, so such a page's reads go in a section too (below). A tab that changes only the query string (/orders?view=lines) is the cheapest kind: the router keeps the page and patches it, and only what reads Navigation.Query redraws (see [[ui-navigation#query]]). The check reads an address written as a string literal; a to: computed at run time (tab.To over a list) is not judged.

An object's tabs: the whole view as one section

A kept HEAD is not the whole saving. When every tab's page draws the same view of one object — OrderView(number, "lines") on /orders/{number}, OrderView(number, tab) on /orders/{number}/{tab} — each move still disposes that view with the page it leaves and builds it again: its reads run again, its on mount runs again, and what it held (a message it was showing, a field half typed) is gone. Mark the VIEW [Section(SectionKey)], keyed by the object's address, and every tab's page shares one instance: a tab move keeps its reads, its state and what was typed, and redraws only what reads tab. The address still changes, so Back, Forward and a link to a tab work as before.

  • Key it by the object, not the tab: "/orders/" + number. A key that includes the tab is a section per tab — the rebuild again.
  • Read by the tab inside the tab's own part. The view's on mount runs once for all its tabs; a read only one tab needs belongs in that tab's part (its own on mount), which is built when the tab is shown.
  • Prove it with a test that clicks a tab and says Assert.RedrawsOnly("<the tab>") (label the tab's content), and, because a rebuilt part can look exactly like a kept one, that the move read nothing the tab did not need: Assert.AtMost(n, Cost.StatementsSince(mark)) (Cost.Mark() and Cost.StatementsSince(mark)).
  • osy lint names the pages that do not: ui-sibling-pages-rebuild-a-shared-part flags a part two or more pages draw, that reads or holds state, and is not a section — with the key to give it.

Examples#

public class Tab { public string Label = ""; public string To = ""; }

[Composable] [Section(SectionKey)]
component AlertsHead(List<Tab> tabs, string current, int open) {
  string SectionKey() => string.Join("|", tabs.Select(t => t.To));
  render {
    Text("Alerts");
    Text($"{open} open");
    foreach (var t in tabs) { TextLink(t.Label, to: t.To); }
    Slot("primary");
  }
}

[Route("/alerts")] [Render(CSR)]
component OpenTab() {
  List<Tab> Tabs() => new List<Tab> { new Tab { Label = "Open", To = "/alerts" }, new Tab { Label = "Causes", To = "/alerts/causes" } };
  render {
    AlertsHead(Tabs(), "/alerts", 3) { slot primary { Text("Acknowledge all"); } }
    Text("Three open alerts");
  }
}

[Route("/alerts/causes")] [Render(CSR)]
component CausesTab() {
  List<Tab> Tabs() => new List<Tab> { new Tab { Label = "Open", To = "/alerts" }, new Tab { Label = "Causes", To = "/alerts/causes" } };
  render {
    AlertsHead(Tabs(), "/alerts/causes", 3) { slot primary { Text("Export"); } }
    Text("Two causes");
  }
}
entity Order {
  string Number;
  string Customer;
  security { allow read when IsAnonymous || IsAuthenticated; }
}

[Layout] [AllowAnonymous]
component Desk() { render { Stack(gap: 2) { Text("Orders desk"); Outlet(retain: 4); } } }

[Route("/orders/{number}")] [Layout(Desk)] [Render(CSR)] [AllowAnonymous]
component OrderPage(string number) { render { OrderView(number, "lines"); } }

[Route("/orders/{number}/{tab}")] [Layout(Desk)] [Render(CSR)] [AllowAnonymous]
component OrderTabPage(string number, string tab) { render { OrderView(number, tab); } }

/// The order on one of its tabs. Every tab's page draws it, keyed by the order's address: one instance for them all.
[AllowAnonymous] [Section(SectionKey)]
component OrderView(string number, string tab) {
  live var order = Order.FirstOrDefault(o => o.Number == number);
  string note = "";
  string SectionKey() => "/orders/" + number;
  render {
    Stack(gap: 2) {
      Text(order == null ? "No such order" : order.Customer);
      Input(value: note, label: "Note");
      TextLink("Lines", to: "/orders/" + number);
      TextLink("History", to: "/orders/" + number + "/history");
      Stack(gap: 1, label: "Order tab") {
        if (tab == "history") { Text("Placed yesterday"); } else { Text("Two lines"); }
      }
    }
  }
}

[TestFixture]
void AnOrder() { new Order { Number = "A-1", Customer = "Wren & Pike" }; }

[Test(AnOrder)]
void a_tab_move_keeps_the_order_and_what_was_typed() {
  Ui.Visit("/orders/A-1");
  Assert.Visible("Wren & Pike");
  Ui.Fill("Note", "Call first");
  var mark = Cost.Mark();
  Ui.Click("History");
  Assert.OnPage("/orders/A-1/history");
  Assert.RedrawsOnly("Order tab");                 // only the tab redrew
  Assert.AtMost(0, Cost.StatementsSince(mark));     // and the order was not read again
  Assert.Value("Note", "Call first");               // what was typed is still there
  Ui.Back();
  Assert.OnPage("/orders/A-1");
  Assert.Visible("Two lines");
}

See also#

Related

routes and pages

How a component becomes a page: it declares a route with `[Route("/catalog/{slug}")]`, and navigating to a matching…

[Composable] — presentational components in public pages

Mark a presentational, composition-only component `[Composable]` so a public page can compose it without marking it…

Slot (child content)

A `Slot` marks where a component renders the content block its caller wrapped around it. Writing `Card { Text("hi"); }`…

Assert.RedrawsOnly — what an action redrew

In a UI test, says that the last action — a click, a Back, a key, a live change — redrew nothing outside the named part…

Cost.Mark() and Cost.StatementsSince(mark)

What a test's unit of work sent to the database between two points: take a mark, do the thing, read what it cost — in…