Skip to content

Commit c6aad06

Browse files
authored
fix (design library): preview of selected design when switching tabs (#3594)
1 parent 592d8e9 commit c6aad06

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/components/design-library-list/use-preview-renderer.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,27 @@ export const usePreviewRenderer = (
6262
const initialRenderRef = useRef( null )
6363
const shadowBodySizeRef = useRef( null )
6464
const prevEnableBackgroundRef = useRef( false )
65+
const prevSelectedTabRef = useRef( selectedTab )
6566

6667
const siteTitle = useSelect( select => select( 'core' ).getEntityRecord( 'root', 'site' )?.title || 'InnovateCo', [] )
6768
const isDesignLibraryDevMode = devMode && localStorage.getItem( 'stk__design_library__dev_mode' ) === '1'
6869

6970
const addHasBackground = selectedTab === 'patterns'
7071

7172
const adjustScale = () => {
72-
if ( ref.current && shadowRoot && ! selectedNum ) {
73+
const shouldAdjust = ref.current && shadowRoot &&
74+
( ! selectedNum || // adjust if design is not selected
75+
prevSelectedTabRef.current !== selectedTab ) // adjust if selected tab changed even if design is selected
76+
77+
if ( shouldAdjust ) {
7378
const newPreviewSize = { ...previewSize }
7479
const newCardHeight = { ...cardHeight }
7580
const cardRect = ref.current.getBoundingClientRect()
7681

7782
const shadowBody = shadowRoot.querySelector( 'body' )
7883
if ( shadowBody ) {
7984
const cardWidth = cardRect.width // Get width of the card
80-
const scaleFactor = cardWidth > 0 ? cardWidth / 1300 : 1 // Divide by 1200, which is the width of preview in the shadow DOM
85+
const scaleFactor = cardWidth > 0 ? cardWidth / 1300 : 1 // Divide by 1300, which is the width of preview in the shadow DOM
8186
newPreviewSize.scale = scaleFactor
8287

8388
let _bodyHeight = 1200
@@ -256,7 +261,8 @@ export const usePreviewRenderer = (
256261

257262
if ( ! content ||
258263
! shadowRoot ||
259-
selectedNum
264+
// don't re-render if design is selected and tab didn't change
265+
( selectedNum && prevSelectedTabRef.current === selectedTab )
260266
) {
261267
return
262268
}
@@ -268,7 +274,7 @@ export const usePreviewRenderer = (
268274
useEffect( () => {
269275
if ( selectedNum === 0 && content && shadowRoot ) {
270276
renderPreview()
271-
adjustScale()
277+
setTimeout( adjustScale, 100 )
272278
}
273279
}, [ selectedNum ] )
274280

@@ -288,7 +294,10 @@ export const usePreviewRenderer = (
288294
if ( ! content || ! blocks.parsed || ! blocks.serialized ) {
289295
return
290296
}
291-
setTimeout( adjustScale, 100 )
297+
setTimeout( () => {
298+
adjustScale()
299+
prevSelectedTabRef.current = selectedTab
300+
}, 100 )
292301
}, [ content ] )
293302

294303
const onClickDesign = () => {

0 commit comments

Comments
 (0)