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