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

PullRequestCommentBody.tsx

59 lines1.8 KB

1import { useId, useRef, useState, type ComponentProps } from "react";
2​
3import { cn } from "~/lib/utils";
4import { Button } from "../ui/button";
5import { PullRequestMarkdown } from "./PullRequestMarkdown";
6import { observeResize } from "~/lib/observeResize";
7​
8export function PullRequestCommentBody({
9 className,
10 ...props
11}: ComponentProps<typeof PullRequestMarkdown>) {
12 const [expanded, setExpanded] = useState(false);
13 const [overflowing, setOverflowing] = useState(false);
14 const clip = useRef<HTMLDivElement>(null);
15 const id = useId();
16​
17 return (
18 <div className={cn("min-w-0", className)}>
19 <div
20 ref={clip}
21 id={id}
22 className={cn("relative", !expanded && "max-h-[240px] overflow-hidden")}
23 onFocusCapture={() => setExpanded(true)}
24 >
25 <div
26 ref={(element) => {
27 if (element === null) return;
28 return observeResize(element, ([entry]) => {
29 if (entry) setOverflowing(entry.contentRect.height > 240);
30 });
31 }}
32 >
33 <PullRequestMarkdown {...props} />
34 </div>
35 {overflowing && !expanded ? (
36 <div
37 aria-hidden
38 className="pointer-events-none absolute inset-x-0 bottom-0 h-10 bg-gradient-to-t from-background to-transparent"
39 />
40 ) : null}
41 </div>
42 {overflowing ? (
43 <Button
44 size="xs"
45 variant="ghost-muted"
46 className="mt-2"
47 aria-expanded={expanded}
48 aria-controls={id}
49 onClick={() => {
50 if (expanded) clip.current?.scrollIntoView({ block: "nearest" });
51 setExpanded(!expanded);
52 }}
53 >
54 {expanded ? "Show less" : "Show full comment"}
55 </Button>
56 ) : null}
57 </div>
58 );