cobalt/web/src/lib/haptics.ts
wukko 95a5a8ae9b
Some checks failed
CodeQL / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Has been cancelled
Run tests / check lockfile correctness (push) Has been cancelled
Run tests / web sanity check (push) Has been cancelled
Run tests / api sanity check (push) Has been cancelled
web/haptics: fix disableHaptics setting path
bub fix
2025-03-07 21:50:02 +06:00

44 lines
1.0 KiB
TypeScript

import { get } from "svelte/store";
import { device } from "$lib/device";
import settings from "$lib/state/settings";
const canUseHaptics = () => {
return device.supports.haptics && !get(settings).accessibility.disableHaptics;
}
export const hapticSwitch = () => {
if (!canUseHaptics()) return;
try {
const label = document.createElement("label");
label.ariaHidden = "true";
label.style.display = "none";
const input = document.createElement("input");
input.type = "checkbox";
input.setAttribute("switch", "");
label.appendChild(input);
document.head.appendChild(label);
label.click();
document.head.removeChild(label);
} catch {
// ignore
}
}
export const hapticConfirm = () => {
if (!canUseHaptics()) return;
hapticSwitch();
setTimeout(() => hapticSwitch(), 120);
}
export const hapticError = () => {
if (!canUseHaptics()) return;
hapticSwitch();
setTimeout(() => hapticSwitch(), 120);
setTimeout(() => hapticSwitch(), 240);
}