Skip to content

useVibrate

Category
Export Size
427 B
Last Changed
5 days ago

Reactive Vibration API

Most modern mobile devices include vibration hardware, which lets software code provides physical feedback to the user by causing the device to shake.

The Vibration API offers Web apps the ability to access this hardware, if it exists, and does nothing if the device doesn't support it.

Demo

Usage

Vibration is described as a pattern of on-off pulses, which may be of varying lengths.

The pattern may consist of either a single integer describing the number of milliseconds to vibrate, or an array of integers describing a pattern of vibrations and pauses.

tsx
import { 
useVibrate
} from '@reause/core'
// This vibrates the device for 300 ms, // then pauses for 100 ms before vibrating the device again for another 300 ms: const {
vibrate
,
stop
,
isSupported
} =
useVibrate
({
pattern
: [300, 100, 300] })
// Start the vibration, it will automatically stop when the pattern is complete:
vibrate
()
// But if you want to stop it, you can:
stop
()

Type Declarations

ts
export interface UseVibrateOptions {
    pattern?: number | number[];
    interval?: number;
    navigator?: Navigator;
}

export interface UseVibrateReturn {
    isSupported: boolean;
    pattern: number | number[];
    intervalControls: {
        pause: () => void;
        resume: () => void;
        isActive: boolean;
    };
    vibrate: (pattern?: number | number[]) => void;
    stop: () => void;
}

export function useVibrate(options?: UseVibrateOptions): UseVibrateReturn

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
a5aec - fix(core): resolve useVibrate audit findings (#677)

Released under the MIT License. v0.1.8