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 option to prevent firing multiple directions #275

Open
wants to merge 1 commit into
base: main
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
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export {
const defaultProps = {
delta: 10,
preventDefaultTouchmoveEvent: false,
preventMixDirections: false,
rotationAngle: 0,
trackMouse: false,
trackTouch: true,
Expand All @@ -47,6 +48,7 @@ const initialState: SwipeableState = {
start: 0,
swiping: false,
xy: [0, 0],
initialDirection: null,
};
const mouseMove = "mousemove";
const mouseUp = "mouseup";
Expand Down Expand Up @@ -108,6 +110,7 @@ function getHandlers(
...state,
...initialState,
initial: [...xy],
initialDirection: null,
xy,
start: event.timeStamp || 0,
};
Expand Down Expand Up @@ -140,7 +143,12 @@ function getHandlers(
? props.delta
: props.delta[dir.toLowerCase() as Lowercase<SwipeDirections>] ||
defaultProps.delta;
if (absX < delta && absY < delta && !state.swiping) return state;
if (absX < delta && absY < delta && !state.swiping)
return { ...state, initialDirection: state.initialDirection || dir };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 i think we'd need to actually set initialDirection outside of the catch for delta. We should probably only count the initial direction once the user has surpassed the delta which signifies the start of a "swipe" or "swiping"


if (props.preventMixDirections && dir !== state.initialDirection) {
return state;
}

const eventData = {
absX,
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type ConfigurationOptionDelta =
export interface ConfigurationOptions {
delta: ConfigurationOptionDelta;
preventDefaultTouchmoveEvent: boolean;
preventMixDirections: boolean;
rotationAngle: number;
trackMouse: boolean;
trackTouch: boolean;
Expand All @@ -70,6 +71,7 @@ export type SwipeableState = {
start: number;
swiping: boolean;
xy: Vector2;
initialDirection: null | SwipeDirections;
};

export type StateSetter = (
Expand Down