@@ -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 ,
@@ -117,22 +118,28 @@ const DesignLibraryItem = props => {
117118 return
118119 }
119120
120- const containerRect = document . querySelector ( '.ugb-modal-design-library__designs' ) . getBoundingClientRect ( )
121- const itemRect = itemRef . current . getBoundingClientRect ( )
121+ // Use a timeout to ensure designs have finished rendering before calculating visibility.
122+ const timeoutRef = setTimeout ( ( ) => {
123+ const containerRect = document . querySelector ( '.ugb-modal-design-library__designs' ) . getBoundingClientRect ( )
124+ const itemRect = itemRef . current . getBoundingClientRect ( )
122125
123- const render = ( itemRect . top > containerRect . top - 250 && itemRect . top < containerRect . bottom + 250 ) ||
124- ( itemRect . bottom > containerRect . top - 250 && itemRect . bottom < containerRect . bottom + 250 )
126+ const render = itemRect . bottom >= containerRect . top - 250 && itemRect . top <= containerRect . bottom + 250
125127
126- setShouldRender ( render )
127- } , [ scrollTop , props . enableBackground ] )
128+ setShouldRender ( render )
129+ } , 250 )
130+
131+ return ( ) => {
132+ clearTimeout ( timeoutRef )
133+ }
134+ } , [ scrollTop , _previewProps . enableBackground , _previewProps . designId ] )
128135
129136 const getCardHeight = ( ) => {
130- const key = props . enableBackground ? 'background' : 'noBackground'
137+ const key = _previewProps . enableBackground ? 'background' : 'noBackground'
131138 return props . selectedTab === 'pages' ? 472 : cardHeight ?. [ key ] || 250
132139 }
133140
134141 if ( ! shouldRender && ! props . selectedNum ) {
135- return < div ref = { itemRef } data-stk-design-id = { props . testId } style = { { height : `${ getCardHeight ( ) } px` } } />
142+ return < div ref = { itemRef } style = { { height : `${ getCardHeight ( ) } px` } } />
136143 }
137144
138145 return < DesignLibraryListItem
0 commit comments