Skip to content

Commit 62d8433

Browse files
GiteaBotsilverwind
andauthored
Fix mermaid diagram height when initially hidden (#32457) (#32464)
Backport #32457 by @silverwind In a hidden iframe, `document.body.clientHeight` is not reliable. Use `IntersectionObserver` to detect the visibility change and update the height there. Fixes: #32392 <img width="885" alt="image" src="https://github.com/user-attachments/assets/a95ef6aa-27e7-443f-9d06-400ef27919ae"> Co-authored-by: silverwind <[email protected]>
1 parent 22a93c1 commit 62d8433

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

web_src/js/markup/mermaid.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,21 @@ export async function renderMermaid() {
5656
btn.setAttribute('data-clipboard-text', source);
5757
mermaidBlock.append(btn);
5858

59+
const updateIframeHeight = () => {
60+
iframe.style.height = `${iframe.contentWindow.document.body.clientHeight}px`;
61+
};
62+
63+
// update height when element's visibility state changes, for example when the diagram is inside
64+
// a <details> + <summary> block and the <details> block becomes visible upon user interaction, it
65+
// would initially set a incorrect height and the correct height is set during this callback.
66+
(new IntersectionObserver(() => {
67+
updateIframeHeight();
68+
}, {root: document.documentElement})).observe(iframe);
69+
5970
iframe.addEventListener('load', () => {
6071
pre.replaceWith(mermaidBlock);
6172
mermaidBlock.classList.remove('tw-hidden');
62-
iframe.style.height = `${iframe.contentWindow.document.body.clientHeight}px`;
73+
updateIframeHeight();
6374
setTimeout(() => { // avoid flash of iframe background
6475
mermaidBlock.classList.remove('is-loading');
6576
iframe.classList.remove('tw-invisible');

0 commit comments

Comments
 (0)