@@ -10,6 +10,7 @@ import { resolveDynamicBinding } from './dynamic';
1010export function ElementWebframe ( props : ContentKitClientElementProps < ContentKitWebFrame > ) {
1111 const { element } = props ;
1212
13+ const [ mounted , setMounted ] = React . useState ( false ) ;
1314 const renderer = useContentKitClientContext ( ) ;
1415 const iframeRef = React . useRef < HTMLIFrameElement > ( null ) ;
1516 const [ size , setSize ] = React . useState < {
@@ -23,10 +24,6 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
2324
2425 const sendMessage = React . useCallback (
2526 ( message : object ) => {
26- if ( ! iframeRef . current ) {
27- return ;
28- }
29-
3027 const target = new URL ( element . source . url ) ;
3128
3229 // For security reasons, only iframe from our integrations domains are allowed
@@ -36,6 +33,10 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
3633 }
3734
3835 if ( readyRef . current ) {
36+ if ( ! iframeRef . current ) {
37+ return ;
38+ }
39+
3940 iframeRef . current . contentWindow ! . postMessage (
4041 message ,
4142 `${ target . protocol } //${ target . host } ` ,
@@ -61,7 +62,7 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
6162
6263 // For security reasons, only iframe from our integrations domains are allowed
6364 // to send and receive messages
64- if ( ! renderer . security . firstPartyDomains . includes ( origin . host ) && 0 ) {
65+ if ( ! renderer . security . firstPartyDomains . includes ( origin . host ) ) {
6566 return ;
6667 }
6768
@@ -117,6 +118,11 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
117118 } ;
118119
119120 window . addEventListener ( 'message' , callback ) ;
121+
122+ // We only render the iframe once we have added the event listener
123+ // otherwise during SSR, we'll miss messages
124+ setMounted ( true ) ;
125+
120126 return ( ) => {
121127 window . removeEventListener ( 'message' , callback ) ;
122128 } ;
@@ -142,26 +148,29 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
142148 < div
143149 className = { `contentkit-webframe` }
144150 style = { {
145- aspectRatio : element . aspectRatio ,
146- ...size ,
151+ aspectRatio : size . aspectRatio || element . aspectRatio || undefined ,
152+ maxWidth : size . maxWidth || undefined ,
153+ maxHeight : size . maxHeight || undefined ,
147154 } }
148155 >
149- < iframe
150- ref = { iframeRef }
151- src = { element . source . url }
152- allowFullScreen
153- allow = "clipboard-write"
154- style = { {
155- position : 'absolute' ,
156- top : 0 ,
157- left : 0 ,
158- bottom : 0 ,
159- right : 0 ,
160- width : '100%' ,
161- height : '100%' ,
162- border : 'none' ,
163- } }
164- />
156+ { mounted ? (
157+ < iframe
158+ ref = { iframeRef }
159+ src = { element . source . url }
160+ allowFullScreen
161+ allow = "clipboard-write"
162+ style = { {
163+ position : 'absolute' ,
164+ top : 0 ,
165+ left : 0 ,
166+ bottom : 0 ,
167+ right : 0 ,
168+ width : '100%' ,
169+ height : '100%' ,
170+ border : 'none' ,
171+ } }
172+ />
173+ ) : null }
165174 </ div >
166175 ) ;
167176}
0 commit comments