Public: anyone can read this; the crew can change it.

Component library: skeleton, toast and confirm dialog as ui.ts parts, each in the gallery #16683

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/5880-component-library-skeleton-toast-and-con → main 0 commits fork 7 threads · 7 unresolved

  • Merged. mason/5880-component-library-skeleton-toast-and-con was merged into main as 6ba9019.

Review

Sign in to review this change.
12 files changed +197 −60
docs/design/areas/dialog.md +4 −3
@@ -1,12 +1,13 @@
11 # Dialog
22
33 A §5 `.dialog` is one component in three parts: a head that names it, a body that holds what it asks or shows, and a
4 foot of buttons aligned to the end. The shell's keyboard-shortcuts dialog (`overlays()` in `src/web/layout.ts`) and the
5 limb page's confirm form (`limbPreviewPage()` in `src/web/pages/limb.ts`) both draw it; the rules live in
4 foot of buttons aligned to the end. `dialogParts()` in `src/web/ui.ts` draws the three; the wrapper is the caller's —
5 the shell's keyboard-shortcuts `<dialog>` (`overlays()` in `src/web/layout.ts`) and the limb page's confirm
6 `<form class="dialog">` (`limbPreviewPage()` in `src/web/pages/limb.ts`). The rules live in
67 `src/web/style/components.ts`.
78
89 - `.dialog-head` — the dialog's first row: its `h2` title (which `aria-labelledby` names) takes the width, anything beside it sits at the end.
9 - `.dialog-body` — the dialog's content, padded to the head's inset; add `.stack` when it holds several fields.
10 - `.dialog-body` — the dialog's content, padded to the head's inset; `stack: true` adds `.stack` when it holds several fields.
1011 - `.dialog-foot` — the dialog's actions on one row, aligned to the end, the primary or danger button last.
1112
1213 The confirm page before a destructive route (`confirmPage()` in `src/web/pages/search.ts`) is not a dialog: it is a page
docs/design/areas/skeleton.md added +10
@@ -0,0 +1,10 @@
1 # Skeleton
2
3 `skeleton()` in `src/web/ui.ts` (#5880): a placeholder for a part that fills in after the first paint. It is a real
4 `.card` holding real `.list` rows (or a real card head and body, `shape: "card"`), so the swap to the loaded part shifts
5 nothing; grey bars stand where the words will be, and the card says "Loading…" to a screen reader (`role="status"`,
6 `aria-busy`). A page the server renders whole needs none. Rules in `src/web/style/components.ts` under
7 `/* ---- skeleton … ---- */`.
8
9 - `.skeleton` — on the card: lays a row's title and meta out as two stacked bars.
10 - `.skeleton-bar` — one grey bar, a line of text tall; it pulses, and under `prefers-reduced-motion` the kill switch holds it still.
docs/design/areas/toast.md +3 −1
@@ -2,4 +2,6 @@
22
33 The flash a write leaves after its redirect, and the same one-line banner a page shows for a failure it found itself: one `.toast` per page, toned by `data-tone=success|danger|attn` (§5's toast / flash row), an icon, then the message.
44
5 - `.toast-body` — the message beside the toast's icon: it takes the rest of the row (`flex: 1; min-width: 0`) so a long message wraps instead of pushing the icon off. Written by `flashToast` in `src/web/layout.ts` (the cookie-driven flash), and by `src/web/pages/repo.ts` (a fetch error), `src/web/pages/browse.ts` (a failed mirror) and `src/web/pages/origins.ts` (origin connected or failed); styled in `src/web/style/components.ts` under `/* ---- toast / flash ---- */`.
5 Drawn by one part, `toast()` in `src/web/ui.ts`: the shell's cookie-driven flash (`flashToast` in `src/web/layout.ts`), `src/web/pages/repo.ts` (a fetch error), `src/web/pages/browse.ts` (a failed mirror, `role="alert"`) and `src/web/pages/origins.ts` (origin connected or failed) all call it; nothing writes `.toast` by hand. Styled in `src/web/style/components.ts` under `/* ---- toast / flash ---- */`.
6
7 - `.toast-body` — the message beside the toast's icon: it takes the rest of the row (`flex: 1; min-width: 0`) so a long message wraps instead of pushing the icon off.
docs/design/sections/5-components.md +6 −5
@@ -38,9 +38,10 @@ specification.
3838 | **segmented control** `seg()` | `.seg > a` | — | `aria-current`, or `:has(:checked)` for radios | `‹ Open | Closed | All ›` |
3939 | **menu** `menu()` | `.menu` (`<details>`) | `data-align=start\|end` | open/closed | a popup list |
4040 | **disclosure** `disclosure()` | `.disclosure` (`<details>`, a quiet `›` summary) | `small` summary, `label` (aria-label), `extra` classes | open/closed; the chevron turns, the summary is at least 44px tall | a folded section |
41 | **dialog** | `.dialog` | `data-side=bottom` (sheet) | JS `showModal()`; every dialog has a real page behind it | a modal box |
41 | **dialog** `dialogParts()` | `.dialog` | `data-side=bottom` (sheet) | JS `showModal()`; every dialog has a real page behind it | a modal box |
4242 | **tooltip** | `[data-tip]` | `data-tip-side=bottom` | hover/focus; hidden on coarse pointers | a status-bar hint |
43 | **toast / flash** `Flash` | `.toast` | `data-tone=success\|danger\|attn` | one per page load, cookie-driven | a one-line banner |
43 | **toast / flash** `toast()` | `.toast` | `data-tone=success\|danger\|attn` | one per page load, cookie-driven | a one-line banner |
44 | **skeleton** `skeleton()` | `.skeleton` (a `.card`), `.skeleton-bar` | `shape=list\|card` | real `.list` rows or a real `.card` holding grey bars, so the swap shifts nothing; `role="status"`, `aria-busy`, "Loading…" for a screen reader; bars hold still under reduced motion | the same card, no pulse |
4445 | **command palette** | `.palette` | — | JS only; degrades to `/search` | a fuzzy-find overlay |
4546 | **page head** `pageHead()` (`chrome.ts`) | `.page-head` (`h1`, `.page-head-sub`; a `.spacer` before the actions) | `lead` before the title, `actions` right-aligned, `sub` on its own line below | wraps; the title's `min-width: 0` lets a long one shrink beside the actions | the title line, actions right, a dim subtitle under it |
4647 | **breadcrumb** `crumbs()` | `.crumbs` | `.mono` | `aria-current` on the last | `a / b / c` |
@@ -86,10 +87,10 @@ specification.
8687 | **stacked bar chart** `windowSvg()` (`fleet-graph.ts`) | `.plots`, `.plot` (`.plot-name`), `.plot-legend` | bars wear `fg-sN`, the `--series-N` token | server-drawn SVG with `width`/`height` set, so it holds its box before the stylesheet; gridlines `--border`, figures `--muted-fg` in mono, panel names `--faint`; `role="img"` with an `aria-label` totalling it, a `<title>` per bar, and a table of the same numbers beside it | a table of the same numbers |
8788 | **chart** `chartBox()` (`ui.ts`) | `.chart-box` wrapping `drawChart`'s svg `.chart` (`.big` for the headline figure) | `.is-table` for a table panel | one query or series drawn on the server: the svg carries its own `width`/`height` and the box holds its space, so nothing shifts; `role="img"` with an `aria-label` of its title. Reach for it wherever a page draws a `boards/chart.ts` chart — `/system`'s memory pair and every board panel — and size it from the page (`.bp-*`, `.plots`, `.bgrid`), never inside the frame | the same numbers as a table |
8889
89 Two things are deliberately **not** in the vocabulary: skeleton loaders (the
90 server renders whole pages — there is nothing to skeletonise) and a custom
90 One thing is deliberately **not** in the vocabulary: a custom
9191 switch control (a native checkbox with `accent-color` is themed, keyboard-correct
92 and honest about what it is).
92 and honest about what it is). The server renders whole pages, so a skeleton
93 (`docs/design/areas/skeleton.md`) is only for a part that fills in after them.
9394
9495 Three rules the browser enforces whether or not you meant them:
9596
src/web/layout.ts +11 −25
@@ -75,7 +75,7 @@ import { noRoom } from "../spawn/door.ts";
7575 import { takeNotice } from "../core/access_link.ts";
7676 import { quotaFull } from "../spawn/quota.ts";
7777 import { icon, inlineIcons, pointAtSprite, themeGlyph } from "./icons.ts";
78 import { btn, copyField, hintAttr, kbd, menu, type MenuItem } from "./ui.ts";
78 import { btn, dialogParts, hintAttr, kbd, menu, toast, type MenuItem } from "./ui.ts";
7979 import { SITE_LINE } from "./site_line.ts";
8080 import { CSP_REPORT_DIRECTIVES, CSP_REPORTING_ENDPOINTS } from "../http/csp_report.ts";
8181
@@ -617,14 +617,16 @@ function overlays(): Html {
617617 </div>
618618 </dialog>
619619 <dialog class="dialog" id="help-dialog" aria-labelledby="help-title">
620 <div class="dialog-head"><h2 id="help-title">Keyboard shortcuts</h2></div>
621 <div class="dialog-body" data-help-body></div>
622 <div class="dialog-foot">
623 <a class="btn" data-variant="link" href="/docs">Docs</a>
620 ${dialogParts({
621 titleId: "help-title",
622 title: "Keyboard shortcuts",
623 body: html``,
624 bodyAttrs: raw("data-help-body"),
625 foot: html`<a class="btn" data-variant="link" href="/docs">Docs</a>
624626 <form method="dialog">
625627 <button class="btn" type="submit">Close</button>
626 </form>
627 </div>
628 </form>`,
629 })}
628630 </dialog>`;
629631 }
630632
@@ -636,24 +638,8 @@ const asksWhoCan = (body: Html, flash: Flash | undefined): boolean =>
636638 flash?.ask !== undefined || body.text.includes(`form="${ASK_FORM_ID}"`);
637639
638640 function flashToast(flash: Flash): Html {
639 const tone = flash.tone ?? "success";
640 const glyph =
641 tone === "danger" ? "alert" : tone === "attn" ? "alert" : "check";
642 return html`<div class="toast" data-tone="${tone}" role="status">
643 ${icon(glyph)}
644 <div class="toast-body">
645 ${flash.message}${when(
646 flash.link,
647 (link) => html` · <a href="${link.href}">${link.label}</a>`,
648 )}
649 ${when(flash.ask, (ask) => html` ${askButton(ask)}`)}
650 ${when(
651 flash.code,
652 (code) =>
653 copyField("flash-code", code, "Code", "Copy code"),
654 )}
655 </div>
656 </div>`;
641 const { ask, ...rest } = flash;
642 return toast({ ...rest, codeId: "flash-code", ...(ask === undefined ? {} : { ask: askButton(ask) }) });
657643 }
658644
659645 // ---- the chrome ----
src/web/pages/browse.ts +2 −4
@@ -77,6 +77,7 @@ import {
7777 tlComment,
7878 userTag,
7979 type IconName,
80 toast,
8081 popout,
8182 } from "../ui.ts";
8283 import { browseCommit, browseCommits } from "./commits.ts";
@@ -1068,10 +1069,7 @@ export function mirrorJobPage(ctx: Ctx): Response {
10681069
10691070 const foot =
10701071 job.stage === "failed"
1071 ? html`<div class="toast" data-tone="danger" role="alert">
1072 ${icon("alert")}
1073 <div class="toast-body">${job.error ?? "The mirror failed."}</div>
1074 </div>
1072 ? html`${toast({ tone: "danger", role: "alert", message: job.error ?? "The mirror failed." })}
10751073 <div class="bar mt">
10761074 <form
10771075 method="post"
src/web/pages/design-components.ts +4 −3
@@ -17,7 +17,7 @@ import { page } from "../layout.ts";
1717 import {
1818 labelCss, avatar, badge, btn, card, counts, crumbs, disclosure, dot, empty, field, iconBtn, kbd, labelChip,
1919 listCard, menu, meter, nameMark, pager, pill, row, seg, sha, stat, stateBadge, tabs, tlComment,
20 tlEvent, userTag, verdictBadge, runStateBadge, envStateBadge,
20 tlEvent, dialogParts, skeleton, toast, userTag, verdictBadge, runStateBadge, envStateBadge,
2121 } from "../ui.ts";
2222
2323 const r = (markup: string): Html => raw(markup);
@@ -57,9 +57,10 @@ export const samples: readonly (readonly [name: string, sample: Html])[] = [
5757 ["segmented control", seg([{ label: "Open", href: "#", current: true }, { label: "Closed", href: "#", current: false }])],
5858 ["menu", menu({ trigger: "Actions", items: [{ label: "Rename", href: "#" }, { label: "Archive", href: "#" }] })],
5959 ["disclosure", disclosure("More detail", html`<p>Hidden until opened.</p>`)],
60 ["dialog", r(`<div class="dialog gallery-static" role="dialog" aria-label="Sample dialog"><div class="dialog-head"><h2>Delete this?</h2></div><div class="dialog-foot"><button class="btn" type="button">Cancel</button></div><p>Delete this?</p></div>`)],
60 ["dialog", html`<div class="dialog gallery-static" role="dialog" aria-label="Sample dialog">${dialogParts({ titleId: "gallery-dialog-title", title: "Delete this?", body: html`<p>It cannot be undone.</p>`, foot: html`<button class="btn" type="button">Cancel</button>${btn("Delete", { variant: "danger" })}` })}</div>`],
6161 ["tooltip", r(`<span class="btn" data-tip="A tip, on hover and focus" tabindex="0">Hover me</span>`)],
62 ["toast / flash", r(`<div class="toast" role="status">Saved.</div>`)],
62 ["toast / flash", html`<div class="gallery-col">${toast({ message: "Saved." })}${toast({ tone: "attn", message: "Check the address.", link: { href: "#", label: "Open it" } })}${toast({ tone: "danger", role: "alert", message: "The mirror failed.", code: "c0de-1234", codeId: "gallery-toast-code" })}</div>`],
63 ["skeleton", html`<div class="gallery-col">${skeleton()}${skeleton({ shape: "card", rows: 2 })}</div>`],
6364 ["command palette", r(`<div class="dialog palette gallery-static"><input class="input" aria-label="Search" placeholder="Type a command" /><div class="palette-list" role="listbox" aria-label="Results"></div></div>`)],
6465 ["breadcrumb", crumbs([{ label: "forge", href: "#" }, { label: "design", href: "#" }, { label: "components" }])],
6566 ["pagination", pager({ prev: "#", next: "#", note: "Page 2" })],
src/web/pages/limb.ts +10 −8
@@ -33,7 +33,7 @@ import {
3333 import { field, readForm } from "../form.ts";
3434 import { html, when, type Html } from "../html.ts";
3535 import { page, redirect } from "../layout.ts";
36 import { badge, btn, card, field as formField, listCard, row, selectField } from "../ui.ts";
36 import { badge, btn, card, dialogParts, field as formField, listCard, row, selectField } from "../ui.ts";
3737 import { replanPreviewPage, replanSubmit } from "./limb_replan.ts";
3838 import { folded, issueRow } from "./limb_view.ts";
3939 import { milestoneHref } from "./milestones.ts";
@@ -280,10 +280,11 @@ export function limbPreviewPage(ctx: Ctx): Response {
280280 class="dialog"
281281 aria-labelledby="limb-confirm-title"
282282 >
283 <div class="dialog-head">
284 <h2 id="limb-confirm-title">${words.go}?</h2>
285 </div>
286 <div class="dialog-body stack">
283 ${dialogParts({
284 titleId: "limb-confirm-title",
285 title: `${words.go}?`,
286 stack: true,
287 body: html`
287288 <input type="hidden" name="action" value="${action}" />
288289 ${when(
289290 request.under,
@@ -316,8 +317,8 @@ export function limbPreviewPage(ctx: Ctx): Response {
316317 ],
317318 }),
318319 )}
319 </div>
320 <div class="dialog-foot">
320 `,
321 foot: html`
321322 ${btn("Back", {
322323 href: milestoneHref(base, number),
323324 variant: "ghost",
@@ -325,7 +326,8 @@ export function limbPreviewPage(ctx: Ctx): Response {
325326 ${btn(words.go, {
326327 variant: action === "close" ? "danger" : "primary",
327328 })}
328 </div>
329 `,
330 })}
329331 </form>
330332 </div>`
331333 : html`<p class="mt">
src/web/pages/origins.ts +8 −11
@@ -95,6 +95,7 @@ import {
9595 field as uiField,
9696 type IconName,
9797 copyField,
98 toast,
9899 } from "../ui.ts";
99100
100101 export type AccountSection =
@@ -1044,19 +1045,15 @@ export async function originsPage(ctx: Ctx): Promise<Response> {
10441045 const error = params.get("error");
10451046 const connected = params.get("connected") === "1";
10461047 const banner = connected
1047 ? html`<div class="toast" data-tone="success" role="status">
1048 ${icon("check")}
1049 <div class="toast-body">Origin connected.</div>
1050 </div>`
1048 ? toast({ tone: "success", message: "Origin connected." })
10511049 : error !== null && error !== ""
1052 ? html`<div class="toast" data-tone="danger" role="status">
1053 ${icon("alert")}
1054 <div class="toast-body">
1055 ${error === "state"
1050 ? toast({
1051 tone: "danger",
1052 message:
1053 error === "state"
10561054 ? "That authorization could not be matched to a connect you started here. Start again."
1057 : error.slice(0, 300)}
1058 </div>
1059 </div>`
1055 : error.slice(0, 300),
1056 })
10601057 : null;
10611058
10621059 const timing = { rendering: performance.now() };
src/web/style/components.ts +13
@@ -1002,6 +1002,19 @@ html[data-overlay] { overflow: hidden; }
10021002 .toast.blocked .toast-body { flex: 1 1 280px; }
10031003 .toast code { display: block; margin-top: 6px; }
10041004
1005 /* ---- skeleton (a list's or card's space, held while it loads) ---- */
1006 .skeleton .row-main { display: flex; flex-direction: column; gap: 6px; flex: 1; min-width: 0; }
1007 .skeleton-bar {
1008 display: inline-block; vertical-align: middle;
1009 width: 60%; height: .75em; border-radius: var(--radius);
1010 background: var(--muted);
1011 animation: skeleton-pulse 1.2s var(--ease) infinite alternate;
1012 }
1013 .skeleton .row-meta .skeleton-bar, .skeleton .card-body p:last-child .skeleton-bar { width: 35%; }
1014 .skeleton li:nth-child(3n + 2) .row-title .skeleton-bar { width: 75%; }
1015 .skeleton .card-head .skeleton-bar { width: 30%; }
1016 @keyframes skeleton-pulse { to { opacity: .45; } }
1017
10051018 /* ---- tooltip ---- */
10061019 [data-tip] { position: relative; }
10071020 [data-tip]::after {
src/web/ui.ts +76
@@ -1462,6 +1462,82 @@ export function empty(o: {
14621462 </div>`;
14631463 }
14641464
1465 export type ToastTone = "success" | "danger" | "attn";
1466
1467 /**
1468 * The one-line banner a page shows once: the flash a write leaves after its
1469 * redirect, or a failure the page found itself. An icon for its tone, the
1470 * message, then an optional link and a copyable one-time code.
1471 */
1472 export function toast(o: {
1473 tone?: ToastTone;
1474 message: string | Html;
1475 link?: { href: string; label: string };
1476 /** A one-time value, shown in a copyable monospace block. */
1477 code?: string;
1478 /** The code block's id; one toast per page, so the default rarely moves. */
1479 codeId?: string;
1480 /** A button that asks whoever can, drawn after the message. */
1481 ask?: Html;
1482 /** `alert` when the failure is what the page is about; `status` otherwise. */
1483 role?: "status" | "alert";
1484 }): Html {
1485 const tone = o.tone ?? "success";
1486 return html`<div class="toast" data-tone="${tone}" role="${o.role ?? "status"}">
1487 ${icon(tone === "success" ? "check" : "alert")}
1488 <div class="toast-body">
1489 ${o.message}${when(o.link, (link) => html` · <a href="${link.href}">${link.label}</a>`)}
1490 ${when(o.ask, (ask) => html` ${ask}`)}
1491 ${when(o.code, (code) => copyField(o.codeId ?? "toast-code", code, "Code", "Copy code"))}
1492 </div>
1493 </div>`;
1494 }
1495
1496 /**
1497 * A §5 dialog's three parts — the head that names it (`titleId` is what the
1498 * wrapper's `aria-labelledby` points at), the body, and the foot of buttons,
1499 * the primary or danger one last. The wrapper is the caller's: a `<dialog>`
1500 * the shell opens, or a `<form class="dialog">` drawn as a page of its own.
1501 */
1502 export function dialogParts(o: {
1503 titleId: string;
1504 title: string | Html;
1505 body: Html;
1506 foot: Html;
1507 /** The body holds several fields, spaced as a stack. */
1508 stack?: boolean;
1509 /** Attributes on the body, for a script that fills it. */
1510 bodyAttrs?: Html;
1511 }): Html {
1512 return html`<div class="dialog-head"><h2 id="${o.titleId}">${o.title}</h2></div>
1513 <div class="dialog-body${o.stack ? " stack" : ""}" ${o.bodyAttrs ?? null}>${o.body}</div>
1514 <div class="dialog-foot">${o.foot}</div>`;
1515 }
1516
1517 /**
1518 * A placeholder that holds a list's or a card's space while what goes there
1519 * loads: real `.list` rows or a real `.card`, so the swap shifts nothing, with
1520 * grey bars where the words will be. The bars pulse; asked for less motion,
1521 * they hold still.
1522 */
1523 export function skeleton(o: { shape?: "list" | "card"; rows?: number } = {}): Html {
1524 const n = Math.min(Math.max(Math.trunc(o.rows ?? 3) || 1, 1), 50);
1525 const bar = raw('<span class="skeleton-bar"></span>');
1526 const lines = Array.from({ length: n }, () => bar);
1527 const body =
1528 o.shape === "card"
1529 ? html`<div class="card-head">${bar}</div>
1530 <div class="card-body stack">${lines.map((b) => html`<p>${b}</p>`)}</div>`
1531 : rowList(
1532 lines.map(
1533 (b) => html`<li><div class="row"><span class="row-main"><span class="row-title">${b}</span><span class="row-meta">${b}</span></span></div></li>`,
1534 ),
1535 );
1536 return html`<div class="card skeleton" role="status" aria-busy="true">
1537 <span class="sr-only">Loading…</span>${body}
1538 </div>`;
1539 }
1540
14651541 export interface TimelineEvent {
14661542 icon: IconName;
14671543 tone?: Tone;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs