/** * Haptic feedback utility for PWA * - iOS 18+: Uses checkbox switch element hack * - Android: Uses Vibration API * - Other: Silently does nothing */ // Platform detection const isIOS = /iPhone|iPad/.test(navigator.userAgent); const hasVibrate = 'vibrate' in navigator; // Lazy-initialized hidden elements for iOS let checkbox = null; let label = null; function ensureElements() { if (checkbox) return; checkbox = document.createElement('input'); checkbox.type = 'checkbox'; checkbox.setAttribute('switch', ''); checkbox.id = 'haptic-trigger'; checkbox.style.cssText = 'position:fixed;left:-9999px;opacity:0;pointer-events:none;'; label = document.createElement('label'); label.htmlFor = 'haptic-trigger'; label.style.cssText = 'position:fixed;left:-9999px;opacity:0;pointer-events:none;'; document.body.append(checkbox, label); } /** * Trigger a light haptic tap */ export function trigger() { if (isIOS) { ensureElements(); label.click(); } else if (hasVibrate) { navigator.vibrate(10); } } /** * Check if haptics are supported on this device */ export function isSupported() { return isIOS || hasVibrate; }