UNPKG

@react-hookz/web

Version:

React hooks done right, for browser and SSR.

31 lines (30 loc) 1.15 kB
import { useEffect } from 'react'; import { isBrowser, noop } from "../util/const.js"; /** * Provides vibration feedback using the Vibration API. * * @param enabled Whether to perform vibration or not. * @param pattern VibrationPattern passed down to `navigator.vibrate`. * @param loop If true - vibration will be looped using `setInterval`. */ export var useVibrate = !isBrowser || typeof navigator.vibrate === 'undefined' ? noop : function useVibrate(enabled, pattern, loop) { useEffect(function () { var interval; if (enabled) { navigator.vibrate(pattern); if (loop) { interval = setInterval(function () { navigator.vibrate(pattern); }, Array.isArray(pattern) ? pattern.reduce(function (a, n) { return a + n; }, 0) : pattern); } return function () { navigator.vibrate(0); if (interval) { clearInterval(interval); } }; } }, [loop, pattern, enabled]); };