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

[feat]: prevent move in selected axis #166

Open
wants to merge 1 commit 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 @@ -40,6 +40,13 @@ declare interface Props {
*/
onCardLeftScreen?: CardLeftScreenHandler

/**
* An array specifying the axes in which movement is prevented. Valid arguments are `'xAxis'`, `'yAxis'``.
*
* @default []
*/
preventMove?: string[];

/**
* An array of directions for which to prevent swiping out of screen. Valid arguments are `'left'`, `'right'`, `'up'` and `'down'`.
*
Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const AnimatedDiv = animated.div

const TinderCard = React.forwardRef(
(
{ flickOnSwipe = true, children, onSwipe, onCardLeftScreen, className, preventSwipe = [], swipeRequirementType = 'velocity', swipeThreshold = settings.swipeThreshold, onSwipeRequirementFulfilled, onSwipeRequirementUnfulfilled },
{ flickOnSwipe = true, children, onSwipe, onCardLeftScreen, className, preventMove = [], preventSwipe = [], swipeRequirementType = 'velocity', swipeThreshold = settings.swipeThreshold, onSwipeRequirementFulfilled, onSwipeRequirementUnfulfilled },
ref
) => {
const { width, height } = useWindowSize()
Expand Down Expand Up @@ -213,7 +213,14 @@ const TinderCard = React.forwardRef(
let rot = gestureState.vx * 15 // Magic number 15 looks about right
if (isNaN(rot)) rot = 0
rot = Math.max(Math.min(rot, settings.maxTilt), -settings.maxTilt)
setSpringTarget.start({ xyrot: [gestureState.dx, gestureState.dy, rot], config: physics.touchResponsive })
setSpringTarget.start({
xyrot: [
preventMove.includes("xAxis") ? 0 : gestureState.dx,
preventMove.includes("yAxis") ? 0 : gestureState.dy,
rot,
],
config: physics.touchResponsive,
})
}

const onMouseMove = (ev) => {
Expand Down
22 changes: 16 additions & 6 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, preventMove = [], preventSwipe = [], swipeRequirementType = 'velocity', swipeThreshold = settings.swipeThreshold, onSwipeRequirementFulfilled, onSwipeRequirementUnfulfilled },
ref
) => {
const [{ x, y, rot }, setSpringTarget] = useSpring(() => ({
Expand Down Expand Up @@ -186,13 +186,23 @@ const TinderCard = React.forwardRef(
}
}
}

// use guestureState.vx / guestureState.vy for velocity calculations

// Limit movement based on preventMove parameter
let newX = gestureState.dx;
let newY = gestureState.dy;

if (preventMove === 'xAxis') {
newY = 0; // Block movement along the y-axis
} else if (preventMove === 'yAxis') {
newX = 0; // Block movement along the x-axis
}

// use gestureState.vx / gestureState.vy for velocity calculations
// translate element
let rot = ((300 * gestureState.vx) / width) * 15// Magic number 300 different on different devices? Run on physical device!
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)
setSpringTarget.current[0].start({ x: gestureState.dx, y: gestureState.dy, rot, config: physics.touchResponsive })
},
setSpringTarget.current[0].start({ x: newX, y: newY, rot, config: physics.touchResponsive })
},
onPanResponderTerminationRequest: (evt, gestureState) => {
return true
},
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ Callback that will be executed when a swipe has been completed. It will be calle

Callback that will be executed when a `TinderCard` has left the screen. It will be called with a single string denoting which direction the swipe was in: `'left'`, `'right'`, `'up'` or `'down'`.

### `preventMove`

- optional
- type: `Array<string>`
- default: `[]`

An array specifying the axes in which movement is prevented. Valid arguments are `'xAxis'`, `'yAxis'`.

### `preventSwipe`

- optional
Expand Down