Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

fix: reduce layout shift in mermaid diagrams (IntersectionObserver + min-height)

PyZhang20b78bf4de26
diff --git a/src/components/mermaid-diagram.tsx b/src/components/mermaid-diagram.tsx
index 1faacdbb..7ee867a6 100644
--- a/src/components/mermaid-diagram.tsx
+++ b/src/components/mermaid-diagram.tsx
@@ -8,8 +8,29 @@ export function MermaidDiagram({ code }: MermaidDiagramProps) {
const containerRef = useRef<HTMLDivElement>(null)
const [error, setError] = useState<string | null>(null)
const [svg, setSvg] = useState<string | null>(null)
+ const [visible, setVisible] = useState(false)
+ // Only render when the diagram scrolls into view
useEffect(() => {
+ const el = containerRef.current
+ if (!el) return
+
+ const observer = new IntersectionObserver(
+ ([entry]) => {
+ if (entry.isIntersecting) {
+ setVisible(true)
+ observer.disconnect()
+ }
+ },
+ { rootMargin: "200px" },
+ )
+ observer.observe(el)
+ return () => observer.disconnect()
+ }, [code])
+
+ // Render mermaid SVG once visible
+ useEffect(() => {
+ if (!visible) return
let cancelled = false
async function render() {
@@ -37,7 +58,10 @@ export function MermaidDiagram({ code }: MermaidDiagramProps) {
render()
return () => { cancelled = true }
- }, [code])
+ }, [visible, code])
+
+ // Prevent layout shift: compute a stable min-height from code line count
+ const estimatedHeight = Math.max(80, code.split("\n").length * 20)
if (error) {
return (
@@ -48,19 +72,23 @@ export function MermaidDiagram({ code }: MermaidDiagramProps) {
)
}
- if (!svg) {
- return (
- <div className="my-2 flex items-center justify-center rounded border border-border/40 bg-muted/30 p-4">
- <span className="text-xs text-muted-foreground animate-pulse">Rendering diagram...</span>
- </div>
- )
- }
-
return (
<div
ref={containerRef}
- className="my-2 overflow-x-auto rounded border border-border/40 bg-muted/20 p-3 [&>svg]:mx-auto [&>svg]:max-w-full [&>svg]:h-auto"
- dangerouslySetInnerHTML={{ __html: svg }}
- />
+ className="my-2 overflow-x-auto rounded border border-border/40 bg-muted/20 [&>svg]:mx-auto [&>svg]:max-w-full [&>svg]:h-auto"
+ style={{ minHeight: svg ? undefined : estimatedHeight }}
+ >
+ {svg ? (
+ <div className="p-3" dangerouslySetInnerHTML={{ __html: svg }} />
+ ) : visible ? (
+ <div className="flex items-center justify-center h-full p-4">
+ <span className="text-xs text-muted-foreground animate-pulse">Rendering diagram...</span>
+ </div>
+ ) : (
+ <div className="flex items-center justify-center h-full p-4">
+ <span className="text-xs text-muted-foreground/50">Diagram</span>
+ </div>
+ )}
+ </div>
)
}