Add copy button to error toasts

- Let users copy error toast descriptions
- Keep toast text selectable for manual copy
This commit is contained in:
Julius Marminge committed 2026-03-25 12:33:01 -07:00
1 parent ff8d87ddee
commit 4ac3f6754b
1 file changed
+42 -10
+42 -10
View File
@@ -5,8 +5,10 @@ import { useEffect, type CSSProperties } from "react";
import { useParams } from "@tanstack/react-router";
import { ThreadId } from "@t3tools/contracts";
import {
CheckIcon,
CircleAlertIcon,
CircleCheckIcon,
CopyIcon,
InfoIcon,
LoaderCircleIcon,
TriangleAlertIcon,
@@ -14,6 +16,7 @@ import {
import { cn } from "~/lib/utils";
import { buttonVariants } from "~/components/ui/button";
import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
import { buildVisibleToastLayout, shouldHideCollapsedToastContent } from "./toast.logic";
type ThreadToastData = {
@@ -35,6 +38,25 @@ const TOAST_ICONS = {
warning: TriangleAlertIcon,
} as const;
function CopyErrorButton({ text }: { text: string }) {
const { copyToClipboard, isCopied } = useCopyToClipboard();
return (
<button
className="shrink-0 cursor-pointer rounded-md p-1 text-muted-foreground opacity-60 transition-opacity hover:opacity-100"
onClick={() => copyToClipboard(text)}
title="Copy error"
type="button"
>
{isCopied ? (
<CheckIcon className="size-3.5 text-success" />
) : (
<CopyIcon className="size-3.5" />
)}
</button>
);
}
type ToastPosition =
| "top-left"
| "top-center"
@@ -284,12 +306,17 @@ function Toasts({ position = "top-right" }: { position: ToastPosition }) {
)}
<div className="flex min-w-0 flex-1 flex-col gap-0.5">
<Toast.Title
className="min-w-0 break-words font-medium"
data-slot="toast-title"
/>
<div className="flex items-center justify-between gap-1">
<Toast.Title
className="min-w-0 break-words font-medium"
data-slot="toast-title"
/>
{toast.type === "error" && toast.description && (
<CopyErrorButton text={toast.description} />
)}
</div>
<Toast.Description
className="min-w-0 break-words text-muted-foreground"
className="min-w-0 select-text break-words text-muted-foreground"
data-slot="toast-description"
/>
</div>
@@ -373,12 +400,17 @@ function AnchoredToasts() {
)}
<div className="flex min-w-0 flex-1 flex-col gap-0.5">
<Toast.Title
className="min-w-0 break-words font-medium"
data-slot="toast-title"
/>
<div className="flex items-center gap-1">
<Toast.Title
className="min-w-0 break-words font-medium"
data-slot="toast-title"
/>
{toast.type === "error" && toast.description && (
<CopyErrorButton text={toast.description} />
)}
</div>
<Toast.Description
className="min-w-0 break-words text-muted-foreground"
className="min-w-0 select-text break-words text-muted-foreground"
data-slot="toast-description"
/>
</div>