Open History to see the last commit
1import type { ToastManagerAddOptions } from "@base-ui/react/toast";
2import type { ComponentPropsWithoutRef, ReactNode } from "react";
3
4import type { ThreadToastData } from "./toast";
5
6export type StackedThreadToastOptions = {
7 type: "error" | "warning" | "success" | "info" | "loading";
8 title: ReactNode;
9 description?: ReactNode;
10 timeout?: number;
11 priority?: "low" | "high";
12 actionProps?: ComponentPropsWithoutRef<"button">;
13 actionVariant?: ThreadToastData["actionVariant"];
14 data?: Omit<ThreadToastData, "actionLayout">;
15};
16
17/**
18 * Defined `actionProps` that hide a previous toast CTA on `toastManager.update`.
19 * Passing `actionProps: undefined` is a no-op because updates omit undefined keys.
20 */
21export const hiddenToastActionProps = {
22 children: null,
23} as const satisfies Pick<ComponentPropsWithoutRef<"button">, "children">;
24
25export function stackedThreadToast(
26 options: StackedThreadToastOptions,
27): ToastManagerAddOptions<ThreadToastData> {
28 const { type, title, description, timeout, priority, actionProps, actionVariant, data } = options;
29
30 const mergedData: ThreadToastData = {
31 ...(data !== undefined ? data : {}),
32 actionLayout: "stacked-end",
33 };
34 if (actionVariant !== undefined) {
35 mergedData.actionVariant = actionVariant;
36 }
37
38 const payload: ToastManagerAddOptions<ThreadToastData> = {
39 type,
40 title,
41 data: mergedData,
42 };
43
44 if (description !== undefined) {
45 payload.description = description;
46 }
47 if (timeout !== undefined) {
48 payload.timeout = timeout;
49 }
50 if (priority !== undefined) {
51 payload.priority = priority;
52 }
53 if (actionProps !== undefined) {
54 payload.actionProps = actionProps;
55 }
56
57 return payload;
58}