# Compose built-in controls and navigation

Aster's built-in controls have shared composition contracts. Use them to connect
forms, grids, tabs, fields and other views to your model without maintaining a
second property list in every application. Start with [Compose a workspace](/docs/guide/declarative-workspace.md)
for model ownership and [Check workspace connections](/docs/guide/workspace-tooling.md) for
JavaScript checks and optional YAML tooling.

## Select the controls your application uses

This registry excerpt assumes your application's `editorModel` is already defined:

```js
import { asterComponents, defineCompositeRegistry } from "../lib/aster-ui.js";

const authoring = defineCompositeRegistry({
  models: { editor: editorModel },
  components: {
    Form: asterComponents.Form,
    Grid: asterComponents.Grid,
    Tabs: asterComponents.Tabs,
    Sidebar: asterComponents.Sidebar,
  },
});
```

Each entry describes the control's public writable properties, including inherited
field properties. Omitted values preserve the element's defaults. Objects such
as `EditModel`, `Collection` and `Selection` are borrowed through references;
bindings read signal values. Contracts participate in validation, TypeScript
checking and schema export. Complex callbacks and object predicates still need
runtime checks; generated schemas cannot describe every application constraint.

For example, this child excerpt borrows an editor exposed by the model:

```yaml
children:
  details:
    type: Form
    props:
      model: { ref: model.editor }
      sections:
        - title: Device details
          columns: 2
          items: [name, owner]
```

If `model.editor` is a signal containing an editor, use `bind` instead of `ref`.
The [device workspace](/playground/composition.html) uses the shared Grid,
TileGrid and CommandBar adapters. Its renderer bodies and save operation remain
ordinary JavaScript in the linked source.

## Drive navigation from data and state

Sidebar is a controlled navigation view. Its `items` are plain objects and `value`
is the current item ID. Each item has a unique `id` and a `label`; optional fields
include `href`, `icon` (a text glyph), `badge`, `hidden`, `disabled`, `reason`, `description` (the tooltip and accessible description) and
nested `children`. Groups use native disclosure controls; `collapsible: false`
makes a group a labelled section instead. Set `persist` to a name to keep the groups a person
folds across reloads, as `Split.persist` keeps a pane's width and collapsed state. Sidebar draws the same look as the
showcase workspace, with a caret on the current item, so an application gets it
without styling navigation itself. Links preserve browser
navigation, including modified clicks. Items without a URL are buttons.

This is complete Sidebar setup inside an existing page:

```js
import { Sidebar } from "../lib/aster-ui.js";

const sidebar = new Sidebar();
sidebar.label = "Device management";
sidebar.items = [
  { id: "devices", label: "Devices", href: "#devices", icon: "▦", badge: "24" },
  { id: "management", label: "Management", children: [
    { id: "policies", label: "Policies", href: "#policies", disabled: true,
      reason: "Policy administration permission required" },
  ] },
];
sidebar.value = "devices";
document.querySelector("main").prepend(sidebar);
```

For application routing, listen for the bubbling, cancelable `navigate` event.
Its detail is the item ID. Prevent default to handle a link yourself, execute the
model's command, and update `value` after navigation is accepted. When a factory
adds listeners, register their removal with its Lifetime. The Sidebar does not
change its own selection or decide whether a dirty workspace can close.

Derive `items` in the model from permission and state signals, and bind the array
to Sidebar. Use `hidden` for unavailable destinations and `disabled` with a reason
for actions users should be able to discover. If the current destination becomes
unavailable, the host chooses its fallback. Server authorization remains with the
service. Set `compact: true` for an icon rail; accessible labels remain present.
Supply a trusted `renderIcon(item)` function for SVG or other templated icons.
In definitions, reference a registered renderer; item data contains text only.
Tab follows native navigation, while Up/Down and Home/End move within visible,
enabled controls. No animation is required for navigation.

## Explore the composed showcase

The [component showcase](/showcase/#showcase) declares its Sidebar, gallery and lazy
preview in YAML. Its catalogue supplies the categories once; the model derives
the preview's file and title from the current route. Choosing a category activates
the preview through a deck. Returning to **All examples** releases its iframe and
restores the gallery. Use **Open full example** for a separate working page.

The source lives in `apps/showcase/src/app/`. Read `showcase.ui.yaml` for the
structure, `model.ts` for derived state, `registry.ts` for component registration,
and `routing.ts` for browser history and mobile navigation. `main.ts` mounts the
composition and owns its lifetime. `views/` contains the gallery and lazy preview.
The examples themselves live in feature folders under `apps/showcase/src/examples/`.
See [Organize an application](/docs/guide/application-structure.md) for the folder conventions.

<details class="example-source">
<summary>Showcase YAML definition</summary>

```yaml
version: 1
name: ShowcaseBrowser
model:
  type: showcase
  inputs:
    active: { ref: inputs.active }
    theme: { ref: inputs.theme }
    scheme: { ref: inputs.scheme }
children:
  navigation:
    type: Sidebar
    props:
      label: Showcase categories
      orientation: auto
      items: { ref: model.navigation }
      renderIcon: { renderer: categoryIcon }
      value: { bind: model.active }
  gallery:
    type: Gallery
    props:
      theme: { bind: model.theme }
      scheme: { bind: model.scheme }
  preview:
    type: Preview
    create: on-demand
    retain: false
    props:
      file: { bind: model.file }
      title: { bind: model.title }
      theme: { bind: model.theme }
      scheme: { bind: model.scheme }
layout:
  type: stack
  items:
    - { child: navigation }
    - type: deck
      active: { bind: model.surface }
      items:
        gallery: { child: gallery }
        preview: { child: preview }

```

</details>

<details class="example-source">
<summary>Lazy preview implementation</summary>

```typescript
import { html } from "lit-html";
import { keyed } from "lit-html/directives/keyed.js";
import { AsterElement, define, type CompositeFactory, type View } from "@getaster/elements";

/** A separate browsing context isolates each demo's data and releases it on deactivation. */
class ShowcasePreview extends AsterElement {
  readonly #file = this.prop("");
  readonly #title = this.prop("");
  readonly #theme = this.prop("lamplight");
  readonly #scheme = this.prop("light");
  get file(): string { return this.#file.value; }
  set file(value: string) { this.#file.value = value; }
  override get title(): string { return this.#title.value; }
  override set title(value: string) { this.#title.value = value; }
  get theme(): string { return this.#theme.value; }
  set theme(value: string) { this.#theme.value = value; }
  get scheme(): string { return this.#scheme.value; }
  set scheme(value: string) { this.#scheme.value = value; }
  #source: { file: string; url: string } | undefined;
  #appearance(): void {
    const frame = this.querySelector<HTMLIFrameElement>("iframe");
    frame?.contentWindow?.postMessage({ type: "aster-example-appearance", theme: this.#theme.peek(), scheme: this.#scheme.peek() }, location.origin);
  }
  protected override updated(): void { this.#appearance(); }
  protected render(): View {
    const root = new URL(document.querySelector<HTMLMetaElement>('meta[name="aster-library-root"]')?.content ?? "../", document.baseURI);
    const url = new URL(`playground/${this.#file.value}`, root);
    url.searchParams.set("theme", this.#theme.value);
    url.searchParams.set("scheme", this.#scheme.value);
    url.searchParams.set("embedded", "1");
    url.searchParams.set("app", new URL(".", document.baseURI).pathname);
    if (this.#source?.file !== this.#file.value) this.#source = { file: this.#file.value, url: url.href };
    // A new browsing context prevents iframe navigation from adding category history entries.
    return html`<div class="showcase-preview-head"><h1>Component showcase</h1><a href=${url.href.replace("embedded=1", "showcase=1")}>Open full example ↗</a></div>
      <p class="showcase-preview-note">Previews restart when you choose another category. Open the full example to keep working independently.</p>
      ${keyed(this.#file.value, html`<iframe class="showcase-live-preview" title=${`${this.#title.value} example`} src=${this.#source.url} @load=${() => this.#appearance()}></iframe>`)}`;
  }
}
define("aster-showcase-preview", ShowcasePreview);
export const createPreview: CompositeFactory = () => ({ element: document.createElement("aster-showcase-preview") });

```

</details>

## Keep lifecycle operations explicit

Property contracts do not turn methods into configuration. Opening a Dialog or
Window, positioning a Menu, registering Dashboard widget types, coordinating
windows with Workspace and sending toast announcements still use their public
methods. A registered factory performs that setup and owns its cleanup. Native
events keep their documented payloads; connect them to application actions in
trusted JavaScript.

For a new app-local element, use `defineElement` and `elementProperty`, then
`compositeElement(Constructor)`. Its declared properties become composition
inputs automatically. The shared adapters preserve existing controls' public
APIs without changing their implementation or reflecting arbitrary DOM attributes.
