-
Notifications
You must be signed in to change notification settings - Fork 1
/
useHaptic.ts
28 lines (24 loc) · 890 Bytes
/
useHaptic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { useCallback, useMemo } from "react";
import { Platform } from "react-native";
import * as Haptics from "expo-haptics";
type FeedbackType = "light" | "medium" | "heavy" | "selection";
export const useHaptic = (feedbackType: FeedbackType = "selection") => {
const createHapticHandler = useCallback(
(type: Haptics.ImpactFeedbackStyle) => {
return Platform.OS === "web"
? undefined
: () => Haptics.impactAsync(type);
},
[],
);
const hapticHandlers = useMemo(
() => ({
light: createHapticHandler(Haptics.ImpactFeedbackStyle.Light),
medium: createHapticHandler(Haptics.ImpactFeedbackStyle.Medium),
heavy: createHapticHandler(Haptics.ImpactFeedbackStyle.Heavy),
selection: Platform.OS === "web" ? undefined : Haptics.selectionAsync,
}),
[createHapticHandler],
);
return hapticHandlers[feedbackType];
};