ismail9k / vue3-carousel

Vue 3 carousel component
https://ismail9k.github.io/vue3-carousel/
MIT License
660 stars 164 forks source link

There seems to be something wrong with the way the library initializes which slide it is on, it's quite broken #95

Closed cooperfrench95 closed 2 years ago

cooperfrench95 commented 2 years ago
 /**
         * Navigation function
         */
        const isSliding = ref(false);
        function slideTo(slideIndex, mute = false) {
            if (currentSlide.value === slideIndex || isSliding.value) { // Error triggered at this line when trying to get currentSlide.value
                return;
            }
            // Wrap slide index
            const lastSlideIndex = slidesCount.value - 1;
            if (slideIndex > lastSlideIndex) {
                return slideTo(slideIndex - slidesCount.value);
            }
            if (slideIndex < 0) {
                return slideTo(slideIndex + slidesCount.value);
            }
            isSliding.value = true;
            prevSlide.value = currentSlide.value;
            currentSlide.value = slideIndex;
            if (!mute) {
                emit('update:modelValue', currentSlide.value);
            }
            setTimeout(() => {
                if (config.wrapAround)
                    updateSlidesBuffer();
                isSliding.value = false;
            }, config.transition);
        }

Reproduction link: https://codesandbox.io/s/vue3-carousel-issue-repro-lo1dx?file=/src/App.vue

avatarbabe commented 2 years ago

I am also getting this error, there seems to be no way to set the current slide programatically that won't trigger this error.

uke5tar commented 2 years ago

Try and set the modelValue or v-model value after the component has mounted. Then you don't get the maximum call stack size exceed error.

Working example code:

<template>
  <carousel
    wrap-around
    v-model="initialSlide">
    <slide v-for="slide in 10" :key="slide">
      {{ slide }}
    </slide>

    <template #addons="{ currentSlide, slidesCount }">
      <Navigation />

      <div>
        {{ currentSlide + 1 }} / {{ slidesCount }}
      </div>
    </template>
  </carousel>
</template>

<script>
import { ref, onMounted } from 'vue';
import { Carousel, Slide, Navigation } from 'vue3-carousel';
import 'vue3-carousel/dist/carousel.css';

export default {
  name: 'Carousel',
  components: {
    Carousel,
    Slide,
    Navigation,
  },
  setup() {
    const initialSlide = ref(undefined);

    onMounted(() => {
      initialSlide.value = 0;
    });

    return {
      initialSlide,
    };
  },
};
</script>
github-actions[bot] commented 2 years ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 2 years ago

This issue was closed because it has been inactive for 14 days since being marked as stale.