Skip to content

Commit

Permalink
chore: replace react-native-haptic-feedback with expo-haptics
Browse files Browse the repository at this point in the history
  • Loading branch information
ksitko committed Mar 8, 2021
1 parent 9406935 commit 5fcc0b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
36 changes: 20 additions & 16 deletions src/components/holdItem/HoldItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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)();
}
}

Expand Down
19 changes: 9 additions & 10 deletions src/components/holdItem/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 5fcc0b2

Please sign in to comment.