Socket
Book a DemoInstallSign in
Socket

@luxonauta/use-vibration

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@luxonauta/use-vibration

🪝 A React hook for controlling device vibration

latest
Source
npmnpm
Version
1.0.8
Version published
Maintainers
1
Created
Source

useVibration

A React hook for controlling device vibration.

Why should we use more haptic feedback on the web? Please read about it here.

Installation

npm install @luxonauta/use-vibration

Basic Usage

import useVibration, { VibrationPatterns } from "@luxonauta/use-vibration";

export const Component = () => {
  const [{ isSupported, isVibrating }, { vibrate, stop }] = useVibration();

  if (!isSupported) {
    return <p>Vibration not supported on your device</p>;
  }

  return (
    <>
      <button
        type="button"
        onClick={() => vibrate(VibrationPatterns.tap)}
        disabled={isVibrating}
      >
        {isVibrating ? "Vibrating" : "Tap me for haptic feedback"}
      </button>
      {isVibrating && (
        <button type="button" onClick={stop}>
          Stop Vibration
        </button>
      )}
    </>
  );
};

API Reference

Hook Return Values

const [state, controls] = useVibration();

State Object

PropertyTypeDescription
isSupportedbooleanWhether the device supports vibration
isVibratingbooleanWhether the device is currently vibrating

Controls Object

MethodTypeDescription
vibrate(pattern?: VibrationPattern) => voidTriggers vibration with an optional pattern
stop() => voidStops any ongoing vibration

Types

// Single duration or pattern array
type VibrationPattern = number | number[];

Predefined Patterns

The hook comes with common vibration patterns for different interactions:

PatternDescriptionValue
tapSubtle feedback100
standardStandard vibration200
heavyEmphasis500
doubleDouble-tap pattern[100, 30, 100]
tripleTriple-tap pattern[100, 30, 100, 30, 100]
successSuccess feedback[100, 50, 200]
errorError or warning[300, 100, 500]
notificationFor notifications[200, 100, 100]
sosSOS in morse code[100, 100, 100, 100, 100, 100, 300, 100, 300, 100, 300, 100, 100, 100, 100, 100, 100]
heartbeatHeartbeat simulation[100, 100, 100, 400]

Advanced Usage

Custom Patterns

You can create custom vibration patterns using arrays where:

  • Even-indexed elements (0, 2, 4, ...) specify vibration durations;
  • Odd-indexed elements (1, 3, 5, ...) specify pause durations.
// Pattern: vibrate 200ms → pause 100ms → vibrate 400ms → pause 100ms → vibrate 200ms
const customPattern = [200, 100, 400, 100, 200];
vibrate(customPattern);

Creating UI Feedback

const FeedbackApp = () => {
  const [, { vibrate }] = useVibration();

  const handleSuccess = () => {
    // Haptic feedback
    vibrate(VibrationPatterns.success);
    // Visual feedback
    setStatus("Success!");
  };

  const handleError = () => {
    // Haptic feedback
    vibrate(VibrationPatterns.error);
    // Visual feedback
    setStatus("Error!");
  };

  // App
};

Game Example

const Game = () => {
  const [, { vibrate }] = useVibration();

  const handleCollision = (intensity) => {
    // Adjust the vibration based on collision intensity
    const duration = Math.min(Math.round(intensity * 300), 1000);
    vibrate(duration);
  };

  // Game
};

Browser Compatibility

The Vibration API is supported across most modern browsers:

Desktop Browsers

  • Chrome: 32+;
  • Edge: 79+;
  • Opera: 19+;
  • Firefox: Not supported;
  • Safari: Not supported.

Mobile Browsers

  • Chrome for Android: 32+;
  • Firefox for Android: 79+;
  • Opera for Android: 19+;
  • Samsung Internet: 2.0+;
  • "WebView Android": 4.4.3+;
  • Safari iOS: Not supported.

Important Notes

  • The Vibration API is primarily designed for mobile devices;
  • Desktop browsers may support the API, but won't produce actual vibration.

Recommendation: Always check isSupported before using vibration features in your app.

Best Practices

  • Always check support first

    const [{ isSupported }] = useVibration();
    if (!isSupported) return <AlternativeFeedback />;
    
  • Use sparingly

    • Excessive vibration can be annoying and drain battery. Use only for important feedback.
  • Respect user preferences

    • Consider adding a setting to disable vibration.
  • Provide alternatives

    • Always pair vibration with visual feedback for accessibility.
  • Keep patterns simple

    • Complex patterns may not work consistently across devices.

Limitations

  • Some Android devices may ignore pattern details and use their default vibration;
  • Vibration might not work when browser is in background.

License

MIT

Keywords

react

FAQs

Package last updated on 22 Nov 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts