new haptics hook
This commit is contained in:
+32
-13
@@ -1,14 +1,39 @@
|
|||||||
import React from 'react'
|
import {useCallback, useMemo} from 'react'
|
||||||
import * as Device from 'expo-device'
|
import * as ExpoHaptics from 'expo-haptics'
|
||||||
import {impactAsync, ImpactFeedbackStyle} from 'expo-haptics'
|
|
||||||
|
|
||||||
import {isIOS, isWeb} from '#/platform/detection'
|
import {isIOS, isWeb} from '#/platform/detection'
|
||||||
import {useHapticsDisabled} from '#/state/preferences/disable-haptics'
|
import {useHapticsDisabled} from '#/state/preferences/disable-haptics'
|
||||||
|
|
||||||
|
export function useHapticFeedback() {
|
||||||
|
const isHapticsDisabled = useHapticsDisabled()
|
||||||
|
|
||||||
|
return useMemo(() => {
|
||||||
|
if (isHapticsDisabled || isWeb) {
|
||||||
|
return {impact: () => {}, notification: () => {}}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
impact: (strength: 'Light' | 'Medium' | 'Heavy' = 'Medium') => {
|
||||||
|
// Users said the medium impact was too strong on Android; see APP-537s
|
||||||
|
const style = isIOS
|
||||||
|
? ExpoHaptics.ImpactFeedbackStyle[strength]
|
||||||
|
: ExpoHaptics.ImpactFeedbackStyle.Light
|
||||||
|
ExpoHaptics.impactAsync(style)
|
||||||
|
},
|
||||||
|
notification: (type: ExpoHaptics.NotificationFeedbackType) => {
|
||||||
|
ExpoHaptics.notificationAsync(type)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}, [isHapticsDisabled])
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated use `haptics.impact()` from `useHapticFeedback()` instead
|
||||||
|
*/
|
||||||
export function useHaptics() {
|
export function useHaptics() {
|
||||||
const isHapticsDisabled = useHapticsDisabled()
|
const isHapticsDisabled = useHapticsDisabled()
|
||||||
|
|
||||||
return React.useCallback(
|
return useCallback(
|
||||||
(strength: 'Light' | 'Medium' | 'Heavy' = 'Medium') => {
|
(strength: 'Light' | 'Medium' | 'Heavy' = 'Medium') => {
|
||||||
if (isHapticsDisabled || isWeb) {
|
if (isHapticsDisabled || isWeb) {
|
||||||
return
|
return
|
||||||
@@ -16,15 +41,9 @@ export function useHaptics() {
|
|||||||
|
|
||||||
// Users said the medium impact was too strong on Android; see APP-537s
|
// Users said the medium impact was too strong on Android; see APP-537s
|
||||||
const style = isIOS
|
const style = isIOS
|
||||||
? ImpactFeedbackStyle[strength]
|
? ExpoHaptics.ImpactFeedbackStyle[strength]
|
||||||
: ImpactFeedbackStyle.Light
|
: ExpoHaptics.ImpactFeedbackStyle.Light
|
||||||
impactAsync(style)
|
ExpoHaptics.impactAsync(style)
|
||||||
|
|
||||||
// DEV ONLY - show a toast when a haptic is meant to fire on simulator
|
|
||||||
if (__DEV__ && !Device.isDevice) {
|
|
||||||
// disabled because it's annoying
|
|
||||||
// Toast.show(`Buzzz!`)
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
[isHapticsDisabled],
|
[isHapticsDisabled],
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user