UNPKG

@supunlakmal/hooks

Version:

A collection of reusable React hooks

46 lines 1.45 kB
import { useState, useCallback, useEffect } from 'react'; const noop = () => { }; /** * Custom hook to interact with the browser's Vibration API. * * @returns An object containing vibration controls and support status. */ export function useVibration() { const [isSupported, setIsSupported] = useState(false); useEffect(() => { // Check support only on the client side setIsSupported(typeof window !== 'undefined' && 'vibrate' in navigator); }, []); const vibrate = useCallback((pattern) => { if (isSupported) { try { navigator.vibrate(pattern); } catch (error) { console.error('Vibration failed:', error); } } else { console.warn('Vibration API not supported.'); } }, [isSupported]); const cancelVibration = useCallback(() => { if (isSupported) { try { navigator.vibrate(0); // Passing 0 cancels vibration } catch (error) { console.error('Failed to cancel vibration:', error); } } else { console.warn('Vibration API not supported.'); } }, [isSupported]); return { isSupported, vibrate: isSupported ? vibrate : noop, cancelVibration: isSupported ? cancelVibration : noop, }; } //# sourceMappingURL=useVibration.js.map