Skip to content

Commit 0a2a90a

Browse files
Merge pull request #32 from Travelopia/fix/slider-arrows
TPSlider: Fix slider arrows
2 parents c6e1a06 + 7329287 commit 0a2a90a

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/slider/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
</tp-slider>
131131

132132
<!--Nested sliders-->
133-
<tp-slider flexible-height="yes" infinite="yes" swipe="yes" behaviour="fade">
133+
<tp-slider flexible-height="yes" swipe="yes" behaviour="fade">
134134
<tp-slider-arrow direction="previous"><button>&laquo; Previous</button></tp-slider-arrow>
135135
<tp-slider-arrow direction="next"><button>Next &raquo;</button></tp-slider-arrow>
136136
<tp-slider-track>

src/slider/tp-slider.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,37 @@ export class TPSliderElement extends HTMLElement {
214214
}
215215
}
216216

217+
/**
218+
* Get the arrow element by selector.
219+
*
220+
* In case of nested sliders, it difficult to find the correct arrow
221+
* because arrows can be placed anywhere.
222+
* This function checks if the parent tp-slider belongs to this component,
223+
* then return that arrow element, using 'this'.
224+
*
225+
* @param {string} selector Selector.
226+
*/
227+
getArrow( selector: string ) {
228+
// Get all arrows.
229+
const arrows: NodeListOf<TPSliderArrowElement> | null = this.querySelectorAll( selector );
230+
const parentSliderElement: TPSliderElement = this;
231+
let theArrow: TPSliderArrowElement | null = this.querySelector( selector );
232+
233+
// Loop through all the arrows including the one's inside nested slider.
234+
arrows.forEach( ( arrow ) => {
235+
/**
236+
* If the closest tp-slider is the same as the parentSliderElement, that means we have found
237+
* the correct arrow.
238+
*/
239+
if ( parentSliderElement === arrow.closest( 'tp-slider' ) ) {
240+
theArrow = arrow;
241+
}
242+
} );
243+
244+
// Return arrow.
245+
return theArrow;
246+
}
247+
217248
/**
218249
* Update stuff when any attribute has changed.
219250
* Example: Update subcomponents.
@@ -222,8 +253,8 @@ export class TPSliderElement extends HTMLElement {
222253
// Get subcomponents.
223254
const sliderNavItems: NodeListOf<TPSliderNavItemElement> | null = this.querySelectorAll( 'tp-slider-nav-item' );
224255
const sliderCounts: NodeListOf<TPSliderCountElement> | null = this.querySelectorAll( 'tp-slider-count' );
225-
const leftArrow: TPSliderArrowElement | null = this.querySelector( 'tp-slider-arrow[direction="previous"]' );
226-
const rightArrow: TPSliderArrowElement | null = this.querySelector( 'tp-slider-arrow[direction="next"]' );
256+
const leftArrow: TPSliderArrowElement | null = this.getArrow( 'tp-slider-arrow[direction="previous"]' );
257+
const rightArrow: TPSliderArrowElement | null = this.getArrow( 'tp-slider-arrow[direction="next"]' );
227258

228259
// Set active slide.
229260
const slides: NodeListOf<TPSliderSlideElement> | null | undefined = this.getSlideElements();

0 commit comments

Comments
 (0)