1- import React , { useRef , useEffect } from 'react' ;
1+ import React , { useRef , useEffect , useState } from 'react' ;
22import PropTypes from 'prop-types' ;
33import styled from 'styled-components' ;
44import { getConfig } from '../../../utils/getConfig' ;
55import { registerFrame } from '../../../utils/dispatcher' ;
66
77const Frame = styled . iframe `
8- min-height: 100%;
9- min-width: 100%;
8+ display: block;
109 position: ${ ( props ) => ( props . fullView ? 'relative' : 'absolute' ) } ;
1110 border-width: 0;
1211` ;
1312
1413function PreviewFrame ( { fullView, isOverlayVisible } ) {
1514 const iframe = useRef ( ) ;
15+ const [ frameSize , setFrameSize ] = useState ( null ) ;
1616 const previewUrl = getConfig ( 'PREVIEW_URL' ) ;
1717 useEffect ( ( ) => {
1818 const unsubscribe = registerFrame ( iframe . current . contentWindow , previewUrl ) ;
@@ -21,6 +21,26 @@ function PreviewFrame({ fullView, isOverlayVisible }) {
2121 } ;
2222 } ) ;
2323
24+ useEffect ( ( ) => {
25+ const parent = iframe . current ?. parentElement ;
26+ if ( ! parent ) {
27+ return ( ) => { } ;
28+ }
29+ const updateSize = ( ) => {
30+ const { width, height } = parent . getBoundingClientRect ( ) ;
31+ setFrameSize ( {
32+ width : Math . floor ( width ) ,
33+ height : Math . floor ( height )
34+ } ) ;
35+ } ;
36+ updateSize ( ) ;
37+ const observer = new ResizeObserver ( updateSize ) ;
38+ observer . observe ( parent ) ;
39+ return ( ) => {
40+ observer . disconnect ( ) ;
41+ } ;
42+ } , [ ] ) ;
43+
2444 const frameUrl = previewUrl ;
2545 const sandboxAttributes = `allow-forms allow-modals allow-pointer-lock allow-popups
2646 allow-same-origin allow-scripts allow-top-navigation-by-user-activation allow-downloads` ;
@@ -45,6 +65,11 @@ function PreviewFrame({ fullView, isOverlayVisible }) {
4565 frameBorder = "0"
4666 ref = { iframe }
4767 fullView = { fullView }
68+ style = {
69+ frameSize
70+ ? { width : frameSize . width , height : frameSize . height }
71+ : undefined
72+ }
4873 />
4974 </ >
5075 ) ;
0 commit comments