Skip to content

Commit

Permalink
feat: add optional haptic feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ksitko committed Mar 8, 2021
1 parent 080d18e commit 9406935
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/components/holdItem/HoldItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'react-native-gesture-handler';
import Animated, {
measure,
runOnJS,
useAnimatedGestureHandler,
useAnimatedProps,
useAnimatedRef,
Expand All @@ -22,6 +23,12 @@ import Animated, {
useAnimatedReaction,
} from 'react-native-reanimated';

// Optional Haptic Feedback
let ReactNativeHaptic: any;
try {
ReactNativeHaptic = require('react-native-haptic-feedback').default;
} catch (error) {}

// Utils
import {
TransformOriginAnchorPosition,
Expand Down Expand Up @@ -55,6 +62,7 @@ const HoldItemComponent = ({
disableMove,
menuAnchorPosition,
activateOn,
hapticFeedback,
methodParams,
children,
}: HoldItemProps) => {
Expand Down Expand Up @@ -155,13 +163,28 @@ const HoldItemComponent = ({
});
};

const hapticResponse = () => {
const haptic =
!hapticFeedback || hapticFeedback === 'enabled'
? 'impactMedium'
: hapticFeedback;
ReactNativeHaptic.trigger(haptic, {
enableVibrateFallback: true,
});
};

const onCompletion = (isFinised: boolean) => {
'worklet';
const isListValid = items && items.length > 0;
if (isFinised && isListValid) {
state.value = CONTEXT_MENU_STATE.ACTIVE;
isActive.value = true;
scaleBack();
if (hapticFeedback !== 'none') {
if (ReactNativeHaptic) {
runOnJS(hapticResponse)();
}
}
}

isAnimationStarted.value = false;
Expand Down
18 changes: 18 additions & 0 deletions src/components/holdItem/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ export type HoldItemProps = {
* activateOn="hold"
*/
activateOn?: 'tap' | 'double-tap' | 'hold';

/**
* Set if you'd like to enable haptic feedback on activation
* @type string
* @default 'impactMedium'
* @examples
* hapticFeedback="none"
*/
hapticFeedback?:
| 'enabled'
| 'none'
| 'selection'
| 'impactLight'
| 'impactMedium'
| 'impactHeavy'
| 'notificationSuccess'
| 'notificationWarning'
| 'notificationError';
};

export type GestureHandlerProps = {
Expand Down

0 comments on commit 9406935

Please sign in to comment.