Open History to see the last commit
1import { useId, useState } from "react";
2
3import { cn } from "../../lib/utils";
4
5/**
6 * Long error text clamped to a few lines with a toggle to reveal the rest.
7 * Short single-line text renders as-is without the toggle.
8 */
9export function ExpandableText({
10 text,
11 className,
12 collapsedClassName = "line-clamp-3",
13 expandLabel = "Show full error",
14}: {
15 text: string;
16 className?: string;
17 collapsedClassName?: string;
18 expandLabel?: string;
19}) {
20 const textId = useId();
21 const [expanded, setExpanded] = useState(false);
22 const canExpand = text.length > 180 || text.includes("\n");
23
24 return (
25 <div className={cn("min-w-0", className)}>
26 <div
27 id={textId}
28 className={cn(
29 "whitespace-pre-wrap break-words",
30 !expanded && canExpand ? collapsedClassName : null,
31 )}
32 >
33 {text}
34 </div>
35 {canExpand ? (
36 <button
37 type="button"
38 aria-expanded={expanded}
39 aria-controls={textId}
40 className="cursor-pointer mt-1 text-[11px] font-medium text-foreground/70 underline-offset-2 hover:text-foreground hover:underline"
41 onClick={() => setExpanded((value) => !value)}
42 >
43 {expanded ? "Show less" : expandLabel}
44 </button>
45 ) : null}
46 </div>
47 );
48}