Skip to content
// EFFECT DAYS 2026

RouterStack unstable

A screen stack on the Router: each history entry is a screen, and the screens of earlier entries stay mounted under the current one.

Details

The root holds the entries up to the current one, as a Stack: a list of routes with an id each. props reads it when the root mounts, subscription reports each navigation, and follow applies it to the list. Screens draws a screen for every entry, and the caller hides the covered ones, so Back shows a screen as it was left, with its Model, scroll offsets, focus and Commands in flight. Nothing is saved or restored on Back, so layer turns the Router's restore off.

A covered screen sets no head entries. Its Subscriptions keep running, but OpenTUI's key and paste Subscriptions, such as OpenTuiSubscription.shortcuts, skip a hidden one: keys go to the screen the user sees. Forward after Back starts the page fresh.

Example A terminal issue tracker

import { Command, Route, Router, RouterStack, UiSchema } from "effect/ui"
import { OpenTui, OpenTuiCommand, OpenTuiHost, OpenTuiSubscription } from "@effect/ui-opentui"
import { Effect, Schema } from "effect"
import { IssueList, IssuePage } from "./pages.ts"
const AppRoute = Route.union({
Issues: Route.make("/"),
Issue: Route.make("/issues/:id", { params: { id: Schema.Int } })
})
const Message = UiSchema.TaggedEnum({
Navigated: { navigation: RouterStack.Navigation(AppRoute) },
PressedBack: {}
})
const App = OpenTui.component({
name: "App",
props: Schema.Struct({ stack: RouterStack.Stack(AppRoute) }),
model: RouterStack.Stack(AppRoute),
message: Message
}).make({
init: ($) => $.props.stack,
update: {
Navigated: ($, { navigation }) => $.set(RouterStack.follow(AppRoute, $.model, navigation)),
PressedBack: ($) =>
$.run($.model.length > 1 ? Router.Back.make().pipe(Command.asNoop) : OpenTuiCommand.quit())
},
subscriptions: ({ model }) => [
RouterStack.subscription(AppRoute, model, (navigation) => Message.Navigated({ navigation })),
OpenTuiSubscription.shortcuts([{ key: "escape", message: Message.PressedBack() }])
],
view: (h, stack) =>
h.box.children(RouterStack.Screens(h, {
entries: stack,
screen: (s, { route }, covered) =>
s.el("box").prop("visible", !covered).children(
AppRoute.match(route, {
Issues: () => IssueList(s, { key: "issues" }),
Issue: ({ params }) => IssuePage(s, { key: "issue", props: params })
})
)
}))
})
OpenTuiHost.run(App, RouterStack.props(AppRoute)).pipe(
Effect.provide(RouterStack.layer({ routes: AppRoute, url: process.argv[2] }))
)
13 exports Since v4.2.0 Source

Combinators

follow

unstable
Since v4.2.0

The stack after a navigation.

Details

The stack keeps the entries before the navigation's index. The entry at the index keeps its id, so its screen stays mounted, while its route is on the same page: after Back, a patchSearch, or a link to the current route. Otherwise it gets a new id.

Signature

declare function follow<U extends Routes>(routes: U, stack: readonly [Entry<U["Type"]>, Entry<U["Type"]>], navigation: Navigation<U["Type"]>): readonly [Entry<U["Type"]>, Entry<U["Type"]>]

Components

Screens

unstable
Since v4.2.0

Draws a screen for every entry of a stack, the top one last.

Details

Each entry is its own screen, keyed by its id, so two entries of one route are two screens. screen draws an entry with the screen's own h, so what it draws sends no messages to the stack's parent: draw a component per route, which navigates with the Router's Commands. screen runs whenever the parent renders. Place the screens in one element, such as h.box.children(RouterStack.Screens(h, options)).

A covered screen stays mounted until it leaves the stack, so Back shows it as it was left. Hide it with the host's own means, on an element drawn with s.el: s.el("box").prop("visible", !covered) in OpenTUI, or the hidden attribute in HTML. It keeps its host state, such as scroll offsets, selection and, in OpenTUI, focus. It keeps rendering, and sets no head entries.

Signature

declare function Screens<Msg, A, N extends Content<never, unknown>>(h: HelperBase<Msg, unknown>, options: ScreensOptions<A, N>): readonly Array<Child<Msg, Requirements<N>>>

Constructors

props

unstable
Since v4.2.0

The props of a root whose only prop is the stack: { stack }, with the Router's current entry.

Signature

declare function props<U extends Routes>(routes: U): Effect<{
readonly stack: readonly [Entry<U["Type"]>, Entry<U["Type"]>];
}, never, Router>

Layers

layer

unstable
Since v4.2.0

Provides the Router and its Commands on a history in memory, for a screen stack.

Signature

declare function layer(options: Options): Layer<Services>

Models

Entry

interface unstable
Since v4.2.0

An entry of a stack: an id that stays the same while the entry is in the stack, and its route.

Signature

interface Entry<A> {
readonly id: number;
readonly route: A;
}

Options

interface unstable
Since v4.2.0

The options of layer.

Signature

interface Options {
readonly exit?: () => void;
readonly load?: (href: string) => void;
readonly routes: AnyUnion;
readonly url?: string;
}

ScreensOptions

interface unstable
Since v4.2.0

The options of Screens.

Signature

interface ScreensOptions<A, N> {
readonly entries: readonly [Entry<A>, Entry<A>];
readonly screen: (s: HelperBase<never, unknown>, entry: Entry<A>, covered: boolean) => N;
}

Stack

type unstable
Since v4.2.0

The entries up to the current one, the current one last. There's always a current one.

Signature

type Stack<A> = readonly [Entry<A>, ...Array<Entry<A>>]

Schemas

Entry

unstable
Since v4.2.0

The Schema of an Entry whose route is route.

Signature

declare const Entry: <S extends Top>(route: S) => Struct<{
readonly id: Number;
route: S;
}>

Stack

unstable
Since v4.2.0

The Schema of a Stack of the routes, for a root's props and Model.

Signature

declare const Stack: <U extends AnyUnion & {
readonly Type: {
readonly _tag: string;
};
} & Top>(routes: U) => NonEmptyArray<Struct<{
readonly id: Number;
route: U;
}>>

Subscriptions

Since v4.2.0

The Subscription that reports each navigation to a root holding a stack. Apply it with follow.

Signature

declare function subscription<U extends Routes, Msg>(routes: U, held: readonly [Entry<U["Type"]>, Entry<U["Type"]>], f: (navigation: Navigation<U["Type"]>) => Msg): Subscription<Msg, Router>