From 94069353d09f1c2fdeb47633a4b7b3e431ed986a Mon Sep 17 00:00:00 2001 From: ksitko Date: Sun, 7 Mar 2021 22:18:37 -0500 Subject: [PATCH] feat: add optional haptic feedback --- src/components/holdItem/HoldItem.tsx | 23 +++++++++++++++++++++++ src/components/holdItem/types.d.ts | 18 ++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/components/holdItem/HoldItem.tsx b/src/components/holdItem/HoldItem.tsx index e5ed881..9d11eac 100644 --- a/src/components/holdItem/HoldItem.tsx +++ b/src/components/holdItem/HoldItem.tsx @@ -10,6 +10,7 @@ import { } from 'react-native-gesture-handler'; import Animated, { measure, + runOnJS, useAnimatedGestureHandler, useAnimatedProps, useAnimatedRef, @@ -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, @@ -55,6 +62,7 @@ const HoldItemComponent = ({ disableMove, menuAnchorPosition, activateOn, + hapticFeedback, methodParams, children, }: HoldItemProps) => { @@ -155,6 +163,16 @@ 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; @@ -162,6 +180,11 @@ const HoldItemComponent = ({ state.value = CONTEXT_MENU_STATE.ACTIVE; isActive.value = true; scaleBack(); + if (hapticFeedback !== 'none') { + if (ReactNativeHaptic) { + runOnJS(hapticResponse)(); + } + } } isAnimationStarted.value = false; diff --git a/src/components/holdItem/types.d.ts b/src/components/holdItem/types.d.ts index 7417110..befeb88 100644 --- a/src/components/holdItem/types.d.ts +++ b/src/components/holdItem/types.d.ts @@ -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 = {