Nested pages in favorites, workspace and private sections that you open, drag, rename and add to.
🎙️ Interview: Fieldwork
Move this page in the tree and its trail follows.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Breadcrumbs } from "./ui/uicontext/components/kit/Breadcrumbs.ts"import { movePage, PageSection, PageTree, trailOf, TreePage } from "./ui/uicontext/components/kit/PageTree.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"// A workspace's pages beside the page that's open. The breadcrumbs come from the same pages with// `trailOf`, so moving or renaming a page in the tree moves its trail too. Drag a row onto another,// or press Alt with the arrow keys, F2 to rename and Shift+F10 for a row's menu.const styles = stylex.create({ frame: { display: "grid", gridTemplateColumns: { default: "15rem minmax(0, 1fr)", "@media (max-width: 640px)": "minmax(0, 1fr)" }, gap: space.xl, inlineSize: "100%" }, side: { minInlineSize: 0 }, page: { display: "flex", flexDirection: "column", gap: space.lg, minInlineSize: 0 }, title: { margin: 0, fontSize: 28, fontWeight: 650, letterSpacing: "-0.02em", color: color.fg }, hint: { margin: 0, color: color.muted, fontSize: font.sm },})const seed: ReadonlyArray<TreePage> = [ { id: "product", title: "Product", icon: "📘", section: "workspace" }, { id: "spec", title: "Shared docs spec", icon: "📝", parent: "product" }, { id: "research", title: "Research notes", icon: "🧪", parent: "product" }, { id: "fieldwork", title: "Interview: Fieldwork", icon: "🎙️", parent: "research" }, { id: "eng", title: "Engineering", icon: "🛠️", section: "workspace" }, { id: "release", title: "Release checklist", icon: "✅", parent: "eng" }, { id: "weekly", title: "Weekly notes", icon: "🌱", section: "private" },]const Message = UiSchema.TaggedEnum({ OpenedPage: { page: Schema.String }, CreatedPage: { parent: Schema.NullOr(Schema.String), section: PageSection }, RenamedPage: { page: Schema.String, title: Schema.String }, MovedPage: { page: Schema.String, parent: Schema.NullOr(Schema.String), section: PageSection, before: Schema.NullOr(Schema.String) }, FavoritedPage: { page: Schema.String, favorite: Schema.Boolean }, TrashedPage: { page: Schema.String },})export const WorkspacePages = Html.component({ name: "WorkspacePages", model: Schema.Struct({ pages: Schema.Array(TreePage), favorites: Schema.Array(Schema.String), current: Schema.String, next: Schema.Number }), message: Message,}).make({ init: () => ({ pages: seed, favorites: ["spec"], current: "fieldwork", next: 1 }), update: { OpenedPage: ($, { page }) => $.evolve({ current: page }), CreatedPage: ($, { parent, section }) => { const id = `new-${$.model.next}` const page: TreePage = parent === null ? { id, title: "", section } : { id, title: "", parent } return $.evolve({ pages: (p) => [...p, page], current: id, next: (n) => n + 1 }) }, RenamedPage: ($, { page, title }) => $.evolve({ pages: (p) => p.map((x) => (x.id === page ? { ...x, title } : x)) }), MovedPage: ($, move) => $.evolve({ pages: (p) => movePage(p, move) }), FavoritedPage: ($, { page, favorite }) => $.evolve({ favorites: (f) => (favorite ? [...f, page] : f.filter((x) => x !== page)) }), TrashedPage: ($, { page }) => $.evolve({ pages: (p) => p.filter((x) => x.id !== page && x.parent !== page) }), }, view: (h, { pages, favorites, current }) => { const page = pages.find((p) => p.id === current) return h.div.attrs(sx(styles.frame)).children( h.nav.attrs({ "aria-label": "Pages", ...sx(styles.side) }).children( PageTree(h, { props: { label: "Pages", pages, favorites, current }, onEmit: { RequestedOpen: ({ page }) => Message.OpenedPage({ page }), RequestedCreate: ({ parent, section }) => Message.CreatedPage({ parent, section }), RequestedRename: ({ page, title }) => Message.RenamedPage({ page, title }), RequestedMove: (move) => Message.MovedPage(move), RequestedFavorite: ({ page, favorite }) => Message.FavoritedPage({ page, favorite }), RequestedDuplicate: () => undefined, RequestedDelete: ({ page }) => Message.TrashedPage({ page }), RequestedCopyLink: () => undefined, Expanded: () => undefined, }, }), ), h.div.attrs(sx(styles.page)).children( Breadcrumbs(h, { props: { trail: trailOf(pages, current) }, onEmit: { RequestedOpen: ({ id }) => Message.OpenedPage({ page: id }) }, }), h.h2.attrs(sx(styles.title)).children(`${page?.icon ?? "📄"} ${page?.title || "Untitled"}`), h.p.attrs(sx(styles.hint)).children("Move this page in the tree and its trail follows."), ), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/page-treeUsage
import { PageTree, type TreePage } from "./ui/uicontext/components/kit/PageTree.ts"const Message = UiSchema.TaggedEnum({ OpenedPage: { page: Schema.String }, RenamedPage: { page: Schema.String, title: Schema.String },})export const Pages = (h: Html.H<typeof Message.Type>, pages: ReadonlyArray<TreePage>, current: string) => PageTree(h, { props: { label: "Pages", pages, current }, onEmit: { RequestedOpen: ({ page }) => Message.OpenedPage({ page }), RequestedRename: ({ page, title }) => Message.RenamedPage({ page, title }), RequestedCreate: () => undefined, RequestedMove: () => undefined, RequestedFavorite: () => undefined, RequestedDuplicate: () => undefined, RequestedDelete: () => undefined, RequestedCopyLink: () => undefined, Expanded: () => undefined, }, })Examples
Read-only
For a guest: pages open and expand, and can: [] takes away creating, renaming, moving and deleting.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { PageTree, type TreePage } from "./ui/uicontext/components/kit/PageTree.ts"import { sx } from "./ui/uicontext/components/Sx.ts"// A guest's view of a shared handbook: pages open and expand, but `can: []` hides create, rename,// move and delete, so there's nothing to drag and no row menu to change things.const styles = stylex.create({ frame: { inlineSize: "min(18rem, 100%)" } })const pages: ReadonlyArray<TreePage> = [ { id: "handbook", title: "Team handbook", icon: "📗", section: "workspace" }, { id: "onboarding", title: "Onboarding", icon: "👋", parent: "handbook" }, { id: "first-week", title: "Your first week", icon: "🗓️", parent: "onboarding" }, { id: "tools", title: "Accounts and tools", icon: "🔑", parent: "onboarding" }, { id: "time-off", title: "Time off", icon: "🌴", parent: "handbook" }, { id: "expenses", title: "Expenses", icon: "🧾", parent: "handbook" },]const Message = UiSchema.TaggedEnum({ OpenedPage: { page: Schema.String } })export const SharedHandbook = Html.component({ name: "SharedHandbook", model: Schema.Struct({ current: Schema.String }), message: Message,}).make({ init: () => ({ current: "first-week" }), update: { OpenedPage: ($, { page }) => $.evolve({ current: page }) }, view: (h, { current }) => h.nav.attrs({ "aria-label": "Handbook", ...sx(styles.frame) }).children( PageTree(h, { props: { label: "Handbook pages", pages, current, can: [] }, onEmit: { RequestedOpen: ({ page }) => Message.OpenedPage({ page }), RequestedCreate: () => undefined, RequestedRename: () => undefined, RequestedMove: () => undefined, RequestedFavorite: () => undefined, RequestedDuplicate: () => undefined, RequestedDelete: () => undefined, RequestedCopyLink: () => undefined, Expanded: () => undefined, }, }), ),})A large workspace
Two thousand pages in forty teams. Only open pages draw their subpages, so the tree stays quick at any size.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { movePage, PageSection, PageTree, TreePage } from "./ui/uicontext/components/kit/PageTree.ts"import { sx } from "./ui/uicontext/components/Sx.ts"// Two thousand pages: 40 teams, each with meeting notes, specs and runbooks. Only open pages draw// their subpages, so the tree stays quick however large the workspace grows.const styles = stylex.create({ frame: { inlineSize: "min(18rem, 100%)", blockSize: 420, overflowY: "auto" } })const teams = ["Billing", "Search", "Mobile", "Payments", "Growth", "Design systems", "Data platform", "Support tools"]const kinds = [ { title: "Meeting notes", icon: "🗒️", child: (n: number) => `Weekly sync, week ${n}` }, { title: "Specs", icon: "📐", child: (n: number) => `Spec ${n}: ${["Saved filters", "Bulk edit", "Audit export", "Offline mode", "Team invites"][n % 5]}` }, { title: "Runbooks", icon: "🧯", child: (n: number) => `Runbook ${n}: ${["Restart a worker", "Rotate keys", "Restore a backup", "Drain a queue"][n % 4]}` },]const pages: ReadonlyArray<TreePage> = Array.from({ length: 40 }, (_, t) => { const team = `team-${t}` const name = `${teams[t % teams.length]}${t >= teams.length ? ` ${Math.floor(t / teams.length) + 1}` : ""}` return [ { id: team, title: name, icon: "🏷️", section: "workspace" as const }, ...kinds.flatMap((kind, k) => { const folder = `${team}-${k}` return [ { id: folder, title: kind.title, icon: kind.icon, parent: team }, ...Array.from({ length: 16 }, (_, n) => ({ id: `${folder}-${n}`, title: kind.child(n + 1), parent: folder })), ] }), ]}).flat()const Message = UiSchema.TaggedEnum({ OpenedPage: { page: Schema.String }, MovedPage: { page: Schema.String, parent: Schema.NullOr(Schema.String), section: PageSection, before: Schema.NullOr(Schema.String) },})export const LargeWorkspace = Html.component({ name: "LargeWorkspace", model: Schema.Struct({ pages: Schema.Array(TreePage), current: Schema.String }), message: Message,}).make({ init: () => ({ pages, current: "team-3-1-4" }), update: { OpenedPage: ($, { page }) => $.evolve({ current: page }), MovedPage: ($, move) => $.evolve({ pages: (p) => movePage(p, move) }), }, view: (h, model) => h.nav.attrs({ "aria-label": "Workspace pages", ...sx(styles.frame) }).children( PageTree(h, { props: { label: "Workspace pages", pages: model.pages, current: model.current, agentId: "page-tree:large" }, onEmit: { RequestedOpen: ({ page }) => Message.OpenedPage({ page }), RequestedMove: (move) => Message.MovedPage(move), RequestedCreate: () => undefined, RequestedRename: () => undefined, RequestedFavorite: () => undefined, RequestedDuplicate: () => undefined, RequestedDelete: () => undefined, RequestedCopyLink: () => undefined, Expanded: () => undefined, }, }), ),})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | string | — | The tree's accessible name: "Pages" |
pagesrequired | TreePage[] | — | In order: siblings show in the order they come here |
favorites | string[] | — | The starred pages, in order |
current | string | null | — | The page shown now: its row is marked, and its parents are open |
expanded | string[] | — | Pages open at first |
can | Can(["create", "rename", "move", "delete"]) | — | Create, rename, move, delete; all by default |
agentId | string | page-tree:<label> | This tree's id for agents, unique on the page; page-tree:<label> by default |
Events
| Event | Payload | Description |
|---|---|---|
RequestedOpen | { page: string } | A page without a link was opened |
RequestedCreate | { parent: string | null; section: PageSection } | A new page: inside parent, or at the top of section |
RequestedRename | { page: string; title: string } | Give this page a new title |
RequestedMove | { page: string; parent: string | null; section: PageSection; before: string | null } | Into parent (null: the top of section), before before (null: last); movePage applies it |
RequestedFavorite | { page: string; favorite: boolean } | Add the page to favorites, or take it out |
RequestedDuplicate | { page: string } | Make a copy of this page beside it |
RequestedDelete | { page: string } | Move it to the trash |
RequestedCopyLink | { page: string } | Copy a link to this page |
Expanded | { page: string } | Opened: load its subpages if they aren't there yet |
Agent actions
| Action | Description |
|---|---|
OpenedPage | Open a page by its id (see page_query for every page) |
MovedPage | Move a page into another (parent: its id) or to the top of a section (parent: null), before a sibling (before: its id) or last (null) |
Accessibility
- Each section is a WAI-ARIA tree and one Tab stop: Up and Down move, Right opens or moves in, Left closes or moves out, Home and End jump, and typing jumps to a title.
- F2 renames in place (Enter keeps it, Escape cancels) and Shift+F10 opens a row's menu. Alt with the arrow keys moves a page, and focus follows it.
- Drops show a line before or after a row, or highlight the row for inside, and every drag has a keyboard twin.