/** * A stack of screens, such as the history entries of `NativeRouter`, with * the last one on top. Each entry is a `RouterStack.Entry`. * * **Details** * * `Stack` is a view function. It draws every entry on its own screen, keyed * by the entry's `id`, so a covered screen stays mounted: its Models, * scroll offsets and text keep their state, and Back shows it as it was. * Each screen fills the stack. A covered one is transparent, takes no * touches and is hidden from the screen reader. * * `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 `Link` or the Router's Commands. `screen` runs * whenever the parent renders. * * A covered screen keeps running its Subscriptions, so a page can gate the * work it only needs on top. Its `BackHandler.onPress` takes no press, nor * does one on a stack drawn inside it. * When a screen comes to the top after the first, the stack moves the * screen reader's focus to it. * * Screens switch immediately unless a `transition` is provided. * `StackTransition.slide` animates them on the native driver and supports * optional iOS swipe-back. Without a transition, the stack bundles neither * `Animation` nor `Pan`. * * **Example** (The screens of a NativeRouter) * * ```ts * import { Stack } from "@effect/ui-native/kit" * * Stack(h, { * entries: model.stack, * screen: (s, { route }) => * AppRoute.match(route, { * Home: () => HomeScreen(s, { key: "home" }), * Person: ({ params }) => PersonScreen(s, { key: "person", props: params }) * }) * }).style({ backgroundColor: "white" }) * ``` * * @stability experimental * @since 4.2.0 */ import type { Entry } from "effect/ui/RouterStack" import type * as Native from "../Native.ts" import { type Draw, Screen } from "./internal/stack.ts" import type { StackTransition } from "./StackTransition.ts" /** * The options of `Stack`. * * @stability experimental * @category models * @since 4.2.0 */ export interface StackOptions { /** The entries, the top one last. */ readonly entries: ReadonlyArray> /** Draws an entry's screen. */ readonly screen: ( s: Native.H, entry: Entry ) => Native.NoTextInput /** Animates screen changes, for example with `StackTransition.slide()`. */ readonly transition?: StackTransition | undefined } /** * Draws `entries` as a stack of screens, the last on top. * * @stability experimental * @category components * @since 4.2.0 */ export const Stack = ( h: Native.H, options: StackOptions ): Native.ViewElement => { const { entries, transition } = options const screen = options.screen as Draw if (transition !== undefined) { return transition.draw(h, entries, screen) as any } const top = entries.length - 1 return h.view.style({ flex: 1 }).children( entries.map((entry, index) => Screen(h, { key: String(entry.id), props: { entry, index, covered: index < top, fade: true, depth: 1, draw: screen } }) ) ) as any }