From ba696c275b46d12632c98c83aabee4139c01d4b8 Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 20 Apr 2021 22:20:21 +0300 Subject: [PATCH] Add pauseOnFocus prop, update storybook, update autoplayInterval logic --- .gitignore | 1 + src/components/Carousel/Carousel.svelte | 33 ++++++++++--------- src/components/Carousel/CarouselView.svelte | 10 +++++- .../Carousel/CarouselViewCustomArrows.svelte | 8 ++++- .../Carousel/CarouselViewCustomDots.svelte | 8 ++++- 5 files changed, 41 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index c4ba4c2..f5f1664 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /node_modules/ /public/build/ dist +/storybook-static .DS_Store diff --git a/src/components/Carousel/Carousel.svelte b/src/components/Carousel/Carousel.svelte index 6248cbb..91389cd 100644 --- a/src/components/Carousel/Carousel.svelte +++ b/src/components/Carousel/Carousel.svelte @@ -61,6 +61,11 @@ */ export let autoplayDirection = NEXT + /** + * Pause autoplay on focus + */ + export let pauseOnFocus = false + /** * Current page indicator dots */ @@ -79,12 +84,14 @@ let pagesElement let hovered = false - let autoplayIntervals = [] + let autoplayInterval = null $: { - if (hovered) { - clearAutoplay() - } else { - applyAutoplay() + if (pauseOnFocus) { + if (hovered) { + clearAutoplay() + } else { + applyAutoplay() + } } } @@ -107,17 +114,16 @@ } function applyAutoplay() { - if (autoplay) { - let autoplayInterval = setInterval(() => { + if (autoplay && !autoplayInterval) { + autoplayInterval = setInterval(() => { directionFnDescription[autoplayDirection]() }, autoplayDuration) - autoplayIntervals.push(autoplayInterval) } } + function clearAutoplay() { - while(autoplayIntervals.length) { - clearInterval(autoplayIntervals.pop()) - } + clearInterval(autoplayInterval) + autoplayInterval = null } function addClones() { @@ -213,15 +219,10 @@ showPage(currentPageIndex, { offsetDelay: 0, animated: true }) } function handleHovered(event) { - console.log('hovered', event.detail.value) hovered = event.detail.value } -
-hovered={hovered} -
-