import { debounce } from '@tamagui/use-debounce' ; import { useForceUpdate } from '@tamagui/use-force-update' ; import { useEffect , useLayoutEffect } from 'react' ; import { Dimensions , Platform } from 'react-native' ; export const isWeb = process . env . TAMAGUI_TARGET === 'web' ? true : Platform ?. OS === 'web' ; export const isSSR = isWeb && typeof window === 'undefined' ; export const useIsomorphicLayoutEffect = isSSR ? useEffect : useLayoutEffect ; type WindowSize = [ number , number ] ; const idFn = ( _ ) => _ ; const getWindowSize = ( ) : WindowSize => [ Dimensions . get ( 'window' ) . width , Dimensions . get ( 'window' ) . height , ] ; class WindowSizeStore { listeners = new Set < Function > ( ) ; size = getWindowSize ( ) ; update = ( ) => { this . size = getWindowSize ( ) ; this . listeners . forEach ( ( x ) => x ( ) ) ; } ; } let store = null ; function createStore ( ) { if ( ! store ) { store = new WindowSizeStore ( ) ; Dimensions . addEventListener ( 'change' , debounce ( store . update , 40 ) ) ; } } export function useWindowSize ( { adjust = idFn , } = { } ) { createStore ( ) ; const size = store ! . size ; const forceUpdate = useForceUpdate ( ) ; useIsomorphicLayoutEffect ( ( ) => { store ! . listeners . add ( forceUpdate ) ; return ( ) => { store ! . listeners . delete ( forceUpdate ) ; } ; } , [ adjust ] ) ; return adjust ( size ) ; }