#49 : Dispatch and handle swipe failed

This commit is contained in:
Vadim
2021-08-06 23:32:41 +03:00
parent 2ac5a9e190
commit 8243883612
2 changed files with 18 additions and 8 deletions

View File

@@ -70,13 +70,17 @@ export function swipeable(node, { thresholdProvider }) {
} }
function handleMouseup(event) { function handleMouseup(event) {
isTouching = false
if (!isValidSwipe()) return
const coords = getCoords(event)
dispatch('end', { x: coords.x, y: coords.y })
removeEndEventListener(window, handleMouseup) removeEndEventListener(window, handleMouseup)
removeMoveEventListener(window, handleMousemove) removeMoveEventListener(window, handleMousemove)
isTouching = false
if (!isValidSwipe()) {
dispatch('swipeFailed')
return
}
const coords = getCoords(event)
dispatch('end', { x: coords.x, y: coords.y })
} }
addStartEventListener(node, handleMousedown) addStartEventListener(node, handleMousedown)

View File

@@ -168,7 +168,7 @@
children[pageIndex].style.maxWidth = `${pageWidth}px` children[pageIndex].style.maxWidth = `${pageWidth}px`
} }
offsetPage(false) offsetPage({ animated: false })
} }
function addClones() { function addClones() {
@@ -228,7 +228,8 @@
await showPage(pageIndex + Number(infinite)) await showPage(pageIndex + Number(infinite))
} }
function offsetPage(animated) { function offsetPage(options) {
const animated = get(options, 'animated', true)
return new Promise((resolve) => { return new Promise((resolve) => {
// _duration is an offset animation time // _duration is an offset animation time
_duration = animated ? duration : 0 _duration = animated ? duration : 0
@@ -262,7 +263,7 @@
disabled = true disabled = true
updateStoreFn() updateStoreFn()
await offsetPage(get(options, 'animated', true)) await offsetPage({ animated: get(options, 'animated', true) })
disabled = false disabled = false
const jumped = await jumpIfNeeded() const jumped = await jumpIfNeeded()
@@ -305,6 +306,10 @@
if (!swiping) return if (!swiping) return
showPage(currentPageIndex) showPage(currentPageIndex)
} }
async function handleSwipeFailed() {
if (!swiping) return
await offsetPage({ animated: true })
}
function handlePausedToggle(event) { function handlePausedToggle(event) {
if (event.detail.isTouchable) { if (event.detail.isTouchable) {
@@ -341,6 +346,7 @@
on:start={handleSwipeStart} on:start={handleSwipeStart}
on:move={handleSwipeMove} on:move={handleSwipeMove}
on:end={handleSwipeEnd} on:end={handleSwipeEnd}
on:swipeFailed={handleSwipeFailed}
on:threshold={handleThreshold} on:threshold={handleThreshold}
style=" style="
transform: translateX({offset}px); transform: translateX({offset}px);