Files
fotospiel-app/resources/js/shared/guest/lib/haptics.ts
Codex Agent 0a08f2704f
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
refactor(guest): retire legacy guest app and move shared modules
2026-02-06 08:42:53 +01:00

63 lines
1.5 KiB
TypeScript

import { prefersReducedMotion } from './motion';
export type HapticPattern = 'selection' | 'light' | 'medium' | 'success' | 'error';
const PATTERNS: Record<HapticPattern, number | number[]> = {
selection: 10,
light: 15,
medium: 30,
success: [10, 30, 10],
error: [20, 30, 20],
};
export const HAPTICS_STORAGE_KEY = 'guestHapticsEnabled';
export function supportsHaptics(): boolean {
return typeof navigator !== 'undefined' && typeof navigator.vibrate === 'function';
}
export function getHapticsPreference(): boolean {
if (typeof window === 'undefined') {
return true;
}
try {
const raw = window.localStorage.getItem(HAPTICS_STORAGE_KEY);
if (raw === null) {
return true;
}
return raw !== '0';
} catch (error) {
console.warn('Failed to read haptics preference', error);
return true;
}
}
export function setHapticsPreference(enabled: boolean): void {
if (typeof window === 'undefined') {
return;
}
try {
window.localStorage.setItem(HAPTICS_STORAGE_KEY, enabled ? '1' : '0');
} catch (error) {
console.warn('Failed to store haptics preference', error);
}
}
export function isHapticsEnabled(): boolean {
return getHapticsPreference() && supportsHaptics() && !prefersReducedMotion();
}
export function triggerHaptic(pattern: HapticPattern): void {
if (!isHapticsEnabled()) {
return;
}
try {
navigator.vibrate(PATTERNS[pattern]);
} catch (error) {
console.warn('Haptic feedback failed', error);
}
}