|
| 1 | +# React Native Haptics Usage Guide |
| 2 | + |
| 3 | +- [React Native Haptics Usage Guide](#react-native-haptics-usage-guide) |
| 4 | + - [Installation](#installation) |
| 5 | + - [Bare React Native](#bare-react-native) |
| 6 | + - [Expo](#expo) |
| 7 | + - [API Overview](#api-overview) |
| 8 | + - [**impact**](#impact) |
| 9 | + - [**notification**](#notification) |
| 10 | + - [**selection**](#selection) |
| 11 | + - [**androidHaptics**](#androidhaptics) |
| 12 | + - [Example Application](#example-application) |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +### Bare React Native |
| 19 | + |
| 20 | +Install the package using either npm or Yarn: |
| 21 | + |
| 22 | +```sh |
| 23 | +npm install @mhpdev/react-native-haptics |
| 24 | +``` |
| 25 | + |
| 26 | +Or with Yarn: |
| 27 | + |
| 28 | +```sh |
| 29 | +yarn add @mhpdev/react-native-haptics |
| 30 | +``` |
| 31 | + |
| 32 | +For iOS, navigate to the ios directory and install the pods: |
| 33 | + |
| 34 | +```sh |
| 35 | +cd ios && pod install |
| 36 | +``` |
| 37 | + |
| 38 | +### Expo |
| 39 | + |
| 40 | +For Expo projects, follow these steps: |
| 41 | + |
| 42 | +1. Install the package: |
| 43 | + |
| 44 | + ```sh |
| 45 | + npx expo install @mhpdev/react-native-haptics |
| 46 | + ``` |
| 47 | + |
| 48 | +2. Since it is not supported on Expo Go, run: |
| 49 | + |
| 50 | + ```sh |
| 51 | + npx expo prebuild |
| 52 | + ``` |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## API Overview |
| 57 | + |
| 58 | +The library exports a default `Haptics` object that provides methods for triggering haptic feedback. |
| 59 | + |
| 60 | +```tsx |
| 61 | +import Haptics from '@mhpdev/react-native-haptics'; |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +### **impact** |
| 67 | + |
| 68 | +Triggers an impact haptic feedback. This corresponds to `UIImpactFeedbackGenerator` on iOS. On Android, it simulates the equivalent vibration effect. |
| 69 | + |
| 70 | +**API Definition:** |
| 71 | + |
| 72 | +```ts |
| 73 | +Haptics.impact(style: ImpactFeedback): Promise<void> |
| 74 | +``` |
| 75 | + |
| 76 | +**ImpactFeedback Type:** |
| 77 | + |
| 78 | +- `light`: A collision between small, light user interface elements. |
| 79 | +- `medium`: A collision between moderately sized user interface elements. |
| 80 | +- `heavy`: A collision between large, heavy user interface elements. |
| 81 | +- `soft`: A collision between user interface elements that are soft, with a large amount of compression or elasticity. (iOS 13.0+) |
| 82 | +- `rigid`: A collision between user interface elements that are rigid, with a small amount of compression or elasticity. (iOS 13.0+) |
| 83 | + |
| 84 | +**Example Usage:** |
| 85 | + |
| 86 | +```ts |
| 87 | +import {ImpactFeedback} from '@mhpdev/react-native-haptics'; |
| 88 | + |
| 89 | +// Trigger a heavy impact |
| 90 | +Haptics.impact('heavy'); |
| 91 | + |
| 92 | +// Trigger a light impact |
| 93 | +Haptics.impact('light'); |
| 94 | +``` |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +### **notification** |
| 99 | + |
| 100 | +Triggers a notification haptic feedback to communicate success, warning, or error. This corresponds to `UINotificationFeedbackGenerator` on iOS. On Android, it simulates the equivalent vibration effect. |
| 101 | + |
| 102 | +**API Definition:** |
| 103 | + |
| 104 | +```ts |
| 105 | +Haptics.notification(type: NotificationFeedback): Promise<void> |
| 106 | +``` |
| 107 | + |
| 108 | +**NotificationFeedback Type:** |
| 109 | + |
| 110 | +- `success`: Indicates that a task or action has completed successfully. |
| 111 | +- `warning`: Indicates that a task or action has produced a warning. |
| 112 | +- `error`: Indicates that a task or action has failed. |
| 113 | + |
| 114 | +**Example Usage:** |
| 115 | + |
| 116 | +```ts |
| 117 | +import {NotificationFeedback} from '@mhpdev/react-native-haptics'; |
| 118 | + |
| 119 | +// Trigger a success notification |
| 120 | +Haptics.notification('success'); |
| 121 | + |
| 122 | +// Trigger an error notification |
| 123 | +Haptics.notification('error'); |
| 124 | +``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +### **selection** |
| 129 | + |
| 130 | +Triggers a haptic feedback to indicate a selection change. This corresponds to `UISelectionFeedbackGenerator` on iOS. On Android, it uses `HapticFeedbackConstants.VIRTUAL_KEY`. |
| 131 | + |
| 132 | +**API Definition:** |
| 133 | + |
| 134 | +```ts |
| 135 | +Haptics.selection(): Promise<void> |
| 136 | +``` |
| 137 | + |
| 138 | +**Example Usage:** |
| 139 | + |
| 140 | +```ts |
| 141 | +// Trigger a selection change haptic |
| 142 | +Haptics.selection(); |
| 143 | +``` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +### **androidHaptics** |
| 148 | + |
| 149 | +Triggers a platform-specific haptic feedback on Android. This method is a no-op on iOS. |
| 150 | + |
| 151 | +> **Note:** Availability of each feedback type varies by Android SDK version. If a requested type is unsupported on the device, it safely falls back to `virtual-key`. |
| 152 | +
|
| 153 | +**API Definition:** |
| 154 | + |
| 155 | +```ts |
| 156 | +Haptics.androidHaptics(type: AndroidHapticsFeedback): Promise<void> |
| 157 | +``` |
| 158 | + |
| 159 | +**AndroidHapticsFeedback Type:** |
| 160 | + |
| 161 | +| Type | Description | Required API | |
| 162 | +| :----------------------------- | :--------------------------------------------------------------- | :----------- | |
| 163 | +| `clock-tick` | Feedback for a clock tick, e.g., while setting the time. | - | |
| 164 | +| `confirm` | Confirms a user's action. | 30+ | |
| 165 | +| `context-click` | Feedback for a context-click or right-click. | 23+ | |
| 166 | +| `drag-start` | Indicates the start of a drag action. | 34+ | |
| 167 | +| `gesture-end` | Indicates the end of a gesture. | 30+ | |
| 168 | +| `gesture-start` | Indicates the start of a gesture. | 30+ | |
| 169 | +| `gesture-threshold-activate` | Indicates the activation of a gesture threshold. | 34+ | |
| 170 | +| `gesture-threshold-deactivate` | Indicates the deactivation of a gesture threshold. | 34+ | |
| 171 | +| `keyboard-press` | Feedback for pressing a key on a soft keyboard. | 27+ | |
| 172 | +| `keyboard-release` | Feedback for releasing a key on a soft keyboard. | 27+ | |
| 173 | +| `keyboard-tap` | Feedback for a tap on a soft keyboard key. | - | |
| 174 | +| `long-press` | Feedback for a long press on an object. | - | |
| 175 | +| `no-haptics` | Explicitly specifies that no haptic feedback should be provided. | 34+ | |
| 176 | +| `reject` | Rejects a user's action. | 30+ | |
| 177 | +| `segment-frequent-tick` | A frequent tick in a segmented control. | 34+ | |
| 178 | +| `segment-tick` | A tick in a segmented control. | 34+ | |
| 179 | +| `text-handle-move` | Feedback for moving a text selection handle. | 27+ | |
| 180 | +| `toggle-off` | Indicates a toggle has been turned off. | 34+ | |
| 181 | +| `toggle-on` | Indicates a toggle has been turned on. | 34+ | |
| 182 | +| `virtual-key` | Feedback for a virtual key press. | - | |
| 183 | +| `virtual-key-release` | Feedback for a virtual key release. | 27+ | |
| 184 | + |
| 185 | +**Example Usage:** |
| 186 | + |
| 187 | +```ts |
| 188 | +import {AndroidHapticsFeedback} from '@mhpdev/react-native-haptics'; |
| 189 | + |
| 190 | +// Trigger a long press haptic on Android |
| 191 | +Haptics.androidHaptics('long-press'); |
| 192 | + |
| 193 | +// Trigger a confirm haptic on Android |
| 194 | +Haptics.androidHaptics('confirm'); |
| 195 | +``` |
| 196 | + |
| 197 | +--- |
| 198 | + |
| 199 | +## Example Application |
| 200 | + |
| 201 | +Check out the [example project](../example/). |
0 commit comments