ios-haptics 0.0.7 → 0.0.9
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 +20 -35
- package/dist/index.cjs +35 -0
- package/dist/index.d.cts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +33 -0
- package/package.json +18 -15
package/README.md
CHANGED
|
@@ -1,57 +1,42 @@
|
|
|
1
1
|
# ios-haptics
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
using a clever browser-specific quirk.
|
|
3
|
+
javascript library for haptic feedback inside of safari on ios
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+

|
|
6
|
+
[](https://npmjs.com/package/ios-haptics)
|
|
7
|
+

|
|
8
|
+

|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
demo: [ios-haptics demo](https://wg.tijn.dev?h=s0kqLSnJz1PITLFVykgsKMlMVrKD0Db6ECk7Lkw1zvl5aZlFuTClCskQPj4trkVF%7EUVwDakgHlw5AA&c=UwAA&j=hYwxDsIwDEX3nMLKBAPJTsQC6sAxUJqSiDqObDMgxN1RlY6Vun3pv_cKNmKFL%7ERH0xLhBxMTgs2qTc7eJ0En2ReSUyfEBmNGim9MVd0z6TCnZV4_9_FgO2OPjmqcS3zBZQ2HPedGdSqMG6qL_dpNDMzEW4G0HMH8AQ)
|
|
11
|
+
|
|
12
|
+
## installation
|
|
9
13
|
|
|
10
14
|
```sh
|
|
11
15
|
npm i ios-haptics
|
|
12
16
|
```
|
|
13
17
|
|
|
14
|
-
##
|
|
15
|
-
|
|
16
|
-
Just call these functions on any event where you want haptic feedback:
|
|
18
|
+
## usage
|
|
17
19
|
|
|
18
20
|
```javascript
|
|
19
|
-
import { haptic } from
|
|
21
|
+
import { haptic } from 'ios-haptics'
|
|
20
22
|
|
|
21
|
-
//
|
|
22
|
-
haptic()
|
|
23
|
+
// a single haptic
|
|
24
|
+
haptic()
|
|
23
25
|
|
|
24
|
-
//
|
|
25
|
-
haptic.confirm()
|
|
26
|
+
// two rapid haptics (good for confirmation)
|
|
27
|
+
haptic.confirm()
|
|
26
28
|
|
|
27
|
-
//
|
|
28
|
-
haptic.error()
|
|
29
|
+
// three rapid haptics (useful for errors)
|
|
30
|
+
haptic.error()
|
|
29
31
|
```
|
|
30
32
|
|
|
31
|
-
##
|
|
32
|
-
|
|
33
|
-
This library leverages a clever workaround that became possible with the
|
|
34
|
-
introduction of the HTML `switch` control in Safari 17.4. These native controls
|
|
35
|
-
gained haptic feedback on iOS.
|
|
36
|
-
|
|
37
|
-
The method involves dynamically creating a temporary, hidden `<label>` element
|
|
38
|
-
containing an `<input type="checkbox" switch />`. Programmatically "clicking"
|
|
39
|
-
this label triggers the native iOS haptic feedback associated with the switch
|
|
40
|
-
control. Immediately after, these temporary elements are removed from the DOM.
|
|
41
|
-
|
|
42
|
-
**I found this technique on [cobalt.tools](https://cobalt.tools). This library
|
|
43
|
-
is largely a port and package of their solution.**
|
|
33
|
+
## how it works
|
|
44
34
|
|
|
45
|
-
|
|
35
|
+
this uses the `<input type="checkbox" switch />` (introduced in safari 17.4), which has haptic feedback when toggled
|
|
46
36
|
|
|
47
|
-
|
|
48
|
-
has haptic feedback.
|
|
49
|
-
- **Best Effort:** The haptics are system-defined; you can't customize intensity
|
|
50
|
-
or pattern beyond what's provided.
|
|
51
|
-
- **Temporary:** This is a neat trick, but it might stop working if Apple
|
|
52
|
-
changes its internal handling of these controls.
|
|
37
|
+
every `haptic` call, it will create one of those in the background, toggle it (which has haptic feedback), then remove it
|
|
53
38
|
|
|
54
|
-
|
|
39
|
+
---
|
|
55
40
|
|
|
56
41
|
Feel free to send a PR or open an issue if you have suggestions or find
|
|
57
42
|
improvements.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ios-haptics v0.0.9
|
|
4
|
+
* tijn.dev
|
|
5
|
+
* @license MIT
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.haptic = haptic;
|
|
9
|
+
function haptic() {
|
|
10
|
+
try {
|
|
11
|
+
const labelEl = document.createElement('label');
|
|
12
|
+
labelEl.ariaHidden = 'true';
|
|
13
|
+
labelEl.style.display = 'none';
|
|
14
|
+
const inputEl = document.createElement('input');
|
|
15
|
+
inputEl.type = 'checkbox';
|
|
16
|
+
inputEl.setAttribute('switch', '');
|
|
17
|
+
labelEl.appendChild(inputEl);
|
|
18
|
+
document.head.appendChild(labelEl);
|
|
19
|
+
labelEl.click();
|
|
20
|
+
document.head.removeChild(labelEl);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// do nothing
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
;
|
|
27
|
+
haptic.confirm = () => {
|
|
28
|
+
haptic();
|
|
29
|
+
setTimeout(() => haptic(), 120);
|
|
30
|
+
};
|
|
31
|
+
haptic.error = () => {
|
|
32
|
+
haptic();
|
|
33
|
+
setTimeout(() => haptic(), 120);
|
|
34
|
+
setTimeout(() => haptic(), 240);
|
|
35
|
+
};
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ios-haptics v0.0.9
|
|
3
|
+
* tijn.dev
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
function haptic() {
|
|
7
|
+
try {
|
|
8
|
+
const labelEl = document.createElement('label');
|
|
9
|
+
labelEl.ariaHidden = 'true';
|
|
10
|
+
labelEl.style.display = 'none';
|
|
11
|
+
const inputEl = document.createElement('input');
|
|
12
|
+
inputEl.type = 'checkbox';
|
|
13
|
+
inputEl.setAttribute('switch', '');
|
|
14
|
+
labelEl.appendChild(inputEl);
|
|
15
|
+
document.head.appendChild(labelEl);
|
|
16
|
+
labelEl.click();
|
|
17
|
+
document.head.removeChild(labelEl);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
// do nothing
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
;
|
|
24
|
+
haptic.confirm = () => {
|
|
25
|
+
haptic();
|
|
26
|
+
setTimeout(() => haptic(), 120);
|
|
27
|
+
};
|
|
28
|
+
haptic.error = () => {
|
|
29
|
+
haptic();
|
|
30
|
+
setTimeout(() => haptic(), 120);
|
|
31
|
+
setTimeout(() => haptic(), 240);
|
|
32
|
+
};
|
|
33
|
+
export { haptic };
|
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ios-haptics",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.9",
|
|
4
5
|
"description": "haptic feedback for ios safari",
|
|
6
|
+
"author": "tijnjh",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": "tijnjh/ios-haptics",
|
|
5
9
|
"keywords": [
|
|
6
10
|
"haptic",
|
|
7
11
|
"ios",
|
|
@@ -11,25 +15,24 @@
|
|
|
11
15
|
"safari",
|
|
12
16
|
"vibrate"
|
|
13
17
|
],
|
|
14
|
-
"repository": "tijnjh/ios-haptics",
|
|
15
|
-
"license": "MIT",
|
|
16
|
-
"author": "tijnjh",
|
|
17
|
-
"type": "module",
|
|
18
18
|
"exports": {
|
|
19
|
-
".":
|
|
20
|
-
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.cts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"require": "./dist/index.cjs"
|
|
23
|
+
}
|
|
21
24
|
},
|
|
22
|
-
"main": "./dist/index.
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
23
26
|
"module": "./dist/index.js",
|
|
24
|
-
"types": "./dist/index.d.
|
|
27
|
+
"types": "./dist/index.d.cts",
|
|
25
28
|
"files": [
|
|
26
29
|
"dist"
|
|
27
30
|
],
|
|
28
|
-
"scripts": {
|
|
29
|
-
"build": "tsdown"
|
|
30
|
-
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
|
|
32
|
+
"@antfu/eslint-config": "^4.16.2",
|
|
33
|
+
"eslint": "^9.31.0",
|
|
34
|
+
"typescript": "^5.8.3",
|
|
35
|
+
"zshy": "^0.2.3"
|
|
36
|
+
},
|
|
37
|
+
"zshy": "./src/index.ts"
|
|
35
38
|
}
|