Skip to content

Commit d746060

Browse files
committed
add auto scroll
1 parent 90f1351 commit d746060

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ const DesignLibraryListItem = forwardRef( ( props, ref ) => {
6868
const blocksForSubstitutionRef = useRef( false )
6969
const hasBackgroundTargetRef = useRef( false )
7070
const initialRenderRef = useRef( null )
71+
const scrollPositionRef = useRef( 0 )
72+
const shadowBodySizeRef = useRef( null )
73+
74+
const scrollIntervalRef = useRef( null )
7175

7276
const { getEditorDom } = useSelect( 'stackable/editor-dom' )
7377
const editorDom = getEditorDom()
@@ -161,6 +165,14 @@ const DesignLibraryListItem = forwardRef( ( props, ref ) => {
161165
}
162166

163167
setTimeout( () => setCardHeight( newCardHeight ), 500 )
168+
169+
if ( shadowBodySizeRef.current === null ) {
170+
shadowBodySizeRef.current = {
171+
clientHeight: shadowBody.clientHeight,
172+
scrollHeight: shadowBody.scrollHeight,
173+
maxScrollTop: shadowBody.scrollHeight - shadowBody.clientHeight
174+
}
175+
}
164176
}
165177
}
166178

@@ -202,6 +214,7 @@ const DesignLibraryListItem = forwardRef( ( props, ref ) => {
202214
blocks={ cleanedBlock }
203215
adjustScale={ adjustScale }
204216
enableBackground={ enableBackground }
217+
onScroll={ onScroll }
205218
/> )
206219
}
207220

@@ -352,12 +365,65 @@ const DesignLibraryListItem = forwardRef( ( props, ref ) => {
352365
onClick( designId, category, parsedBlocks, blocksForSubstitutionRef.current, selectedPreviewSize )
353366
}
354367

368+
const onMouseOver = () => {
369+
const shadowDomBody = hostRef?.current?.shadowRoot.querySelector( 'body' )
370+
if ( shadowDomBody && shadowBodySizeRef.current ) {
371+
setTimeout( () => {
372+
if ( scrollIntervalRef.current ) {
373+
clearInterval( scrollIntervalRef.current )
374+
}
375+
376+
if ( scrollPositionRef.current === -1 ) {
377+
scrollPositionRef.current = shadowDomBody.scrollTop
378+
}
379+
380+
scrollIntervalRef.current = setInterval( () => {
381+
console.log( 'scrolling' )
382+
const scrollDifference = shadowBodySizeRef.current.maxScrollTop - scrollPositionRef.current
383+
const shouldScroll = shadowBodySizeRef.current.maxScrollTop - scrollPositionRef.current > 0
384+
385+
if ( shadowDomBody && shouldScroll ) {
386+
const scrollBy = scrollDifference >= 20 ? 20 : scrollDifference
387+
shadowDomBody.scrollTop = scrollPositionRef.current + scrollBy
388+
scrollPositionRef.current += scrollBy
389+
} else {
390+
clearInterval( scrollIntervalRef.current )
391+
}
392+
}, 20 )
393+
}, 500 )
394+
}
395+
}
396+
397+
const onMouseOut = () => {
398+
if ( scrollPositionRef.current <= 0 ) {
399+
return
400+
}
401+
402+
const shadowDomBody = hostRef?.current?.shadowRoot.querySelector( 'body' )
403+
if ( shadowDomBody ) {
404+
clearInterval( scrollIntervalRef.current )
405+
shadowDomBody.scrollTo({
406+
top: 0,
407+
behavior: 'smooth'
408+
})
409+
scrollPositionRef.current = -1
410+
}
411+
}
412+
413+
const onScroll = () => {
414+
if ( scrollIntervalRef.current ) {
415+
clearInterval( scrollIntervalRef.current )
416+
}
417+
}
418+
355419
return (
356420
<button
357421
className={ mainClasses }
358422
ref={ ref }
359423
data-stk-design-id={ props.designId }
360424
onClick={ () => selectedTab === 'patterns' ? onClickDesign() : {} }
425+
onMouseOut={ () => selectedTab === 'patterns' ? {} : onMouseOut() }
426+
onMouseOver={ () => selectedTab === 'patterns' ? {} : onMouseOver() }
361427
>
362428
{ ! isPro && plan !== 'free' && <span className="stk-pulsating-circle" role="presentation" /> }
363429
<div style={ { position: 'relative' } } className={ `${ getDesignPreviewSize() > 100 ? 'stk--design-preview-large' : 'stk--design-preview-small' }` }>
@@ -431,7 +497,7 @@ DesignLibraryListItem.defaultProps = {
431497
export default DesignLibraryListItem
432498

433499
const DesignPreview = ( {
434-
blocks, adjustScale, enableBackground,
500+
blocks, adjustScale, enableBackground, onScroll
435501
} ) => {
436502
useEffect( () => {
437503
// Adjust scale if the background was toggled
@@ -444,6 +510,7 @@ const DesignPreview = ( {
444510
return (
445511
<body
446512
className={ shadowBodyClasses }
513+
onWheel={ () => onScroll() }
447514
>
448515
<div
449516
dangerouslySetInnerHTML={ { __html: blocks } }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ export const getAdditionalStylesForPreview = () => {
270270

271271
styles += 'body { --stk-block-wide-width: 1000px; max-height: 1500px;}'
272272

273-
// Enable smooth scrolling for pages tab hover effect
274-
styles += 'body { scroll-behavior: smooth; overflow-y: auto; }'
273+
// Use 'auto' for scroll behavior. Let the scroll interval handle the speed.
274+
styles += 'body { scroll-behavior: auto; overflow-y: auto; }'
275275

276276
styles += '::-webkit-scrollbar { width: 0; }'
277277

0 commit comments

Comments
 (0)