# Navigation Transition Integration

This guide explains how a host application can use the OTF 0.5.x Navigation
Transition family and the transition lifecycle coordinator.

## Responsibility Boundary

OTF owns visual state and sequencing. The host application owns routes, URLs,
links, browser history, Astro or another router, destination loading, and
application navigation state.

OTF does not navigate. The host calls the visual lifecycle around its own
navigation operation:

```text
host requests OUT
    -> OTF disables signals and yields ambient activity
    -> Presentation OUT
    -> Navigation OUT
host changes the page or route
    -> host requests IN
    -> Navigation IN
    -> Presentation IN
    -> OTF restores occupants, ambient activity, and signals
```

## Public API

The coordinator is exported from the package root:

```ts
import { createTransitionLifecycle } from "operational-topology";
```

It accepts host-owned callbacks. These callbacks may be synchronous or return
Promises. The coordinator does not query the DOM, inspect routes, or mutate
topology data.

```ts
const lifecycle = createTransitionLifecycle({
  playNavigation: (phase) => navigationAdapter.play("flow", phase),
  playPresentation: (phase) => presentationAdapter.play("handoff", phase),
  setAmbientEnabled: (enabled) => ambientAdapter.setEnabled(enabled),
  setSignalsEnabled: (enabled) => signalAdapter.setEnabled(enabled),
  setOccupantsVisible: (visible) => sceneAdapter.setOccupantsVisible(visible),
});
```

The lifecycle exposes exit(), enter(), cancel(), getState(), and isRunning().

## Default Ordering

Exit runs in this order:

1. Disable signal reactions.
2. Yield or pause ambient activity without destroying its state.
3. Run the host's Presentation OUT behavior.
4. Run the host's Navigation OUT behavior.
5. Resolve when the visual boundary is ready.

The host can now perform its route change.

Entrance runs in this order:

1. Keep occupants hidden.
2. Keep ambient activity and signals paused.
3. Run Navigation IN.
4. Run Presentation IN.
5. Reveal occupants.
6. Resume ambient activity.
7. Enable signal reactions.

The Presentation callbacks are where a host should animate or hide actual
occupant and connector layers. Navigation callbacks should normally remain
non-destructive and operate on the current visual field.

## Astro Host Pattern

Keep the lifecycle instance in a client-side controller used by the shared
page shell. BaseLayout.astro may mount that controller, but it should not
choose transitions based on destination URLs.

```ts
import { createTransitionLifecycle } from "operational-topology";

const lifecycle = createTransitionLifecycle({
  playNavigation: (phase) => topologyScene.playNavigation({
    name: "flow",
    phase,
    direction: "forward",
  }),
  playPresentation: (phase) => topologyScene.playPresentation({
    name: "handoff",
    phase,
  }),
  setAmbientEnabled: (enabled) => topologyScene.setAmbientEnabled(enabled),
  setSignalsEnabled: (enabled) => topologyScene.setSignalsEnabled(enabled),
  setOccupantsVisible: (visible) => topologyScene.setOccupantsVisible(visible),
});

async function navigateWithinTheApplication(destination: string) {
  await lifecycle.exit();
  await applicationRouter.navigate(destination);
  await lifecycle.enter();
}
```

The adapter names in this example are application-owned. OTF provides the
animation frame functions and lifecycle contract; it does not provide an
application router or a universal topologyScene controller.

## Full-Page Navigation

For a full document reload, the source page cannot keep the same JavaScript
instance alive after navigation. The host may coordinate the boundary with its
own mechanism, such as the Astro View Transitions API or an application-owned
short-lived navigation marker. That mechanism belongs in the host application.

```ts
await lifecycle.exit();
sessionStorage.setItem("otf-navigation-enter", "flow");
window.location.assign(destination);
```

The destination page reads and clears that marker, creates its own lifecycle
instance, and calls enter(). OTF itself must not read or write sessionStorage,
localStorage, URLs, or browser history.

## Topology and Layouts

Navigation transitions do not create a destination topology. The host should
prepare its topology independently, preferably using a predefined topologyLayout
when semantic content needs generated coordinates:

```ts
const topology = createPresentationTopology({
  id: "destination-scene",
  title: "Destination",
  topologyLayout: "radial-sectors",
  anchors: [
    {
      id: "origin",
      label: "Origin",
      children: [{ id: "service", label: "Service" }],
    },
  ],
});
```

Pass the resulting nodes and relationships to the normal OTF renderer. Do not
use a Navigation Transition to replace topology data; that remains the job of
the Presentation Transition system.

## Reduced Motion and Failure Handling

The host should pass reduced-motion state to the underlying frame generators or
adapters. In reduced-motion mode, Navigation transitions should complete
immediately or use a short static state. nodeFlicker should not flicker.

If a callback fails, the lifecycle enters cancelled and rethrows the error. The
host should restore its own scene controller to a valid stable state before
reporting or retrying navigation. Calling cancel() prevents later phases from
starting; it does not perform route navigation or guess how the host should
restore its UI.

## Preview

The OTF repository includes:

- /navigation-lab/ for individual Navigation Transition frames
- /transition-lifecycle-lab/ for complete coordinator ordering
- /transition-lab/ for Presentation Transitions
- /presentation-simulator/ for deck-level Presentation Transition behavior

These labs are development aids. They are not required by package consumers and
do not represent a router integration.

## Scene Adapter

Browser consumers can use the public scene adapter to render `navigation.flow` into an existing OTF scene without querying individual cells or connector elements.

```ts
import { createTransitionLifecycle } from "operational-topology";
import { createTopologySceneAnimationAdapter } from "operational-topology/browser";

const scene = document.querySelector<HTMLElement>(".topology-scene");
if (!scene) throw new Error("OTF scene not found");

const sceneAdapter = createTopologySceneAnimationAdapter({
  scene,
});

const lifecycle = createTransitionLifecycle({
  playNavigation: (phase) => sceneAdapter.playNavigation({ animation: "flow", phase, direction: "forward" }),
  setOccupantsVisible: (visible) => sceneAdapter.setOccupantsVisible(visible),
  setConnectorsVisible: (visible) => sceneAdapter.setConnectorsVisible(visible),
});
```

The adapter precomputes axial cells, owns Navigation frame application for `flow`, `converge`, `expand`, `originArrival`, and occupant-only `nodeFlicker`, restores prior cell state after playback, and supports reduced motion. Presentation playback, ambient arbitration, and signal controls remain host callbacks because they depend on the host scene and dataset policy. Pass `animation` plus any transition-specific options when selecting another Navigation preset.
