Duolith docs

navigation-kit

A small inbox-and-library app showing every kit navigation part (TabBar with a badge and a rail on wide windows, NavHeader with large titles and automatic back, Toolbar, Breadcrumbs, Link), light and dark.

Run it

cargo run -p duolith-cli -- dev examples/navigation-kit     # live, reloads on save
cargo run -p duolith-cli -- build examples/navigation-kit   # static output in examples/navigation-kit/dist

The folder on the web: examples/navigation-kit.

Source

pages/_layout.duo
---
import { state } from "@duolith/client";
import { TabBar, TabBarItem } from "@duolith/kit";

// The ids of unread messages, shared with the inbox screens, so the badge follows them.
let unread = state([1, 2, 3], { shared: "unread" });
---

<view class="lg:row">
  <slot />
  <TabBar rail>
    <TabBarItem label="Home" icon="home" href="/" />
    <TabBarItem label="Inbox" icon="inbox" href="/inbox" badge={unread.length} />
    <TabBarItem label="Library" icon="bookmark" href="/library" />
    <TabBarItem label="Settings" icon="settings" href="/settings" />
  </TabBar>
</view>
pages/inbox/[id].duo
---
import { back, route, state } from "@duolith/client";
import { Breadcrumbs, IconButton, NavHeader, Toolbar } from "@duolith/kit";

type Message = { id: number; from: string; subject: string; body: string };

let { id } = route();
const number = Number(id);

const messages: [Message, ...Message[]] = [
  { id: 1, from: "Ada", subject: "Quarterly plan", body: "Draft attached. The second half leans on the new engine; can we talk it through on Thursday?" },
  { id: 2, from: "Grace", subject: "Found the moth", body: "Relay 70, panel F. Taped it into the log book for posterity." },
  { id: 3, from: "Alan", subject: "Long walk, short proof", body: "Twenty miles along the river and the lemma finally fell out." },
  { id: 4, from: "Katherine", subject: "Checked the numbers twice", body: "The trajectory holds. Splashdown within a mile of the ship." },
  { id: 5, from: "Edsger", subject: "Notes on structure", body: "Wrote it by hand again. The fountain pen keeps me honest." },
];
const message = messages.find((each) => each.id === number) ?? messages[0];

// Opening a message marks it read, so the Inbox tab's badge counts one fewer.
let unread = state([1, 2, 3], { shared: "unread" });
unread = unread.filter((each) => each !== message.id);

let starred = state(false);
let status = state("");
---

<screen title={message.subject}>
  <NavHeader title="Message">
    <piece name="trailing">
      <IconButton icon="trash" label="Delete" variant="danger" onPress={back} />
    </piece>
  </NavHeader>

  <view class="gap-4 p-4">
    <view class="hidden lg:flex">
      <Breadcrumbs items={[{ label: "Inbox", href: "/inbox" }, { label: message.subject }]} />
    </view>

    <view class="gap-2">
      <text role="heading" class="text-heading text-ink">{message.subject}</text>
      <text class="text-caption text-muted">From {message.from}</text>
    </view>
    <text class="text-body text-ink">{message.body}</text>

    <Toolbar label="Message actions">
      <IconButton
        icon="send"
        label="Reply"
        onPress={() => (status = `Replying to ${message.from}`)}
      />
      <IconButton
        icon="star"
        label="Star"
        selected={starred}
        onPress={() => (starred = !starred)}
      />
      <IconButton icon="share" label="Share" onPress={() => (status = "Link copied")} />
      <IconButton icon="download" label="Save" onPress={() => (status = "Saved for later")} />
    </Toolbar>
    <text role="status" class="text-caption text-muted">{status}</text>
  </view>
</screen>
pages/inbox/index.duo
---
import { state } from "@duolith/client";
import { Badge, Button, ListRow, NavHeader } from "@duolith/kit";

let unread = state([1, 2, 3], { shared: "unread" });

const messages = [
  { id: 1, from: "Ada", subject: "Quarterly plan" },
  { id: 2, from: "Grace", subject: "Found the moth" },
  { id: 3, from: "Alan", subject: "Long walk, short proof" },
  { id: 4, from: "Katherine", subject: "Checked the numbers twice" },
  { id: 5, from: "Edsger", subject: "Notes on structure" },
];
---

<screen title="Inbox">
  <NavHeader title="Inbox" large>
    <piece name="leading">
      <Button
        label="Read all"
        variant="ghost"
        size="sm"
        disabled={unread.length === 0}
        onPress={() => (unread = [])}
      />
    </piece>
  </NavHeader>

  <view class="gap-4 p-4">
    <list
      each={messages}
      as="message"
      key={message.id}
      class="rows rounded-lg border overflow-hidden"
    >
      <ListRow
        title={message.subject}
        subtitle={`From ${message.from}`}
        href={`/inbox/${message.id}`}
        chevron
      >
        <show when={unread.includes(message.id)}>
          <Badge label="New" tone="primary" />
        </show>
      </ListRow>
    </list>
    <text class="text-caption text-muted px-4">
      Opening a message marks it read; the tab's badge follows.
    </text>
  </view>
</screen>

<style>
  .rows > * + * {
    border-top: 1px solid var(--color-border);
  }
</style>
pages/index.duo
---
import { navigate } from "@duolith/client";
import { IconButton, Link, ListRow, NavHeader, Section } from "@duolith/kit";
---

<screen title="Home">
  <NavHeader title="Home" large>
    <piece name="trailing">
      <IconButton icon="bell" label="Notifications" onPress={() => navigate("/inbox")} />
    </piece>
  </NavHeader>

  <view class="gap-6 p-4 pb-8">
    <text class="text-body text-muted">
      Every screen here is built from the kit's navigation parts: the tab bar below (a rail on a
      wide window), a header on each screen, a toolbar on a message and breadcrumbs in the library.
      Scroll to see the large title hand over to the bar.
    </text>

    <Section title="Jump back in">
      <ListRow
        title="Quarterly plan"
        subtitle="From Ada, unread"
        icon="mail"
        href="/inbox/1"
        chevron
      />
      <ListRow
        title="Field notes"
        subtitle="Library, Travel"
        icon="bookmark"
        href="/library/travel/field-notes"
        chevron
      />
      <ListRow title="About this app" icon="info" href="/settings/about" chevron />
    </Section>

    <Section
      title="Shelves"
      footer="A shelf opens inside the Library tab, with breadcrumbs back up."
    >
      <ListRow title="Travel" value="3 books" href="/library/travel" chevron />
      <ListRow title="Cooking" value="2 books" href="/library/cooking" chevron />
      <ListRow title="Computing" value="3 books" href="/library/computing" chevron />
    </Section>

    <view class="gap-3 px-4">
      <text class="text-label text-ink">Links</text>
      <Link href="/settings">Open Settings</Link>
      <Link href="https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/">
        The toolbar pattern, on another site
      </Link>
    </view>
  </view>
</screen>
pages/library/[shelf]/[book].duo
---
import { route, state } from "@duolith/client";
import { Breadcrumbs, IconButton, Link, NavHeader, Toolbar } from "@duolith/kit";

let { shelf, book } = route();

// "field-notes" reads as "Field notes".
function named(slug: string): string {
  const words = slug.replace(/-/g, " ");
  return words.charAt(0).toUpperCase() + words.slice(1);
}

const title = named(book);
const shelfName = named(shelf);
let saved = state(false);
// 0, 1 or 2: caption, body or heading text.
let size = state(1);
---

<screen title={title}>
  <NavHeader title={title}>
    <piece name="trailing">
      <IconButton icon="bookmark" label="Keep" selected={saved} onPress={() => (saved = !saved)} />
    </piece>
  </NavHeader>

  <view class="gap-4 p-4">
    <Breadcrumbs
      items={[{ label: "Library", href: "/library" }, { label: shelfName, href: `/library/${shelf}` }, { label: title }]}
    />
    <text role="heading" class="text-title text-ink">{title}</text>
    <Toolbar label="Reading" flat>
      <IconButton
        icon="minus"
        label="Smaller text"
        size="sm"
        variant="secondary"
        disabled={size === 0}
        onPress={() => (size -= 1)}
      />
      <IconButton
        icon="plus"
        label="Larger text"
        size="sm"
        variant="secondary"
        disabled={size === 2}
        onPress={() => (size += 1)}
      />
      <IconButton icon="moon" label="Night reading" size="sm" variant="secondary" />
    </Toolbar>
    <text
      class="text-ink"
      class:text-caption={size === 0}
      class:text-body={size === 1}
      class:text-heading={size === 2}
    >
      The first chapter opens at a harbour before dawn. Nothing moves but the gulls, and the writer,
      who has a long road ahead and only a notebook to keep it in. Every page after this one is a
      day of that road.
    </text>
    <Link href="https://www.gutenberg.org/">Find more books like this, on another site</Link>
  </view>
</screen>
pages/library/[shelf]/index.duo
---
import { route } from "@duolith/client";
import { Breadcrumbs, ListRow, NavHeader } from "@duolith/kit";

let { shelf } = route();

const shelves: Record<string, { name: string; books: { slug: string; title: string; author: string }[] }> = {
  travel: {
    name: "Travel",
    books: [
      { slug: "field-notes", title: "Field notes", author: "M. Kingsley" },
      { slug: "the-long-road", title: "The long road", author: "I. Bird" },
      { slug: "harbour-lights", title: "Harbour lights", author: "F. Stark" },
    ],
  },
  cooking: {
    name: "Cooking",
    books: [
      { slug: "salt", title: "Salt", author: "E. David" },
      { slug: "one-pot", title: "One pot", author: "C. Roden" },
    ],
  },
  computing: {
    name: "Computing",
    books: [
      { slug: "structure", title: "Notes on structure", author: "E. Dijkstra" },
      { slug: "the-engine", title: "The engine", author: "A. Lovelace" },
      { slug: "relays", title: "Relays", author: "G. Hopper" },
    ],
  },
};
const current = shelves[shelf] ?? { name: "Unknown shelf", books: [] };
---

<screen title={current.name}>
  <NavHeader title={current.name} />

  <view class="gap-4 p-4">
    <Breadcrumbs items={[{ label: "Library", href: "/library" }, { label: current.name }]} />
    <text class="text-caption font-semibold text-muted px-4">{current.books.length} books</text>
    <list
      each={current.books}
      as="book"
      key={book.slug}
      class="rows rounded-lg border overflow-hidden"
    >
      <ListRow
        title={book.title}
        subtitle={book.author}
        href={`/library/${shelf}/${book.slug}`}
        chevron
      />
    </list>
  </view>
</screen>

<style>
  .rows > * + * {
    border-top: 1px solid var(--color-border);
  }
</style>
pages/library/index.duo
---
import { ListRow, NavHeader, Section } from "@duolith/kit";
---

<screen title="Library">
  <NavHeader title="Library" large />

  <view class="gap-4 p-4">
    <Section title="Shelves">
      <ListRow title="Travel" icon="globe" value="3 books" href="/library/travel" chevron />
      <ListRow title="Cooking" icon="heart" value="2 books" href="/library/cooking" chevron />
      <ListRow
        title="Computing"
        icon="settings"
        value="3 books"
        href="/library/computing"
        chevron
      />
    </Section>
  </view>
</screen>
pages/settings/about.duo
---
import { Link, NavHeader } from "@duolith/kit";
---

<screen title="About">
  <NavHeader title="About" />

  <view class="gap-4 p-4">
    <text class="text-body text-ink">
      A small app built with the kit's navigation parts: TabBar and TabBarItem, NavHeader, Toolbar,
      Breadcrumbs and Link. The header's back button appears because this screen was opened from
      another; open this address in a new tab and it is gone.
    </text>
    <view class="gap-3">
      <Link href="/">Back to Home</Link>
      <Link href="https://www.w3.org/WAI/ARIA/apg/patterns/landmarks/">
        Landmarks, on another site
      </Link>
    </view>
  </view>
</screen>
pages/settings/index.duo
---
import { state } from "@duolith/client";
import { ListRow, NavHeader, Section, ToggleRow } from "@duolith/kit";

let notifications = state(true);
---

<screen title="Settings">
  <NavHeader title="Settings" large />

  <view class="gap-6 p-4">
    <Section title="General">
      <ToggleRow label="Notifications" icon="bell" bind:checked={notifications} />
      <ListRow title="Privacy" icon="lock" href="/settings/privacy" chevron />
      <ListRow title="About this app" icon="info" href="/settings/about" chevron />
    </Section>
  </view>
</screen>
pages/settings/privacy.duo
---
import { state } from "@duolith/client";
import { NavHeader, Section, ToggleRow } from "@duolith/kit";

let analytics = state(false);
let readReceipts = state(true);
---

<screen title="Privacy">
  <NavHeader title="Privacy" />

  <view class="gap-6 p-4">
    <Section footer="Both stay on this device.">
      <ToggleRow label="Share analytics" bind:checked={analytics} />
      <ToggleRow label="Read receipts" bind:checked={readReceipts} />
    </Section>
  </view>
</screen>