Use subscription

This commit is contained in:
Vadim
2021-01-22 20:20:17 +03:00
parent eac2574586
commit 92d7ee043a
2 changed files with 14 additions and 5 deletions

View File

@@ -29,6 +29,7 @@
align-items: center;
justify-content: center;
transition: opacity 100ms ease;
cursor: pointer;
}
.circle:hover {
opacity: 0.9;

View File

@@ -1,8 +1,7 @@
<script>
// TODO: rename image carousel to just carousel
// TODO: subscribe on mount and unsubscribe on destroy to
// $store.currentPageIndex to avoid multiple subscriptions
import { onMount } from 'svelte'
import { onDestroy, onMount } from 'svelte'
import { store } from '../store'
import {
getPageIndex,
@@ -59,12 +58,17 @@
*/
export let dots = true
let currentPageIndex = 0
let pagesCount = 0
let pageWidth = 0
let offset
let contentContainerElement
let innerContentContainerElement
const unsubscribe = store.subscribe(value => {
currentPageIndex = value.currentPageIndex
})
function applySlideSizes() {
const children = innerContentContainerElement.children
pageWidth = contentContainerElement.clientWidth
@@ -113,12 +117,16 @@
}
})
onDestroy(() => {
unsubscribe()
})
function handlePageChange(event) {
showPage(event.detail)
}
function applyOffset() {
offset = -$store.currentPageIndex * pageWidth
offset = -currentPageIndex * pageWidth
}
function showPage(pageIndex) {
@@ -169,13 +177,13 @@
{#if dots}
<slot
name="dots"
currentPageIndex={$store.currentPageIndex}
currentPageIndex={currentPageIndex}
{pagesCount}
{showPage}
>
<Dots
{pagesCount}
currentPageIndex={$store.currentPageIndex}
currentPageIndex={currentPageIndex}
on:pageChange={handlePageChange}
></Dots>
</slot>