1
- import { useCallback , useEffect , useState } from "react" ;
1
+ import { useCallback , useEffect , useMemo , useState } from "react" ;
2
2
import { hookToStateComp } from "../generators/hookToComp" ;
3
+ import { CanvasContainerID } from "@lowcoder-ee/index.sdk" ;
3
4
4
5
enum ScreenTypes {
5
6
Mobile = 'mobile' ,
@@ -19,9 +20,13 @@ type ScreenInfo = {
19
20
}
20
21
21
22
function useScreenInfo ( ) {
22
- const getDeviceType = ( ) => {
23
- if ( window . innerWidth < 768 ) return ScreenTypes . Mobile ;
24
- if ( window . innerWidth < 889 ) return ScreenTypes . Tablet ;
23
+ const canvasContainer = document . getElementById ( CanvasContainerID ) ;
24
+ const canvas = document . getElementsByClassName ( 'lowcoder-app-canvas' ) ?. [ 0 ] ;
25
+ const canvasWidth = canvasContainer ?. clientWidth || canvas ?. clientWidth ;
26
+
27
+ const getDeviceType = ( width : number ) => {
28
+ if ( width < 768 ) return ScreenTypes . Mobile ;
29
+ if ( width < 889 ) return ScreenTypes . Tablet ;
25
30
return ScreenTypes . Desktop ;
26
31
}
27
32
const getFlagsByDeviceType = ( deviceType : ScreenType ) => {
@@ -41,16 +46,17 @@ function useScreenInfo() {
41
46
42
47
const getScreenInfo = useCallback ( ( ) => {
43
48
const { innerWidth, innerHeight } = window ;
44
- const deviceType = getDeviceType ( ) ;
49
+ const deviceType = getDeviceType ( canvasWidth || window . innerWidth ) ;
45
50
const flags = getFlagsByDeviceType ( deviceType ) ;
46
51
47
52
return {
48
53
width : innerWidth ,
49
54
height : innerHeight ,
55
+ canvasWidth,
50
56
deviceType,
51
57
...flags
52
58
} ;
53
- } , [ ] )
59
+ } , [ canvasWidth ] )
54
60
55
61
const [ screenInfo , setScreenInfo ] = useState < ScreenInfo > ( { } ) ;
56
62
@@ -64,6 +70,10 @@ function useScreenInfo() {
64
70
return ( ) => window . removeEventListener ( 'resize' , updateScreenInfo ) ;
65
71
} , [ updateScreenInfo ] )
66
72
73
+ useEffect ( ( ) => {
74
+ updateScreenInfo ( ) ;
75
+ } , [ canvasWidth ] ) ;
76
+
67
77
return screenInfo ;
68
78
}
69
79
0 commit comments