@@ -37,7 +37,7 @@ const DEFAULT_CONTENT = { ...DEFAULT }
3737export const usePreviewRenderer = (
3838 props , previewSize , plan , spacingSize ,
3939 selectedTab , selectedNum , selectedData ,
40- ref , shadowRoot , setIsLoading
40+ ref , hostRef , shadowRoot , isLoading , setIsLoading
4141) => {
4242 const {
4343 designId,
@@ -61,23 +61,33 @@ export const usePreviewRenderer = (
6161 const hasBackgroundTargetRef = useRef ( false )
6262 const initialRenderRef = useRef ( null )
6363 const shadowBodySizeRef = useRef ( null )
64- const prevEnableBackgroundRef = useRef ( false )
64+ const prevEnableBackgroundRef = useRef ( null )
6565 const prevSelectedTabRef = useRef ( selectedTab )
66+ const adjustAnimateFrameRef = useRef ( null )
6667
6768 const siteTitle = useSelect ( select => select ( 'core' ) . getEntityRecord ( 'root' , 'site' ) ?. title || 'InnovateCo' , [ ] )
6869 const isDesignLibraryDevMode = devMode && localStorage . getItem ( 'stk__design_library__dev_mode' ) === '1'
6970
7071 const addHasBackground = selectedTab === 'patterns'
7172
72- const adjustScale = ( ) => {
73- const shouldAdjust = ref . current && shadowRoot &&
73+ const adjustScale = ( force = true ) => {
74+ const shouldAdjust = ref . current && hostRef . current && shadowRoot &&
7475 ( ! selectedNum || // adjust if design is not selected
7576 prevSelectedTabRef . current !== selectedTab ) // adjust if selected tab changed even if design is selected
7677
7778 if ( shouldAdjust ) {
7879 const newPreviewSize = { ...previewSize }
7980 const newCardHeight = { ...cardHeight }
81+
8082 const cardRect = ref . current . getBoundingClientRect ( )
83+ const hostRect = hostRef . current . getBoundingClientRect ( )
84+
85+ const cardWidth = cardRect . width
86+ const hostWidth = hostRect . width
87+
88+ if ( ! force && cardWidth === hostWidth ) {
89+ return
90+ }
8191
8292 const shadowBody = shadowRoot . querySelector ( 'body' )
8393 if ( shadowBody ) {
@@ -119,6 +129,9 @@ export const usePreviewRenderer = (
119129
120130 setTimeout ( ( ) => setCardHeight ( newCardHeight ) , 500 )
121131 }
132+
133+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
134+ adjustAnimateFrameRef . current = requestAnimationFrame ( ( ) => adjustScale ( false ) )
122135 }
123136
124137 const renderPreview = ( blockContent = content ) => {
@@ -274,7 +287,8 @@ export const usePreviewRenderer = (
274287 useEffect ( ( ) => {
275288 if ( selectedNum === 0 && content && shadowRoot ) {
276289 renderPreview ( )
277- setTimeout ( adjustScale , 100 )
290+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
291+ adjustAnimateFrameRef . current = requestAnimationFrame ( adjustScale )
278292 }
279293 } , [ selectedNum ] )
280294
@@ -285,7 +299,8 @@ export const usePreviewRenderer = (
285299
286300 if ( prevEnableBackgroundRef . current !== enableBackground ) {
287301 prevEnableBackgroundRef . current = enableBackground
288- adjustScale ( )
302+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
303+ adjustAnimateFrameRef . current = requestAnimationFrame ( adjustScale )
289304 }
290305 } , [ blocks ] )
291306
@@ -294,12 +309,22 @@ export const usePreviewRenderer = (
294309 if ( ! content || ! blocks . parsed || ! blocks . serialized ) {
295310 return
296311 }
297- setTimeout ( ( ) => {
312+
313+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
314+ requestAnimationFrame ( ( ) => {
298315 adjustScale ( )
299316 prevSelectedTabRef . current = selectedTab
300- } , 100 )
317+ } )
301318 } , [ content ] )
302319
320+ // cleanup any pending animation on unmount
321+ useEffect ( ( ) => {
322+ return ( ) => {
323+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
324+ adjustAnimateFrameRef . current = null
325+ }
326+ } , [ ] )
327+
303328 const onClickDesign = ( ) => {
304329 if ( ! isPro && plan !== 'free' ) {
305330 return
@@ -319,6 +344,6 @@ export const usePreviewRenderer = (
319344 return {
320345 blocks : blocks . serialized , enableBackground,
321346 shadowBodySizeRef, blocksForSubstitutionRef,
322- adjustScale , onClickDesign,
347+ onClickDesign,
323348 }
324349}
0 commit comments