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/
/public/build/
dist
/storybook-static
.DS_Store

View File

@@ -61,6 +61,11 @@
*/
export let autoplayDirection = NEXT
/**
* Pause autoplay on focus
*/
export let pauseOnFocus = false
/**
* Current page indicator dots
*/
@@ -79,14 +84,16 @@
let pagesElement
let hovered = false
let autoplayIntervals = []
let autoplayInterval = null
$: {
if (pauseOnFocus) {
if (hovered) {
clearAutoplay()
} else {
applyAutoplay()
}
}
}
// used for lazy loading images, preloaded only current, adjacent and cloanable images
$: loaded = getAdjacentIndexes(originalCurrentPageIndex, originalPagesCount, infinite)
@@ -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
}
</script>
<div>
hovered={hovered}
</div>
<div class="sc-carousel__carousel-container">
<div class="sc-carousel__content-container">
{#if arrows}

View File

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

View File

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

View File

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