1- import { useCallback , useEffect , useState } from "react" ;
1+ import { useCallback , useEffect , useMemo , useState } from "react" ;
22import { hookToStateComp } from "../generators/hookToComp" ;
3+ import { CanvasContainerID } from "@lowcoder-ee/index.sdk" ;
34
45enum ScreenTypes {
56 Mobile = 'mobile' ,
@@ -19,9 +20,13 @@ type ScreenInfo = {
1920}
2021
2122function 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 ;
2530 return ScreenTypes . Desktop ;
2631 }
2732 const getFlagsByDeviceType = ( deviceType : ScreenType ) => {
@@ -41,16 +46,17 @@ function useScreenInfo() {
4146
4247 const getScreenInfo = useCallback ( ( ) => {
4348 const { innerWidth, innerHeight } = window ;
44- const deviceType = getDeviceType ( ) ;
49+ const deviceType = getDeviceType ( canvasWidth || window . innerWidth ) ;
4550 const flags = getFlagsByDeviceType ( deviceType ) ;
4651
4752 return {
4853 width : innerWidth ,
4954 height : innerHeight ,
55+ canvasWidth,
5056 deviceType,
5157 ...flags
5258 } ;
53- } , [ ] )
59+ } , [ canvasWidth ] )
5460
5561 const [ screenInfo , setScreenInfo ] = useState < ScreenInfo > ( { } ) ;
5662
@@ -64,6 +70,10 @@ function useScreenInfo() {
6470 return ( ) => window . removeEventListener ( 'resize' , updateScreenInfo ) ;
6571 } , [ updateScreenInfo ] )
6672
73+ useEffect ( ( ) => {
74+ updateScreenInfo ( ) ;
75+ } , [ canvasWidth ] ) ;
76+
6777 return screenInfo ;
6878}
6979
0 commit comments