Skip to content
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

Dynamically add slides in the middle of a glide produce strange result #8

Closed
vcastro45 opened this issue Jan 4, 2019 · 8 comments
Closed

Comments

@vcastro45
Copy link

If I have a glide like this (where slides is an array of 3 elements):

<vue-glide>
    <vue-glide-slide v-for="(slide, i) of slides" :key="i">
        Heyy, I am slide { i }
    </vue-glide-slide>
</vue-glide>

In the rendered HTML, each slide (.glide__slide) has a data-glide-index attribute. I this case, each data-glide-index is respectively 0, 1 and 2.

but now, if I create a button who adds an element in the middle of my list (for example between index 1 and 2), the data-glide-index of each slide will be respectively 0, 1, 3, 2.

By the way, the display will be broken and I will not be able to focus the new slide.

@vcastro45
Copy link
Author

vcastro45 commented Jan 4, 2019

I found a work around:

<template>
<vue-glide ref="glide">
    <vue-glide-slide v-for="(slide, i) of slides" :key="i">
        Heyy, I am slide { i }
    </vue-glide-slide>
</vue-glide>
</template>
@Component({
  components: {
    [Glide.name]: Glide,
    [GlideSlide.name]: GlideSlide
  }
})
export default class SlidesComponent extends Vue {
    slides = [{}, {}, {}]
    addSlide () {
        const glide = this.$refs.glide
        this.slides.push({})
        glide.glide.destroy()
        this.$nextTick(() => {
          glide.init()
        })
    }
}

But it still glitch a little bit (and it's not very nice if you want to stay focus on the same slide after adding the new slide)

@antonreshetov
Copy link
Owner

After add new slide into array slides between index 1 and 2, data-glide-index of each slide will be respectively 0, 1, 2, 3 not 0, 1, 3, 2.

Example https://codesandbox.io/s/n722774xl

@vcastro45
Copy link
Author

@antonreshetov My real case of use is a little more complex and when I wrote my first message I guessed that the result will be the same... My bad, sorry.

The real case look like this: https://codesandbox.io/s/kovjkr4m67

@antonreshetov
Copy link
Owner

@vcastro45 Because you have manually added an item to the slide. The other of the slides are rendered through a loop from the array in which you add a new item.

@antonreshetov
Copy link
Owner

@vcastro45 If you need reinit, it can be done without calling glide methods. You can add just a unique key for vue-glide, for example it can be length of array, to prevent reuse of the vue component

<template>
<vue-glide ref="glide" :key="slides.length">
    <vue-glide-slide v-for="(slide, i) of slides" :key="i">
        Heyy, I am slide { i }
    </vue-glide-slide>
</vue-glide>
</template>
``

@antonreshetov
Copy link
Owner

@vcastro45 Does the problem still exist?

@vcastro45
Copy link
Author

@antonreshetov In my exemple, it still bug but with the key on the vue-glide it works fine

@antonreshetov
Copy link
Owner

@vcastro45 then I will close this issue if you don't mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants