Hooks
No hooks found in any category.
useVibration
sensorsInstallation
npx usehooks-cli@latest add use-vibration
Description
A React hook for controlling device vibration using the Vibration API. Provides functions to trigger vibration patterns and stop ongoing vibrations.
Return Type
UseVibrationReturn
Property | Type | Description |
---|---|---|
vibrate | (pattern: number | number[]) => boolean | Trigger vibration with specified pattern (duration in ms or array of durations) |
stop | () => void | Stop any ongoing vibration |
isSupported | boolean | Whether the Vibration API is supported in the current browser |
Examples
Notification with vibration
Basic vibration patterns for different notification types
1import { useVibration } from '@usehooks.io/use-vibration';
2
3function NotificationComponent() {
4 const { vibrate, stop, isSupported } = useVibration();
5
6 const handleNotification = () => {
7 // Single vibration for 200ms
8 vibrate(200);
9 };
10
11 const handleUrgentNotification = () => {
12 // Pattern: vibrate 100ms, pause 50ms, vibrate 100ms, pause 50ms, vibrate 200ms
13 vibrate([100, 50, 100, 50, 200]);
14 };
15
16 const handleError = () => {
17 // Long vibration for errors
18 vibrate(500);
19 };
20
21 if (!isSupported) {
22 return <div>Vibration not supported on this device</div>;
23 }
24
25 return (
26 <div>
27 <button onClick={handleNotification}>
28 Normal Notification
29 </button>
30
31 <button onClick={handleUrgentNotification}>
32 Urgent Notification
33 </button>
34
35 <button onClick={handleError}>
36 Error Notification
37 </button>
38
39 <button onClick={stop}>
40 Stop Vibration
41 </button>
42 </div>
43 );
44}
Dependencies
react
Notes
- •Vibration API is primarily supported on mobile devices
- •Pattern arrays alternate between vibration and pause durations
- •Returns false if vibration fails or is not supported
- •Some browsers may ignore vibration requests for security/UX reasons
- •Vibration may be disabled in browser settings or by user preference
Implementation
1'use client';
2
3import { useCallback, useRef } from "react";
4
5interface UseVibrationReturn {
6 vibrate: (pattern: number | number[]) => boolean;
7 stop: () => void;
8 isSupported: boolean;
9}
10
11export const useVibration = (): UseVibrationReturn => {
12 const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
13
14 const isSupported =
15 typeof navigator !== "undefined" && "vibrate" in navigator;
16
17 const vibrate = useCallback(
18 (pattern: number | number[]): boolean => {
19 if (!isSupported) {
20 return false;
21 }
22
23 try {
24 return navigator.vibrate(pattern);
25 } catch (error) {
26 console.warn("Vibration failed:", error);
27 return false;
28 }
29 },
30 [isSupported]
31 );
32
33 const stop = useCallback(() => {
34 if (intervalRef.current) {
35 clearInterval(intervalRef.current);
36 intervalRef.current = null;
37 }
38
39 if (isSupported) {
40 navigator.vibrate(0);
41 }
42 }, [isSupported]);
43
44 return {
45 vibrate,
46 stop,
47 isSupported,
48 };
49};
50
51export default useVibration;
52