@@ -76,66 +76,73 @@ export const usePreviewRenderer = (
7676 ( ! selectedNum || // adjust if design is not selected
7777 prevSelectedTabRef . current !== selectedTab ) // adjust if selected tab changed even if design is selected
7878
79- if ( shouldAdjust ) {
80- const newPreviewSize = { ...previewSize }
81- const newCardHeight = { ...cardHeight }
79+ if ( ! shouldAdjust ) {
80+ return
81+ }
82+ const newPreviewSize = { ...previewSize }
83+ const newCardHeight = { ...cardHeight }
8284
83- const cardRect = ref . current . getBoundingClientRect ( )
84- const hostRect = hostRef . current . getBoundingClientRect ( )
85- const parentDivRect = parentDiv . getBoundingClientRect ( )
85+ const cardRect = ref . current . getBoundingClientRect ( )
86+ const hostRect = hostRef . current . getBoundingClientRect ( )
87+ const parentDivRect = parentDiv . getBoundingClientRect ( )
8688
87- const cardWidth = cardRect . width
88- const hostWidth = hostRect . width
89+ const cardWidth = cardRect . width
90+ const hostWidth = hostRect . width
8991
90- // Consider heights equal if the difference is less than 1px
91- const isEqualHeight = Math . abs ( parentDivRect . height - hostRect . height ) < 1
92+ // Consider heights equal if the difference is less than 1px
93+ const isEqualHeight = Math . abs ( parentDivRect . height - hostRect . height ) < 1
9294
93- if ( ! force && cardWidth === hostWidth && isEqualHeight ) {
94- return
95+ if ( ! force && cardWidth === hostWidth && isEqualHeight ) {
96+ if ( adjustAnimateFrameRef . current !== null ) {
97+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
9598 }
99+ adjustAnimateFrameRef . current = null
100+ return
101+ }
96102
97- const shadowBody = shadowRoot . querySelector ( 'body' )
98- if ( shadowBody ) {
99- const cardWidth = cardRect . width // Get width of the card
100- const scaleFactor = cardWidth > 0 ? cardWidth / 1300 : 1 // Divide by 1300, which is the width of preview in the shadow DOM
101- newPreviewSize . scale = scaleFactor
102-
103- let _bodyHeight = 1200
104- if ( selectedTab === 'patterns' ) {
105- _bodyHeight = shadowBody . offsetHeight
106- }
107-
108- const _height = parseFloat ( _bodyHeight ) * scaleFactor // Also adjust the height
103+ const shadowBody = shadowRoot . querySelector ( 'body' )
104+ if ( shadowBody ) {
105+ const cardWidth = cardRect . width // Get width of the card
106+ const scaleFactor = cardWidth > 0 ? cardWidth / 1300 : 1 // Divide by 1300, which is the width of preview in the shadow DOM
107+ newPreviewSize . scale = scaleFactor
109108
110- if ( Object . keys ( newPreviewSize ) . length === 1 ) {
111- newPreviewSize . heightBackground = _height
112- newPreviewSize . heightNoBackground = _height
113- } else {
114- const heightKey = enableBackground ? 'heightBackground' : 'heightNoBackground'
115- newPreviewSize [ heightKey ] = _height
116- }
109+ let _bodyHeight = 1200
110+ if ( selectedTab === 'patterns' ) {
111+ _bodyHeight = shadowBody . offsetHeight
112+ }
117113
118- setPreviewSize ( newPreviewSize )
114+ const _height = parseFloat ( _bodyHeight ) * scaleFactor // Also adjust the height
119115
120- shadowBodySizeRef . current = {
121- clientHeight : shadowBody . clientHeight ,
122- scrollHeight : shadowBody . scrollHeight ,
123- maxScrollTop : shadowBody . scrollHeight - shadowBody . clientHeight ,
124- }
116+ if ( Object . keys ( newPreviewSize ) . length === 1 ) {
117+ newPreviewSize . heightBackground = _height
118+ newPreviewSize . heightNoBackground = _height
119+ } else {
120+ const heightKey = enableBackground ? 'heightBackground' : 'heightNoBackground'
121+ newPreviewSize [ heightKey ] = _height
125122 }
126123
127- if ( ! Object . keys ( newCardHeight ) . length ) {
128- newCardHeight . background = cardRect . height
129- newCardHeight . noBackground = cardRect . height
130- } else {
131- const CardHeightKey = enableBackground ? 'background' : 'noBackground'
132- newCardHeight [ CardHeightKey ] = cardRect . height
124+ setPreviewSize ( newPreviewSize )
125+
126+ shadowBodySizeRef . current = {
127+ clientHeight : shadowBody . clientHeight ,
128+ scrollHeight : shadowBody . scrollHeight ,
129+ maxScrollTop : shadowBody . scrollHeight - shadowBody . clientHeight ,
133130 }
131+ }
134132
135- setTimeout ( ( ) => setCardHeight ( newCardHeight ) , 500 )
133+ if ( ! Object . keys ( newCardHeight ) . length ) {
134+ newCardHeight . background = cardRect . height
135+ newCardHeight . noBackground = cardRect . height
136+ } else {
137+ const CardHeightKey = enableBackground ? 'background' : 'noBackground'
138+ newCardHeight [ CardHeightKey ] = cardRect . height
136139 }
137140
138- cancelAnimationFrame ( adjustAnimateFrameRef . current )
141+ setTimeout ( ( ) => setCardHeight ( newCardHeight ) , 500 )
142+
143+ if ( adjustAnimateFrameRef . current !== null ) {
144+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
145+ }
139146 adjustAnimateFrameRef . current = requestAnimationFrame ( ( ) => adjustScale ( false ) )
140147 }
141148
@@ -292,7 +299,9 @@ export const usePreviewRenderer = (
292299 useEffect ( ( ) => {
293300 if ( selectedNum === 0 && content && shadowRoot ) {
294301 renderPreview ( )
295- cancelAnimationFrame ( adjustAnimateFrameRef . current )
302+ if ( adjustAnimateFrameRef . current !== null ) {
303+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
304+ }
296305 adjustAnimateFrameRef . current = requestAnimationFrame ( adjustScale )
297306 }
298307 } , [ selectedNum ] )
@@ -304,7 +313,10 @@ export const usePreviewRenderer = (
304313
305314 if ( prevEnableBackgroundRef . current !== enableBackground ) {
306315 prevEnableBackgroundRef . current = enableBackground
307- cancelAnimationFrame ( adjustAnimateFrameRef . current )
316+
317+ if ( adjustAnimateFrameRef . current !== null ) {
318+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
319+ }
308320 adjustAnimateFrameRef . current = requestAnimationFrame ( adjustScale )
309321 }
310322 } , [ blocks ] )
@@ -315,7 +327,10 @@ export const usePreviewRenderer = (
315327 return
316328 }
317329
318- cancelAnimationFrame ( adjustAnimateFrameRef . current )
330+ if ( adjustAnimateFrameRef . current !== null ) {
331+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
332+ }
333+
319334 requestAnimationFrame ( ( ) => {
320335 adjustScale ( )
321336 prevSelectedTabRef . current = selectedTab
0 commit comments