Skip to content

[URH-66] useDeviceDetect 신규 #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 29, 2024
Next Next commit
✨ feat: useDeviceDetect 기본 형태 작성
  • Loading branch information
foresec committed Aug 23, 2024
commit 23fd480e3590ef00844bcf654a44d355fd9b00bb
32 changes: 32 additions & 0 deletions src/hooks/useDeviceDetect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useEffect, useState } from 'react';
import { isClient } from '../utils';

const useDeviceDetect = () => {
const [deviceType, setDeviceType] = useState({
isMobile: false,
isTablet: false,
isDesktop: false,
});

useEffect(() => {
if (!isClient) return;

const UA = navigator.userAgent;

const isMobile = /Mobi/i.test(UA);
const isTablet = /Tablet|iPad/i.test(UA);
const isDesktop = !isMobile && !isTablet;

setDeviceType({
isMobile,
isTablet,
isDesktop,
});
}, []);

const { isMobile, isTablet, isDesktop } = deviceType;

return { isMobile, isTablet, isDesktop };
};

export default useDeviceDetect;