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