Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 14 additions & 6 deletions src/macro-carousel/macro-carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,19 @@ class MacroCarousel extends HTMLElement {
return intGetter(this, 'slides-per-view', 1);
}

/**
* Get the indexes of slides currently in view.
* @return {Array<number>}
*/
get slidesInView() {
const slidesInViewIndexes = [];
for (let i = 0; i < this.slidesPerView; i++) {
slidesInViewIndexes.push((this.selected + i) % this._slides.length);
}

return slidesInViewIndexes;
}

/**
* If true, disables CSS transitions and drag deceleration.
* @type {boolean}
Expand Down Expand Up @@ -1135,16 +1148,11 @@ value. Add CSS units to its value to avoid breaking the slides layout.`);
* @private
*/
_updateSlidesA11y() {
const slidesInView = [];
for (let i = 0; i < this.slidesPerView; i++) {
slidesInView.push((this.selected + i) % this._slides.length);
}

// Set aria-hidden to false only for the slides whose indexes are included
// in the slidesInView array.
let isSlideInView;
this._slides.map(slide => slide.element).forEach((slideEl, slideIndex) => {
isSlideInView = !isUndefined(slidesInView.find(i => i === slideIndex));
isSlideInView = !isUndefined(this.slidesInView.find(i => i === slideIndex));
// Slides in view have `aria-hidden` set to `false`.
slideEl.setAttribute(ATTRS.STANDARD.ARIA.HIDDEN,
isSlideInView ? ATTR_VALUES.FALSE : ATTR_VALUES.TRUE);
Expand Down
8 changes: 8 additions & 0 deletions test/selected-slides-per-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,13 @@
await wcutils.flush();
expect(this.slider.selected).to.equal(numberOfSlides - 1);
});

it('returns indexes of slides in view', async function() {
this.slider.selected = 0;
this.slider.slidesPerView = 3;

await wcutils.flush();
expect(this.slider.slidesInView).to.deep.equal([0, 1, 2]);
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need more tests:

  • with different values of selected (not just 0)
  • with different values of slidesPerView (definitely at least 1 as well)

And then we need to test for edge cases:

  • what if selected is set on purpose out of range? (e.g. there are 5 slides, and we set selected to 7)
  • what if slidesPerView is set to 0?
  • what if looping is enabled, and the selected slide is the last? slidesInView should be [last, first...]

Any anything else that comes to mind

});
})();