Skip to content

fix(mermaid): defer rendering until web fonts are ready#2754

Open
puppylpg wants to merge 1 commit into
cotes2020:masterfrom
puppylpg:fix/mermaid-font-loading-clip
Open

fix(mermaid): defer rendering until web fonts are ready#2754
puppylpg wants to merge 1 commit into
cotes2020:masterfrom
puppylpg:fix/mermaid-font-loading-clip

Conversation

@puppylpg

Copy link
Copy Markdown

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Description

Mermaid measures label widths at initialization time. When loadMermaid() calls mermaid.initialize() (default startOnLoad: true), Mermaid renders immediately — before the page's web font has finished loading. Width is measured with the narrower fallback font; once the web font arrives the glyphs are wider and overflow the already-sized foreignObject box, clipping the text (default overflow: hidden).

Fix:

  • Set startOnLoad: false so mermaid.initialize() no longer auto-renders.
  • Defer mermaid.run() until document.fonts.ready, so label widths are always measured with the final font.
  • Guard against a slow or blocked font CDN with a 2.5 s timeout: if the timeout fires first, rendering proceeds anyway.

Only _javascript/modules/components/mermaid.js is changed. Lint (npm run lint:js) and build (npm run build) both pass locally.

Additional context

Fixes #2753

Mermaid measures label widths at initialization time. When web fonts are
still loading at that moment, the measurement uses a narrower fallback
font; once the web font arrives the text is wider and gets clipped by the
SVG foreignObject default overflow:hidden.

Fix by setting startOnLoad:false and deferring mermaid.run() until
document.fonts.ready, with a 2.5 s timeout so a slow or blocked font
source never stalls diagram rendering.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mermaid node labels clipped when web font loads after initial render

1 participant