Skip to content

Commit

Permalink
fix: Recollect slides during update to support dynamic additions, rem…
Browse files Browse the repository at this point in the history
…ovals and reordering (glidejs#457)
  • Loading branch information
driskell authored Nov 22, 2021
1 parent 15b3d62 commit ded7e74
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/components/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isString } from '../utils/unit'

const TRACK_SELECTOR = '[data-glide-el="track"]'

export default function (Glide, Components) {
export default function (Glide, Components, Events) {
const Html = {
/**
* Setup slider HTML nodes.
Expand All @@ -15,6 +15,13 @@ export default function (Glide, Components) {
mount () {
this.root = Glide.selector
this.track = this.root.querySelector(TRACK_SELECTOR)
this.collectSlides()
},

/**
* Collect slides
*/
collectSlides () {
this.slides = Array.prototype.slice.call(this.wrapper.children).filter((slide) => {
return !slide.classList.contains(Glide.settings.classes.slide.clone)
})
Expand Down Expand Up @@ -84,5 +91,12 @@ export default function (Glide, Components) {
}
})

/**
* Add/remove/reorder dynamic slides
*/
Events.on('update', () => {
Html.collectSlides()
})

return Html
}
14 changes: 14 additions & 0 deletions tests/functional/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,18 @@ describe('Calling `update()` method with', () => {
expect(glide.index).toBe(1)
expect(slides[1].classList.contains(defaults.classes.slide.active)).toBe(true)
})

test('additional slide should be updated', () => {
let { wrapper } = query(document)
let newSlide = document.createElement('li')
newSlide.className = 'glide__slide'
newSlide.innerHTML = 0

let glide = new Glide('#glide', { startAt: 0 }).mount()

wrapper.append(newSlide)
glide.update()

expect(newSlide.style.width).toBeTruthy()
})
})

0 comments on commit ded7e74

Please sign in to comment.