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

PullRequestCommentBody.tsx

59 lines1.8 KB

1import { useEffect, 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 content = useRef<HTMLDivElement>(null);
15 const id = useId();
16​
17 useEffect(() => {
18 const element = content.current;
19 if (!element) return;
20 const measure = () => setOverflowing(element.getBoundingClientRect().height > 240);
21 measure();
22 return observeResize(element, measure);
23 }, []);
24​
25 return (
26 <div className={cn("min-w-0", className)}>
27 <div
28 id={id}
29 className={cn("relative", !expanded && "max-h-[240px] overflow-hidden")}
30 onFocusCapture={() => setExpanded(true)}
31 >
32 <div ref={content}>
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) content.current?.parentElement?.scrollIntoView({ block: "nearest" });
51 setExpanded(!expanded);
52 }}
53 >
54 {expanded ? "Show less" : "Show full comment"}
55 </Button>
56 ) : null}
57 </div>
58 );