Created
May 11, 2023 14:23
-
-
Save tgmarinho/1c689c975c3678a68456c41dfe910c7d to your computer and use it in GitHub Desktop.
react hook useDeviceDetect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getMobileDetect = (userAgent: string) => { | |
const isAndroid = (): boolean => Boolean(userAgent.match(/Android/i)) | |
const isIos = (): boolean => Boolean(userAgent.match(/iPhone|iPad|iPod/i)) | |
const isOpera = (): boolean => Boolean(userAgent.match(/Opera Mini/i)) | |
const isWindows = (): boolean => Boolean(userAgent.match(/IEMobile/i)) | |
const isSSR = (): boolean => Boolean(userAgent.match(/SSR/i)) | |
const isMobile = (): boolean => Boolean(isAndroid() || isIos() || isOpera() || isWindows()) | |
const isDesktop = (): boolean => Boolean(!isMobile() && !isSSR()) | |
return { | |
isMobile, | |
isDesktop, | |
isAndroid, | |
isIos, | |
isSSR, | |
} | |
} | |
export const useDeviceDetect = () => { | |
const userAgent = typeof navigator === 'undefined' ? 'SSR' : navigator.userAgent | |
return getMobileDetect(userAgent) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment