Fix broken diagrams in hidden containers (Reveal.js, tabs)#227
Open
drillan wants to merge 2 commits intomgaitan:masterfrom
Open
Fix broken diagrams in hidden containers (Reveal.js, tabs)#227drillan wants to merge 2 commits intomgaitan:masterfrom
drillan wants to merge 2 commits intomgaitan:masterfrom
Conversation
Mermaid's layout engine measures text dimensions via the DOM, which fails for elements hidden by a parent (e.g., Reveal.js non-active slides, sphinx-design unopened tabs). The layout calculation produces broken SVGs with near-zero viewBox dimensions. Use IntersectionObserver to defer rendering of hidden elements until they become visible. Visible elements render immediately as before. Fixes mgaitan#126 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Replace forEach+async with for...of and promise chain to serialize concurrent mermaid.run() calls from IntersectionObserver - Move data-mermaid-deferred removal to after successful render to prevent orphaned elements causing infinite polling - Add data-mermaid-render-failed attribute for failed elements and exclude them from completion checks - Add try/catch for visible element rendering - Warn explicitly when IntersectionObserver is unavailable - Add test for lazy rendering code presence Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Feb 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IntersectionObserverto defer rendering of hidden Mermaid elements until they become visibledisplay: nonecontainers (e.g., Reveal.js non-active slides, sphinx-design unopened tabs)Problem
When
mermaid_output_format = "raw",mermaid.run()processes all.mermaidelements at page load. For elements hidden by a parent (display: none), the browser returns zero for all dimension measurements. This causes Mermaid's layout engine (dagre/ELK) to produce broken SVGs with near-zeroviewBoxdimensions, or throw"Could not find a suitable point for the distance"errors that manifest as "Syntax error in text".Affected contexts:
Approach
In the
runMermaid()function withindefault.js.j2:.mermaidelements into visible (offsetParent !== null || getClientRects().length > 0) and hiddenmermaid.run({ nodes: visible })for visible elements onlyIntersectionObserverfor hidden elements, rendering each when it becomes visiblemermaid.run()calls when multiple hidden elements become visible simultaneouslyTest plan
test_lazy_rendering_code_presentverifies IntersectionObserver and deferred rendering code is emittedFixes #126
🤖 Generated with Claude Code