@@ -57,16 +57,12 @@ export async function renderMermaid() {
57
57
mermaidBlock . append ( btn ) ;
58
58
59
59
const updateIframeHeight = ( ) => {
60
- iframe . style . height = `${ iframe . contentWindow . document . body . clientHeight } px` ;
60
+ const body = iframe . contentWindow ?. document ?. body ;
61
+ if ( body ) {
62
+ iframe . style . height = `${ body . clientHeight } px` ;
63
+ }
61
64
} ;
62
65
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
-
70
66
iframe . addEventListener ( 'load' , ( ) => {
71
67
pre . replaceWith ( mermaidBlock ) ;
72
68
mermaidBlock . classList . remove ( 'tw-hidden' ) ;
@@ -75,6 +71,13 @@ export async function renderMermaid() {
75
71
mermaidBlock . classList . remove ( 'is-loading' ) ;
76
72
iframe . classList . remove ( 'tw-invisible' ) ;
77
73
} , 0 ) ;
74
+
75
+ // update height when element's visibility state changes, for example when the diagram is inside
76
+ // a <details> + <summary> block and the <details> block becomes visible upon user interaction, it
77
+ // would initially set a incorrect height and the correct height is set during this callback.
78
+ ( new IntersectionObserver ( ( ) => {
79
+ updateIframeHeight ( ) ;
80
+ } , { root : document . documentElement } ) ) . observe ( iframe ) ;
78
81
} ) ;
79
82
80
83
document . body . append ( mermaidBlock ) ;
0 commit comments