-
Notifications
You must be signed in to change notification settings - Fork 1
/
haptics.ts
72 lines (66 loc) · 2.73 KB
/
haptics.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as Haptics from 'expo-haptics';
import { Platform } from 'react-native';
/**
* _[CUSTOMIZE]_
*
* Determines whether to use haptic feedback.
*
* Haptic feedback provides a physical response to user interactions,
* enhancing the user experience in native applications. It is commonly
* used to signify actions such as clicks, selections, or the display of a toast message
*
* Note: iOS devices produce more subtle haptic feedback compared to Android devices. It is now only used on iOS devices by default
*/
const USE_HAPTICS = Platform.OS === 'ios';
export const HapticFeedback = {
impactLight: 'impactLight',
impactMedium: 'impactMedium',
impactHeavy: 'impactHeavy',
selection: 'selection',
notificationSuccess: 'notificationSuccess',
notificationWarning: 'notificationWarning',
notificationError: 'notificationError',
} as const;
export type HapticFeedbackType =
(typeof HapticFeedback)[keyof typeof HapticFeedback];
const createHapticHandler = (type: Haptics.ImpactFeedbackStyle) => {
return () => Haptics.impactAsync(type);
};
const createNotificationFeedback = (type: Haptics.NotificationFeedbackType) => {
return () => Haptics.notificationAsync(type);
};
// Function to trigger haptics based on the feedback type
const hapticToTrigger = (haptic: HapticFeedbackType) => {
if (!USE_HAPTICS || Platform.OS === 'web') return () => {};
switch (haptic) {
case HapticFeedback.impactLight:
return createHapticHandler(Haptics.ImpactFeedbackStyle.Light);
case HapticFeedback.impactMedium:
return createHapticHandler(Haptics.ImpactFeedbackStyle.Medium);
case HapticFeedback.impactHeavy:
return createHapticHandler(Haptics.ImpactFeedbackStyle.Heavy);
case HapticFeedback.selection:
return () => Haptics.selectionAsync();
case HapticFeedback.notificationSuccess:
return createNotificationFeedback(
Haptics.NotificationFeedbackType.Success
);
case HapticFeedback.notificationWarning:
return createNotificationFeedback(
Haptics.NotificationFeedbackType.Warning
);
case HapticFeedback.notificationError:
return createNotificationFeedback(Haptics.NotificationFeedbackType.Error);
default:
return () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
}
};
export const haptics = {
selection: hapticToTrigger(HapticFeedback.selection),
impactLight: hapticToTrigger(HapticFeedback.impactLight),
impactMedium: hapticToTrigger(HapticFeedback.impactMedium),
impactHeavy: hapticToTrigger(HapticFeedback.impactHeavy),
notificationSuccess: hapticToTrigger(HapticFeedback.notificationSuccess),
notificationWarning: hapticToTrigger(HapticFeedback.notificationWarning),
notificationError: hapticToTrigger(HapticFeedback.notificationError),
};