Skip to content

Commit 568bdb3

Browse files
committed
refactor(utils): ♻️ change useHaptic hook to return specific function based on arg passed
1 parent 5274a2c commit 568bdb3

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

src/utils/useHaptic.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
11
import { useCallback, useMemo } from "react";
22
import * as Haptics from "expo-haptics";
33

4-
export const hapticOptions = {
5-
enableVibrateFallback: true,
6-
ignoreAndroidSystemSettings: true,
7-
};
4+
type FeedbackType = "light" | "medium" | "heavy" | "selection";
85

9-
export const useHaptic = () => {
10-
const hapticHeavy = useCallback(
11-
() => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy),
12-
[],
13-
);
14-
const hapticMedium = useCallback(
15-
() => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium),
6+
export const useHaptic = (feedbackType: FeedbackType = "selection") => {
7+
const createHapticHandler = useCallback(
8+
(type: Haptics.ImpactFeedbackStyle) => {
9+
return () => Haptics.impactAsync(type);
10+
},
1611
[],
1712
);
18-
const hapticLight = useCallback(
19-
() => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light),
20-
[],
13+
14+
const hapticHandlers = useMemo(
15+
() => ({
16+
light: createHapticHandler(Haptics.ImpactFeedbackStyle.Light),
17+
medium: createHapticHandler(Haptics.ImpactFeedbackStyle.Medium),
18+
heavy: createHapticHandler(Haptics.ImpactFeedbackStyle.Heavy),
19+
selection: Haptics.selectionAsync,
20+
}),
21+
[createHapticHandler],
2122
);
22-
const hapticSelection = useCallback(() => Haptics.selectionAsync(), []);
23-
const hapticHandlers = useMemo(() => {
24-
return {
25-
hapticLight,
26-
hapticMedium,
27-
hapticHeavy,
28-
hapticSelection,
29-
};
30-
}, [hapticHeavy, hapticLight, hapticMedium, hapticSelection]);
3123

32-
return hapticHandlers;
24+
return hapticHandlers[feedbackType];
3325
};

0 commit comments

Comments
 (0)