|
1 | 1 | import { useCallback, useMemo } from "react"; |
2 | 2 | import * as Haptics from "expo-haptics"; |
3 | 3 |
|
4 | | -export const hapticOptions = { |
5 | | - enableVibrateFallback: true, |
6 | | - ignoreAndroidSystemSettings: true, |
7 | | -}; |
| 4 | +type FeedbackType = "light" | "medium" | "heavy" | "selection"; |
8 | 5 |
|
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 | + }, |
16 | 11 | [], |
17 | 12 | ); |
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], |
21 | 22 | ); |
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]); |
31 | 23 |
|
32 | | - return hapticHandlers; |
| 24 | + return hapticHandlers[feedbackType]; |
33 | 25 | }; |
0 commit comments