navigation
A tab bar layout built with the kit's TabBar and NavHeader (Feed, Search, Profile), post screens at /post/[id] with back, a settings stack inside a nested Profile layout, a guarded /account, a [...topic] help route and a not-found screen.
Run it
cargo run -p duolith-cli -- dev examples/navigation # live, reloads on save
cargo run -p duolith-cli -- build examples/navigation # static output in examples/navigation/dist
The folder on the web: examples/navigation.
Features it shows
| Feature | Where | Screenshot |
|---|---|---|
Layouts (_layout.duo), nested | gallery pages/_layout.duo; navigation pages/profile/_layout.duo | gallery-home.png, navigation-settings.png |
A list straight inside a screen, recycled through Lynx's <list> | navigation pages/(tabs)/index.duo | packages/engine/test/native-in-browser.test.ts |
Source
components/Post.duo
---
import { props } from "@duolith/client";
let { id, full = false } = props<{ id: number; full?: boolean }>();
type Post = { author: string; caption: string; body: string; likes: number };
const posts: [Post, ...Post[]] = [
{ author: "Ada", caption: "First light over the harbour.", body: "Up at five to catch it. The water was glass until the ferry came through.", likes: 12 },
{ author: "Linus", caption: "New desk, same bugs.", body: "Standing desk, two monitors, and the flaky test is still flaky.", likes: 4 },
{ author: "Grace", caption: "Found the moth.", body: "Relay 70, panel F. Taped it into the log book for posterity.", likes: 31 },
{ author: "Alan", caption: "Long walk, short proof.", body: "Twenty miles along the river and the lemma finally fell out.", likes: 18 },
{ author: "Katherine", caption: "Checked the numbers twice.", body: "The trajectory holds. Splashdown within a mile of the ship.", likes: 27 },
{ author: "Edsger", caption: "Notes on structure.", body: "Wrote it by hand again. The fountain pen keeps me honest.", likes: 9 },
];
function post() {
return posts[(id - 1) % posts.length] ?? posts[0];
}
---
<view class="gap-2 p-4 rounded-lg bg-surface border">
<view class="row items-center justify-between">
<text class="text-label text-ink">{post().author}</text>
<text class="text-caption text-muted">#{id}</text>
</view>
<text class="text-body text-ink">{post().caption}</text>
<show when={full}>
<text class="text-body text-muted">{post().body}</text>
<text class="text-caption text-muted">♥ {post().likes} likes</text>
</show>
</view>pages/_layout.duo
---
import { route } from "@duolith/client";
import { TabBar, TabBarItem } from "@duolith/kit";
// Posts open inside the Feed tab, so Feed is current there too; every other
// screen not under /search belongs to Profile.
function isFeed() {
return route().path === "/" || route().path.startsWith("/post/");
}
function isProfile() {
return !isFeed() && !route().path.startsWith("/search");
}
---
<view class="bg-canvas">
<slot />
<TabBar>
<TabBarItem label="Feed" icon="home" href="/" current={isFeed()} />
<TabBarItem label="Search" icon="search" href="/search" />
<TabBarItem label="Profile" icon="user" href="/profile" current={isProfile()} />
</TabBar>
</view>pages/_not-found.duo
---
import { route } from "@duolith/client";
let { path } = route();
---
<screen title="Not found" class="p-4 gap-3 items-center justify-center">
<text class="text-title text-ink">Nothing here</text>
<text class="text-body text-muted">No screen is at {path}.</text>
<view href="/" class="px-4 py-2 rounded-md bg-primary">
<text class="text-label text-on-primary">Go to the feed</text>
</view>
</screen>pages/account.duo
---
import { guard, state } from "@duolith/client";
let signedIn = state(false, { shared: "signed-in" });
// Until the user signs in, opening /account goes to /sign-in instead. Signing
// out here sends them there too, because the guard follows signedIn.
guard(() => signedIn || "/sign-in?next=/account");
---
<screen title="Account" class="p-4 gap-3">
<text class="text-title text-ink">Account</text>
<view class="gap-1 p-4 rounded-lg bg-surface border">
<text class="text-label text-ink">ada@example.com</text>
<text class="text-caption text-muted">Member since 1843</text>
</view>
<view class="row items-center justify-between p-3 rounded-md bg-surface border">
<text class="text-body text-ink">Signed in</text>
<toggle label="Signed in" bind:checked={signedIn} />
</view>
</screen>pages/help/[...topic].duo
---
import { back, route } from "@duolith/client";
// A rest parameter takes the rest of the path: /help/getting-started/tabs
// gives topic "getting-started/tabs".
let { topic } = route();
const parts = topic.split("/");
---
<screen title={`Help: ${parts.at(-1)}`} class="p-4 gap-3">
<button
label="Back"
class="self-start px-3 py-2 rounded-md bg-raised text-label text-ink"
on:press={back}
>
‹ Back
</button>
<text class="text-title text-ink">Help</text>
<list each={parts} as="part" index="i" key={i} class="row gap-2">
<text class="text-caption text-muted">{i > 0 ? "› " : ""}{part}</text>
</list>
<text class="text-body text-ink">
Tabs follow the route: the tab whose part of the app you are in is marked current.
</text>
</screen>pages/index.duo
---
import Post from "../components/Post.duo";
const ids = Array.from({ length: 12 }, (_, index) => index + 1);
---
<screen title="Feed" class="p-4 gap-3">
<text class="text-title text-ink">Feed</text>
<text class="text-caption text-muted">
Press a post to open it. Back, or the browser's back button, returns here at the same scroll
position.
</text>
<list each={ids} as="id" key={id} class="gap-3">
<view href={`/post/${id}`} class="pressed:opacity-75">
<Post id={id} />
</view>
</list>
</screen>pages/post/[id].duo
---
import { route } from "@duolith/client";
import { NavHeader } from "@duolith/kit";
import Post from "../../components/Post.duo";
// The screen was opened for this id; another id opens another screen.
let { id } = route();
const number = Number(id);
---
<screen title={`Post ${id}`}>
<!-- Opened from a shared link, there is nothing to go back to: the header offers the feed instead. -->
<NavHeader title={`Post ${id}`} up="/" upTitle="Feed" />
<view class="p-4 gap-4">
<Post id={number} full />
<view class="row gap-3">
<show when={number > 1}>
<view
href={`/post/${number - 1}`}
class="grow p-3 rounded-md bg-surface border items-center"
>
<text class="text-label text-primary">‹ Post {number - 1}</text>
</view>
</show>
<view href={`/post/${number + 1}`} class="grow p-3 rounded-md bg-surface border items-center">
<text class="text-label text-primary">Post {number + 1} ›</text>
</view>
</view>
</view>
</screen>pages/profile/_layout.duo
---
import { effect, route, state, untracked } from "@duolith/client";
import { NavHeader } from "@duolith/kit";
// The layout stays while the user moves between the screens inside it, so
// this count keeps going until they leave Profile.
let opened = state(0);
effect(() => {
route().path;
untracked(() => (opened += 1));
});
// One level up the Profile stack, offered when a screen was opened from its address.
function parent(): string | undefined {
const path = route().path;
return path === "/profile" ? undefined : path.slice(0, path.lastIndexOf("/"));
}
---
<view>
<!-- In a layout the header follows the screen in front: Back shows once a screen is pushed,
and Up when there is nothing to go back to. -->
<NavHeader title="Profile" up={parent()}>
<piece name="trailing">
<text class="text-caption text-muted truncate">{opened} screens</text>
</piece>
</NavHeader>
<slot />
</view>pages/profile/index.duo
---
import { state } from "@duolith/client";
let signedIn = state(false, { shared: "signed-in" });
// No page serves this path, on purpose. A path written as text, or held
// in a const, must name a route (E0353), so the not-found demo joins it
// from parts; the router checks it when it is followed.
const nowhere = ["", "nowhere"].join("/");
---
<screen title="Profile" class="p-4 gap-3">
<view class="row items-center gap-3">
<view class="w-12 h-12 rounded-full bg-primary items-center justify-center">
<text class="text-heading text-on-primary">A</text>
</view>
<view class="gap-1">
<text class="text-heading text-ink">Ada Lovelace</text>
<text class="text-caption text-muted">{signedIn ? "Signed in" : "Signed out"}</text>
</view>
</view>
<view class="gap-2">
<view href="/profile/settings" class="row justify-between p-3 rounded-md bg-surface border">
<text class="text-body text-ink">Settings</text>
<text class="text-label text-muted">›</text>
</view>
<view href="/account" class="row justify-between p-3 rounded-md bg-surface border">
<text class="text-body text-ink">Account (needs sign-in)</text>
<text class="text-label text-muted">›</text>
</view>
<view
href="/help/getting-started/tabs"
class="row justify-between p-3 rounded-md bg-surface border"
>
<text class="text-body text-ink">Help: tabs</text>
<text class="text-label text-muted">›</text>
</view>
<view href={nowhere} class="row justify-between p-3 rounded-md bg-surface border">
<text class="text-body text-ink">A link to nowhere</text>
<text class="text-label text-muted">›</text>
</view>
<view href="https://example.com" class="row justify-between p-3 rounded-md bg-surface border">
<text class="text-body text-ink">example.com (opens as a normal link)</text>
<text class="text-label text-muted">↗</text>
</view>
</view>
</screen>pages/profile/settings/index.duo
---
import { state } from "@duolith/client";
let compact = state(false, { shared: "compact" });
---
<screen title="Settings" class="p-4 gap-3">
<text class="text-title text-ink">Settings</text>
<view class="gap-2">
<view
href="/profile/settings/notifications"
class="row justify-between p-3 rounded-md bg-surface border"
>
<text class="text-body text-ink">Notifications</text>
<text class="text-label text-muted">›</text>
</view>
<view
href="/profile/settings/privacy"
class="row justify-between p-3 rounded-md bg-surface border"
>
<text class="text-body text-ink">Privacy</text>
<text class="text-label text-muted">›</text>
</view>
<view class="row items-center justify-between p-3 rounded-md bg-surface border">
<text class="text-body text-ink">Compact feed</text>
<toggle label="Compact feed" bind:checked={compact} />
</view>
</view>
</screen>pages/profile/settings/notifications.duo
---
import { state } from "@duolith/client";
let replies = state(true, { shared: "notify-replies" });
let digest = state(false, { shared: "notify-digest" });
---
<screen title="Notifications" class="p-4 gap-3">
<text class="text-title text-ink">Notifications</text>
<view class="row items-center justify-between p-3 rounded-md bg-surface border">
<text class="text-body text-ink">Replies and mentions</text>
<toggle label="Replies and mentions" bind:checked={replies} />
</view>
<view class="row items-center justify-between p-3 rounded-md bg-surface border">
<text class="text-body text-ink">Weekly digest</text>
<toggle label="Weekly digest" bind:checked={digest} />
</view>
</screen>pages/profile/settings/privacy.duo
---
import { navigate, state } from "@duolith/client";
let isPrivate = state(false, { shared: "private-account" });
---
<screen title="Privacy" class="p-4 gap-3">
<text class="text-title text-ink">Privacy</text>
<view class="row items-center justify-between p-3 rounded-md bg-surface border">
<text class="text-body text-ink">Private account</text>
<toggle label="Private account" bind:checked={isPrivate} />
</view>
<button
class="p-3 rounded-md bg-raised text-label text-ink pressed:opacity-75"
on:press={() => navigate("/profile", { replace: true })}
>
Done: back to Profile, replacing this screen
</button>
</screen>pages/search.duo
---
import { derived, state } from "@duolith/client";
const topics = [
{ id: 1, name: "Harbour mornings" },
{ id: 2, name: "Desk setups" },
{ id: 3, name: "Debugging stories" },
{ id: 4, name: "Proofs on foot" },
{ id: 5, name: "Orbital mechanics" },
{ id: 6, name: "Handwritten notes" },
];
let query = state("");
let found = derived(() => topics.filter((topic) => topic.name.toLowerCase().includes(query.toLowerCase())));
---
<screen title="Search" class="p-4 gap-3">
<text class="text-title text-ink">Search</text>
<input type="search" placeholder="Search topics" label="Search topics" bind:value={query} />
<text class="text-caption text-muted">{found.length} of {topics.length} topics</text>
<list each={found} as="topic" key={topic.id} class="gap-2">
<view
href={`/post/${topic.id}`}
class="row items-center justify-between p-3 rounded-md bg-surface border"
>
<text class="text-body text-ink">{topic.name}</text>
<text class="text-label text-muted">›</text>
</view>
</list>
</screen>pages/sign-in.duo
---
import { navigate, route, state } from "@duolith/client";
let { query } = route();
let signedIn = state(false, { shared: "signed-in" });
// `next` comes from the address, so anyone can write it: follow it only when
// it is a path in this app. "//evil.com" and "/\evil.com" name another site.
const next = isAppPath(query["next"]) ? query["next"] : undefined;
function isAppPath(path: string | undefined): path is string {
return path !== undefined && path.startsWith("/") && !path.startsWith("//") && !path.startsWith("/\\");
}
function carryOn() {
navigate(next ?? "/profile", { replace: true });
}
---
<screen title="Sign in" class="p-4 gap-4">
<text class="text-title text-ink">Sign in</text>
<show when={next}>
<text class="text-body text-muted">Sign in to open {next}.</text>
</show>
<view class="row items-center justify-between p-3 rounded-md bg-surface border">
<text class="text-body text-ink">Signed in</text>
<toggle label="Signed in" bind:checked={signedIn} />
</view>
<show when={signedIn}>
<button
class="p-3 rounded-md bg-primary text-on-primary text-label pressed:opacity-75"
on:press={carryOn}
>
Continue
</button>
</show>
<otherwise>
<text class="text-caption text-muted">Turn on Signed in to continue.</text>
</otherwise>
</screen>