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

toast.logic.ts

46 lines1.2 KB

1export function shouldHideCollapsedToastContent(
2 visibleToastIndex: number,
3 visibleToastCount: number,
4): boolean {
5 // Keep the front-most toast readable even if Base UI marks it as "behind"
6 // due to toasts hidden by thread filtering.
7 if (visibleToastCount <= 1) return false;
8 return visibleToastIndex > 0;
9}
10​
11type ToastWithHeight = {
12 height?: number | null | undefined;
13};
14​
15type VisibleToastLayoutItem<TToast extends object> = {
16 toast: TToast;
17 visibleIndex: number;
18 offsetY: number;
19};
20​
21export function buildVisibleToastLayout<TToast extends object>(
22 visibleToasts: readonly (TToast & ToastWithHeight)[],
23): {
24 frontmostHeight: number;
25 items: VisibleToastLayoutItem<TToast & ToastWithHeight>[];
26} {
27 let offsetY = 0;
28​
29 return {
30 frontmostHeight: normalizeToastHeight(visibleToasts[0]?.height),
31 items: visibleToasts.map((toast, visibleIndex) => {
32 const item = {
33 toast,
34 visibleIndex,
35 offsetY,
36 };
37​
38 offsetY += normalizeToastHeight(toast.height);
39 return item;
40 }),
41 };
43​
44function normalizeToastHeight(height: number | null | undefined): number {
45 return typeof height === "number" && Number.isFinite(height) && height > 0 ? height : 0;