UNPKG

use-mobile-detect-hook

Version:

The React hook to detect if the device is mobile or desktop.

24 lines (23 loc) 876 B
"use strict"; const { useEffect } = require('react'); const getMobileDetect = (userAgent) => { const isAndroid = () => Boolean(userAgent.match(/Android/i)); const isIos = () => Boolean(userAgent.match(/iPhone|iPad|iPod/i)); const isOpera = () => Boolean(userAgent.match(/Opera Mini/i)); const isWindows = () => Boolean(userAgent.match(/IEMobile/i)); const isSSR = () => Boolean(userAgent.match(/SSR/i)); const isMobile = () => Boolean(isAndroid() || isIos() || isOpera() || isWindows()); const isDesktop = () => Boolean(!isMobile() && !isSSR()); return { isMobile, isDesktop, isAndroid, isIos, isSSR }; }; const useMobileDetect = () => { const userAgent = typeof navigator === 'undefined' ? 'SSR' : navigator.userAgent; return getMobileDetect(userAgent); }; module.exports = useMobileDetect;