Created
March 18, 2024 18:15
-
-
Save Steellgold/380592375d28df8954f44a9002ed3b92 to your computer and use it in GitHub Desktop.
Double clickable component (react native)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { PropsWithChildren, ReactElement } from "react"; | |
import { useState } from "react"; | |
import { TouchableWithoutFeedback } from "react-native-gesture-handler"; | |
type DoubleClickableViewProps = { | |
onDoubleClick: () => void; | |
delay?: number; | |
} & PropsWithChildren; | |
export const DoubleClickableView = ({ onDoubleClick, delay = 300, children }: DoubleClickableViewProps): ReactElement => { | |
const [lastTap, setLastTap] = useState(null); | |
const handleDoubleTap = (): void => { | |
const now = Date.now(); | |
if (lastTap && (now - lastTap) < delay) { | |
onDoubleClick(); | |
} else { | |
// @ts-ignore | |
setLastTap(now); | |
} | |
}; | |
return ( | |
<TouchableWithoutFeedback onPress={handleDoubleTap}> | |
{children} | |
</TouchableWithoutFeedback> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment