ios-haptics 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# ios-haptics
|
|
2
|
+
|
|
3
|
+
A small JavaScript utility for triggering subtle haptic feedback on iOS devices,
|
|
4
|
+
using a clever browser-specific quirk.
|
|
5
|
+
|
|
6
|
+
## 🚀 Installation
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install ios-haptics
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 📦 Usage
|
|
13
|
+
|
|
14
|
+
Just call these functions on any event where you want haptic feedback:
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
import { haptic } from "ios-haptics";
|
|
18
|
+
|
|
19
|
+
// A single haptic "click"
|
|
20
|
+
haptic();
|
|
21
|
+
|
|
22
|
+
// Two rapid haptics (good for confirmation)
|
|
23
|
+
haptic.confirm();
|
|
24
|
+
|
|
25
|
+
// Three rapid haptics (useful for errors)
|
|
26
|
+
haptic.error();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## ⚙️ How it works (The Trick)
|
|
30
|
+
|
|
31
|
+
This library leverages a clever workaround that became possible with the
|
|
32
|
+
introduction of the HTML `switch` control in Safari 17.4. These native controls
|
|
33
|
+
gained haptic feedback on iOS.
|
|
34
|
+
|
|
35
|
+
The method involves dynamically creating a temporary, hidden `<label>` element
|
|
36
|
+
containing an `<input type="checkbox" switch />`. Programmatically "clicking"
|
|
37
|
+
this label triggers the native iOS haptic feedback associated with the switch
|
|
38
|
+
control. Immediately after, these temporary elements are removed from the DOM.
|
|
39
|
+
|
|
40
|
+
**I found this technique on [cobalt.tools](https://cobalt.tools). This library
|
|
41
|
+
is largely a port and package of their solution.**
|
|
42
|
+
|
|
43
|
+
## ⚠️ Notes
|
|
44
|
+
|
|
45
|
+
- **iOS Specific:** This only works on iOS devices where the `switch` input type
|
|
46
|
+
has haptic feedback.
|
|
47
|
+
- **Best Effort:** The haptics are system-defined; you can't customize intensity
|
|
48
|
+
or pattern beyond what's provided.
|
|
49
|
+
- **Temporary:** This is a neat trick, but it might stop working if Apple
|
|
50
|
+
changes its internal handling of these controls.
|
|
51
|
+
|
|
52
|
+
## 🤝 Contributions
|
|
53
|
+
|
|
54
|
+
Feel free to send a PR or open an issue if you have suggestions or find
|
|
55
|
+
improvements.
|