Skip to content

Commit 2491ff0

Browse files
authored
Fix (design library): render designs if within view (#3591)
* fix rendering of designs * fix from Coderabbit QA
1 parent eadb634 commit 2491ff0

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ const DesignLibraryListItem = forwardRef( ( props, ref ) => {
8383
<button
8484
className={ mainClasses }
8585
ref={ ref }
86-
data-stk-design-id={ props.designId }
8786
onClick={ onClickHost }
8887
onMouseOut={ onMouseOut }
8988
onMouseOver={ onMouseOver }

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const DesignLibraryList = props => {
7474
selectedData={ selectedData }
7575
selectedTab={ props.selectedTab }
7676
scrollTop={ scrollTop }
77+
designKey={ i }
7778
/>
7879
)
7980
} ) }
@@ -103,7 +104,7 @@ const DesignLibraryItem = props => {
103104
const itemRef = useRef( null )
104105
const [ cardHeight, setCardHeight ] = useState( {} )
105106
const [ previewSize, setPreviewSize ] = useState( {} )
106-
const [ shouldRender, setShouldRender ] = useState( props.testKey < 9 )
107+
const [ shouldRender, setShouldRender ] = useState( props.designKey < 9 )
107108

108109
const previewProps = {
109110
..._previewProps,
@@ -113,26 +114,37 @@ const DesignLibraryItem = props => {
113114
}
114115

115116
useEffect( () => {
116-
if ( ! itemRef.current ) {
117-
return
118-
}
117+
// Use a timeout to ensure designs have finished rendering before calculating visibility.
118+
const timeoutRef = setTimeout( () => {
119+
const itemEl = itemRef.current
120+
const containerEl = itemEl?.closest( '.ugb-modal-design-library__designs' ) || document.querySelector( '.ugb-modal-design-library__designs' )
121+
122+
if ( ! itemEl || ! containerEl ) {
123+
return
124+
}
125+
126+
const containerRect = containerEl.getBoundingClientRect()
127+
const itemRect = itemEl.getBoundingClientRect()
119128

120-
const containerRect = document.querySelector( '.ugb-modal-design-library__designs' ).getBoundingClientRect()
121-
const itemRect = itemRef.current.getBoundingClientRect()
129+
const BOUNDARY = 250
122130

123-
const render = ( itemRect.top > containerRect.top - 250 && itemRect.top < containerRect.bottom + 250 ) ||
124-
( itemRect.bottom > containerRect.top - 250 && itemRect.bottom < containerRect.bottom + 250 )
131+
const render = itemRect.bottom >= containerRect.top - BOUNDARY && itemRect.top <= containerRect.bottom + BOUNDARY
125132

126-
setShouldRender( render )
127-
}, [ scrollTop, props.enableBackground ] )
133+
setShouldRender( render )
134+
}, 250 )
135+
136+
return () => {
137+
clearTimeout( timeoutRef )
138+
}
139+
}, [ scrollTop, _previewProps.enableBackground, _previewProps.designId ] )
128140

129141
const getCardHeight = () => {
130-
const key = props.enableBackground ? 'background' : 'noBackground'
142+
const key = _previewProps.enableBackground ? 'background' : 'noBackground'
131143
return props.selectedTab === 'pages' ? 472 : cardHeight?.[ key ] || 250
132144
}
133145

134146
if ( ! shouldRender && ! props.selectedNum ) {
135-
return <div ref={ itemRef } data-stk-design-id={ props.testId } style={ { height: `${ getCardHeight() }px` } } />
147+
return <div ref={ itemRef } style={ { height: `${ getCardHeight() }px` } } />
136148
}
137149

138150
return <DesignLibraryListItem

0 commit comments

Comments
 (0)