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; align-items: center;
justify-content: center; justify-content: center;
transition: opacity 100ms ease; transition: opacity 100ms ease;
cursor: pointer;
} }
.circle:hover { .circle:hover {
opacity: 0.9; opacity: 0.9;

View File

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