From 5fcc0b2b144b9ea93debafe05f80a244b91d1c99 Mon Sep 17 00:00:00 2001 From: ksitko Date: Mon, 8 Mar 2021 10:49:44 -0500 Subject: [PATCH] chore: replace react-native-haptic-feedback with expo-haptics --- package.json | 1 + src/components/holdItem/HoldItem.tsx | 36 +++++++++++++++------------- src/components/holdItem/types.d.ts | 19 +++++++-------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index baa507e..f842496 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "dependencies": { "@gorhom/portal": "^1.0.3", "expo-blur": "^9.0.0", + "expo-haptics": "^9.0.0", "lodash.isequal": "^4.5.0", "nanoid": "^3.1.20" }, diff --git a/src/components/holdItem/HoldItem.tsx b/src/components/holdItem/HoldItem.tsx index 9d11eac..86d9660 100644 --- a/src/components/holdItem/HoldItem.tsx +++ b/src/components/holdItem/HoldItem.tsx @@ -23,11 +23,7 @@ import Animated, { useAnimatedReaction, } from 'react-native-reanimated'; -// Optional Haptic Feedback -let ReactNativeHaptic: any; -try { - ReactNativeHaptic = require('react-native-haptic-feedback').default; -} catch (error) {} +import * as Haptics from 'expo-haptics'; // Utils import { @@ -164,13 +160,23 @@ const HoldItemComponent = ({ }; const hapticResponse = () => { - const haptic = - !hapticFeedback || hapticFeedback === 'enabled' - ? 'impactMedium' - : hapticFeedback; - ReactNativeHaptic.trigger(haptic, { - enableVibrateFallback: true, - }); + const style = !hapticFeedback ? 'Medium' : hapticFeedback; + switch (style) { + case `Selection`: + Haptics.selectionAsync(); + break; + case `Light`: + case `Medium`: + case `Heavy`: + Haptics.impactAsync(Haptics.ImpactFeedbackStyle[style]); + break; + case `Success`: + case `Warning`: + case `Error`: + Haptics.notificationAsync(Haptics.NotificationFeedbackType[style]); + break; + default: + } }; const onCompletion = (isFinised: boolean) => { @@ -180,10 +186,8 @@ const HoldItemComponent = ({ state.value = CONTEXT_MENU_STATE.ACTIVE; isActive.value = true; scaleBack(); - if (hapticFeedback !== 'none') { - if (ReactNativeHaptic) { - runOnJS(hapticResponse)(); - } + if (hapticFeedback !== 'None') { + runOnJS(hapticResponse)(); } } diff --git a/src/components/holdItem/types.d.ts b/src/components/holdItem/types.d.ts index befeb88..4419866 100644 --- a/src/components/holdItem/types.d.ts +++ b/src/components/holdItem/types.d.ts @@ -93,20 +93,19 @@ export type HoldItemProps = { /** * Set if you'd like to enable haptic feedback on activation * @type string - * @default 'impactMedium' + * @default 'Medium' * @examples * hapticFeedback="none" */ hapticFeedback?: - | 'enabled' - | 'none' - | 'selection' - | 'impactLight' - | 'impactMedium' - | 'impactHeavy' - | 'notificationSuccess' - | 'notificationWarning' - | 'notificationError'; + | 'None' + | 'Selection' + | 'Light' + | 'Medium' + | 'Heavy' + | 'Success' + | 'Warning' + | 'Error'; }; export type GestureHandlerProps = {