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

ExpandableText.tsx

45 lines1.1 KB

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