|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { type ReactNode, useRef, useLayoutEffect, useState } from "react" |
| 4 | + |
| 5 | +function slugify(text: string): string { |
| 6 | + return String(text) |
| 7 | + .toLowerCase() |
| 8 | + .replace(/[^a-z0-9]+/g, "-") |
| 9 | + .replace(/(^-|-$)/g, "") |
| 10 | +} |
| 11 | + |
| 12 | +function FaqH1({ id, children }: { id?: string; children?: ReactNode }) { |
| 13 | + const slug = id ?? slugify(String(children)) |
| 14 | + return ( |
| 15 | + <h2 |
| 16 | + id={slug} |
| 17 | + className="typography-h2 mb-4 mt-8 scroll-mt-24 text-neu-900 first:mt-0" |
| 18 | + > |
| 19 | + <a href={`#${slug}`} className="hover:underline"> |
| 20 | + {children} |
| 21 | + </a> |
| 22 | + </h2> |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +function FaqH2({ id, children }: { id?: string; children?: ReactNode }) { |
| 27 | + const slug = id ?? slugify(String(children)) |
| 28 | + return ( |
| 29 | + <details className="group border-b border-neu-100 dark:border-neu-200 [&:first-of-type]:border-t"> |
| 30 | + <summary className="flex cursor-pointer list-none items-center justify-between gap-4 py-4 [&::-webkit-details-marker]:hidden"> |
| 31 | + <h3 id={slug} className="typography-body-lg text-neu-900"> |
| 32 | + {children} |
| 33 | + </h3> |
| 34 | + <ChevronIcon className="size-5 shrink-0 text-neu-600 transition-transform group-open:rotate-180" /> |
| 35 | + </summary> |
| 36 | + </details> |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +function ChevronIcon({ className }: { className?: string }) { |
| 41 | + return ( |
| 42 | + <svg |
| 43 | + xmlns="http://www.w3.org/2000/svg" |
| 44 | + viewBox="0 0 24 24" |
| 45 | + fill="none" |
| 46 | + stroke="currentColor" |
| 47 | + strokeWidth={2} |
| 48 | + strokeLinecap="round" |
| 49 | + strokeLinejoin="round" |
| 50 | + className={className} |
| 51 | + > |
| 52 | + <polyline points="6 9 12 15 18 9" /> |
| 53 | + </svg> |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +export function FaqAggregator({ children }: { children: ReactNode }) { |
| 58 | + const containerRef = useRef<HTMLDivElement>(null) |
| 59 | + const [, forceUpdate] = useState(0) |
| 60 | + |
| 61 | + useLayoutEffect(() => { |
| 62 | + const container = containerRef.current |
| 63 | + if (!container) return |
| 64 | + |
| 65 | + const details = container.querySelectorAll("details") |
| 66 | + details.forEach(detail => { |
| 67 | + const answerContent: Node[] = [] |
| 68 | + let sibling = detail.nextSibling |
| 69 | + |
| 70 | + while (sibling) { |
| 71 | + const next = sibling.nextSibling |
| 72 | + if (sibling instanceof Element) { |
| 73 | + if (sibling.tagName === "DETAILS" || sibling.tagName === "H2") break |
| 74 | + answerContent.push(sibling) |
| 75 | + } else if ( |
| 76 | + sibling.nodeType === Node.TEXT_NODE && |
| 77 | + sibling.textContent?.trim() |
| 78 | + ) { |
| 79 | + answerContent.push(sibling) |
| 80 | + } |
| 81 | + sibling = next |
| 82 | + } |
| 83 | + |
| 84 | + if (answerContent.length > 0) { |
| 85 | + const wrapper = document.createElement("div") |
| 86 | + wrapper.className = "pb-4" |
| 87 | + answerContent.forEach(node => wrapper.appendChild(node)) |
| 88 | + detail.appendChild(wrapper) |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + forceUpdate(n => n + 1) |
| 93 | + }, []) |
| 94 | + |
| 95 | + return <div ref={containerRef}>{children}</div> |
| 96 | +} |
| 97 | + |
| 98 | +export const faqMdxComponents = { |
| 99 | + h1: FaqH1, |
| 100 | + h2: FaqH2, |
| 101 | +} |
0 commit comments