11import * as React from 'react' ;
2- import { AllProps , IState , AnchorType } from 'src/Globals/Types' ;
2+ import { AllProps , IState , AnchorType , ISize } from 'src/Globals/Types' ;
33import { initialState , isHorizontalSpace , isVerticalSpace } from 'src/Globals/Utils' ;
44import { ISpaceContext , updateSpace , removeSpace , registerSpace , createSpaceContext , getSpace } from 'src/Globals/ISpaceContext' ;
55import { SpaceLayerContext , SpaceContext } from 'src/Globals/Contexts' ;
66import { ResizeSensor } from 'css-element-queries' ;
7+ import { throttle } from 'src/Globals/Throttle' ;
78
89export const useSpace = ( props : AllProps , divElementRef : React . MutableRefObject < HTMLElement | undefined > ) => {
910
1011 const [ state , changeState ] = React . useState < IState > ( initialState ( props ) ) ;
12+ const [ currentSize , setCurrentSize ] = React . useState < ISize > ( { parsedSize : undefined , width : 0 , height : 0 } ) ;
1113 const resizeSensor = React . useRef < ResizeSensor > ( ) ;
1214 const onRemove = React . useRef < ( ( ) => void ) > ( ) ;
1315
@@ -24,11 +26,15 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
2426
2527 // Deal with property changes to size / anchoring
2628 React . useEffect ( ( ) => {
29+ setCurrentSize ( prev => ( {
30+ parsedSize : typeof props . anchorSize === "string" ? 0 : props . anchorSize as number | undefined ,
31+ width : prev . width ,
32+ height : prev . height
33+ } ) ) ;
2734 updateSpace (
2835 parentContext ,
2936 state . id ,
30- {
31- parsedSize : typeof props . anchorSize === "string" ? 0 : props . anchorSize as number | undefined ,
37+ {
3238 width : isHorizontalSpace ( props . anchor ) ? props . anchorSize || 0 : props . width ,
3339 height : isVerticalSpace ( props . anchor ) ? props . anchorSize || 0 : props . height ,
3440 adjustedSize : 0
@@ -37,11 +43,15 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
3743 } , [ props . anchorSize ] ) ;
3844
3945 React . useEffect ( ( ) => {
46+ setCurrentSize ( prev => ( {
47+ parsedSize : typeof props . anchorSize === "string" ? 0 : props . anchorSize as number | undefined ,
48+ width : prev . width ,
49+ height : prev . height
50+ } ) ) ;
4051 updateSpace (
4152 parentContext ,
4253 state . id ,
4354 {
44- parsedSize : typeof props . anchorSize === "string" ? 0 : props . anchorSize as number | undefined ,
4555 left : props . anchor !== AnchorType . Right ? props . left || 0 : undefined ,
4656 top : props . anchor !== AnchorType . Bottom ? props . top || 0 : undefined ,
4757 right : props . anchor !== AnchorType . Left ? props . right || 0 : undefined ,
@@ -68,18 +78,14 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
6878 if ( props . trackSize ) {
6979 resizeSensor . current = new ResizeSensor (
7080 divElementRef . current ,
71- ( size ) =>
72- updateSpace (
73- parentContext ,
74- state . id ,
75- {
76- parsedSize : ( isHorizontalSpace ( props . anchor ) ? size . width : size . height ) ,
77- currentWidth : size . width ,
78- currentHeight : size . height
79- }
80- )
81+ ( size ) => setCurrentSize ( {
82+ parsedSize : ( isHorizontalSpace ( props . anchor ) ? size . width : size . height ) ,
83+ width : size . width ,
84+ height : size . height
85+ } )
8186 ) ;
8287 }
88+ determineCurrentSize ( ) ;
8389 }
8490
8591 const cleanup = ( ) => {
@@ -90,19 +96,25 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
9096 return cleanup ;
9197 } , [ ] )
9298
93- React . useEffect ( ( ) => {
99+ const determineCurrentSize = React . useCallback ( throttle ( ( ) => {
94100 var space = getSpace ( parentContext , state . id ) ;
95- if ( space && divElementRef . current && ! space . parsedSize ) {
101+ if ( space && divElementRef . current ) {
96102 const currentRect = divElementRef . current . getBoundingClientRect ( ) ;
97- updateSpace (
98- parentContext ,
99- state . id ,
100- {
103+ if ( currentRect . width !== currentSize . width ||
104+ currentRect . height !== currentSize . height ||
105+ ( isHorizontalSpace ( props . anchor ) ? currentRect . width : currentRect . height ) !== currentSize . parsedSize ) {
106+ setCurrentSize ( {
101107 parsedSize : ( isHorizontalSpace ( props . anchor ) ? currentRect . width : currentRect . height ) ,
102- currentWidth : currentRect . width ,
103- currentHeight : currentRect . height
104- }
105- ) ;
108+ width : currentRect . width ,
109+ height : currentRect . height
110+ } ) ;
111+ }
112+ }
113+ } , 200 ) , [ ] ) ;
114+
115+ React . useEffect ( ( ) => {
116+ if ( ! props . trackSize ) {
117+ determineCurrentSize ( ) ;
106118 }
107119 } ) ;
108120
@@ -120,11 +132,8 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
120132 anchorType : props . anchor ,
121133 size : props . anchorSize || 0 ,
122134 adjustedSize : 0 ,
123- currentWidth : 0 ,
124- currentHeight : 0 ,
125135 adjustedLeft : 0 ,
126136 adjustedTop : 0 ,
127- parsedSize : typeof props . anchorSize === "string" ? 0 : props . anchorSize as number | undefined ,
128137 width : props . anchor && isHorizontalSpace ( props . anchor ) ? props . anchorSize || 0 : props . width ,
129138 height : props . anchor && isVerticalSpace ( props . anchor ) ? props . anchorSize || 0 : props . height
130139 }
@@ -141,9 +150,8 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
141150
142151 return {
143152 space,
153+ currentSize,
144154 parentContext,
145- currentContext,
146- currentWidth : space . currentWidth ,
147- currentHeight : space . currentHeight
155+ currentContext
148156 }
149157}
0 commit comments