Skip to content

Avoid using callback refs #12

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
137 changes: 71 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,19 @@ const mouseCoordinatesFromEvent = (e) => {
}

const TinderCard = ({ flickOnSwipe = true, children, onSwipe, onCardLeftScreen, className, preventSwipe = [] }) => {
const swipeAlreadyReleased = React.useRef(false)
const state = React.useRef({
lastLocation: { x: 0, y: 0, time: Date.now() },
mouseIsClicked: false,
offset: { x: null, y: null },
speed: { x: 0, y: 0 },
swipeAlreadyReleased: false,
})

const handleSwipeReleased = React.useCallback(async (element, speed) => {
if (swipeAlreadyReleased.current) { return }
swipeAlreadyReleased.current = true
const handleSwipeReleased = React.useCallback(async (element) => {
if (state.current.swipeAlreadyReleased) return
state.current.swipeAlreadyReleased = true

const { speed } = state.current

// Check if this is a swipe
if (Math.abs(speed.x) > settings.swipeThreshold || Math.abs(speed.y) > settings.swipeThreshold) {
Expand All @@ -138,73 +146,70 @@ const TinderCard = ({ flickOnSwipe = true, children, onSwipe, onCardLeftScreen,

// Card was not flicked away, animate back to start
animateBack(element)
}, [swipeAlreadyReleased, flickOnSwipe, onSwipe, onCardLeftScreen, preventSwipe])

const handleSwipeStart = React.useCallback(() => {
swipeAlreadyReleased.current = false
}, [swipeAlreadyReleased])

const ref = React.useCallback((element) => {
if (!element) { return } // necesarry?
let offset = { x: null, y: null }
let speed = { x: 0, y: 0 }
let lastLocation = { x: 0, y: 0, time: new Date().getTime() }
let mouseIsClicked = false

element.addEventListener(('touchstart'), (ev) => {
ev.preventDefault()
handleSwipeStart()
offset = { x: -touchCoordinatesFromEvent(ev).x, y: -touchCoordinatesFromEvent(ev).y }
})

element.addEventListener(('mousedown'), (ev) => {
ev.preventDefault()
mouseIsClicked = true
handleSwipeStart()
offset = { x: -mouseCoordinatesFromEvent(ev).x, y: -mouseCoordinatesFromEvent(ev).y }
})
}, [state, flickOnSwipe, onSwipe, onCardLeftScreen, preventSwipe])

const onTouchStart = React.useCallback((ev) => {
ev.preventDefault()
state.current.swipeAlreadyReleased = false
state.current.offset = { x: -touchCoordinatesFromEvent(ev).x, y: -touchCoordinatesFromEvent(ev).y }
}, [state])

const onMouseDown = React.useCallback((ev) => {
ev.preventDefault()
state.current.mouseIsClicked = true
state.current.swipeAlreadyReleased = false
state.current.offset = { x: -mouseCoordinatesFromEvent(ev).x, y: -mouseCoordinatesFromEvent(ev).y }
}, [state])

const onTouchMove = React.useCallback((ev) => {
ev.preventDefault()
const { offset, lastLocation } = state.current
const newLocation = dragableTouchmove(touchCoordinatesFromEvent(ev), ev.currentTarget, offset, lastLocation)
state.current.speed = calcSpeed(lastLocation, newLocation)
state.current.lastLocation = newLocation
}, [state])

const onMouseMove = React.useCallback((ev) => {
ev.preventDefault()
if (state.current.mouseIsClicked) {
const { offset, lastLocation } = state.current
const newLocation = dragableTouchmove(mouseCoordinatesFromEvent(ev), ev.currentTarget, offset, lastLocation)
state.current.speed = calcSpeed(lastLocation, newLocation)
state.current.lastLocation = newLocation
}
}, [state])

element.addEventListener(('touchmove'), (ev) => {
ev.preventDefault()
const newLocation = dragableTouchmove(touchCoordinatesFromEvent(ev), element, offset, lastLocation)
speed = calcSpeed(lastLocation, newLocation)
lastLocation = newLocation
})
const onTouchEnd = React.useCallback((ev) => {
ev.preventDefault()
handleSwipeReleased(ev.currentTarget)
}, [handleSwipeReleased])

element.addEventListener(('mousemove'), (ev) => {
const onMouseUp = React.useCallback((ev) => {
if (state.current.mouseIsClicked) {
ev.preventDefault()
if (mouseIsClicked) {
const newLocation = dragableTouchmove(mouseCoordinatesFromEvent(ev), element, offset, lastLocation)
speed = calcSpeed(lastLocation, newLocation)
lastLocation = newLocation
}
})
state.current.mouseIsClicked = true
handleSwipeReleased(ev.currentTarget)
}
}, [state, handleSwipeReleased])

element.addEventListener(('touchend'), (ev) => {
const onMouseLeave = React.useCallback((ev) => {
if (state.current.mouseIsClicked) {
ev.preventDefault()
handleSwipeReleased(element, speed)
})

element.addEventListener(('mouseup'), (ev) => {
if (mouseIsClicked) {
ev.preventDefault()
mouseIsClicked = false
handleSwipeReleased(element, speed)
}
})

element.addEventListener(('mouseleave'), (ev) => {
if (mouseIsClicked) {
ev.preventDefault()
mouseIsClicked = false
handleSwipeReleased(element, speed)
}
})
}, [handleSwipeReleased, handleSwipeStart])

return (
React.createElement('div', { ref, className }, children)
)
state.current.mouseIsClicked = true
handleSwipeReleased(ev.currentTarget)
}
}, [state, handleSwipeReleased])

return React.createElement('div', {
className,
onMouseDown,
onMouseLeave,
onMouseMove,
onMouseUp,
onTouchEnd,
onTouchMove,
onTouchStart
}, children)
}

module.exports = TinderCard