Skip to content
Closed
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: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
TapCallback,
UP,
Vector2,
OnSwipedDirs,
} from "./types";

export {
Expand Down Expand Up @@ -183,10 +184,8 @@ function getHandlers(
eventData = { ...state.eventData, event };
props.onSwiped && props.onSwiped(eventData);

const onSwipedDir = `onSwiped${eventData.dir}`;
if (onSwipedDir in props) {
((props as any)[onSwipedDir] as SwipeCallback)(eventData);
}
const onSwipedDir = `onSwiped${eventData.dir}` as OnSwipedDirs;
props[onSwipedDir]?.(eventData);
} else {
props.onTap && props.onTap({ event });
}
Expand Down
11 changes: 6 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type SwipeDirections =
| typeof RIGHT
| typeof UP
| typeof DOWN;
export type OnSwipedDirs = `onSwiped${SwipeDirections}`;
export interface SwipeEventData {
absX: number;
absY: number;
Expand All @@ -27,13 +28,13 @@ export interface SwipeEventData {
export type SwipeCallback = (eventData: SwipeEventData) => void;
export type TapCallback = ({ event }: { event: HandledEvents }) => void;

export type SwipeableCallbacks = {
type SwipedCallbacks = {
[K in OnSwipedDirs]?: SwipeCallback;
}

export type SwipeableCallbacks = SwipedCallbacks & {
// Event handler/callbacks
onSwiped: SwipeCallback;
onSwipedDown: SwipeCallback;
onSwipedLeft: SwipeCallback;
onSwipedRight: SwipeCallback;
onSwipedUp: SwipeCallback;
onSwiping: SwipeCallback;
onTap: TapCallback;
};
Expand Down