Skip to content

Instantly share code, notes, and snippets.

@MeetMartin
Created September 11, 2021 01:04
Show Gist options
  • Save MeetMartin/42f9858fd2304c15c5a75f9fccadc1a3 to your computer and use it in GitHub Desktop.
Save MeetMartin/42f9858fd2304c15c5a75f9fccadc1a3 to your computer and use it in GitHub Desktop.
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