Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix multiple call onCardLeftScreen and onSwipe when one card left the screen #161

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,26 @@ const TinderCard = React.forwardRef(
let lastPosition = { dx: 0, dy: 0, vx: 0, vy: 0, timeStamp: Date.now() }
let isClicking = false

element.current.addEventListener(('touchstart'), (ev) => {
const onTouchStart = (ev) => {
if (!ev.srcElement.className.includes('pressable') && ev.cancelable) {
ev.preventDefault()
}

const gestureState = gestureStateFromWebEvent(ev, startPositon, lastPosition, true)
lastPosition = gestureState
startPositon = { x: ev.touches[0].clientX, y: ev.touches[0].clientY }
})
}

element.current.addEventListener(('touchstart'), onTouchStart)

element.current.addEventListener(('mousedown'), (ev) => {
const onMouseDown = (ev) => {
isClicking = true
const gestureState = gestureStateFromWebEvent(ev, startPositon, lastPosition, false)
lastPosition = gestureState
startPositon = { x: ev.clientX, y: ev.clientY }
})
}

element.current.addEventListener(('mousedown'), onMouseDown)

const handleMove = (gestureState) => {
// Check fulfillment
Expand All @@ -212,33 +216,50 @@ const TinderCard = React.forwardRef(
setSpringTarget.start({ xyrot: [gestureState.dx, gestureState.dy, rot], config: physics.touchResponsive })
}

window.addEventListener(('mousemove'), (ev) => {
const onMouseMove = (ev) => {
if (!isClicking) return
const gestureState = gestureStateFromWebEvent(ev, startPositon, lastPosition, false)
lastPosition = gestureState
handleMove(gestureState)
})
}

window.addEventListener(('mouseup'), (ev) => {
window.addEventListener(('mousemove'), onMouseMove)

const onMouseUp = (ev) => {
if (!isClicking) return
isClicking = false
handleSwipeReleased(setSpringTarget, lastPosition)
startPositon = { x: 0, y: 0 }
lastPosition = { dx: 0, dy: 0, vx: 0, vy: 0, timeStamp: Date.now() }
})
}

window.addEventListener(('mouseup'), onMouseUp)

element.current.addEventListener(('touchmove'), (ev) => {
const onTouchMove = (ev) => {
const gestureState = gestureStateFromWebEvent(ev, startPositon, lastPosition, true)
lastPosition = gestureState
handleMove(gestureState)
})
}

element.current.addEventListener(('touchmove'), onTouchMove)

element.current.addEventListener(('touchend'), (ev) => {
const onTouchEnd = (ev) => {
handleSwipeReleased(setSpringTarget, lastPosition)
startPositon = { x: 0, y: 0 }
lastPosition = { dx: 0, dy: 0, vx: 0, vy: 0, timeStamp: Date.now() }
})
})
}

element.current.addEventListener(('touchend'), onTouchEnd)

return () => {
element.current.removeEventListener(('touchstart'), onTouchStart)
element.current.removeEventListener(('touchmove'), onTouchMove)
element.current.removeEventListener(('touchend'), onTouchEnd)
window.removeEventListener(('mousemove'), onMouseMove)
window.removeEventListener(('mouseup'), onMouseUp)
element.current.removeEventListener(('mousedown'), onMouseDown)
}
}, [handleSwipeReleased, setSpringTarget, onSwipeRequirementFulfilled, onSwipeRequirementUnfulfilled])

const element = React.useRef()

Expand Down
Loading