guandan.dev

guandan.dev

https://git.tonybtw.com/guandan.dev.git git://git.tonybtw.com/guandan.dev.git
630 bytes raw
1
import { useState, useEffect } from 'react'
2
3
// Detect mobile: small width OR small height (catches landscape phones)
4
function check_is_mobile(): boolean {
5
  if (typeof window === 'undefined') return false
6
  return window.innerWidth < 768 || window.innerHeight < 500
7
}
8
9
export function use_is_mobile(): boolean {
10
  const [is_mobile, set_is_mobile] = useState(check_is_mobile)
11
12
  useEffect(() => {
13
    const handle_resize = () => {
14
      set_is_mobile(check_is_mobile())
15
    }
16
17
    window.addEventListener('resize', handle_resize)
18
    return () => window.removeEventListener('resize', handle_resize)
19
  }, [])
20
21
  return is_mobile
22
}