components
A profile and settings screen built from three components (Card, Avatar, SettingRow) with props, a slot and scoped styles.
Run it
cargo run -p duolith-cli -- dev examples/components # live, reloads on save
cargo run -p duolith-cli -- build examples/components # static output in examples/components/dist
The folder on the web: examples/components.
Features it shows
| Feature | Where | Screenshot |
|---|---|---|
<toggle> | gallery pages/language.duo (Two-way binding); components pages/index.duo | gallery-language-binding.png |
<slot>, named slots | gallery components/Panel.duo; components components/Card.duo | gallery-language-components.png |
Source
behaviours/character-count.behaviour.duo
---
// characterCount: how many characters a text has left before its limit. Its input stays live,
// so the count follows the text as it is typed.
import { derived } from "@duolith/client";
export function characterCount(input: { text: string; limit: number }) {
let left = derived(() => input.limit - input.text.length);
let tooLong = derived(() => left < 0);
return { left, tooLong };
}
---behaviours/disclosure.behaviour.ts
// disclosure: something that opens and closes. Card and Faq both use it (decision 0152),
// so the open state, the button's word and the press that toggles it are written once.
// A behaviour may be written as TypeScript like this, or as a .behaviour.duo file.
import { derived, state } from "@duolith/client";
export type DisclosureInput = {
/** Whether it starts open. */
open: boolean;
};
export function disclosure(input: DisclosureInput) {
let open = state(input.open);
let action = derived(() => (open ? "Hide" : "Show"));
function toggle() {
open = !open;
}
return { open, action, toggle };
}components/Avatar.duo
---
import { derived, props } from "@duolith/client";
let { name, online = false } = props<{ name: string; online?: boolean }>();
let initials = derived(() =>
name
.split(" ")
.filter((word) => word !== "")
.slice(0, 2)
.map((word) => word[0]?.toUpperCase())
.join(""),
);
---
<view class="avatar items-center justify-center bg-primary" label={`Avatar for ${name}`}>
<text class="text-label text-on-primary">{initials}</text>
<show when={online}>
<view class="dot" />
</show>
</view>
<style>
.avatar {
position: relative;
width: 56px;
height: 56px;
border-radius: 9999px;
}
.dot {
position: absolute;
right: 0;
bottom: 0;
width: 16px;
height: 16px;
border-radius: 9999px;
background: #2fb46c;
border: 3px solid var(--color-surface);
}
</style>components/Card.duo
---
import { props } from "@duolith/client";
import { disclosure } from "../behaviours/disclosure.behaviour.ts";
let { title, subtitle, collapsible = false } = props<{ title: string; subtitle?: string; collapsible?: boolean }>();
// Kept whole: body.open is state, body.action a derived value, body.toggle a function.
let body = disclosure({ open: true });
---
<view class="card gap-3 p-4 rounded-lg bg-surface border">
<view class="row items-center gap-3">
<view class="grow gap-1">
<text class="text-heading text-ink">{title}</text>
<show when={subtitle}>
<text class="text-caption text-muted">{subtitle}</text>
</show>
</view>
<show when={collapsible}>
<button class="px-2 py-1 rounded-md" label={`${body.action} ${title}`} on:press={body.toggle}>
<text class="text-label text-primary">{body.action}</text>
</button>
</show>
</view>
<show when={body.open}>
<slot />
</show>
</view>
<style>
.card {
box-shadow: 0 1px 3px rgb(0 0 0 / 0.08);
}
</style>components/Card.test.ts
// Tests for the components on their own and on the profile page:
// duolith test examples/components
import { expect, render, renderComponent, test } from "@duolith/testing";
test("a card shows its title, its subtitle and what it is given", async () => {
const screen = await renderComponent("Card", { props: { title: "You", subtitle: "Signed in" }, slot: "Hello" });
await expect(screen.byText("You", { exact: true })).toBeShown();
await expect(screen.byText("Signed in")).toBeShown();
await expect(screen.byText("Hello")).toBeShown();
});
test("a card without a subtitle leaves it out", async () => {
const screen = await renderComponent("components/Card.duo", { props: { title: "Plain" } });
await expect(screen.byText("Plain")).toBeShown();
await expect(screen.byText("Signed in")).toBeHidden();
});
test("an avatar shows the initials of the first two names", async () => {
const screen = await renderComponent("Avatar", { props: { name: "Grace Brewster Hopper" } });
await expect(screen.byLabel("Avatar for Grace Brewster Hopper")).toHaveText("GB");
});
test("renaming on the profile page updates the handle", async () => {
const screen = await render("/");
await screen.byLabel("Name").type("Alan Turing");
await expect(screen.byText("@alanturing")).toBeShown();
});
test("a collapsible card hides and shows what it is given", async () => {
const screen = await renderComponent("Card", { props: { title: "Settings", collapsible: true }, slot: "Inside" });
await expect(screen.byText("Inside")).toBeShown();
await screen.byLabel("Hide Settings").press();
await expect(screen.byText("Inside")).toBeHidden();
await screen.byLabel("Show Settings").press();
await expect(screen.byText("Inside")).toBeShown();
});
test("two components share one behaviour, each with its own state", async () => {
const screen = await render("/");
await expect(screen.byText("Everyone who can see your posts.")).toBeHidden();
await screen.byLabel("Show the answer").nth(0).press();
await expect(screen.byText("Everyone who can see your posts.")).toBeShown();
await expect(screen.byText("Yes: it follows your name.")).toBeHidden();
await expect(screen.byText("Anyone can see your posts.")).toBeShown();
});
test("a behaviour's input stays live: the count follows the name as it is typed", async () => {
const screen = await render("/");
await expect(screen.byText("12 characters left")).toBeShown();
await screen.byLabel("Name").type("Alan Turing");
await expect(screen.byText("13 characters left")).toBeShown();
});components/Faq.duo
---
import { props } from "@duolith/client";
import { disclosure } from "../behaviours/disclosure.behaviour.ts";
let { question, answer } = props<{ question: string; answer: string }>();
// Unpacked: open is state, action a derived value, toggle a function.
let { open, action, toggle } = disclosure({ open: false });
---
<view class="gap-1 py-2">
<view class="row items-center gap-3">
<text class="grow text-body text-ink">{question}</text>
<button class="px-2 py-1 rounded-md" label={`${action} the answer`} on:press={toggle}>
<text class="text-label text-primary">{action}</text>
</button>
</view>
<show when={open}>
<text class="text-caption text-muted">{answer}</text>
</show>
</view>components/SettingRow.duo
---
import { props } from "@duolith/client";
let { label, hint, checked, onChange } = props<{
label: string;
hint?: string;
checked: boolean;
onChange: (on: boolean) => void;
}>();
---
<view class="row items-center gap-3 py-2">
<view class="grow gap-1">
<text class="text-body text-ink">{label}</text>
<show when={hint}>
<text class="text-caption text-muted">{hint}</text>
</show>
</view>
<toggle label={label} checked={checked} on:change={(event) => onChange(event.checked ?? false)} />
</view>pages/index.duo
---
import { derived, state } from "@duolith/client";
import Avatar from "../components/Avatar.duo";
import Card from "../components/Card.duo";
import SettingRow from "../components/SettingRow.duo";
import Faq from "../components/Faq.duo";
import { characterCount } from "../behaviours/character-count.behaviour.duo";
let name = state("Ada Lovelace");
let notifications = state(true);
let digest = state(false);
let location = state(false);
let isPrivate = state(false);
let handle = derived(() => "@" + name.toLowerCase().replace(/[^a-z]+/g, ""));
let enabled = derived(() => [notifications, digest, location].filter((on) => on).length);
// A behaviour called from a page: its input follows name as it is typed.
let { left, tooLong } = characterCount({ text: name, limit: 24 });
---
<screen title="Profile" class="p-4 gap-4 bg-canvas">
<text class="text-title text-ink">Profile</text>
<Card title="You">
<view class="row items-center gap-3">
<Avatar name={name} online={notifications} />
<view class="gap-1">
<text class="text-label text-ink">{name}</text>
<text class="text-caption text-muted">{handle}</text>
</view>
</view>
<input label="Name" placeholder="Your name" bind:value={name} />
<text class="text-caption text-muted" class:text-danger={tooLong}>{left} characters left</text>
</Card>
<Card title="Settings" subtitle={`${enabled} of 3 on`} collapsible>
<SettingRow
label="Notifications"
hint="Replies and mentions"
checked={notifications}
onChange={(on) => (notifications = on)}
/>
<SettingRow label="Weekly digest" checked={digest} onChange={(on) => (digest = on)} />
<SettingRow
label="Share location"
hint="Only with people you follow"
checked={location}
onChange={(on) => (location = on)}
/>
</Card>
<Card title="Questions">
<Faq question="Who sees my handle?" answer="Everyone who can see your posts." />
<Faq question="Can I change it later?" answer="Yes: it follows your name." />
</Card>
<view class="card row items-center gap-3 p-4 rounded-lg">
<view class="grow gap-1">
<text class="text-body text-ink">Private account</text>
<show when={isPrivate}>
<text class="text-caption text-muted">Only people you approve see your posts.</text>
</show>
<otherwise>
<text class="text-caption text-muted">Anyone can see your posts.</text>
</otherwise>
</view>
<toggle label="Private account" bind:checked={isPrivate} />
</view>
</screen>
<style>
/* The page's own .card: Card's .card is scoped to Card, so the two never meet. */
.card {
border: 1px dashed var(--color-primary);
}
</style>