Package Exports
- web-haptics
- web-haptics/react
- web-haptics/svelte
- web-haptics/vue
Readme
WebHaptics
Haptic feedback for the mobile web. Supports React, Vue, and Svelte.
Install
npm i web-hapticsReact
import { useWebHaptics } from "web-haptics/react";
function App() {
const { trigger } = useWebHaptics();
return <button onClick={() => trigger("success")}>Tap me</button>;
}Vue
<script setup>
import { useWebHaptics } from "web-haptics/vue";
const { trigger } = useWebHaptics();
</script>
<template>
<button @click="trigger('success')">Tap me</button>
</template>Svelte
<script>
import { createWebHaptics } from "web-haptics/svelte";
import { onDestroy } from "svelte";
const { trigger, destroy } = createWebHaptics();
onDestroy(destroy);
</script>
<button on:click={() => trigger("success")}>Tap me</button>Vanilla
import { WebHaptics } from "web-haptics";
const haptics = new WebHaptics();
haptics.trigger("success");Built-in Presets
| Name | Pattern | Intensity |
|---|---|---|
success |
[50, 50, 50] |
0.5 |
nudge |
[80, 100, 200] |
0.5 |
error |
[50, 50, 50, 50, 50] |
0.75 |
buzz |
[1000] |
1.0 |
You can also pass custom patterns directly:
trigger([100, 50, 100]); // custom pattern
trigger(200); // single vibration
trigger({ pattern: [50, 50, 50], description: "custom", intensity: 0.8 }); // full presetAPI
trigger(input?, options?)
Trigger haptic feedback.
input— preset name ("success"), number,number[], orHapticPresetoptions.intensity— override intensity (0–1)
cancel()
Stop the current pattern and cancel any ongoing vibration.
WebHaptics.isSupported
Static boolean — true if the device supports the Vibration API.
License
MIT
Found this useful?
Follow me on Twitter.
Other projects
You might also like:
- Torph - Dependency-free animated text component.
- easing.dev - Easily create custom easing graphs.
Acknowledgements
- Special thanks to Alex for assistance with the site design.