Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ExpandableText.tsx

44 lines1.2 KB

1import { 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 [expanded, setExpanded] = useState(false);
21 const canExpand = text.length > 180 || text.includes("\n");
22​
23 return (
24 <div className={cn("min-w-0", className)}>
25 <div
26 className={cn(
27 "whitespace-pre-wrap break-words",
28 !expanded && canExpand ? collapsedClassName : null,
29 )}
30 >
31 {text}
32 </div>
33 {canExpand ? (
34 <button
35 type="button"
36 className="cursor-pointer mt-1 text-[11px] font-medium text-foreground/70 underline-offset-2 hover:text-foreground hover:underline"
37 onClick={() => setExpanded((value) => !value)}
38 >
39 {expanded ? "Show less" : expandLabel}
40 </button>
41 ) : null}
42 </div>
43 );