Install web-haptics via npm
mainInstall the web-haptics package using npm to add haptic feedback capabilities to your mobile web project.
npm i web-hapticsrepository·main·Indexed 25 days ago
https://github.com/lochie/web-hapticsA library providing haptic feedback capabilities for mobile web applications. It includes a core WebHaptics class for vanilla TypeScript and specialized integrations for React (useWebHaptics hook), Vue (useWebHaptics hook), and Svelte (createWebHaptics function). The library supports built-in presets like success, nudge, error, and buzz, as well as custom vibration patterns using durations, intensities, and delays.
Install the web-haptics package using npm to add haptic feedback capabilities to your mobile web project.
npm i web-hapticsInstall the web-haptics library via npm to add tactile feedback to your web applications. The library uses the Web Vibration API and silently no-ops on unsupported platforms like desktop browsers, so no feature detection is required.
npm i web-hapticspackages/web-haptics/README.md for usage examples.To ensure a high-quality user experience, follow these design principles:
light/selection for small interactions, medium/success for standard actions, and heavy/error/warning for major changes.success for positive outcomes, error for negative, warning for caution, and selection for discrete ticks.try {
await submit();
haptic.trigger("success");
} catch {
haptic.trigger("error");
}Import the WebHaptics class from web-haptics to create a new instance and trigger haptics manually.
import { WebHaptics } from "web-haptics";
const haptics = new WebHaptics();
haptics.trigger("success");For vanilla JS environments, instantiate the WebHaptics class directly.
import { WebHaptics } from "web-haptics";
const haptics = new WebHaptics();
haptics.trigger(); // medium impact
haptics.trigger("success");Use the useWebHaptics hook from web-haptics/react to access the trigger function within your React components.
import { useWebHaptics } from "web-haptics/react";
function App() {
const { trigger } = useWebHaptics();
return <button onClick={() => trigger("success")}>Tap me</button>;
}To use haptics in a React application, import the useWebHaptics hook from web-haptics/react. The hook returns a trigger function that accepts a haptic pattern name (e.g., "success") to execute feedback on user interaction.
import { useWebHaptics } from "web-haptics/react";
function App() {
const { trigger } = useWebHaptics();
return <button onClick={() => trigger("success")}>Tap me</button>;
}Use the useWebHaptics hook from web-haptics/vue within your Vue components via <script setup> to access the trigger function.
<script setup>
import { useWebHaptics } from "web-haptics/vue";
const { trigger } = useWebHaptics();
</script>
<template>
<button @click="trigger('success')">Tap me</button>
</template>In Svelte, use createWebHaptics from web-haptics/svelte. Remember to call haptic.destroy() in the onDestroy lifecycle hook to clean up.
<script>
import { createWebHaptics } from "web-haptics/svelte";
import { onDestroy } from "svelte";
const haptic = createWebHaptics();
onDestroy(() => haptic.destroy());
</script>
<button on:click={() => haptic.trigger()}>Tap me</button>In Vue, use the useWebHaptics hook from web-haptics/vue within a <script setup> block.
<script setup>
import { useWebHaptics } from "web-haptics/vue";
const haptic = useWebHaptics();
</script>
<template>
<button @click="haptic.trigger()">Tap me</button>
</template>In React, use the useWebHaptics hook from web-haptics/react. If using Next.js, ensure you add the "use client" directive to any component using this hook.
import { useWebHaptics } from "web-haptics/react";
const haptic = useWebHaptics();
<button onClick={() => haptic.trigger()}>Tap me</button>;