Open History to see the last commit
1import { useGT } from "gt-react";
2
3import { InlineButton } from "../ui/button";
4import { useId, useState } from "react";
5
6import { cn } from "../../lib/utils";
7
8export function ExpandableText({
9 text,
10 collapsedText = text,
11 className,
12 collapsedClassName = "line-clamp-3",
13 expandLabel,
14}: {
15 text: string;
16 collapsedText?: string;
17 className?: string;
18 collapsedClassName?: string;
19 expandLabel?: string;
20}) {
21 const gt = useGT();
22 const textId = useId();
23 const [expanded, setExpanded] = useState(false);
24 const canExpand = collapsedText !== text || text.length > 180 || text.includes("\n");
25
26 return (
27 <div className={cn("min-w-0", className)}>
28 <div
29 id={textId}
30 className={cn(
31 "whitespace-pre-wrap break-words",
32 !expanded && canExpand ? collapsedClassName : null,
33 )}
34 >
35 {expanded ? text : collapsedText}
36 </div>
37 {canExpand ? (
38 <InlineButton
39 aria-expanded={expanded}
40 aria-controls={textId}
41 tone="muted"
42 className="mt-1"
43 onClick={() => setExpanded((value) => !value)}
44 >
45 {expanded ? gt("Show less") : (expandLabel ?? gt("Show full error"))}
46 </InlineButton>
47 ) : null}
48 </div>
49 );
50}