32 lines
1.1 KiB
JavaScript
32 lines
1.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var react_1 = require("react");
|
|
var util_1 = require("./misc/util");
|
|
var isVibrationApiSupported = util_1.isNavigator && 'vibrate' in navigator;
|
|
function useVibrate(enabled, pattern, loop) {
|
|
if (enabled === void 0) { enabled = true; }
|
|
if (pattern === void 0) { pattern = [1000, 1000]; }
|
|
if (loop === void 0) { loop = true; }
|
|
react_1.useEffect(function () {
|
|
var interval;
|
|
if (enabled) {
|
|
navigator.vibrate(pattern);
|
|
if (loop) {
|
|
var duration = pattern instanceof Array ? pattern.reduce(function (a, b) { return a + b; }) : pattern;
|
|
interval = setInterval(function () {
|
|
navigator.vibrate(pattern);
|
|
}, duration);
|
|
}
|
|
}
|
|
return function () {
|
|
if (enabled) {
|
|
navigator.vibrate(0);
|
|
if (loop) {
|
|
clearInterval(interval);
|
|
}
|
|
}
|
|
};
|
|
}, [enabled]);
|
|
}
|
|
exports.default = isVibrationApiSupported ? useVibrate : util_1.noop;
|