Skip to content

Commit

Permalink
Apply anchor element bugfix from pr pawelgrzybek#248
Browse files Browse the repository at this point in the history
  • Loading branch information
brabli committed Nov 7, 2021
1 parent 91bc183 commit f0b25ef
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/siema.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default class Siema {
if (this.config.draggable) {
// Keep track pointer hold and dragging distance
this.pointerDown = false;
this.draggedBeyondThreshold = false;
this.drag = {
startX: 0,
endX: 0,
Expand Down Expand Up @@ -506,6 +507,12 @@ export default class Siema {
e.stopPropagation();
this.pointerDown = true;
this.drag.startX = e.pageX;

// if dragged element is a link
// mark preventClick prop as a true
// to detemine about browser redirection later on
this.drag.preventClick = this.insideAnchor(e.target);
this.draggedBeyondThreshold = false;
}


Expand All @@ -523,24 +530,31 @@ export default class Siema {
this.clearDrag();
}

/**
* check if an element is or is inside an anchor tag
*/
insideAnchor(elem) {
var value = false;

do {
value = elem.nodeName === 'A';
} while (!value && (elem = elem.parentNode))

return value;
}

/**
* mousemove event handler
*/
mousemoveHandler(e) {
e.preventDefault();
if (this.pointerDown) {
// if dragged element is a link
// mark preventClick prop as a true
// to detemine about browser redirection later on
if (e.target.nodeName === 'A') {
this.drag.preventClick = true;
}

this.drag.endX = e.pageX;
this.selector.style.cursor = '-webkit-grabbing';
this.sliderFrame.style.webkitTransition = `all 0ms ${this.config.easing}`;
this.sliderFrame.style.transition = `all 0ms ${this.config.easing}`;
this.draggedBeyondThreshold = Math.abs(this.drag.startX - this.drag.endX) > this.config.threshold;

const currentSlide = this.config.loop ? this.currentSlide + this.perPage : this.currentSlide;
const currentOffset = currentSlide * (this.selectorWidth / this.perPage);
Expand Down Expand Up @@ -573,7 +587,7 @@ export default class Siema {
clickHandler(e) {
// if the dragged element is a link
// prevent browsers from folowing the link
if (this.drag.preventClick) {
if (this.drag.preventClick && this.draggedBeyondThreshold) {
e.preventDefault();
}
this.drag.preventClick = false;
Expand Down

0 comments on commit f0b25ef

Please sign in to comment.