Fix children width, init state after reload
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
<script>
|
<script>
|
||||||
// TODO: rename image carousel to just carousel
|
// TODO: rename image carousel to just carousel
|
||||||
|
// TODO: seems CarouselChild component can be removed
|
||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
import { store } from './store'
|
import { store } from './store'
|
||||||
|
|
||||||
|
console.log('init')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable Next/Prev arrows
|
* Enable Next/Prev arrows
|
||||||
*/
|
*/
|
||||||
@@ -18,6 +21,9 @@
|
|||||||
let children
|
let children
|
||||||
let w = 0
|
let w = 0
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
store.reset() // to init after hot reload
|
||||||
|
console.log('mounted!')
|
||||||
|
store.setCurrentItemIndex(0) // to init index after hot reload, check one more time
|
||||||
children = innerContentContainerElement.children
|
children = innerContentContainerElement.children
|
||||||
console.log('children', children.length)
|
console.log('children', children.length)
|
||||||
w = contentContainerElement.clientWidth
|
w = contentContainerElement.clientWidth
|
||||||
@@ -33,7 +39,7 @@
|
|||||||
}
|
}
|
||||||
function handleNextClick() {
|
function handleNextClick() {
|
||||||
store.next({ infinite })
|
store.next({ infinite })
|
||||||
console.log('currentItemId', $store.currentItemId)
|
console.log('children.length', children.length, $store.currentItemIndex)
|
||||||
offset = -$store.currentItemIndex * children[$store.currentItemIndex].clientWidth
|
offset = -$store.currentItemIndex * children[$store.currentItemIndex].clientWidth
|
||||||
console.log('offset', offset, children[$store.currentItemIndex].clientWidth)
|
console.log('offset', offset, children[$store.currentItemIndex].clientWidth)
|
||||||
}
|
}
|
||||||
@@ -80,7 +86,8 @@
|
|||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
.content-container > div {
|
.content-container > div {
|
||||||
display: flex;
|
width: 100%;
|
||||||
|
display: flex; /* to put child elements in one row */
|
||||||
transition: transform 1s ease-in-out;
|
transition: transform 1s ease-in-out;
|
||||||
background-color: chocolate;
|
background-color: chocolate;
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/store.js
30
src/store.js
@@ -2,20 +2,27 @@ import { writable } from 'svelte/store';
|
|||||||
import { v4 as uuid } from 'uuid'
|
import { v4 as uuid } from 'uuid'
|
||||||
import { getNextItemIndexFn, getPrevItemIndexFn } from './utils/item-index'
|
import { getNextItemIndexFn, getPrevItemIndexFn } from './utils/item-index'
|
||||||
|
|
||||||
|
// TODO: try to split writable store items
|
||||||
|
// or try to use immer
|
||||||
|
|
||||||
function createStore() {
|
const initState = {
|
||||||
const { subscribe, set, update } = writable({
|
|
||||||
items: [],
|
items: [],
|
||||||
currentItemId: null,
|
currentItemId: null,
|
||||||
currentItemIndex: null,
|
currentItemIndex: null,
|
||||||
action: 'next'
|
action: 'next'
|
||||||
});
|
}
|
||||||
|
|
||||||
|
function createStore() {
|
||||||
|
const { subscribe, set, update } = writable(initState);
|
||||||
|
|
||||||
|
function reset() {
|
||||||
|
set(initState)
|
||||||
|
}
|
||||||
|
|
||||||
function setItem(id = uuid()) {
|
function setItem(id = uuid()) {
|
||||||
update(store => ({
|
update(store => ({
|
||||||
...store,
|
...store,
|
||||||
currentItemId: id,
|
currentItemId: id, // TODO: seems is not used at all, can be removed
|
||||||
currentItemIndex: 0, // store.items.length - 1, TODO: use as a param
|
|
||||||
items: [
|
items: [
|
||||||
...store.items,
|
...store.items,
|
||||||
id
|
id
|
||||||
@@ -23,6 +30,13 @@ function createStore() {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setCurrentItemIndex(index) {
|
||||||
|
update(store => ({
|
||||||
|
...store,
|
||||||
|
currentItemIndex: index,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
function removeItem(id) {
|
function removeItem(id) {
|
||||||
update(store => ({
|
update(store => ({
|
||||||
...store,
|
...store,
|
||||||
@@ -33,7 +47,9 @@ function createStore() {
|
|||||||
function next({ infinite }) {
|
function next({ infinite }) {
|
||||||
update(store => {
|
update(store => {
|
||||||
const currentItemIndex = store.currentItemIndex // store.items.findIndex(item => item === store.currentItemId)
|
const currentItemIndex = store.currentItemIndex // store.items.findIndex(item => item === store.currentItemId)
|
||||||
|
console.log('next old currentItemIndex', currentItemIndex)
|
||||||
const newCurrentItemIndex = getNextItemIndexFn(infinite)(currentItemIndex, store.items)
|
const newCurrentItemIndex = getNextItemIndexFn(infinite)(currentItemIndex, store.items)
|
||||||
|
// console.log('newCurrentItemIndex', newCurrentItemIndex)
|
||||||
return {
|
return {
|
||||||
...store,
|
...store,
|
||||||
currentItemId: store.items[newCurrentItemIndex],
|
currentItemId: store.items[newCurrentItemIndex],
|
||||||
@@ -61,7 +77,9 @@ function createStore() {
|
|||||||
setItem,
|
setItem,
|
||||||
removeItem,
|
removeItem,
|
||||||
next,
|
next,
|
||||||
prev
|
prev,
|
||||||
|
setCurrentItemIndex,
|
||||||
|
reset
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,28 +18,24 @@
|
|||||||
{arrows}
|
{arrows}
|
||||||
{infinite}
|
{infinite}
|
||||||
>
|
>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
class="child-content-container"
|
class="child-content-container"
|
||||||
style="background-color: green;"
|
style="background-color: green;"
|
||||||
>
|
>
|
||||||
<h1>Element 1</h1>
|
<h1>Element 1</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
class="child-content-container"
|
class="child-content-container"
|
||||||
style="background-color: yellow;"
|
style="background-color: yellow;"
|
||||||
>
|
>
|
||||||
<h1>Element 2</h1>
|
<h1>Element 2</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
class="child-content-container"
|
class="child-content-container"
|
||||||
style="background-color: blue;"
|
style="background-color: blue;"
|
||||||
>
|
>
|
||||||
<h1>Element 3</h1>
|
<h1>Element 3</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</ImageCarousel>
|
</ImageCarousel>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -49,8 +45,9 @@
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
.child-content-container {
|
.child-content-container {
|
||||||
width: 500px;
|
min-width: 100%;
|
||||||
height: 100px;
|
height: 100px;
|
||||||
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user