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