@@ -10,6 +10,7 @@ import { resolveDynamicBinding } from './dynamic';
10
10
export function ElementWebframe ( props : ContentKitClientElementProps < ContentKitWebFrame > ) {
11
11
const { element } = props ;
12
12
13
+ const [ mounted , setMounted ] = React . useState ( false ) ;
13
14
const renderer = useContentKitClientContext ( ) ;
14
15
const iframeRef = React . useRef < HTMLIFrameElement > ( null ) ;
15
16
const [ size , setSize ] = React . useState < {
@@ -23,10 +24,6 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
23
24
24
25
const sendMessage = React . useCallback (
25
26
( message : object ) => {
26
- if ( ! iframeRef . current ) {
27
- return ;
28
- }
29
-
30
27
const target = new URL ( element . source . url ) ;
31
28
32
29
// For security reasons, only iframe from our integrations domains are allowed
@@ -36,6 +33,10 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
36
33
}
37
34
38
35
if ( readyRef . current ) {
36
+ if ( ! iframeRef . current ) {
37
+ return ;
38
+ }
39
+
39
40
iframeRef . current . contentWindow ! . postMessage (
40
41
message ,
41
42
`${ target . protocol } //${ target . host } ` ,
@@ -61,7 +62,7 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
61
62
62
63
// For security reasons, only iframe from our integrations domains are allowed
63
64
// to send and receive messages
64
- if ( ! renderer . security . firstPartyDomains . includes ( origin . host ) && 0 ) {
65
+ if ( ! renderer . security . firstPartyDomains . includes ( origin . host ) ) {
65
66
return ;
66
67
}
67
68
@@ -117,6 +118,11 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
117
118
} ;
118
119
119
120
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
+
120
126
return ( ) => {
121
127
window . removeEventListener ( 'message' , callback ) ;
122
128
} ;
@@ -142,26 +148,29 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
142
148
< div
143
149
className = { `contentkit-webframe` }
144
150
style = { {
145
- aspectRatio : element . aspectRatio ,
146
- ...size ,
151
+ aspectRatio : size . aspectRatio || element . aspectRatio || undefined ,
152
+ maxWidth : size . maxWidth || undefined ,
153
+ maxHeight : size . maxHeight || undefined ,
147
154
} }
148
155
>
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 }
165
174
</ div >
166
175
) ;
167
176
}
0 commit comments