From 789f21a14799d11366325bb644691bc9c6642843 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 28 Oct 2026 21:41:19 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20dashboard:=20toasts=20show=20a=20co?= =?UTF-8?q?untdown=20bar=20timed=20to=20their=20auto-dismiss?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every timed toast (success/info/warning, error with an explicit deadline, and an in-flight toast resolved by dismissToast) now carries a 2px .toast-bar along its bottom edge that drains from full to empty over the same milliseconds the dismiss timer uses: toastCountdownBar arms both the setTimeout and the bar's inline animation-duration from one value. Sticky (error) and persistent (spinner) toasts show no bar. The bar is absolutely positioned (no layout shift), takes the severity token at reduced opacity, is clipped to the toast radius, and is hidden under prefers-reduced-motion. Fixes #15230 Signed-off-by: Andrew Anderson --- changelog.d/added-toast-countdown-bar.md | 1 + src/pkg/dashboard/static/index.html | 36 ++++++++-- src/pkg/dashboard/toast_countdown_bar_test.go | 67 +++++++++++++++++++ 3 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 changelog.d/added-toast-countdown-bar.md create mode 100644 src/pkg/dashboard/toast_countdown_bar_test.go diff --git a/changelog.d/added-toast-countdown-bar.md b/changelog.d/added-toast-countdown-bar.md new file mode 100644 index 0000000000..bf2ed69803 --- /dev/null +++ b/changelog.d/added-toast-countdown-bar.md @@ -0,0 +1 @@ +- **Dashboard toasts show a countdown bar timed to their auto-dismiss.** Every timed toast — success, info, warning, an error given an explicit deadline, and an in-flight toast resolved into its result — now draws a 2px bar along its bottom edge that drains from full to empty over exactly the toast's dismiss window, so it reaches zero at the moment the toast leaves. The bar takes the toast's severity colour at reduced opacity, never shifts layout, is hidden under `prefers-reduced-motion`, and is absent on sticky (error) and persistent (spinner) toasts that wait for a click. diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index fce48fe60c..b6e2a6fd2e 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -3496,7 +3496,21 @@ border: 1px solid var(--line); border-left: 3px solid var(--muted); background: var(--panel); box-shadow: var(--shadow-raised); animation: toast-in 0.25s ease-out; max-width: 360px; - } + position: relative; overflow: hidden; + } + /* Countdown bar: a timed toast's remaining life, drained along the bottom + edge over exactly its auto-dismiss window (animation-duration is set + inline by toastCountdownBar from the same ms the dismiss timer uses). + Absolutely positioned so it never shifts the toast's layout; clipped + to the toast radius by the parent's overflow. Sticky/persistent + toasts carry no bar. */ + .toast-bar { position: absolute; left: 0; right: 0; bottom: 0; height: 2px; pointer-events: none; transform-origin: left center; background: var(--muted); opacity: 0.55; animation: toast-bar-drain linear forwards; } + .toast.success .toast-bar { background: var(--green); } + .toast.error .toast-bar { background: var(--red); } + .toast.info .toast-bar { background: var(--blue); } + .toast.warning .toast-bar { background: var(--status-warn); } + @keyframes toast-bar-drain { from { transform: scaleX(1); } to { transform: scaleX(0); } } + @media (prefers-reduced-motion: reduce) { .toast-bar { display: none; } } .toast.success { background: color-mix(in srgb, var(--green) 14%, var(--panel)); border-color: color-mix(in srgb, var(--green) 35%, var(--line)); border-left-color: var(--green); } .toast.error { background: color-mix(in srgb, var(--red) 14%, var(--panel)); border-color: color-mix(in srgb, var(--red) 35%, var(--line)); border-left-color: var(--red); } .toast.info { background: color-mix(in srgb, var(--blue) 14%, var(--panel)); border-color: color-mix(in srgb, var(--blue) 35%, var(--line)); border-left-color: var(--blue); } @@ -39769,6 +39783,20 @@

GitHub Projects sync

el.appendChild(close); el.addEventListener('click', () => dismissToast(el, null, null, true)); } + // toastCountdownBar arms a toast's auto-dismiss: one setTimeout that + // closes it after `ms`, and a .toast-bar whose drain animation runs for + // the same `ms`, so the bar hits zero as the toast leaves. Every timed + // toast path (success/info/warning, error-with-deadline, in-flight toast + // resolved by dismissToast) goes through here; sticky and persistent + // toasts never call it and so show no bar. + function toastCountdownBar(el, ms, onExpire) { + const bar = document.createElement('span'); + bar.className = 'toast-bar'; + bar.setAttribute('aria-hidden', 'true'); + bar.style.animationDuration = ms + 'ms'; + el.appendChild(bar); + setTimeout(() => { if (el.parentNode) onExpire(); }, ms); + } function showToast(msg, type = 'info', persistent = false, autoDismissMs = 0) { const opts = (persistent && typeof persistent === 'object') ? persistent : {}; if (persistent && typeof persistent === 'object') persistent = !!opts.persistent; @@ -39799,10 +39827,10 @@

GitHub Projects sync

/* autoDismissMs opts an error toast out of stick-forever: it keeps the × for early dismissal but self-clears after the deadline (forced — dismissToast ignores sticky toasts otherwise). */ - if (autoDismissMs > 0) setTimeout(() => { if (el.parentNode) dismissToast(el, null, null, true); }, autoDismissMs); + if (autoDismissMs > 0) toastCountdownBar(el, autoDismissMs, () => dismissToast(el, null, null, true)); return el; } - setTimeout(() => dismissToast(el), autoDismissMs > 0 ? autoDismissMs : TOAST_DURATION_MS); + toastCountdownBar(el, autoDismissMs > 0 ? autoDismissMs : TOAST_DURATION_MS, () => dismissToast(el)); return el; } function handleGitHubSetupToast() { @@ -39833,7 +39861,7 @@

GitHub Projects sync

// A resolved in-flight toast that resolved to an error stays put for // the same reason a direct error toast does. if (newType === 'error') { makeToastSticky(el); return; } - setTimeout(() => { el.style.animation = 'toast-out 0.3s ease-in forwards'; setTimeout(() => el.remove(), 300); }, TOAST_DURATION_MS); + toastCountdownBar(el, TOAST_DURATION_MS, () => { el.style.animation = 'toast-out 0.3s ease-in forwards'; setTimeout(() => el.remove(), 300); }); } else { if (el._sticky && !force) return; el.style.animation = 'toast-out 0.3s ease-in forwards'; diff --git a/src/pkg/dashboard/toast_countdown_bar_test.go b/src/pkg/dashboard/toast_countdown_bar_test.go new file mode 100644 index 0000000000..baa5549709 --- /dev/null +++ b/src/pkg/dashboard/toast_countdown_bar_test.go @@ -0,0 +1,67 @@ +package dashboard + +import ( + "strings" + "testing" +) + +// TestToastCountdownBarWiredToDismissTimer asserts the dashboard's timed +// toasts carry a countdown bar whose animation duration is the same number +// of milliseconds as the dismiss timer: both come from toastCountdownBar, so +// a 12 s toast drains over 12 s and a 3 s toast over 3 s. Sticky errors and +// persistent spinner toasts must not grow a bar, and reduced-motion hides it. +func TestToastCountdownBarWiredToDismissTimer(t *testing.T) { + b, err := staticFS.ReadFile("static/index.html") + if err != nil { + t.Fatalf("reading embedded static/index.html: %v", err) + } + html := string(b) + + helper := extractJSFunction(t, html, "toastCountdownBar") + for _, want := range []string{ + "bar.className = 'toast-bar'", + "bar.style.animationDuration = ms + 'ms'", + "setTimeout(() => { if (el.parentNode) onExpire(); }, ms)", + } { + if !strings.Contains(helper, want) { + t.Errorf("toastCountdownBar missing %q — the bar and the dismiss timer must share one duration", want) + } + } + + show := extractJSFunction(t, html, "showToast") + for _, want := range []string{ + "toastCountdownBar(el, autoDismissMs > 0 ? autoDismissMs : TOAST_DURATION_MS, () => dismissToast(el))", + "if (autoDismissMs > 0) toastCountdownBar(el, autoDismissMs, () => dismissToast(el, null, null, true))", + } { + if !strings.Contains(show, want) { + t.Errorf("showToast missing %q", want) + } + } + if strings.Contains(show, "setTimeout(() => dismissToast(el)") { + t.Error("showToast still arms a bare dismiss timer; timed toasts must go through toastCountdownBar") + } + // The persistent (spinner) branch keeps its safety-net timeout but no bar. + persistent := show[strings.Index(show, "if (persistent) {"):strings.Index(show, "if (type === 'error') {")] + if strings.Contains(persistent, "toastCountdownBar(") { + t.Error("persistent toasts have no fixed lifetime and must not show a countdown bar") + } + + dismiss := extractJSFunction(t, html, "dismissToast") + if !strings.Contains(dismiss, "toastCountdownBar(el, TOAST_DURATION_MS,") { + t.Error("dismissToast(el, newMsg) rewrites a toast into a timed result and must arm the countdown bar") + } + + for _, want := range []string{ + ".toast-bar { position: absolute;", + "animation: toast-bar-drain linear forwards;", + "@keyframes toast-bar-drain { from { transform: scaleX(1); } to { transform: scaleX(0); } }", + "@media (prefers-reduced-motion: reduce) { .toast-bar { display: none; } }", + ".toast.success .toast-bar { background: var(--green); }", + ".toast.error .toast-bar { background: var(--red); }", + ".toast.warning .toast-bar { background: var(--status-warn); }", + } { + if !strings.Contains(html, want) { + t.Errorf("toast countdown CSS missing %q", want) + } + } +}