browser-haptic 1.0.0
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/LICENSE +21 -0
- package/README.md +86 -0
- package/dist/haptic.d.ts +24 -0
- package/dist/haptic.d.ts.map +1 -0
- package/dist/haptic.js +75 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Emirhan Kemal Kosem emirhankemalkosem@gmail.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# browser-haptic
|
|
2
|
+
|
|
3
|
+
Lightweight haptic feedback for JavaScript. This repo uses **Bun**. Uses the [Vibration API](https://developer.mozilla.org/en-US/docs/Web/API/Vibration_API) where available (e.g. Android). On **iOS Safari 17.4+** it uses a hidden `input[switch]` toggle so the native switch haptic fires. No runtime dependencies.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add browser-haptic
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Import the default export as **`Haptic`** and call its methods:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import Haptic from "browser-haptic";
|
|
17
|
+
|
|
18
|
+
if (Haptic.isSupported()) {
|
|
19
|
+
Haptic.light();
|
|
20
|
+
Haptic.success();
|
|
21
|
+
Haptic.vibrate([10, 50, 10, 50, 10]);
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**React example:**
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import Haptic from "browser-haptic";
|
|
29
|
+
|
|
30
|
+
const App = () => {
|
|
31
|
+
const supported = Haptic.isSupported();
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<button onClick={() => Haptic.heavy()}>
|
|
35
|
+
Tap me
|
|
36
|
+
</button>
|
|
37
|
+
);
|
|
38
|
+
};
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
You can also use named imports if you prefer: `import { light, success, isSupported } from "browser-haptic"`.
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
| Method | Description |
|
|
46
|
+
|--------|-------------|
|
|
47
|
+
| `Haptic.isSupported()` | `true` if feedback is available (Vibration API or DOM for iOS switch fallback) |
|
|
48
|
+
| `Haptic.hasVibration()` | `true` only when the Vibration API is available (e.g. Android) |
|
|
49
|
+
| `Haptic.vibrate(pattern)` | Trigger feedback: `number` (ms) or `number[]` (vibrate/pause sequence) |
|
|
50
|
+
| `Haptic.light()` | Short tap |
|
|
51
|
+
| `Haptic.medium()` | Medium tap |
|
|
52
|
+
| `Haptic.heavy()` | Strong tap |
|
|
53
|
+
| `Haptic.success()` | Double tap |
|
|
54
|
+
| `Haptic.warning()` | Triple tap |
|
|
55
|
+
| `Haptic.error()` | Longer alert pattern |
|
|
56
|
+
|
|
57
|
+
**Type:** `VibrationPattern` = `number | number[]` (for `vibrate()`).
|
|
58
|
+
|
|
59
|
+
On iOS Safari 17.4+, `isSupported()` is `true` but `hasVibration()` is `false`; the library uses a hidden native switch toggle.
|
|
60
|
+
|
|
61
|
+
## Test
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
bun test
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Coverage (enforces 80% lines and functions):
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
bun run coverage
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Contributing
|
|
74
|
+
|
|
75
|
+
Contributions are welcome. To get started:
|
|
76
|
+
|
|
77
|
+
1. Fork the repo and clone it locally.
|
|
78
|
+
2. Install dependencies: `bun install`
|
|
79
|
+
3. Make your changes and run tests: `bun test`
|
|
80
|
+
4. Open a pull request with a short description of the change.
|
|
81
|
+
|
|
82
|
+
For bugs or feature ideas, please open an issue first so we can discuss.
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
MIT
|
package/dist/haptic.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type VibrationPattern = number | number[];
|
|
2
|
+
export interface Haptic {
|
|
3
|
+
hasVibration(): boolean;
|
|
4
|
+
isSupported(): boolean;
|
|
5
|
+
vibrate(pattern: VibrationPattern): void;
|
|
6
|
+
light(): void;
|
|
7
|
+
medium(): void;
|
|
8
|
+
heavy(): void;
|
|
9
|
+
success(): void;
|
|
10
|
+
warning(): void;
|
|
11
|
+
error(): void;
|
|
12
|
+
}
|
|
13
|
+
export declare const hasVibration: () => boolean;
|
|
14
|
+
export declare const isSupported: () => boolean;
|
|
15
|
+
export declare const vibrate: (pattern: VibrationPattern) => void;
|
|
16
|
+
export declare const light: () => void;
|
|
17
|
+
export declare const medium: () => void;
|
|
18
|
+
export declare const heavy: () => void;
|
|
19
|
+
export declare const success: () => void;
|
|
20
|
+
export declare const warning: () => void;
|
|
21
|
+
export declare const error: () => void;
|
|
22
|
+
declare const Haptic: Haptic;
|
|
23
|
+
export default Haptic;
|
|
24
|
+
//# sourceMappingURL=haptic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"haptic.d.ts","sourceRoot":"","sources":["../src/haptic.ts"],"names":[],"mappings":"AA4BA,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;AAUjD,MAAM,WAAW,MAAM;IACrB,YAAY,IAAI,OAAO,CAAC;IACxB,WAAW,IAAI,OAAO,CAAC;IACvB,OAAO,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAAC;IACzC,KAAK,IAAI,IAAI,CAAC;IACd,MAAM,IAAI,IAAI,CAAC;IACf,KAAK,IAAI,IAAI,CAAC;IACd,OAAO,IAAI,IAAI,CAAC;IAChB,OAAO,IAAI,IAAI,CAAC;IAChB,KAAK,IAAI,IAAI,CAAC;CACf;AA6ED,eAAO,MAAM,YAAY,QAAO,OAA4B,CAAC;AAmB7D,eAAO,MAAM,WAAW,QAAO,OAG9B,CAAC;AAoBF,eAAO,MAAM,OAAO,GAAI,SAAS,gBAAgB,KAAG,IAAwB,CAAC;AAQ7E,eAAO,MAAM,KAAK,QAAO,IAA8B,CAAC;AAQxD,eAAO,MAAM,MAAM,QAAO,IAA+B,CAAC;AAQ1D,eAAO,MAAM,KAAK,QAAO,IAA8B,CAAC;AAQxD,eAAO,MAAM,OAAO,QAAO,IAAqC,CAAC;AASjE,eAAO,MAAM,OAAO,QAAO,IAAqC,CAAC;AAQjE,eAAO,MAAM,KAAK,QAAO,IAAmC,CAAC;AAE7D,QAAA,MAAM,MAAM,EAAE,MAUb,CAAC;AAiBF,eAAe,MAAM,CAAC"}
|
package/dist/haptic.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const PRESETS = {
|
|
2
|
+
light: 10,
|
|
3
|
+
medium: 20,
|
|
4
|
+
heavy: 40,
|
|
5
|
+
success: [10, 50, 10],
|
|
6
|
+
warning: [30, 30, 30],
|
|
7
|
+
error: [50, 30, 50, 30, 50],
|
|
8
|
+
};
|
|
9
|
+
const hasVibrationAPI = () => typeof navigator !== "undefined" && typeof navigator.vibrate === "function";
|
|
10
|
+
const hasDOM = () => typeof document !== "undefined" && !!document.body;
|
|
11
|
+
const fireIOSSwitch = () => {
|
|
12
|
+
if (!hasDOM())
|
|
13
|
+
return;
|
|
14
|
+
try {
|
|
15
|
+
const label = document.createElement("label");
|
|
16
|
+
label.setAttribute("aria-hidden", "true");
|
|
17
|
+
label.style.cssText =
|
|
18
|
+
"position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);border:0;";
|
|
19
|
+
const input = document.createElement("input");
|
|
20
|
+
input.type = "checkbox";
|
|
21
|
+
input.setAttribute("switch", "");
|
|
22
|
+
label.appendChild(input);
|
|
23
|
+
document.body.appendChild(label);
|
|
24
|
+
label.click();
|
|
25
|
+
document.body.removeChild(label);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const toPatternArray = (pattern) => Array.isArray(pattern) ? [...pattern] : [pattern];
|
|
31
|
+
const trigger = (pattern) => {
|
|
32
|
+
if (hasVibrationAPI()) {
|
|
33
|
+
try {
|
|
34
|
+
const p = Array.isArray(pattern) ? [...pattern] : pattern;
|
|
35
|
+
navigator.vibrate(p);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const arr = toPatternArray(pattern);
|
|
42
|
+
let delay = 0;
|
|
43
|
+
for (let i = 0; i < arr.length; i += 2) {
|
|
44
|
+
const vibrateMs = arr[i] ?? 0;
|
|
45
|
+
const pauseMs = arr[i + 1] ?? 0;
|
|
46
|
+
if (vibrateMs > 0)
|
|
47
|
+
setTimeout(fireIOSSwitch, delay);
|
|
48
|
+
delay += vibrateMs + pauseMs;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
export const hasVibration = () => hasVibrationAPI();
|
|
52
|
+
export const isSupported = () => {
|
|
53
|
+
if (typeof navigator === "undefined" || typeof document === "undefined")
|
|
54
|
+
return false;
|
|
55
|
+
return hasVibrationAPI() || hasDOM();
|
|
56
|
+
};
|
|
57
|
+
export const vibrate = (pattern) => trigger(pattern);
|
|
58
|
+
export const light = () => trigger(PRESETS.light);
|
|
59
|
+
export const medium = () => trigger(PRESETS.medium);
|
|
60
|
+
export const heavy = () => trigger(PRESETS.heavy);
|
|
61
|
+
export const success = () => trigger([...PRESETS.success]);
|
|
62
|
+
export const warning = () => trigger([...PRESETS.warning]);
|
|
63
|
+
export const error = () => trigger([...PRESETS.error]);
|
|
64
|
+
const Haptic = {
|
|
65
|
+
hasVibration,
|
|
66
|
+
isSupported,
|
|
67
|
+
vibrate,
|
|
68
|
+
light,
|
|
69
|
+
medium,
|
|
70
|
+
heavy,
|
|
71
|
+
success,
|
|
72
|
+
warning,
|
|
73
|
+
error,
|
|
74
|
+
};
|
|
75
|
+
export default Haptic;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,WAAW,EACX,OAAO,EACP,KAAK,EACL,MAAM,EACN,KAAK,EACL,OAAO,EACP,OAAO,EACP,KAAK,EACL,KAAK,gBAAgB,EACrB,KAAK,MAAM,GACZ,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "browser-haptic",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Lightweight haptic feedback for JS (Vibration API + iOS Safari switch fallback). No deps.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "bun test",
|
|
21
|
+
"coverage": "bun test --coverage",
|
|
22
|
+
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
23
|
+
"build": "bun run clean && tsc",
|
|
24
|
+
"prepublishOnly": "bun run build"
|
|
25
|
+
},
|
|
26
|
+
"packageManager": "bun",
|
|
27
|
+
"keywords": [
|
|
28
|
+
"browser-haptic",
|
|
29
|
+
"haptic",
|
|
30
|
+
"vibration",
|
|
31
|
+
"vibrate",
|
|
32
|
+
"ios",
|
|
33
|
+
"safari",
|
|
34
|
+
"feedback",
|
|
35
|
+
"mobile",
|
|
36
|
+
"vibration-api"
|
|
37
|
+
],
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=18"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"typescript": "^5.3.0"
|
|
44
|
+
}
|
|
45
|
+
}
|