feat(studio): toasts show as a centred pill that rises in (#7714)

* feat(studio): toasts show as a centred pill that rises in

* fix(studio): error toasts stay up on a click so their text can be copied
This commit is contained in:
Miguel Ángel
2026-11-14 04:15:03 +00:00
committed by GitHub
parent 3ce9ebeaf1
commit 690b07e455
7 changed files with 112 additions and 73 deletions
@@ -75,19 +75,21 @@ export const StudioOverlays = memo(function StudioOverlays({
}}
/>
)}
{/* One bottom-right stack so the feedback card and toasts queue instead
of covering each other. Empty when nothing is showing. */}
<div className="absolute bottom-6 right-6 z-91 flex flex-col items-end gap-2">
{toasts.map((toast) => (
<StudioToast
key={toast.id}
message={toast.message}
tone={toast.tone}
leaving={toast.leaving}
onDismiss={() => dismissToast(toast.id)}
/>
))}
<StudioFeedbackCard />
{/* Toasts centre in the room the feedback card leaves, so the two never cover each other. */}
<div className="pointer-events-none absolute inset-x-6 bottom-6 z-91 flex items-end gap-2">
<div className="flex min-w-0 flex-1 flex-col items-center gap-2 *:pointer-events-auto">
{toasts.map((toast) => (
<StudioToast
key={toast.id}
message={toast.message}
tone={toast.tone}
onDismiss={() => dismissToast(toast.id)}
/>
))}
</div>
<div className="pointer-events-auto">
<StudioFeedbackCard />
</div>
</div>
</>
);
@@ -0,0 +1,41 @@
// @vitest-environment happy-dom
import React, { act } from "react";
import { afterEach, describe, expect, it } from "vitest";
import { mountReactHarness } from "../hooks/domSelectionTestHarness";
import { StudioToast } from "./StudioToast";
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
afterEach(() => {
document.body.innerHTML = "";
});
function mount(tone: "error" | "info") {
const dismissed: string[] = [];
mountReactHarness(
<StudioToast message="Saved" tone={tone} onDismiss={() => dismissed.push(tone)} />,
);
return dismissed;
}
describe("StudioToast", () => {
it("dismisses an info toast on a click anywhere on it, with no Dismiss button", () => {
const dismissed = mount("info");
expect(document.querySelector("button")).toBeNull();
act(() => document.querySelector<HTMLElement>("[role=status]")?.click());
expect(dismissed).toEqual(["info"]);
});
it("keeps an error toast up on a click on it, so its text can be selected and copied", () => {
const dismissed = mount("error");
act(() => document.querySelector<HTMLElement>("[role=alert]")?.click());
expect(dismissed).toEqual([]);
});
it("gives a persisting error toast a Dismiss button that dismisses it once", () => {
const dismissed = mount("error");
act(() => document.querySelector<HTMLElement>("[aria-label=Dismiss]")?.click());
expect(dismissed).toEqual(["error"]);
});
});
+23 -39
View File
@@ -1,50 +1,34 @@
import { X } from "../icons/SystemIcons";
import { toastPersists } from "../hooks/useToast";
interface StudioToastProps {
message: string;
tone?: "error" | "info";
/** Plays the exit animation when true (owner removes the node after ~160ms). */
leaving?: boolean;
onDismiss?: () => void;
tone: "error" | "info";
onDismiss: () => void;
}
export function StudioToast({ message, tone, leaving, onDismiss }: StudioToastProps) {
const isError = tone === "error";
export function StudioToast({ message, tone, onDismiss }: StudioToastProps) {
const persists = toastPersists(tone);
return (
<div
role={isError ? "alert" : "status"}
className={`motion-reduce:animate-none ${leaving ? "hf-toast-exit" : "hf-toast-enter"}`}
role={tone === "error" ? "alert" : "status"}
onClick={persists ? undefined : onDismiss}
onKeyDown={(event) => {
if (event.key === "Escape") onDismiss();
}}
className={`hf-notice-in flex min-h-12 w-fit max-w-[min(680px,100%)] items-center gap-1.5 rounded-3xl bg-text-0 py-1.5 pr-1.5 pl-5 text-[14px] leading-5 font-medium wrap-anywhere text-bg-0 shadow-notice not-has-[button]:pr-5 ${persists ? "" : "cursor-pointer"}`}
>
<div
className={`relative flex max-w-[min(420px,calc(100vw-48px))] items-center gap-3 overflow-hidden rounded-2xl py-3 pl-4 pr-2 text-[12px] ${toastSurface(isError)}`}
>
<span
className={`min-w-0 wrap-break-word leading-5 ${isError ? "text-danger-ink" : "text-text-0"}`}
<span className="min-w-0 flex-1 text-pretty">{message}</span>
{persists && (
<button
type="button"
onClick={onDismiss}
className="relative ml-2 grid h-9 w-9 shrink-0 place-items-center rounded-full text-bg-0/72 transition-[background-color,color,scale] duration-hover ease-out hover:bg-bg-0/16 hover:text-bg-0 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-bg-0 active:scale-[0.96]"
aria-label="Dismiss"
>
{message}
</span>
{onDismiss && (
<button
type="button"
onClick={onDismiss}
className="flex h-5 w-5 shrink-0 items-center justify-center rounded-md text-neutral-500 transition-colors hover:bg-hover hover:text-neutral-300"
aria-label="Dismiss"
>
<svg
width="10"
height="10"
viewBox="0 0 10 10"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
>
<path d="M2 2l6 6M8 2l-6 6" />
</svg>
</button>
)}
</div>
<X size={14} />
</button>
)}
</div>
);
}
export function toastSurface(isError: boolean): string {
return `border shadow-popover bg-raised ${isError ? "border-danger/40" : "border-border"}`;
}
@@ -1,5 +1,4 @@
import { memo, useState, useCallback, useRef, useEffect } from "react";
import { toastSurface } from "../StudioToast";
import {
trackStudioFeedback,
trackStudioFeedbackShown,
@@ -213,7 +212,7 @@ export const StudioFeedbackCard = memo(function StudioFeedbackCard() {
}}
>
<div
className={`flex w-[min(340px,calc(100vw-48px))] flex-col gap-2.5 overflow-hidden rounded-2xl px-3.5 py-3 ${toastSurface(failed)}`}
className={`flex w-[min(340px,calc(100vw-48px))] flex-col gap-2.5 overflow-hidden rounded-2xl px-3.5 py-3 ${cardSurface(failed)}`}
>
<div className="flex items-start gap-3">
<p
@@ -359,3 +358,7 @@ export const StudioFeedbackCard = memo(function StudioFeedbackCard() {
</div>
);
});
function cardSurface(isError: boolean): string {
return `border shadow-popover bg-raised ${isError ? "border-danger/40" : "border-border"}`;
}
+6 -18
View File
@@ -4,19 +4,18 @@ import type { AppToast } from "../utils/studioHelpers";
interface ToastItem extends AppToast {
id: number;
/** True while the exit animation plays, just before removal. */
leaving?: boolean;
}
const AUTO_DISMISS_MS = 4000;
const EXIT_MS = 160;
const MAX_TOASTS = 3;
/** Error toasts persist until dismissed so failures can't silently vanish. */
export const toastPersists = (tone: AppToast["tone"]) => tone === "error";
let nextToastId = 1;
/**
* Stacked toasts (max 3). Info toasts auto-dismiss after 4s; error toasts
* persist until explicitly dismissed so failures can't silently vanish.
* Stacked toasts (max 3). Info toasts auto-dismiss after 4s; error toasts persist.
*/
export function useToast() {
const [toasts, setToasts] = useState<ToastItem[]>([]);
@@ -30,7 +29,7 @@ export function useToast() {
}
}, []);
const removeToast = useCallback(
const dismissToast = useCallback(
(id: number) => {
clearTimer(id);
setToasts((prev) => prev.filter((t) => t.id !== id));
@@ -38,17 +37,6 @@ export function useToast() {
[clearTimer],
);
const dismissToast = useCallback(
(id: number) => {
clearTimer(id);
// Mark leaving so the exit animation plays, then remove.
setToasts((prev) => prev.map((t) => (t.id === id ? { ...t, leaving: true } : t)));
const timer = setTimeout(() => removeToast(id), EXIT_MS);
timersRef.current.set(id, timer);
},
[clearTimer, removeToast],
);
const showToast = useCallback(
(message: string, tone: AppToast["tone"] = "error"): number => {
const id = nextToastId++;
@@ -61,7 +49,7 @@ export function useToast() {
}
return next;
});
if (tone !== "error") {
if (!toastPersists(tone)) {
const timer = setTimeout(() => dismissToast(id), AUTO_DISMISS_MS);
timersRef.current.set(id, timer);
}
+19 -1
View File
@@ -605,7 +605,7 @@
}
}
/* Toast enter/exit (StudioToast) */
/* Feedback card enter/exit (StudioFeedbackCard) */
@keyframes hf-toast-in {
from {
opacity: 0;
@@ -646,6 +646,24 @@
}
}
/* A toast rises in and leaves at once (StudioToast). */
@keyframes hf-notice-in {
from {
opacity: 0;
transform: translateY(8px) scale(0.98);
}
}
.hf-notice-in {
animation: hf-notice-in 200ms cubic-bezier(0.32, 0.72, 0, 1) backwards;
}
@media (prefers-reduced-motion: reduce) {
.hf-notice-in {
animation: none;
}
}
/* Overlay/backdrop entrance — the heaviest visual changes shouldn't pop in */
@keyframes hf-backdrop-in {
from {
+3
View File
@@ -285,6 +285,9 @@
0 16px 40px light-dark(rgb(0 0 0 / 0.14), rgb(0 0 0 / 0.55)),
0 0 0 1px light-dark(rgb(0 0 0 / 0.07), rgb(0 0 0 / 0.45));
/* The toast pill: a close contact shadow under a soft lift. */
--shadow-notice: 0 2px 4px rgb(0 0 0 / 0.12), 0 14px 36px -8px rgb(0 0 0 / 0.4);
/* The primary button reads as a raised key rather than a floating panel: a tight
* contact shadow, a soft ambient one, and an inner top highlight. */
--shadow-btn-primary: