diff --git a/src/components/mermaid-renderer.tsx b/src/components/mermaid-renderer.tsx index fd82ce7..587f90c 100644 --- a/src/components/mermaid-renderer.tsx +++ b/src/components/mermaid-renderer.tsx @@ -1,4 +1,4 @@ -import { h } from "preact"; +import { Fragment, h } from "preact"; import { useEffect, useMemo, useState, useRef } from "preact/hooks"; import Panzoom, { PanzoomObject } from "@panzoom/panzoom"; @@ -39,6 +39,7 @@ const MermaidRenderer = ({ const _id = useMemo(() => id || `renderer-${randomIshId()}`, [id]); const containerRef = useRef(null); + const hiddenRef = useRef(null); const panzoomRef = useRef(null); const [rendered, setRendered] = useState(null); @@ -53,11 +54,14 @@ const MermaidRenderer = ({ } try { - const { svg } = await mermaid.render(_id, data); + const { svg } = await mermaid.render( + _id, + data, + hiddenRef.current || undefined, + ); setRendered(svg); setError(null); } catch (err) { - setRendered(null); if (err instanceof Error) { setError(err?.message); } else { @@ -119,31 +123,40 @@ const MermaidRenderer = ({ const resetZoom = _controlHandler((x) => x.reset({ animate: false })); return ( -
-
-
" }} - /> + + {/* Needed for mermaid to render into without messing up the layout. */} +
+
- {enableZoom && showZoomControls && ( -
-
- Reset +
+
+
" }} + /> +
+ {enableZoom && showZoomControls && ( +
+
+ Reset +
+
+ - + + +
-
- - - + + )} + {error && ( +
+

Something went wrong.

+ {!!rendered &&

Showing last valid result.

} +
+
{error}
-
- )} - {error && ( -
- {error} -
- )} -
+ )} +
+ ); };