Created
September 11, 2021 01:04
-
-
Save MeetMartin/42f9858fd2304c15c5a75f9fccadc1a3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { useEffect, useState } from 'react'; | |
import { includes, reduce, lowerCaseOf } from '@7urtle/lambda'; | |
const mobileDevices = ['ipad', 'iphone', 'ipod', 'android']; | |
const includesAnyOf = where => reduce(false)((a, c) => includes(c)(where) ? true : a); | |
const isDeviceMobile = () => | |
includesAnyOf(lowerCaseOf(navigator.userAgent))(mobileDevices) || | |
includesAnyOf(lowerCaseOf(navigator.platform))(mobileDevices) || | |
(includes("Mac")(navigator.userAgent) && "ontouchend" in document); | |
const useDeviceType = () => { | |
const [isMobile, setMobile] = useState(false); | |
useEffect(() => setMobile(isDeviceMobile()), []); | |
return isMobile; | |
}; | |
export default useDeviceType; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment