Open History to see the last commit
1import { ChevronRightIcon } from "lucide-react";
2import {
3 Children,
4 isValidElement,
5 use,
6 useState,
7 type ComponentProps,
8 type ReactNode,
9} from "react";
10
11import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
12import { findRevealRef, MarkdownFindContext } from "./markdownFindContext";
13
14export function MarkdownDetails({
15 children,
16 open = false,
17}: Pick<ComponentProps<"details">, "children" | "open">) {
18 const [isOpen, setIsOpen] = useState(open);
19 const searching = use(MarkdownFindContext);
20 const expanded = isOpen;
21 const revealForFind = () => setIsOpen(true);
22 // Base UI only listens for `beforematch` on a panel mounted at its first
23 // render; a closed panel mounts later, when find starts, so listen here.
24 const revealRef = findRevealRef(revealForFind);
25 const childNodes = Children.toArray(children);
26 const summaryIndex = childNodes.findIndex(
27 (child) => isValidElement(child) && child.type === "summary",
28 );
29 const summaryNode = summaryIndex >= 0 ? childNodes[summaryIndex] : null;
30 const summary =
31 isValidElement<{ children?: ReactNode }>(summaryNode) && summaryNode.props.children
32 ? summaryNode.props.children
33 : "Details";
34 const content = childNodes.filter((_, index) => index !== summaryIndex);
35
36 return (
37 <div className="my-2 border-y border-border/60">
38 <Collapsible
39 open={expanded}
40 onOpenChange={setIsOpen}
41 data-markdown-details=""
42 data-markdown-details-open={expanded ? "true" : "false"}
43 >
44 <CollapsibleTrigger
45 className="flex w-full items-center gap-2 py-2 text-left text-sm font-medium text-foreground data-panel-open:[&_svg]:rotate-90"
46 data-markdown-details-summary=""
47 >
48 <ChevronRightIcon
49 className="size-4 shrink-0 text-muted-foreground transition-transform"
50 aria-hidden
51 />
52 <span>{summary}</span>
53 </CollapsibleTrigger>
54 <CollapsiblePanel ref={revealRef} hiddenUntilFound={searching}>
55 <div
56 className="pb-3 ps-6 text-foreground/[calc(80%+var(--appearance-contrast-boost)/5)]"
57 data-markdown-details-content=""
58 >
59 {content}
60 </div>
61 </CollapsiblePanel>
62 </Collapsible>
63 </div>
64 );
65}