|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { MutableRefObject, ReactElement, useEffect, useId, useRef, useState } from "react" |
| 4 | +import { MermaidConfig } from "mermaid" |
| 5 | + |
| 6 | +function useIsVisible(ref: MutableRefObject<HTMLElement>) { |
| 7 | + const [isIntersecting, setIsIntersecting] = useState(false) |
| 8 | + |
| 9 | + useEffect(() => { |
| 10 | + const observer = new IntersectionObserver(([entry]) => { |
| 11 | + if (entry.isIntersecting) { |
| 12 | + // disconnect after once visible to avoid re-rendering of chart when `isIntersecting` will |
| 13 | + // be changed to true/false |
| 14 | + observer.disconnect() |
| 15 | + setIsIntersecting(true) |
| 16 | + } |
| 17 | + }) |
| 18 | + |
| 19 | + observer.observe(ref.current) |
| 20 | + return () => { |
| 21 | + observer.disconnect() |
| 22 | + } |
| 23 | + }, [ref]) |
| 24 | + |
| 25 | + return isIntersecting |
| 26 | +} |
| 27 | + |
| 28 | +export function Mermaid({ chart }: { chart: string }): ReactElement { |
| 29 | + const id = useId() |
| 30 | + const [svg, setSvg] = useState("") |
| 31 | + const containerRef = useRef<HTMLDivElement>(null!) |
| 32 | + const isVisible = useIsVisible(containerRef) |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + // Fix when inside element with `display: hidden` https://github.com/shuding/nextra/issues/3291 |
| 36 | + if (!isVisible) { |
| 37 | + return |
| 38 | + } |
| 39 | + const htmlElement = document.documentElement |
| 40 | + const observer = new MutationObserver(renderChart) |
| 41 | + observer.observe(htmlElement, { attributes: true }) |
| 42 | + renderChart() |
| 43 | + |
| 44 | + return () => { |
| 45 | + observer.disconnect() |
| 46 | + } |
| 47 | + |
| 48 | + // Switching themes taken from https://github.com/mermaid-js/mermaid/blob/1b40f552b20df4ab99a986dd58c9d254b3bfd7bc/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue#L53 |
| 49 | + async function renderChart() { |
| 50 | + const isDarkTheme = |
| 51 | + htmlElement.classList.contains("dark") || |
| 52 | + htmlElement.attributes.getNamedItem("data-theme")?.value === "dark" |
| 53 | + |
| 54 | + const mermaidConfig: MermaidConfig = { |
| 55 | + securityLevel: "loose", |
| 56 | + fontFamily: "inherit", |
| 57 | + themeCSS: "margin: 1.5rem auto 0;", |
| 58 | + theme: isDarkTheme ? "dark" : "default", |
| 59 | + themeVariables: { |
| 60 | + background: isDarkTheme ? "#97eae5" : "#003366", |
| 61 | + primaryColor: isDarkTheme ? "#97eae5" : "#003366", |
| 62 | + git0: "#97eae5", |
| 63 | + git1: "#DC606B", |
| 64 | + git2: "#DC9B14" |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + const { default: mermaid } = await import("mermaid") |
| 69 | + |
| 70 | + try { |
| 71 | + mermaid.initialize(mermaidConfig) |
| 72 | + const { svg } = await mermaid.render( |
| 73 | + // strip invalid characters for `id` attribute |
| 74 | + id.replaceAll(":", ""), |
| 75 | + chart.replaceAll("\\n", "\n"), |
| 76 | + containerRef.current |
| 77 | + ) |
| 78 | + setSvg(svg) |
| 79 | + } catch (error) { |
| 80 | + console.error("Error while rendering mermaid", error) |
| 81 | + } |
| 82 | + } |
| 83 | + }, [chart, isVisible]) |
| 84 | + |
| 85 | + return <div ref={containerRef} dangerouslySetInnerHTML={{ __html: svg }} /> |
| 86 | +} |
0 commit comments