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
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/5880-component-library-skeleton-toast-and-con → main 0 commits fork 7 threads · 7 unresolved
12 files
changed +197
−60
- docs/design/areas/dialog.md
- docs/design/areas/skeleton.md
- docs/design/areas/toast.md
- docs/design/sections/5-components.md
- src/web/layout.ts
- src/web/pages/browse.ts
- src/web/pages/design-components.ts
- src/web/pages/limb.ts
- src/web/pages/origins.ts
- src/web/style/components.ts
- src/web/ui.ts
- test/design-gallery.test.ts
docs/design/areas/dialog.md +4 −3
| @@ -1,12 +1,13 @@ | ||
| 1 | 1 | # Dialog |
| 2 | 2 | |
| 3 | 3 | 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 | |
| 6 | 7 | `src/web/style/components.ts`. |
| 7 | 8 | |
| 8 | 9 | - `.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. | |
| 10 | 11 | - `.dialog-foot` — the dialog's actions on one row, aligned to the end, the primary or danger button last. |
| 11 | 12 | |
| 12 | 13 | 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 @@ | ||
| 2 | 2 | |
| 3 | 3 | 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. |
| 4 | 4 | |
| 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. | ||
| 38 | 38 | | **segmented control** `seg()` | `.seg > a` | — | `aria-current`, or `:has(:checked)` for radios | `‹ Open | Closed | All ›` | |
| 39 | 39 | | **menu** `menu()` | `.menu` (`<details>`) | `data-align=start\|end` | open/closed | a popup list | |
| 40 | 40 | | **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 | | |
| 42 | 42 | | **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 | | |
| 44 | 45 | | **command palette** | `.palette` | — | JS only; degrades to `/search` | a fuzzy-find overlay | |
| 45 | 46 | | **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 | |
| 46 | 47 | | **breadcrumb** `crumbs()` | `.crumbs` | `.mono` | `aria-current` on the last | `a / b / c` | |
| @@ -86,10 +87,10 @@ specification. | ||
| 86 | 87 | | **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 | |
| 87 | 88 | | **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 | |
| 88 | 89 | |
| 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 | |
| 91 | 91 | 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. | |
| 93 | 94 | |
| 94 | 95 | Three rules the browser enforces whether or not you meant them: |
| 95 | 96 |
src/web/layout.ts +11 −25
| @@ -75,7 +75,7 @@ import { noRoom } from "../spawn/door.ts"; | ||
| 75 | 75 | import { takeNotice } from "../core/access_link.ts"; |
| 76 | 76 | import { quotaFull } from "../spawn/quota.ts"; |
| 77 | 77 | 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"; | |
| 79 | 79 | import { SITE_LINE } from "./site_line.ts"; |
| 80 | 80 | import { CSP_REPORT_DIRECTIVES, CSP_REPORTING_ENDPOINTS } from "../http/csp_report.ts"; |
| 81 | 81 | |
| @@ -617,14 +617,16 @@ function overlays(): Html { | ||
| 617 | 617 | </div> |
| 618 | 618 | </dialog> |
| 619 | 619 | <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> | |
| 624 | 626 | <form method="dialog"> |
| 625 | 627 | <button class="btn" type="submit">Close</button> |
| 626 | </form> | |
| 627 | </div> | |
| 628 | </form>`, | |
| 629 | })} | |
| 628 | 630 | </dialog>`; |
| 629 | 631 | } |
| 630 | 632 | |
| @@ -636,24 +638,8 @@ const asksWhoCan = (body: Html, flash: Flash | undefined): boolean => | ||
| 636 | 638 | flash?.ask !== undefined || body.text.includes(`form="${ASK_FORM_ID}"`); |
| 637 | 639 | |
| 638 | 640 | 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) }) }); | |
| 657 | 643 | } |
| 658 | 644 | |
| 659 | 645 | // ---- the chrome ---- |
src/web/pages/browse.ts +2 −4
| @@ -77,6 +77,7 @@ import { | ||
| 77 | 77 | tlComment, |
| 78 | 78 | userTag, |
| 79 | 79 | type IconName, |
| 80 | toast, | |
| 80 | 81 | popout, |
| 81 | 82 | } from "../ui.ts"; |
| 82 | 83 | import { browseCommit, browseCommits } from "./commits.ts"; |
| @@ -1068,10 +1069,7 @@ export function mirrorJobPage(ctx: Ctx): Response { | ||
| 1068 | 1069 | |
| 1069 | 1070 | const foot = |
| 1070 | 1071 | 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." })} | |
| 1075 | 1073 | <div class="bar mt"> |
| 1076 | 1074 | <form |
| 1077 | 1075 | method="post" |
src/web/pages/design-components.ts +4 −3
| @@ -17,7 +17,7 @@ import { page } from "../layout.ts"; | ||
| 17 | 17 | import { |
| 18 | 18 | labelCss, avatar, badge, btn, card, counts, crumbs, disclosure, dot, empty, field, iconBtn, kbd, labelChip, |
| 19 | 19 | 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, | |
| 21 | 21 | } from "../ui.ts"; |
| 22 | 22 | |
| 23 | 23 | const r = (markup: string): Html => raw(markup); |
| @@ -57,9 +57,10 @@ export const samples: readonly (readonly [name: string, sample: Html])[] = [ | ||
| 57 | 57 | ["segmented control", seg([{ label: "Open", href: "#", current: true }, { label: "Closed", href: "#", current: false }])], |
| 58 | 58 | ["menu", menu({ trigger: "Actions", items: [{ label: "Rename", href: "#" }, { label: "Archive", href: "#" }] })], |
| 59 | 59 | ["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>`], | |
| 61 | 61 | ["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>`], | |
| 63 | 64 | ["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>`)], |
| 64 | 65 | ["breadcrumb", crumbs([{ label: "forge", href: "#" }, { label: "design", href: "#" }, { label: "components" }])], |
| 65 | 66 | ["pagination", pager({ prev: "#", next: "#", note: "Page 2" })], |
src/web/pages/limb.ts +10 −8
| @@ -33,7 +33,7 @@ import { | ||
| 33 | 33 | import { field, readForm } from "../form.ts"; |
| 34 | 34 | import { html, when, type Html } from "../html.ts"; |
| 35 | 35 | 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"; | |
| 37 | 37 | import { replanPreviewPage, replanSubmit } from "./limb_replan.ts"; |
| 38 | 38 | import { folded, issueRow } from "./limb_view.ts"; |
| 39 | 39 | import { milestoneHref } from "./milestones.ts"; |
| @@ -280,10 +280,11 @@ export function limbPreviewPage(ctx: Ctx): Response { | ||
| 280 | 280 | class="dialog" |
| 281 | 281 | aria-labelledby="limb-confirm-title" |
| 282 | 282 | > |
| 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` | |
| 287 | 288 | <input type="hidden" name="action" value="${action}" /> |
| 288 | 289 | ${when( |
| 289 | 290 | request.under, |
| @@ -316,8 +317,8 @@ export function limbPreviewPage(ctx: Ctx): Response { | ||
| 316 | 317 | ], |
| 317 | 318 | }), |
| 318 | 319 | )} |
| 319 | </div> | |
| 320 | <div class="dialog-foot"> | |
| 320 | `, | |
| 321 | foot: html` | |
| 321 | 322 | ${btn("Back", { |
| 322 | 323 | href: milestoneHref(base, number), |
| 323 | 324 | variant: "ghost", |
| @@ -325,7 +326,8 @@ export function limbPreviewPage(ctx: Ctx): Response { | ||
| 325 | 326 | ${btn(words.go, { |
| 326 | 327 | variant: action === "close" ? "danger" : "primary", |
| 327 | 328 | })} |
| 328 | </div> | |
| 329 | `, | |
| 330 | })} | |
| 329 | 331 | </form> |
| 330 | 332 | </div>` |
| 331 | 333 | : html`<p class="mt"> |
src/web/pages/origins.ts +8 −11
| @@ -95,6 +95,7 @@ import { | ||
| 95 | 95 | field as uiField, |
| 96 | 96 | type IconName, |
| 97 | 97 | copyField, |
| 98 | toast, | |
| 98 | 99 | } from "../ui.ts"; |
| 99 | 100 | |
| 100 | 101 | export type AccountSection = |
| @@ -1044,19 +1045,15 @@ export async function originsPage(ctx: Ctx): Promise<Response> { | ||
| 1044 | 1045 | const error = params.get("error"); |
| 1045 | 1046 | const connected = params.get("connected") === "1"; |
| 1046 | 1047 | 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." }) | |
| 1051 | 1049 | : 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" | |
| 1056 | 1054 | ? "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 | }) | |
| 1060 | 1057 | : null; |
| 1061 | 1058 | |
| 1062 | 1059 | const timing = { rendering: performance.now() }; |
src/web/style/components.ts +13
| @@ -1002,6 +1002,19 @@ html[data-overlay] { overflow: hidden; } | ||
| 1002 | 1002 | .toast.blocked .toast-body { flex: 1 1 280px; } |
| 1003 | 1003 | .toast code { display: block; margin-top: 6px; } |
| 1004 | 1004 | |
| 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 | ||
| 1005 | 1018 | /* ---- tooltip ---- */ |
| 1006 | 1019 | [data-tip] { position: relative; } |
| 1007 | 1020 | [data-tip]::after { |
src/web/ui.ts +76
| @@ -1462,6 +1462,82 @@ export function empty(o: { | ||
| 1462 | 1462 | </div>`; |
| 1463 | 1463 | } |
| 1464 | 1464 | |
| 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 | ||
| 1465 | 1541 | export interface TimelineEvent { |
| 1466 | 1542 | icon: IconName; |
| 1467 | 1543 | tone?: Tone; |
test/design-gallery.test.ts added +50
| @@ -0,0 +1,50 @@ | ||
| 1 | /** | |
| 2 | * The parts #5880 adds to ui.ts — toast, dialogParts, skeleton — and their specimens in | |
| 3 | * /design/gallery. | |
| 4 | */ | |
| 5 | import { describe, expect, test } from "bun:test"; | |
| 6 | import { html } from "../src/web/html.ts"; | |
| 7 | import { samples } from "../src/web/pages/design-components.ts"; | |
| 8 | import * as ui from "../src/web/ui.ts"; | |
| 9 | ||
| 10 | describe("the gallery shows the new parts", () => { | |
| 11 | test("toast / flash, dialog and skeleton each have a specimen that draws the part", () => { | |
| 12 | const drawn = (name: string) => samples.find(([n]) => n === name)?.[1].text ?? ""; | |
| 13 | expect(drawn("toast / flash")).toContain('data-tone="danger" role="alert"'); | |
| 14 | expect(drawn("dialog")).toContain('<h2 id="gallery-dialog-title">'); | |
| 15 | expect(drawn("skeleton")).toContain('class="card skeleton"'); | |
| 16 | }); | |
| 17 | }); | |
| 18 | ||
| 19 | describe("toast, dialogParts and skeleton (#5880)", () => { | |
| 20 | test("a toast carries its tone's icon, escapes its message, and adds a link and a code only when given", () => { | |
| 21 | const ok = ui.toast({ message: "<b>done</b>" }).text; | |
| 22 | expect(ok).toContain('class="toast" data-tone="success" role="status"'); | |
| 23 | expect(ok).toContain("#i-check"); | |
| 24 | expect(ok).toContain("<b>done</b>"); | |
| 25 | expect(ok).not.toContain("<a "); | |
| 26 | const bad = ui.toast({ tone: "danger", role: "alert", message: "x", link: { href: "/a", label: "See it" }, code: "c0de", codeId: "k" }).text; | |
| 27 | expect(bad).toContain('data-tone="danger" role="alert"'); | |
| 28 | expect(bad).toContain("#i-alert"); | |
| 29 | expect(bad).toContain('<a href="/a">See it</a>'); | |
| 30 | expect(bad).toContain('id="k"'); | |
| 31 | expect(bad).toContain("c0de"); | |
| 32 | }); | |
| 33 | ||
| 34 | test("dialogParts draws head, body and foot, the title named by its id", () => { | |
| 35 | const d = ui.dialogParts({ titleId: "t", title: "Sure?", body: html`<p>b</p>`, foot: html`<button>f</button>`, stack: true }).text; | |
| 36 | expect(d).toMatch(/<div class="dialog-head"><h2 id="t">Sure\?<\/h2><\/div>\s*<div class="dialog-body stack"\s*><p>b<\/p><\/div>\s*<div class="dialog-foot"><button>f<\/button><\/div>/); | |
| 37 | }); | |
| 38 | ||
| 39 | test("a skeleton holds real list rows or a real card, says it is loading, and keeps its row count in bounds", () => { | |
| 40 | const list = ui.skeleton({ rows: 4 }).text; | |
| 41 | expect(list).toContain('class="card skeleton" role="status" aria-busy="true"'); | |
| 42 | expect(list).toContain("Loading…"); | |
| 43 | expect(list.match(/<li><div class="row">/g)?.length).toBe(4); | |
| 44 | expect(ui.skeleton({ rows: 999 }).text.match(/<li>/g)?.length).toBe(50); | |
| 45 | expect(ui.skeleton({ rows: Number.NaN }).text.match(/<li>/g)?.length).toBe(1); | |
| 46 | const card = ui.skeleton({ shape: "card", rows: 2 }).text; | |
| 47 | expect(card).toContain('class="card-head"'); | |
| 48 | expect(card.match(/<p><span class="skeleton-bar">/g)?.length).toBe(2); | |
| 49 | }); | |
| 50 | }); |
new above
offline