Skip to content

Commit 0e11f07

Browse files
committed
fix preview when loading
1 parent e8945f5 commit 0e11f07

File tree

5 files changed

+23
-42
lines changed

5 files changed

+23
-42
lines changed

src/components/design-library-list/design-library-list-item.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const DesignLibraryListItem = memo( props => {
4646
const {
4747
blocks, enableBackground,
4848
shadowBodySizeRef, blocksForSubstitutionRef,
49-
previewSize, cardHeight, onClickDesign,
49+
previewSize, onClickDesign,
5050
updateShadowBodySize,
5151
} = usePreviewRenderer( props, shouldRender, spacingSize,
5252
ref, hostRef, shadowRoot, setIsLoading )
@@ -56,17 +56,16 @@ const DesignLibraryListItem = memo( props => {
5656
} = useAutoScroll( hostRef, shadowBodySizeRef, selectedTab )
5757

5858
const getDesignPreviewSize = () => {
59-
return selectedNum && selectedData ? selectedData.selectedPreviewSize.preview
59+
const tempHeight = selectedTab === 'pages' ? 345 : 100
60+
61+
const previewHeight = selectedNum && selectedData ? selectedData.selectedPreviewSize.preview
6062
: ( enableBackground ? previewSize.heightBackground : previewSize.heightNoBackground )
61-
}
6263

63-
const getCardHeight = () => {
64-
const key = props.enableBackground ? 'background' : 'noBackground'
65-
return cardHeight?.[ key ] ?? ( props.selectedTab === 'pages' ? 413 : 250 )
66-
}
64+
if ( ! blocks || ! previewHeight ) {
65+
return tempHeight
66+
}
6767

68-
if ( ! shouldRender && ! props.selectedNum ) {
69-
return <div style={ { height: `${ getCardHeight() }px` } } />
68+
return previewHeight
7069
}
7170

7271
const mainClasses = classnames( [
@@ -103,18 +102,18 @@ const DesignLibraryListItem = memo( props => {
103102
showHideNote={ false }
104103
/>
105104
) }
106-
<div className={ `stk-spinner-container ${ isLoading ? '' : 'stk-hide-spinner' }` }><Spinner /></div>
105+
<div className={ `stk-spinner-container ${ isLoading || ! shouldRender ? '' : 'stk-hide-spinner' }` }><Spinner /></div>
107106
<div
108107
className="stk-block-design__host-container"
109108
style={ {
110109
transform: `scale(${ selectedNum && selectedData ? selectedData.selectedPreviewSize.scale : previewSize?.scale })`,
111110
transformOrigin: 'top left',
112-
height: ! blocks ? ( selectedTab === 'pages' ? 345 : 100 ) : getDesignPreviewSize(),
111+
height: getDesignPreviewSize(),
113112
} }
114113
>
115114
<div className="stk-block-design__host" ref={ hostRef }>
116-
{ shadowRoot && <DesignPreview
117-
blocks={ blocks }
115+
{ shouldRender && shadowRoot && <DesignPreview
116+
blocks={ blocks }s
118117
shadowRoot={ shadowRoot }
119118
selectedTab={ selectedTab }
120119
designIndex={ props.designIndex }

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,20 @@ export const DesignPreview = ( {
8888
return
8989
}
9090

91+
setIsLoading( true )
92+
9193
const ric = window.requestIdleCallback || ( cb => setTimeout( cb, designIndex * 20 ) )
9294
const sanitizedHTML = safeHTML( blocks )
9395

9496
if ( selectedTab !== 'pages' || designIndex < 9 ) {
9597
// insert HTML for patterns and for the first 9 pages
9698
wrapper.innerHTML = sanitizedHTML
97-
// No need to call setIsLoading(false) here; isLoading is already false at this point.
99+
requestAnimationFrame( () => {
100+
ric( () => setIsLoading( false ) )
101+
} )
98102
return
99103
}
100104

101-
setIsLoading( true )
102-
103105
requestAnimationFrame( () => {
104106
ric( () => {
105107
wrapper.innerHTML = sanitizedHTML

src/components/design-library-list/editor.scss

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,10 @@
234234
transition: opacity 0.3s cubic-bezier(0.2, 0.6, 0.4, 1);
235235
will-change: opacity;
236236
opacity: 1;
237+
display: flex;
237238
&.stk-hide-spinner {
238239
opacity: 0;
239240
}
240-
.components-spinner {
241-
display: block;
242-
margin: 50px auto;
243-
}
244-
}
245-
246-
&.stk-design-library__item-pages .stk-spinner-container {
247-
display: flex;
248241
.components-spinner {
249242
margin: auto;
250243
}

src/components/design-library-list/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ const DesignLibraryItem = props => {
102102
const presetMarks = useMemo( () => getPresetMarks() || null, [] )
103103

104104
useEffect( () => {
105+
if ( selectedTab !== 'pages' ) {
106+
return
107+
}
105108
let id
106109
if ( typeof requestIdleCallback !== 'undefined' ) {
107110
id = requestIdleCallback( () => ! shouldRender ? setShouldRender( true ) : {} )
@@ -117,7 +120,7 @@ const DesignLibraryItem = props => {
117120
clearTimeout( id )
118121
}
119122
}
120-
}, [] )
123+
}, [ selectedTab ] )
121124

122125
useEffect( () => {
123126
if ( selectedTab === 'pages' ) {

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export const usePreviewRenderer = (
5656
const [ content, setContent ] = useState( '' )
5757
const [ contentForInsertion, setContentForInsertion ] = useState( null )
5858

59-
const [ cardHeight, setCardHeight ] = useState( {} )
6059
const [ previewSize, setPreviewSize ] = useState( {} )
6160

6261
const categoriesRef = useRef( [] )
@@ -142,21 +141,6 @@ export const usePreviewRenderer = (
142141
updateShadowBodySize( shadowBody )
143142
}
144143

145-
// Update card height more efficiently
146-
setCardHeight( prev => {
147-
const newCardHeight = { ...prev }
148-
149-
if ( ! Object.keys( prev ).length ) {
150-
newCardHeight.background = cardRect.height
151-
newCardHeight.noBackground = cardRect.height
152-
} else {
153-
const CardHeightKey = enableBackground ? 'background' : 'noBackground'
154-
newCardHeight[ CardHeightKey ] = cardRect.height
155-
}
156-
157-
return newCardHeight
158-
} )
159-
160144
if ( adjustAnimateFrameRef.current !== null ) {
161145
cancelAnimationFrame( adjustAnimateFrameRef.current )
162146
}
@@ -389,7 +373,7 @@ export const usePreviewRenderer = (
389373
return {
390374
blocks: blocks.serialized, enableBackground,
391375
shadowBodySizeRef, blocksForSubstitutionRef,
392-
previewSize, cardHeight, onClickDesign,
376+
previewSize, onClickDesign,
393377
updateShadowBodySize,
394378
}
395379
}

0 commit comments

Comments
 (0)