device.ts 752 Bytes
const MOBILE_OR_TABLET_REGEX = /android|iphone|ipad|ipod|mobile|tablet|kindle|silk|playbook|bb10|iemobile|opera mini|webos/i
const MOBILE_MAX_VIEWPORT_WIDTH = 1024

export function isMobileOrTabletUserAgent(userAgent: string | null | undefined) {
  if (!userAgent) {
    return false
  }

  return MOBILE_OR_TABLET_REGEX.test(userAgent)
}

export function isMobileOrTablet() {
  let userAgent = ''

  if (import.meta.client) {
    userAgent = navigator.userAgent
  } else {
    const headers = useRequestHeaders(['user-agent'])
    userAgent = headers['user-agent'] ?? ''
  }

  if (isMobileOrTabletUserAgent(userAgent)) {
    return true
  }

  if (import.meta.client) {
    return window.innerWidth <= MOBILE_MAX_VIEWPORT_WIDTH
  }

  return false
}