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

Add rotateOnDrag prop to give ability to disable rotation on drag #73

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ declare interface Props {
*/
onSwipeRequirementUnfulfilled?: SwipeRequirementUnfufillUpdate

/**
* Multiplier used to reduce the rotate effect while dragging (when set between 0 - 1). When set to `0`, rotate will be disabled rotation all together. Values greater than 1 are valid, however, this may lead to undesirable effects.
*
* @default 1
*/
rotateMultiplier?: number

/**
* HTML attribute class
*/
Expand Down
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ const animateBack = async (element) => {
element.style.transform = translation + rotation

await sleep(settings.snapBackDuration * 0.75)
element.style.transform = 'none'
element.style.removeProperty('transform')

await sleep(settings.snapBackDuration)
element.style.transition = '10ms'
element.style.removeProperty('transition')
}

const getSwipeDirection = (property) => {
Expand Down Expand Up @@ -123,12 +123,16 @@ const getRotation = (element) => {
return ans
}

const dragableTouchmove = (coordinates, element, offset, lastLocation) => {
const dragableTouchmove = (coordinates, element, offset, lastLocation, rotateMultiplier) => {
const pos = { x: coordinates.x + offset.x, y: coordinates.y + offset.y }
const newLocation = { x: pos.x, y: pos.y, time: new Date().getTime() }
const translation = translationString(pos.x, pos.y)
const rotCalc = calcSpeed(lastLocation, newLocation).x / 1000
const rotation = rotationString(rotCalc * settings.maxTilt)
let rotation = ''
if (rotateMultiplier) {
const rotCalc = calcSpeed(lastLocation, newLocation).x / 1000
rotation = rotationString(rotCalc * settings.maxTilt * rotateMultiplier)
}

element.style.transform = translation + rotation
return newLocation
}
Expand All @@ -142,7 +146,7 @@ const mouseCoordinatesFromEvent = (e) => {
return { x: e.clientX, y: e.clientY }
}

const TinderCard = React.forwardRef(({ flickOnSwipe = true, children, onSwipe, onCardLeftScreen, className, preventSwipe = [], swipeRequirementType = 'velocity', swipeThreshold = settings.swipeThreshold, onSwipeRequirementFulfilled, onSwipeRequirementUnfulfilled }, ref) => {
const TinderCard = React.forwardRef(({ flickOnSwipe = true, children, onSwipe, onCardLeftScreen, className, preventSwipe = [], swipeRequirementType = 'velocity', swipeThreshold = settings.swipeThreshold, onSwipeRequirementFulfilled, onSwipeRequirementUnfulfilled, rotateMultiplier = 1 }, ref) => {
settings.swipeThreshold = swipeThreshold
const swipeAlreadyReleased = React.useRef(false)

Expand All @@ -166,7 +170,7 @@ const TinderCard = React.forwardRef(({ flickOnSwipe = true, children, onSwipe, o
if (onCardLeftScreen) onCardLeftScreen(dir)
},
async restoreCard () {
element.current.style.display = 'block'
element.current.style.removeProperty('display')
await animateBack(element.current)
}
}))
Expand Down Expand Up @@ -236,7 +240,7 @@ const TinderCard = React.forwardRef(({ flickOnSwipe = true, children, onSwipe, o
}

// Move
const newLocation = dragableTouchmove(coordinates, element.current, offset, lastLocation)
const newLocation = dragableTouchmove(coordinates, element.current, offset, lastLocation, rotateMultiplier)
speed = calcSpeed(lastLocation, newLocation)
lastLocation = newLocation
}
Expand Down
9 changes: 6 additions & 3 deletions index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const AnimatedView = animated(View)

const TinderCard = React.forwardRef(
(
{ flickOnSwipe = true, children, onSwipe, onCardLeftScreen, className, preventSwipe = [], swipeRequirementType = 'velocity', swipeThreshold = settings.swipeThreshold, onSwipeRequirementFulfilled, onSwipeRequirementUnfulfilled },
{ flickOnSwipe = true, children, onSwipe, onCardLeftScreen, className, preventSwipe = [], swipeRequirementType = 'velocity', swipeThreshold = settings.swipeThreshold, onSwipeRequirementFulfilled, onSwipeRequirementUnfulfilled, rotateMultiplier = 1 },
ref
) => {
const [{ x, y, rot }, setSpringTarget] = useSpring(() => ({
Expand Down Expand Up @@ -183,8 +183,11 @@ const TinderCard = React.forwardRef(

// use guestureState.vx / guestureState.vy for velocity calculations
// translate element
let rot = ((300 * gestureState.vx) / width) * 15// Magic number 300 different on different devices? Run on physical device!
rot = Math.max(Math.min(rot, settings.maxTilt), -settings.maxTilt)
let rot = 0
if (rotateMultiplier) {
rot = ((300 * gestureState.vx) / width) * 15// Magic number 300 different on different devices? Run on physical device!
rot = Math.max(Math.min(rot, settings.maxTilt), -settings.maxTilt) * rotateMultiplier
}
setSpringTarget({ x: gestureState.dx, y: gestureState.dy, rot, config: physics.touchResponsive })
},
onPanResponderTerminationRequest: (evt, gestureState) => {
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ It will be called with a single string denoting which direction the user is swip

Callback that will be executed when a `TinderCard` has unfulfilled the requirement necessary to be swiped in a direction on release.

### `rotateMultiplier`

- optional
- type: `number`
- default: `1`

Multiplier used to reduce the rotate effect while dragging (when set between 0 - 1). When set to `0`, rotate will be disabled rotation all together. Values greater than 1 are valid, however, this may lead to undesirable effects.

### `className`

- optional
Expand Down