Skip to content

Commit 56f22db

Browse files
committed
clone necessary slides only
1 parent 54f6ea8 commit 56f22db

File tree

1 file changed

+55
-53
lines changed

1 file changed

+55
-53
lines changed

src/block/carousel/frontend-carousel.js

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -82,64 +82,66 @@ class _StackableCarousel {
8282
this.clones = []
8383
const clonesToAdd = []
8484
let lastClone = null
85-
let frame = 0
85+
let index = 0
86+
let step = 0
8687

8788
const runInitSteps = () => {
88-
if ( frame === 0 ) {
89-
this.slideEls.forEach( ( original, i ) => {
90-
const clone = original.cloneNode( true )
91-
clone.classList.add( `stk-slide-clone-${ i + 1 }` )
92-
93-
this.clones.push( clone )
94-
95-
// Ensure click events on cloned slides are delegated to the corresponding original slide elements.
96-
// This preserves expected interactivity for cloned slides in infinite scroll.
97-
clone.addEventListener( 'click', e => {
98-
const targetClassList = [ ...e.target.classList ]
99-
if ( targetClassList.length ) {
100-
const targetClasses = `.${ targetClassList.join( '.' ) }`
101-
const originalTarget = original.querySelector( targetClasses )
102-
originalTarget.click()
103-
}
104-
} )
105-
106-
if ( i === this.slideEls.length - 1 ) {
107-
lastClone = clone
108-
109-
// Also add the last slide clone at the end
110-
if ( this.slidesToShow === this.slideEls.length ) {
111-
const lastCloneClone = clone.cloneNode( true )
112-
lastCloneClone.classList.add( `stk-slide-clone-${ i + 1 }-clone` )
113-
clonesToAdd.push( lastCloneClone )
114-
}
115-
} else {
116-
clonesToAdd.push( clone )
89+
if ( step === 0 ) {
90+
// Clone only the last slide and the first N slides (where N equals to slidesToShow)
91+
const slideIndex = index === this.slidesToShow ? this.slideEls.length - 1 : index
92+
const original = this.slideEls[ slideIndex ]
93+
const clone = original.cloneNode( true )
94+
clone.classList.add( `stk-slide-clone-${ slideIndex + 1 }` )
95+
96+
this.clones.push( clone )
97+
98+
// Ensure click events on cloned slides are delegated to the corresponding original slide elements.
99+
// This preserves expected interactivity for cloned slides in infinite scroll.
100+
clone.addEventListener( 'click', e => {
101+
const targetClassList = [ ...e.target.classList ]
102+
if ( targetClassList.length ) {
103+
const targetClasses = `.${ targetClassList.join( '.' ) }`
104+
const originalTarget = original.querySelector( targetClasses )
105+
originalTarget.click()
117106
}
118107
} )
119-
} else if ( frame === 2 ) {
108+
109+
if ( index === this.slidesToShow ) {
110+
lastClone = clone
111+
step++
112+
} else {
113+
clonesToAdd.push( clone )
114+
}
115+
116+
index++
117+
} else if ( step === 1 ) {
120118
// Append clones at the end except for the last slide clone which will be placed at the front
121-
this.sliderEl.appendChild( ...clonesToAdd )
119+
this.sliderEl.append( ...clonesToAdd )
122120
if ( lastClone ) {
123121
this.sliderEl.insertBefore( lastClone, this.slideEls[ 0 ] )
124122
}
125-
} else if ( frame === 3 ) {
123+
step++
124+
} else if ( step === 2 ) {
126125
// IMPORTANT: Do style reads before applying style change to improve performance
127126
// https://web.dev/articles/avoid-large-complex-layouts-and-layout-thrashing
128127
const targetOffsetLeft = this.slideEls[ 0 ].offsetLeft
129128

130129
// Scroll without animation to the first slide
131130
this.sliderEl.style.scrollBehavior = 'unset'
132131
this.sliderEl.scrollLeft = targetOffsetLeft
132+
step++
133+
} else if ( step === 3 ) {
133134
this.sliderEl.style.scrollBehavior = ''
134135

135136
this.currentSlide = 1
136137
this.updateDots()
137-
} else if ( frame === 4 ) {
138+
step++
139+
} else if ( step === 4 ) {
138140
otherInitCalls()
141+
step++
139142
}
140143

141-
frame++
142-
if ( frame < 5 ) {
144+
if ( step < 5 ) {
143145
requestAnimationFrame( runInitSteps )
144146
}
145147
}
@@ -252,7 +254,7 @@ class _StackableCarousel {
252254
nextSlide = () => {
253255
let newSlide = this.currentSlide + 1
254256

255-
if ( this.infiniteScroll && newSlide > this.slideEls.length ) {
257+
if ( this.type === 'slide' && this.infiniteScroll && newSlide > this.slideEls.length ) {
256258
this.swapSlides( newSlide, 'N' )
257259
return
258260
}
@@ -266,7 +268,7 @@ class _StackableCarousel {
266268
prevSlide = () => {
267269
let newSlide = this.currentSlide - 1
268270

269-
if ( this.infiniteScroll && newSlide < this.slideOffset ) {
271+
if ( this.type === 'slide' && this.infiniteScroll && newSlide < this.slideOffset ) {
270272
this.swapSlides( newSlide, 'P' )
271273
return
272274
}
@@ -288,7 +290,7 @@ class _StackableCarousel {
288290

289291
const runSteps = () => {
290292
if ( steps === 0 ) {
291-
const lastCloneSlide = this.clones[ this.currentSlide - 1 ].offsetLeft
293+
const lastCloneSlide = this.clones[ this.clones.length - 1 ].offsetLeft
292294
const firstCloneSide = this.clones[ 0 ].offsetLeft
293295

294296
let initSlide = null
@@ -321,26 +323,26 @@ class _StackableCarousel {
321323
if ( slide === this.currentSlide && ! force ) {
322324
return
323325
}
326+
const currentSlideEl = this.slideEls[ this.currentSlide - 1 ]
327+
const newSlideEl = this.slideEls[ slide - 1 ]
328+
const offsetLeft = newSlideEl.offsetLeft
324329

325-
this.slideEls[ this.currentSlide - 1 ].classList.remove( 'stk-block-carousel__slide--active' )
326-
this.slideEls[ slide - 1 ].classList.add( 'stk-block-carousel__slide--active' )
330+
currentSlideEl.classList.remove( 'stk-block-carousel__slide--active' )
331+
newSlideEl.classList.add( 'stk-block-carousel__slide--active' )
327332

328333
if ( this.type === 'slide' ) {
329-
const offsetLeft = this.slideEls[ slide - 1 ].offsetLeft
330334
this.sliderEl.scrollLeft = offsetLeft
331335
} else if ( this.type === 'fade' ) { // fade
332-
const slidePrevEl = this.slideEls[ this.currentSlide - 1 ]
333-
slidePrevEl.style.opacity = 0
334-
335-
const slideEl = this.slideEls[ slide - 1 ]
336-
slideEl.style.zIndex = ++this.currentZIndex
337-
slideEl.style.transition = 'none'
338-
slideEl.style.opacity = 0
339-
slideEl.style.visibility = 'visible'
340-
slideEl.style.left = `${ this.isRTL ? '' : '-' }${ 100 * ( slide - 1 ) }%`
336+
currentSlideEl.style.opacity = 0
337+
338+
newSlideEl.style.zIndex = ++this.currentZIndex
339+
newSlideEl.style.transition = 'none'
340+
newSlideEl.style.opacity = 0
341+
newSlideEl.style.visibility = 'visible'
342+
newSlideEl.style.left = `${ this.isRTL ? '' : '-' }${ 100 * ( slide - 1 ) }%`
341343
setTimeout( () => {
342-
slideEl.style.transition = ''
343-
slideEl.style.opacity = 1
344+
newSlideEl.style.transition = ''
345+
newSlideEl.style.opacity = 1
344346
}, 1 )
345347
}
346348
this.fixAccessibility( slide )

0 commit comments

Comments
 (0)