Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions mobile/services/haptics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as Haptics from 'expo-haptics';
import { Platform } from 'react-native';

type ImpactLevel = 'light' | 'medium' | 'heavy';

function runImpact(level: ImpactLevel) {
if (Platform.OS === 'web') return;
try {
const styleMap = {
light: Haptics.ImpactFeedbackStyle.Light,
medium: Haptics.ImpactFeedbackStyle.Medium,
heavy: Haptics.ImpactFeedbackStyle.Heavy,
};
Haptics.impactAsync(styleMap[level]);
} catch {
// Silently fail if haptics aren't supported (e.g., simulators).
}
}

function runNotification(type: Haptics.NotificationFeedbackType) {
if (Platform.OS === 'web') return;
try {
Haptics.notificationAsync(type);
} catch {
// Silently fail if haptics aren't supported (e.g., simulators).
}
}

export const triggerHapticFeedback = {
light: () => runImpact('light'),
medium: () => runImpact('medium'),
heavy: () => runImpact('heavy'),
selection: () => {
if (Platform.OS === 'web') return;
try {
Haptics.selectionAsync();
} catch {
// Silently fail if haptics aren't supported (e.g., simulators).
}
},
success: () => runNotification(Haptics.NotificationFeedbackType.Success),
warning: () => runNotification(Haptics.NotificationFeedbackType.Warning),
error: () => runNotification(Haptics.NotificationFeedbackType.Error),
};
68 changes: 1 addition & 67 deletions mobile/utils/haptics.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1 @@
import * as Haptics from 'expo-haptics';
import { Platform } from 'react-native';

export const triggerHapticFeedback = {
light: () => {
if (Platform.OS === 'web') return;
try {
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
} catch (error) {
// Silently fail if haptics aren't available (e.g., simulator)
}
},

medium: () => {
if (Platform.OS === 'web') return;
try {
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
} catch (error) {
// Silently fail if haptics aren't available (e.g., simulator)
}
},

heavy: () => {
if (Platform.OS === 'web') return;
try {
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);
} catch (error) {
// Silently fail if haptics aren't available (e.g., simulator)
}
},

selection: () => {
if (Platform.OS === 'web') return;
try {
Haptics.selectionAsync();
} catch (error) {
// Silently fail if haptics aren't available (e.g., simulator)
}
},

success: () => {
if (Platform.OS === 'web') return;
try {
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
} catch (error) {
// Silently fail if haptics aren't available (e.g., simulator)
}
},

error: () => {
if (Platform.OS === 'web') return;
try {
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
} catch (error) {
// Silently fail if haptics aren't available (e.g., simulator)
}
},

warning: () => {
if (Platform.OS === 'web') return;
try {
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning);
} catch (error) {
// Silently fail if haptics aren't available (e.g., simulator)
}
}
};
export { triggerHapticFeedback } from '../services/haptics';