Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

toast.logic.ts

115 lines3.3 KB

1import type { ScopedThreadRef, ThreadId } from "@t3tools/contracts";
2​
3export function shouldHideCollapsedToastContent(
4 visibleToastIndex: number,
5 visibleToastCount: number,
6): boolean {
7 // Keep the front-most toast readable even if Base UI marks it as "behind"
8 // due to toasts hidden by thread filtering.
9 if (visibleToastCount <= 1) return false;
10 return visibleToastIndex > 0;
12​
13type ToastWithHeight = {
14 height?: number | null | undefined;
15};
16​
17type ToastWithTransitionStatus = {
18 transitionStatus?: "starting" | "ending" | undefined;
19};
20​
21type ToastWithLayoutProps = ToastWithHeight & ToastWithTransitionStatus;
22​
23type VisibleToastLayoutItem<TToast extends object> = {
24 toast: TToast;
25 visibleIndex: number;
26 offsetY: number;
27};
28​
29export function buildVisibleToastLayout<TToast extends object>(
30 visibleToasts: readonly (TToast & ToastWithLayoutProps)[],
31): {
32 frontmostHeight: number;
33 items: VisibleToastLayoutItem<TToast & ToastWithLayoutProps>[];
34} {
35 // Two parallel cursors:
36 // - `full*` advances on every toast, so an ending toast keeps the slot it
37 // occupied before dismissal and its data-ending-style exit transform
38 // originates from the correct position (critical for dismissing a
39 // non-front toast in the expanded stack — otherwise it would snap to
40 // Y=0 and slide off diagonally).
41 // - `live*` advances only on non-ending toasts, so live toasts reflow
42 // past the vacated slot in parallel with the exit animation instead of
43 // waiting for it to finish (which caused a visible "stop and bump").
44 let fullIndex = 0;
45 let fullOffsetY = 0;
46 let liveIndex = 0;
47 let liveOffsetY = 0;
48​
49 const items: VisibleToastLayoutItem<TToast & ToastWithLayoutProps>[] = visibleToasts.map(
50 (toast) => {
51 const height = normalizeToastHeight(toast.height);
52​
53 if (toast.transitionStatus === "ending") {
54 const item = {
55 toast,
56 visibleIndex: fullIndex,
57 offsetY: fullOffsetY,
58 };
59 fullOffsetY += height;
60 fullIndex += 1;
61 return item;
62 }
63​
64 const item = {
65 toast,
66 visibleIndex: liveIndex,
67 offsetY: liveOffsetY,
68 };
69​
70 fullOffsetY += height;
71 fullIndex += 1;
72 liveOffsetY += height;
73 liveIndex += 1;
74 return item;
75 },
76 );
77​
78 // Frontmost height should reflect the first non-ending (live) toast so the
79 // stack sizes to what's actually staying on screen.
80 const frontmostLiveToast = visibleToasts.find((toast) => toast.transitionStatus !== "ending");
81​
82 return {
83 frontmostHeight: normalizeToastHeight(frontmostLiveToast?.height),
84 items,
85 };
87​
88function normalizeToastHeight(height: number | null | undefined): number {
89 return typeof height === "number" && Number.isFinite(height) && height > 0 ? height : 0;
91​
92export function shouldRenderThreadScopedToast(
93 data:
94 | {
95 threadRef?: ScopedThreadRef | null;
96 threadId?: ThreadId | null;
97 }
98 | undefined,
99 activeThreadRef: ScopedThreadRef | null,
100): boolean {
101 if (data?.threadRef) {
102 return (
103 activeThreadRef !== null &&
104 data.threadRef.environmentId === activeThreadRef.environmentId &&
105 data.threadRef.threadId === activeThreadRef.threadId
106 );
107 }
108​
109 const toastThreadId = data?.threadId;
110 if (!toastThreadId) {
111 return true;
112 }
113​
114 return activeThreadRef?.threadId === toastThreadId;