UNPKG

@react-hookz/web

Version:

React hooks done right, for browser and SSR.

34 lines (33 loc) 1.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useVibrate = void 0; var react_1 = require("react"); var const_1 = require("../util/const"); /** * 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`. */ exports.useVibrate = !const_1.isBrowser || typeof navigator.vibrate === 'undefined' ? const_1.noop : function useVibrate(enabled, pattern, loop) { (0, react_1.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]); };