Skip to content

Fix carousel vModel is allowed toSlide outside of bounds #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions src/components/Carousel/Carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,25 +484,18 @@
if (!skipTransition && isSliding.value) {
return
}

let targetIndex = slideIndex
let mappedIndex = slideIndex

prevSlideIndex.value = currentSlideIndex.value

if (!config.wrapAround) {
targetIndex = getNumberInRange({
val: targetIndex,
max: maxSlideIndex.value,
min: minSlideIndex.value,
})
} else {
mappedIndex = mapNumberToRange({
val: targetIndex,
max: maxSlideIndex.value,
min: minSlideIndex.value,
})

const targetIndex = (config.wrapAround ? mapNumberToRange : getNumberInRange)({
val: slideIndex,
max: maxSlideIndex.value,
min: minSlideIndex.value,
})

if (currentSlideIndex.value === targetIndex) {
return;

Check warning on line 495 in src/components/Carousel/Carousel.ts

View workflow job for this annotation

GitHub Actions / coverage-report

This line is not covered by a test
}

prevSlideIndex.value = currentSlideIndex.value

emit('slide-start', {
slidingToIndex: slideIndex,
Expand All @@ -514,17 +507,17 @@
stopAutoplay()
isSliding.value = true

currentSlideIndex.value = targetIndex
if (mappedIndex !== targetIndex) {
currentSlideIndex.value = slideIndex
if (targetIndex !== slideIndex) {
modelWatcher.pause()
}
emit('update:modelValue', mappedIndex)
emit('update:modelValue', targetIndex)

const transitionCallback = (): void => {
if (config.wrapAround && mappedIndex !== targetIndex) {
if (config.wrapAround && targetIndex !== slideIndex) {
modelWatcher.resume()

currentSlideIndex.value = mappedIndex
currentSlideIndex.value = targetIndex

Check warning on line 520 in src/components/Carousel/Carousel.ts

View workflow job for this annotation

GitHub Actions / coverage-report

This line is not covered by a test
emit('loop', {
currentSlideIndex: currentSlideIndex.value,
slidingToIndex: slideIndex,
Expand Down