manifold/web/hooks/use-is-mobile.ts

14 lines
455 B
TypeScript
Raw Permalink Normal View History

import { useEffect, useState } from 'react'
2022-09-16 15:28:39 +00:00
export function useIsMobile(threshold?: number) {
const [isMobile, setIsMobile] = useState<boolean>()
useEffect(() => {
// 640 matches tailwind sm breakpoint
const onResize = () => setIsMobile(window.innerWidth < (threshold ?? 640))
onResize()
window.addEventListener('resize', onResize)
return () => window.removeEventListener('resize', onResize)
}, [threshold])
return isMobile
2022-09-16 15:30:09 +00:00
}