Skip to content

Commit

Permalink
Bugfix: Carousel Swiper Styling & Direction (themesberg#1023)
Browse files Browse the repository at this point in the history
* Bugfix: Replace missing slideCls and imgCls for custom carousel styling

* Bugfix: Invert Carousel swiping direction (now swipe from left->right goes back, right->left goes forward)

* Bugfix: Touch gestures not activating buttons
  • Loading branch information
jsonMartin authored Sep 3, 2023
1 parent 876c193 commit 0a104e0
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/lib/carousels/Carousel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
let carouselDiv: HTMLDivElement;
let percentOffset: number = 0;
let touchEvent: MouseEvent | TouchEvent | null = null;
const getPositionFromEvent = (evt: MouseEvent | TouchEvent) => {
const mousePos = (evt as MouseEvent)?.clientX;
Expand All @@ -99,6 +100,7 @@
};
const onDragStart = (evt: MouseEvent | TouchEvent) => {
touchEvent = evt;
evt.preventDefault();
const start = getPositionFromEvent(evt);
const width = carouselDiv.getBoundingClientRect().width;
Expand Down Expand Up @@ -137,13 +139,20 @@
const distance = position - start;
if (Math.abs(distance) >= SWIPE_MIN_DISTANCE && duration <= SWIPE_MAX_DURATION && duration > 0) {
if (distance > 0) nextSlide();
else prevSlide();
} else if (percentOffset > DRAG_MIN_PERCENT) nextSlide();
else if (percentOffset < -DRAG_MIN_PERCENT) prevSlide();
if (distance > 0) prevSlide();
else nextSlide();
} else if (percentOffset > DRAG_MIN_PERCENT) prevSlide();
else if (percentOffset < -DRAG_MIN_PERCENT) nextSlide();
else {
// The gesture is a tap not drag, so manually issue a click event to trigger tap click gestures lost via preventDefault
touchEvent?.target?.dispatchEvent(new Event('click', {
bubbles: true,
}))
}
}
percentOffset = 0;
activeDragGesture = undefined;
touchEvent = null;
};
</script>

Expand All @@ -160,17 +169,20 @@
class="relative"
on:mousedown={onDragStart}
on:touchstart={onDragStart}
role="button"
role="button"
aria-label="Draggable Carousel"
tabindex="0"
>
tabindex="0">
<div
style={`transform: translateX(${percentOffset}%)`}
class={twJoin(
divClass,
{ 'transition-transform': activeDragGesture === undefined }
)}>
<Slide image={image.imgurl} altTag={image.name} attr={image.attribution} {slideClass} />
{ 'transition-transform': activeDragGesture === undefined })}>
<Slide
image={image.imgurl}
slideClass={slideCls}
imgClass={imgCls}
altTag={image.name}
attr={image.attribution} />
</div>
{#if showIndicators}
<!-- Slider indicators -->
Expand Down

0 comments on commit 0a104e0

Please sign in to comment.