Add pauseOnFocus prop, update storybook, update autoplayInterval logic

This commit is contained in:
Vadim
2021-04-20 22:20:21 +03:00
parent 29d012dce0
commit ba696c275b
5 changed files with 41 additions and 19 deletions

1
.gitignore vendored
View File

@@ -1,5 +1,6 @@
/node_modules/ /node_modules/
/public/build/ /public/build/
dist dist
/storybook-static
.DS_Store .DS_Store

View File

@@ -61,6 +61,11 @@
*/ */
export let autoplayDirection = NEXT export let autoplayDirection = NEXT
/**
* Pause autoplay on focus
*/
export let pauseOnFocus = false
/** /**
* Current page indicator dots * Current page indicator dots
*/ */
@@ -79,12 +84,14 @@
let pagesElement let pagesElement
let hovered = false let hovered = false
let autoplayIntervals = [] let autoplayInterval = null
$: { $: {
if (hovered) { if (pauseOnFocus) {
clearAutoplay() if (hovered) {
} else { clearAutoplay()
applyAutoplay() } else {
applyAutoplay()
}
} }
} }
@@ -107,17 +114,16 @@
} }
function applyAutoplay() { function applyAutoplay() {
if (autoplay) { if (autoplay && !autoplayInterval) {
let autoplayInterval = setInterval(() => { autoplayInterval = setInterval(() => {
directionFnDescription[autoplayDirection]() directionFnDescription[autoplayDirection]()
}, autoplayDuration) }, autoplayDuration)
autoplayIntervals.push(autoplayInterval)
} }
} }
function clearAutoplay() { function clearAutoplay() {
while(autoplayIntervals.length) { clearInterval(autoplayInterval)
clearInterval(autoplayIntervals.pop()) autoplayInterval = null
}
} }
function addClones() { function addClones() {
@@ -213,15 +219,10 @@
showPage(currentPageIndex, { offsetDelay: 0, animated: true }) showPage(currentPageIndex, { offsetDelay: 0, animated: true })
} }
function handleHovered(event) { function handleHovered(event) {
console.log('hovered', event.detail.value)
hovered = event.detail.value hovered = event.detail.value
} }
</script> </script>
<div>
hovered={hovered}
</div>
<div class="sc-carousel__carousel-container"> <div class="sc-carousel__carousel-container">
<div class="sc-carousel__content-container"> <div class="sc-carousel__content-container">
{#if arrows} {#if arrows}

View File

@@ -41,6 +41,11 @@
* Auto play change direction ('next', 'prev') * Auto play change direction ('next', 'prev')
*/ */
export let autoplayDirection = NEXT export let autoplayDirection = NEXT
/**
* Pause autoplay on focus
*/
export let pauseOnFocus = false
/** /**
* Current page indicator dots * Current page indicator dots
@@ -69,6 +74,7 @@
<div class="main-container"> <div class="main-container">
<Carousel <Carousel
{timingFunction}
{arrows} {arrows}
{infinite} {infinite}
{initialPageIndex} {initialPageIndex}
@@ -76,8 +82,8 @@
{autoplay} {autoplay}
{autoplayDuration} {autoplayDuration}
{autoplayDirection} {autoplayDirection}
{pauseOnFocus}
{dots} {dots}
{timingFunction}
on:pageChange={ on:pageChange={
event => console.log(`Current page index: ${event.detail}`) event => console.log(`Current page index: ${event.detail}`)
} }
@@ -93,6 +99,7 @@
</Carousel> </Carousel>
<Carousel <Carousel
{timingFunction}
{arrows} {arrows}
{infinite} {infinite}
{initialPageIndex} {initialPageIndex}
@@ -100,6 +107,7 @@
{autoplay} {autoplay}
{autoplayDuration} {autoplayDuration}
{autoplayDirection} {autoplayDirection}
{pauseOnFocus}
{dots} {dots}
> >
{#each colors2 as { color, text } (color)} {#each colors2 as { color, text } (color)}

View File

@@ -42,6 +42,11 @@
*/ */
export let autoplayDirection = NEXT export let autoplayDirection = NEXT
/**
* Pause autoplay on focus
*/
export let pauseOnFocus = false
/** /**
* Current page indicator dots * Current page indicator dots
*/ */
@@ -63,6 +68,7 @@
<div class="main-container"> <div class="main-container">
<Carousel <Carousel
{timingFunction}
{arrows} {arrows}
{infinite} {infinite}
{initialPageIndex} {initialPageIndex}
@@ -70,8 +76,8 @@
{autoplay} {autoplay}
{autoplayDuration} {autoplayDuration}
{autoplayDirection} {autoplayDirection}
{pauseOnFocus}
{dots} {dots}
{timingFunction}
let:showPrevPage let:showPrevPage
let:showNextPage let:showNextPage
> >

View File

@@ -42,6 +42,11 @@
*/ */
export let autoplayDirection = NEXT export let autoplayDirection = NEXT
/**
* Pause autoplay on focus
*/
export let pauseOnFocus = false
/** /**
* Current page indicator dots * Current page indicator dots
*/ */
@@ -67,6 +72,7 @@
<div class="main-container"> <div class="main-container">
<Carousel <Carousel
{timingFunction}
{arrows} {arrows}
{infinite} {infinite}
{initialPageIndex} {initialPageIndex}
@@ -74,8 +80,8 @@
{autoplay} {autoplay}
{autoplayDuration} {autoplayDuration}
{autoplayDirection} {autoplayDirection}
{pauseOnFocus}
{dots} {dots}
{timingFunction}
let:currentPageIndex let:currentPageIndex
let:pagesCount let:pagesCount
let:showPage let:showPage