about summary refs log tree commit diff
path: root/src/lib/haptics.ts
blob: 234be777d337c90e38da6aca545102da5eaff967 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React from 'react'
import * as Device from 'expo-device'
import {impactAsync, ImpactFeedbackStyle} from 'expo-haptics'

import {isIOS, isWeb} from '#/platform/detection'
import {useHapticsDisabled} from '#/state/preferences/disable-haptics'
import * as Toast from '#/view/com/util/Toast'

export function useHaptics() {
  const isHapticsDisabled = useHapticsDisabled()

  return React.useCallback(
    (strength: 'Light' | 'Medium' | 'Heavy' = 'Medium') => {
      if (isHapticsDisabled || isWeb) {
        return
      }

      // Users said the medium impact was too strong on Android; see APP-537s
      const style = isIOS
        ? ImpactFeedbackStyle[strength]
        : ImpactFeedbackStyle.Light
      impactAsync(style)

      // DEV ONLY - show a toast when a haptic is meant to fire on simulator
      if (__DEV__ && !Device.isDevice) {
        Toast.show(`Buzzz!`)
      }
    },
    [isHapticsDisabled],
  )
}