Skip to content

Commit 14be947

Browse files
committed
refactor: replace getScrolledIndex utility with computed properties for min and max visible slides in Carousel component
1 parent f086824 commit 14be947

File tree

5 files changed

+46
-56
lines changed

5 files changed

+46
-56
lines changed

src/components/Carousel/Carousel.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,46 @@ export const Carousel = defineComponent({
469469
document.removeEventListener(endEvent, handleDragEnd)
470470
}
471471

472+
const minVisibleSlideIndex = computed(() => {
473+
let output = 0
474+
const snapAlignOffset = getSnapAlignOffset({
475+
itemsToShow: +config.itemsToShow,
476+
align: config.snapAlign,
477+
})
478+
479+
if (config.wrapAround) {
480+
output = currentSlideIndex.value - snapAlignOffset
481+
} else {
482+
output = getNumberInRange({
483+
val: currentSlideIndex.value - snapAlignOffset,
484+
max: slidesCount.value - +config.itemsToShow,
485+
min: 0,
486+
})
487+
}
488+
489+
return Math.floor(output)
490+
})
491+
492+
const maxVisibleSlideIndex = computed(() => {
493+
let output = +config.itemsToShow - 1
494+
495+
const snapAlignOffset = getSnapAlignOffset({
496+
itemsToShow: +config.itemsToShow,
497+
align: config.snapAlign,
498+
})
499+
500+
if (config.wrapAround) {
501+
output += currentSlideIndex.value - snapAlignOffset
502+
} else {
503+
output += getNumberInRange({
504+
val: currentSlideIndex.value - snapAlignOffset,
505+
max: slidesCount.value - +config.itemsToShow,
506+
min: 0,
507+
})
508+
}
509+
510+
return Math.ceil(output)
511+
})
472512
/**
473513
* Autoplay
474514
*/
@@ -584,6 +624,8 @@ export const Carousel = defineComponent({
584624
slides,
585625
currentSlide: currentSlideIndex,
586626
activeSlide: activeSlideIndex,
627+
maxVisibleSlide: maxVisibleSlideIndex,
628+
minVisibleSlide: minVisibleSlideIndex,
587629
maxSlide: maxSlideIndex,
588630
minSlide: minSlideIndex,
589631
slideSize,

src/components/Carousel/Carousel.types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export type InjectedCarousel = Reactive<{
2121
slidesCount: ComputedRef<number>
2222
activeSlide: Ref<number>
2323
currentSlide: Ref<number>
24-
maxSlide: ComputedRef<number>
25-
minSlide: ComputedRef<number>
24+
maxVisibleSlide: ComputedRef<number>
25+
minVisibleSlide: ComputedRef<number>
2626
slideSize: Ref<number>
2727
isVertical: ComputedRef<boolean>
2828
normalizedDir: ComputedRef<NormalizedDir>

src/components/Slide/Slide.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717

1818
import { injectCarousel } from '@/shared'
1919
import { disableChildrenTabbing } from '@/utils'
20-
import { getScrolledIndex } from '@/utils/getScrolledIndex'
2120

2221
import { SlideProps } from './Slide.types'
2322

@@ -78,18 +77,9 @@ export const Slide = defineComponent({
7877
() => currentIndex.value === carousel.activeSlide + 1
7978
)
8079
const isVisible: ComputedRef<boolean> = computed(() => {
81-
if (carousel.config.itemsToShow === 'auto') {
82-
return false
83-
}
84-
const scrolledIndex = getScrolledIndex({
85-
config: carousel.config,
86-
currentSlide: carousel.currentSlide,
87-
slidesCount: carousel.slidesCount,
88-
})
89-
9080
return (
91-
currentIndex.value >= Math.floor(scrolledIndex) &&
92-
currentIndex.value < Math.ceil(scrolledIndex) + carousel.config.itemsToShow
81+
currentIndex.value >= carousel.minVisibleSlide &&
82+
currentIndex.value <= carousel.maxVisibleSlide
9383
)
9484
})
9585

src/utils/getScrolledIndex.spec.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/utils/getScrolledIndex.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)