NavigationStack & NavigationLink

NavigationStack & NavigationLink

NavigationStack is a container that shows one page at a time and keeps a back stack of the pages above its root. It works in one of two modes, chosen by whether routes is set:

Mode Set up with Pages are Navigation driven by
Link mode controls=[...] containing NavigationLink views Built up front, as each link's destination The user tapping a link
Route mode routes={...} and initial= Built by a route function on each push() Your code calling push(), pop(), pop_to_root()

Constructor

nib.NavigationStack(
    controls=None,
    routes=None,
    initial=None,
    show_back_button=True,
    on_navigate=None,
    **modifiers,
)
nib.NavigationLink(
    label="",
    destination=None,
    content=None,
    **modifiers,
)

Parameters

Parameter Type Default Description
controls list[View] None Link mode. The root page, laid out in a vertical stack. Ignored when routes is set.
routes dict[str, Callable[[], View]] None Route mode. Maps each route name to a function that returns the page as one view.
initial str None Route mode. Name of the root route, built once at construction. Required with routes.
show_back_button bool True Shows a Back button row at the top of each pushed page. With False, pushed pages have no header and you return with pop() or pop_to_root().
on_navigate Callable[[str], None] None Called with the name of the route now on top whenever the top route changes. See Back stack and on_navigate.
**modifiers View modifiers such as width, height, padding, background.
Parameter Type Default Description
label str "" Text of the link. Also shown as the title of the destination page, centered on the Back button row.
destination list[View] None Views shown, stacked vertically, when the link is tapped.
content View None A view used as the tappable link instead of the label text. label still sets the destination title; leave it empty for no title.
**modifiers View modifiers.

A NavigationLink navigates only inside a NavigationStack. Outside one, tapping it does nothing.

Put NavigationLink views in controls, usually inside a List. Each destination is part of the view tree from the start, so it can hold any views, including further links.

import nib

nav = nib.NavigationStack(
    controls=[
        nib.List(
            controls=[
                nib.NavigationLink(
                    "Profile",
                    destination=[
                        nib.VStack(
                            controls=[
                                nib.TextField(value="", placeholder="Display name"),
                                nib.Toggle("Show online status", is_on=True),
                            ],
                            spacing=12,
                            padding=16,
                            alignment=nib.HorizontalAlignment.LEADING,
                        )
                    ],
                ),
                nib.NavigationLink(
                    destination=[nib.Text("Version 1.0", padding=16)],
                    content=nib.Label("About", icon="info.circle"),
                ),
            ]
        )
    ],
)

The destination page has a header row with a Back button and the link's label as title. push(), pop() and pop_to_root() belong to route mode.

Route mode

Give routes and initial. The initial route is the root; every other page is built when you push it, so a route function can read current state.

import nib

def home_page():
    return nib.VStack(
        controls=[
            nib.Text("Home", font=nib.Font.TITLE),
            nib.Button("About", on_click=lambda: nav.push("about")),
        ],
        padding=16,
    )

def about_page():
    return nib.VStack(
        controls=[
            nib.Text("About", font=nib.Font.TITLE),
            nib.Button("Home", on_click=lambda: nav.pop_to_root()),
        ],
        padding=16,
    )

nav = nib.NavigationStack(
    routes={"home": home_page, "about": about_page},
    initial="home",
    on_navigate=lambda route: print("now on", route),
)

Pushed pages show a Back button row with no title. Put a title in the page itself.

Methods

Method Description
push(route) Calls the route function, then pushes the page after the next render. A name not in routes is ignored.
pop() Pops the top page after the next render. Does nothing on the root.
pop_to_root() Pops every pushed page, back to initial.

These methods only act in route mode.

Back stack and on_navigate

  • The back stack holds route names above initial, bottom to top. The Back button, pop() and pop_to_root() remove names from the top.
  • on_navigate(route) fires each time the top route changes, from any source: push(), pop(), pop_to_root() or the Back button. route is the name now on top, and it is the initial name when the stack returns to the root.
  • on_navigate does not fire when the stack first appears.
  • When a page is popped, its view is released. Pushing the route again calls the route function again.

The Navigation example is a complete route-mode app.

Examples

Basic navigation

navigation_basic_navigation

import nib


def main(app: nib.App):
    app.title = "Recipes"
    app.icon = nib.SFSymbol("book")
    app.width = 280
    app.height = 240

    def recipe(name, minutes, steps):
        return nib.NavigationLink(
            name,
            destination=[
                nib.VStack(
                    controls=[
                        nib.Text(name, font=nib.Font.TITLE2),
                        nib.Text(
                            f"{minutes} minutes", foreground_color=nib.Color.SECONDARY
                        ),
                        *[nib.Text(f"{i}. {step}") for i, step in enumerate(steps, 1)],
                    ],
                    spacing=8,
                    padding=16,
                    alignment=nib.HorizontalAlignment.LEADING,
                )
            ],
        )

    app.build(
        nib.NavigationStack(
            controls=[
                nib.List(
                    controls=[
                        recipe(
                            "Carbonara",
                            20,
                            [
                                "Boil the pasta",
                                "Fry the guanciale",
                                "Mix eggs and pecorino",
                                "Toss off the heat",
                            ],
                        ),
                        recipe(
                            "Pesto",
                            10,
                            [
                                "Blend basil, pine nuts and garlic",
                                "Add oil and parmesan",
                            ],
                        ),
                        recipe(
                            "Risotto",
                            35,
                            [
                                "Toast the rice",
                                "Add stock a ladle at a time",
                                "Mantecare with butter",
                            ],
                        ),
                    ]
                )
            ],
            width=280,
            height=220,
        )
    )


nib.run(main)

Settings navigation with sections

navigation_settings_navigation_sections

import nib


def main(app: nib.App):
    app.title = "Settings"
    app.icon = nib.SFSymbol("gearshape")
    app.width = 280
    app.height = 280

    def page(title, *controls):
        return [
            nib.VStack(
                controls=[nib.Text(title, font=nib.Font.TITLE2), *controls],
                spacing=12,
                padding=16,
                alignment=nib.HorizontalAlignment.LEADING,
            )
        ]

    app.build(
        nib.NavigationStack(
            controls=[
                nib.List(
                    controls=[
                        nib.Section(
                            controls=[
                                nib.NavigationLink(
                                    "Profile",
                                    destination=page(
                                        "Profile",
                                        nib.TextField("Display name", value="Jane"),
                                    ),
                                ),
                                nib.NavigationLink(
                                    "Notifications",
                                    destination=page(
                                        "Notifications",
                                        nib.Toggle("Push", is_on=True),
                                        nib.Toggle("Email", is_on=False),
                                    ),
                                ),
                            ],
                            header="Account",
                        ),
                        nib.Section(
                            controls=[
                                nib.NavigationLink(
                                    "Privacy",
                                    destination=page(
                                        "Privacy",
                                        nib.Toggle("Share analytics", is_on=False),
                                    ),
                                ),
                                nib.NavigationLink(
                                    "About",
                                    destination=page("About", nib.Text("nib 1.2.0")),
                                ),
                            ],
                            header="General",
                        ),
                    ]
                )
            ],
            width=280,
            height=260,
        )
    )


nib.run(main)