Files
standardnotes-app-web/packages/web/src/javascripts/Hooks/useIsTabletOrMobileScreen.tsx
Mo c928f8a281 fix: fixes issue where add button would not be visible at times on iOS (#2087)
* fix: fixes issue where add button would not be visible at times on iOS

* fix: fixes issue where screens could not be navigated with certain window sizes
2022-12-05 15:50:19 -06:00

42 lines
1.2 KiB
TypeScript

import { WebApplication } from '@/Application/Application'
import { useApplication } from '@/Components/ApplicationProvider'
import { debounce, isMobileScreen, isTabletOrMobileScreen, isTabletScreen } from '@/Utils'
import { useEffect, useState } from 'react'
export function getIsTabletOrMobileScreen(application: WebApplication) {
const isNativeMobile = application.isNativeMobileWeb()
const isTabletOrMobile = isTabletOrMobileScreen() || isNativeMobile
const isTablet = isTabletScreen() || (isNativeMobile && !isMobileScreen())
const isMobile = isMobileScreen() || (isNativeMobile && !isTablet)
if (isTablet && isMobile) {
throw Error('isTablet and isMobile cannot both be true')
}
return {
isTabletOrMobile,
isTablet,
isMobile,
}
}
export default function useIsTabletOrMobileScreen() {
const [_windowSize, setWindowSize] = useState(0)
const application = useApplication()
useEffect(() => {
const handleResize = debounce(() => {
setWindowSize(window.innerWidth)
}, 100)
window.addEventListener('resize', handleResize)
handleResize()
return () => {
window.removeEventListener('resize', handleResize)
}
}, [])
return getIsTabletOrMobileScreen(application)
}