Skip to content

Commit 863f18a

Browse files
committed
feat(studio): toasts show as a centred pill that rises in
1 parent 4588202 commit 863f18a

7 files changed

Lines changed: 110 additions & 73 deletions

File tree

‎packages/studio/src/components/StudioOverlays.tsx‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,21 @@ export const StudioOverlays = memo(function StudioOverlays({
7575
}}
7676
/>
7777
)}
78-
{/* One bottom-right stack so the feedback card and toasts queue instead
79-
of covering each other. Empty when nothing is showing. */}
80-
<div className="absolute bottom-6 right-6 z-91 flex flex-col items-end gap-2">
81-
{toasts.map((toast) => (
82-
<StudioToast
83-
key={toast.id}
84-
message={toast.message}
85-
tone={toast.tone}
86-
leaving={toast.leaving}
87-
onDismiss={() => dismissToast(toast.id)}
88-
/>
89-
))}
90-
<StudioFeedbackCard />
78+
{/* Toasts centre in the room the feedback card leaves, so the two never cover each other. */}
79+
<div className="pointer-events-none absolute inset-x-6 bottom-6 z-91 flex items-end gap-2">
80+
<div className="flex min-w-0 flex-1 flex-col items-center gap-2 *:pointer-events-auto">
81+
{toasts.map((toast) => (
82+
<StudioToast
83+
key={toast.id}
84+
message={toast.message}
85+
tone={toast.tone}
86+
onDismiss={() => dismissToast(toast.id)}
87+
/>
88+
))}
89+
</div>
90+
<div className="pointer-events-auto">
91+
<StudioFeedbackCard />
92+
</div>
9193
</div>
9294
</>
9395
);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// @vitest-environment happy-dom
2+
3+
import React, { act } from "react";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import { mountReactHarness } from "../hooks/domSelectionTestHarness";
6+
import { StudioToast } from "./StudioToast";
7+
8+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
9+
10+
afterEach(() => {
11+
document.body.innerHTML = "";
12+
});
13+
14+
function mount(tone: "error" | "info") {
15+
const dismissed: string[] = [];
16+
mountReactHarness(
17+
<StudioToast message="Saved" tone={tone} onDismiss={() => dismissed.push(tone)} />,
18+
);
19+
return dismissed;
20+
}
21+
22+
describe("StudioToast", () => {
23+
it("dismisses an info toast on a click anywhere on it, with no Dismiss button", () => {
24+
const dismissed = mount("info");
25+
expect(document.querySelector("button")).toBeNull();
26+
act(() => document.querySelector<HTMLElement>("[role=status]")?.click());
27+
expect(dismissed).toEqual(["info"]);
28+
});
29+
30+
it("gives a persisting error toast a Dismiss button that dismisses it once", () => {
31+
const dismissed = mount("error");
32+
act(() => document.querySelector<HTMLElement>("[aria-label=Dismiss]")?.click());
33+
expect(dismissed).toEqual(["error"]);
34+
});
35+
});
Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,38 @@
1+
import { X } from "../icons/SystemIcons";
2+
import { toastPersists } from "../hooks/useToast";
3+
14
interface StudioToastProps {
25
message: string;
3-
tone?: "error" | "info";
4-
/** Plays the exit animation when true (owner removes the node after ~160ms). */
5-
leaving?: boolean;
6-
onDismiss?: () => void;
6+
tone: "error" | "info";
7+
onDismiss: () => void;
78
}
89

9-
export function StudioToast({ message, tone, leaving, onDismiss }: StudioToastProps) {
10-
const isError = tone === "error";
10+
/** An inverted pill: a click or Escape dismisses it, and one that persists also gets a Dismiss button. */
11+
export function StudioToast({ message, tone, onDismiss }: StudioToastProps) {
1112
return (
1213
<div
13-
role={isError ? "alert" : "status"}
14-
className={`motion-reduce:animate-none ${leaving ? "hf-toast-exit" : "hf-toast-enter"}`}
14+
role={tone === "error" ? "alert" : "status"}
15+
onClick={onDismiss}
16+
onKeyDown={(event) => {
17+
if (event.key === "Escape") onDismiss();
18+
}}
19+
className="hf-notice-in flex min-h-12 w-fit max-w-[min(680px,100%)] cursor-pointer 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"
1520
>
16-
<div
17-
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)}`}
18-
>
19-
<span
20-
className={`min-w-0 wrap-break-word leading-5 ${isError ? "text-danger-ink" : "text-text-0"}`}
21+
<span className="min-w-0 flex-1 text-pretty">{message}</span>
22+
{toastPersists(tone) && (
23+
<button
24+
type="button"
25+
onClick={(event) => {
26+
// The pill's own click would dismiss it a second time.
27+
event.stopPropagation();
28+
onDismiss();
29+
}}
30+
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]"
31+
aria-label="Dismiss"
2132
>
22-
{message}
23-
</span>
24-
{onDismiss && (
25-
<button
26-
type="button"
27-
onClick={onDismiss}
28-
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"
29-
aria-label="Dismiss"
30-
>
31-
<svg
32-
width="10"
33-
height="10"
34-
viewBox="0 0 10 10"
35-
fill="none"
36-
stroke="currentColor"
37-
strokeWidth="1.5"
38-
>
39-
<path d="M2 2l6 6M8 2l-6 6" />
40-
</svg>
41-
</button>
42-
)}
43-
</div>
33+
<X size={14} />
34+
</button>
35+
)}
4436
</div>
4537
);
4638
}
47-
48-
export function toastSurface(isError: boolean): string {
49-
return `border shadow-popover bg-raised ${isError ? "border-danger/40" : "border-border"}`;
50-
}

‎packages/studio/src/components/feedback/StudioFeedbackCard.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { memo, useState, useCallback, useRef, useEffect } from "react";
2-
import { toastSurface } from "../StudioToast";
32
import {
43
trackStudioFeedback,
54
trackStudioFeedbackShown,
@@ -213,7 +212,7 @@ export const StudioFeedbackCard = memo(function StudioFeedbackCard() {
213212
}}
214213
>
215214
<div
216-
className={`flex w-[min(340px,calc(100vw-48px))] flex-col gap-2.5 overflow-hidden rounded-2xl px-3.5 py-3 ${toastSurface(failed)}`}
215+
className={`flex w-[min(340px,calc(100vw-48px))] flex-col gap-2.5 overflow-hidden rounded-2xl px-3.5 py-3 ${cardSurface(failed)}`}
217216
>
218217
<div className="flex items-start gap-3">
219218
<p
@@ -359,3 +358,7 @@ export const StudioFeedbackCard = memo(function StudioFeedbackCard() {
359358
</div>
360359
);
361360
});
361+
362+
function cardSurface(isError: boolean): string {
363+
return `border shadow-popover bg-raised ${isError ? "border-danger/40" : "border-border"}`;
364+
}

‎packages/studio/src/hooks/useToast.ts‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import type { AppToast } from "../utils/studioHelpers";
44

55
interface ToastItem extends AppToast {
66
id: number;
7-
/** True while the exit animation plays, just before removal. */
8-
leaving?: boolean;
97
}
108

119
const AUTO_DISMISS_MS = 4000;
12-
const EXIT_MS = 160;
1310
const MAX_TOASTS = 3;
1411

12+
/** Error toasts persist until dismissed so failures can't silently vanish. */
13+
export const toastPersists = (tone: AppToast["tone"]) => tone === "error";
14+
1515
let nextToastId = 1;
1616

1717
/**
18-
* Stacked toasts (max 3). Info toasts auto-dismiss after 4s; error toasts
19-
* persist until explicitly dismissed so failures can't silently vanish.
18+
* Stacked toasts (max 3). Info toasts auto-dismiss after 4s; error toasts persist.
2019
*/
2120
export function useToast() {
2221
const [toasts, setToasts] = useState<ToastItem[]>([]);
@@ -30,25 +29,14 @@ export function useToast() {
3029
}
3130
}, []);
3231

33-
const removeToast = useCallback(
32+
const dismissToast = useCallback(
3433
(id: number) => {
3534
clearTimer(id);
3635
setToasts((prev) => prev.filter((t) => t.id !== id));
3736
},
3837
[clearTimer],
3938
);
4039

41-
const dismissToast = useCallback(
42-
(id: number) => {
43-
clearTimer(id);
44-
// Mark leaving so the exit animation plays, then remove.
45-
setToasts((prev) => prev.map((t) => (t.id === id ? { ...t, leaving: true } : t)));
46-
const timer = setTimeout(() => removeToast(id), EXIT_MS);
47-
timersRef.current.set(id, timer);
48-
},
49-
[clearTimer, removeToast],
50-
);
51-
5240
const showToast = useCallback(
5341
(message: string, tone: AppToast["tone"] = "error"): number => {
5442
const id = nextToastId++;
@@ -61,7 +49,7 @@ export function useToast() {
6149
}
6250
return next;
6351
});
64-
if (tone !== "error") {
52+
if (!toastPersists(tone)) {
6553
const timer = setTimeout(() => dismissToast(id), AUTO_DISMISS_MS);
6654
timersRef.current.set(id, timer);
6755
}

‎packages/studio/src/styles/components.css‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@
605605
}
606606
}
607607

608-
/* Toast enter/exit (StudioToast) */
608+
/* Feedback card enter/exit (StudioFeedbackCard) */
609609
@keyframes hf-toast-in {
610610
from {
611611
opacity: 0;
@@ -646,6 +646,24 @@
646646
}
647647
}
648648

649+
/* A toast rises in and leaves at once (StudioToast). */
650+
@keyframes hf-notice-in {
651+
from {
652+
opacity: 0;
653+
transform: translateY(8px) scale(0.98);
654+
}
655+
}
656+
657+
.hf-notice-in {
658+
animation: hf-notice-in 200ms cubic-bezier(0.32, 0.72, 0, 1) backwards;
659+
}
660+
661+
@media (prefers-reduced-motion: reduce) {
662+
.hf-notice-in {
663+
animation: none;
664+
}
665+
}
666+
649667
/* Overlay/backdrop entrance — the heaviest visual changes shouldn't pop in */
650668
@keyframes hf-backdrop-in {
651669
from {

‎packages/studio/src/styles/theme.css‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@
285285
0 16px 40px light-dark(rgb(0 0 0 / 0.14), rgb(0 0 0 / 0.55)),
286286
0 0 0 1px light-dark(rgb(0 0 0 / 0.07), rgb(0 0 0 / 0.45));
287287

288+
/* The toast pill: a close contact shadow under a soft lift. */
289+
--shadow-notice: 0 2px 4px rgb(0 0 0 / 0.12), 0 14px 36px -8px rgb(0 0 0 / 0.4);
290+
288291
/* The primary button reads as a raised key rather than a floating panel: a tight
289292
* contact shadow, a soft ambient one, and an inner top highlight. */
290293
--shadow-btn-primary:

0 commit comments

Comments
 (0)