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

MarkdownDetails.tsx

107 lines4.0 KB

1import { useGT } from "gt-react";
2import { ChevronRightIcon } from "lucide-react";
3import { flushSync } from "react-dom";
4import {
5 Children,
6 isValidElement,
7 use,
8 useState,
9 type ComponentProps,
10 type ReactNode,
11} from "react";
12​
13import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
14import { findRevealRef, MarkdownFindContext } from "./markdownFindContext";
15​
16const focusOnMount = (element: HTMLElement | null) => {
17 if (element && element.ownerDocument.activeElement === element.ownerDocument.body)
18 element.focus();
19};
20​
21export function MarkdownDetails({
22 children,
23 open = false,
24 dir,
25}: Pick<ComponentProps<"details">, "children" | "open" | "dir">) {
26 const gt = useGT();
27 const [isOpen, setIsOpen] = useState(open);
28 const [armed, setArmed] = useState<"hover" | "focus" | null>(null);
29 const searching = use(MarkdownFindContext);
30 const expanded = isOpen;
31 const revealForFind = () => setIsOpen(true);
32 // Base UI only listens for `beforematch` on a panel mounted at its first
33 // render; a closed panel mounts later, when find starts, so listen here.
34 const revealRef = findRevealRef(revealForFind);
35 const childNodes = Children.toArray(children);
36 const summaryIndex = childNodes.findIndex(
37 (child) => isValidElement(child) && child.type === "summary",
38 );
39 const summaryNode = summaryIndex >= 0 ? childNodes[summaryIndex] : null;
40 const summary =
41 isValidElement<{ children?: ReactNode }>(summaryNode) && summaryNode.props.children
42 ? summaryNode.props.children
43 : gt("Details", { $context: "fallback title of a collapsible section" });
44 const content = childNodes.filter((_, index) => index !== summaryIndex);
45​
46 // Most details stay closed: mount Base UI on hover or focus, before the first click.
47 if (!armed && !expanded && !searching) {
48 return (
49 <div className="my-2 border-y border-border/60" dir={dir}>
50 <div data-slot="collapsible" data-markdown-details="" data-markdown-details-open="false">
51 <button
52 type="button"
53 aria-expanded="false"
54 className="flex w-full cursor-pointer items-center gap-2 py-2 text-start text-sm font-medium text-foreground [&:dir(rtl)_svg]:rotate-180"
55 data-slot="collapsible-trigger"
56 data-markdown-details-summary=""
57 // Synchronously, so the pointer's next events reach Base UI's trigger.
58 onPointerEnter={() => flushSync(() => setArmed("hover"))}
59 onFocus={() => flushSync(() => setArmed("focus"))}
60 onClick={() => {
61 setArmed("hover");
62 setIsOpen(true);
63 }}
64 >
65 <ChevronRightIcon
66 className="size-4 shrink-0 text-muted-foreground transition-transform"
67 aria-hidden
68 />
69 <span>{summary}</span>
70 </button>
71 </div>
72 </div>
73 );
74 }
75​
76 return (
77 <div className="my-2 border-y border-border/60" dir={dir}>
78 <Collapsible
79 open={expanded}
80 onOpenChange={setIsOpen}
81 data-markdown-details=""
82 data-markdown-details-open={expanded ? "true" : "false"}
83 >
84 <CollapsibleTrigger
85 className="flex w-full items-center gap-2 py-2 text-start text-sm font-medium text-foreground [&:dir(rtl):not([data-panel-open])_svg]:rotate-180 data-panel-open:[&_svg]:rotate-90"
86 data-markdown-details-summary=""
87 // Keyboard focus armed it, and the swap replaced the focused button.
88 ref={armed === "focus" ? focusOnMount : undefined}
89 >
90 <ChevronRightIcon
91 className="size-4 shrink-0 text-muted-foreground transition-transform"
92 aria-hidden
93 />
94 <span>{summary}</span>
95 </CollapsibleTrigger>
96 <CollapsiblePanel ref={revealRef} hiddenUntilFound={searching}>
97 <div
98 className="pb-3 ps-6 text-foreground/[calc(90%+var(--appearance-contrast-boost)/10)]"
99 data-markdown-details-content=""
100 >
101 {content}
102 </div>
103 </CollapsiblePanel>
104 </Collapsible>
105 </div>
106 );