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] })))Combinators
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
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
Layers
Models
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;}The options of layer.
Signature
interface Options { readonly exit?: () => void; readonly load?: (href: string) => void; readonly routes: AnyUnion; readonly url?: string;}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;}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
The Schema of an Entry whose route is route.
Signature
declare const Entry: <S extends Top>(route: S) => Struct<{ readonly id: Number; route: S;}>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
subscription
unstableThe 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>