-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dd8d32
commit d000d85
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
client/packages/lowcoder/src/comps/hooks/screenInfoComp.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { hookToStateComp } from "../generators/hookToComp"; | ||
|
||
enum ScreenTypes { | ||
Mobile = 'mobile', | ||
Tablet = 'tablet', | ||
Desktop = 'desktop', | ||
} | ||
|
||
type ScreenType = typeof ScreenTypes[keyof typeof ScreenTypes] | ||
|
||
type ScreenInfo = { | ||
width?: number; | ||
height?: number; | ||
deviceType?: ScreenType; | ||
isDesktop?: boolean; | ||
isTablet?: boolean; | ||
isMobile?: boolean; | ||
} | ||
|
||
function useScreenInfo() { | ||
const getDeviceType = () => { | ||
if (window.screen.width < 768) return ScreenTypes.Mobile; | ||
if (window.screen.width < 889) return ScreenTypes.Tablet; | ||
return ScreenTypes.Desktop; | ||
} | ||
const getFlagsByDeviceType = (deviceType: ScreenType) => { | ||
const flags = { | ||
isMobile: false, | ||
isTablet: false, | ||
isDesktop: false, | ||
}; | ||
if(deviceType === ScreenTypes.Mobile) { | ||
return { ...flags, isMobile: true }; | ||
} | ||
if(deviceType === ScreenTypes.Tablet) { | ||
return { ...flags, Tablet: true }; | ||
} | ||
return { ...flags, isDesktop: true }; | ||
} | ||
|
||
const getScreenInfo = useCallback(() => { | ||
const { width, height } = window.screen; | ||
const deviceType = getDeviceType(); | ||
const flags = getFlagsByDeviceType(deviceType); | ||
|
||
return { width, height, deviceType, ...flags }; | ||
}, []) | ||
|
||
const [screenInfo, setScreenInfo] = useState<ScreenInfo>({}); | ||
|
||
const updateScreenInfo = useCallback(() => { | ||
setScreenInfo(getScreenInfo()); | ||
}, [getScreenInfo]) | ||
|
||
useEffect(() => { | ||
window.addEventListener('resize', updateScreenInfo); | ||
updateScreenInfo(); | ||
return () => window.removeEventListener('resize', updateScreenInfo); | ||
}, [ updateScreenInfo ]) | ||
|
||
return screenInfo; | ||
} | ||
|
||
export const ScreenInfoHookComp = hookToStateComp(useScreenInfo); |