25 lines
618 B
TypeScript
25 lines
618 B
TypeScript
import { useEffect, useState } from 'react'
|
|
|
|
export function useIsMobile(breakpoint = 768): boolean {
|
|
const [isMobile, setIsMobile] = useState<boolean>(() => {
|
|
if (typeof window === 'undefined') return false
|
|
return window.innerWidth <= breakpoint
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (typeof window === 'undefined') return
|
|
|
|
function checkMobile() {
|
|
setIsMobile(window.innerWidth <= breakpoint)
|
|
}
|
|
|
|
// Initialize state
|
|
checkMobile()
|
|
|
|
window.addEventListener('resize', checkMobile)
|
|
return () => window.removeEventListener('resize', checkMobile)
|
|
}, [breakpoint])
|
|
|
|
return isMobile
|
|
}
|