11import * as React from 'react' ;
22import { AllProps , IState , AnchorType , ISize } from 'src/Globals/Types' ;
33import { initialState , isHorizontalSpace , isVerticalSpace } from 'src/Globals/Utils' ;
4- import { ISpaceContext , updateSpace , removeSpace , registerSpace , createSpaceContext , getSpace } from 'src/Globals/ISpaceContext' ;
4+ import { ISpaceContext , updateSpace , removeSpace , registerSpace , createSpaceContext } from 'src/Globals/ISpaceContext' ;
55import { SpaceLayerContext , SpaceContext } from 'src/Globals/Contexts' ;
66import { ResizeSensor } from 'css-element-queries' ;
77import { throttle } from 'src/Globals/Throttle' ;
88
99export const useSpace = ( props : AllProps , divElementRef : React . MutableRefObject < HTMLElement | undefined > ) => {
1010
1111 const [ state , changeState ] = React . useState < IState > ( initialState ( props ) ) ;
12- const [ currentSize , setCurrentSize ] = React . useState < ISize > ( { parsedSize : undefined , width : 0 , height : 0 } ) ;
12+ const [ currentSize , setCurrentSize ] = React . useState < ISize | null > ( null ) ;
1313 const resizeSensor = React . useRef < ResizeSensor > ( ) ;
1414 const onRemove = React . useRef < ( ( ) => void ) > ( ) ;
1515
@@ -24,12 +24,12 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
2424 const layerContext = React . useContext ( SpaceLayerContext ) ;
2525 const currentZIndex = props . zIndex || layerContext || 0 ;
2626
27- // Deal with property changes to size / anchoring
27+ // Deal with property changes to size / zIndex / anchoring
2828 React . useEffect ( ( ) => {
2929 setCurrentSize ( prev => ( {
3030 parsedSize : typeof props . anchorSize === "string" ? 0 : props . anchorSize as number | undefined ,
31- width : prev . width ,
32- height : prev . height
31+ width : prev ? prev . width : 0 ,
32+ height : prev ? prev . height : 0
3333 } ) ) ;
3434 updateSpace (
3535 parentContext ,
@@ -45,8 +45,8 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
4545 React . useEffect ( ( ) => {
4646 setCurrentSize ( prev => ( {
4747 parsedSize : typeof props . anchorSize === "string" ? 0 : props . anchorSize as number | undefined ,
48- width : prev . width ,
49- height : prev . height
48+ width : prev ? prev . width : 0 ,
49+ height : prev ? prev . height : 0
5050 } ) ) ;
5151 updateSpace (
5252 parentContext ,
@@ -71,8 +71,36 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
7171 }
7272 ) ;
7373 } , [ currentZIndex ] ) ;
74+
75+ const space =
76+ registerSpace (
77+ parentContext ,
78+ {
79+ id : state . id ,
80+ zIndex : currentZIndex ,
81+ order : props . order === undefined ? 1 : props . order ,
82+ anchorType : props . anchor ,
83+ size : props . anchorSize || 0 ,
84+ adjustedSize : 0 ,
85+ adjustedLeft : 0 ,
86+ adjustedTop : 0 ,
87+ width : props . anchor && isHorizontalSpace ( props . anchor ) ? props . anchorSize || 0 : props . width ,
88+ height : props . anchor && isVerticalSpace ( props . anchor ) ? props . anchorSize || 0 : props . height
89+ }
90+ ) ;
7491
7592 // Setup / cleanup
93+ const determineCurrentSize = React . useCallback ( throttle ( ( ) => {
94+ if ( divElementRef . current ) {
95+ const currentRect = divElementRef . current . getBoundingClientRect ( ) ;
96+ setCurrentSize ( {
97+ parsedSize : ( isHorizontalSpace ( props . anchor ) ? currentRect . width : currentRect . height ) ,
98+ width : currentRect . width ,
99+ height : currentRect . height
100+ } ) ;
101+ }
102+ } , 200 ) , [ ] ) ;
103+
76104 React . useEffect ( ( ) => {
77105 if ( divElementRef . current ) {
78106 if ( props . trackSize ) {
@@ -94,58 +122,18 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
94122 }
95123
96124 return cleanup ;
97- } , [ ] )
98-
99- const determineCurrentSize = React . useCallback ( throttle ( ( ) => {
100- var space = getSpace ( parentContext , state . id ) ;
101- if ( space && divElementRef . current ) {
102- const currentRect = divElementRef . current . getBoundingClientRect ( ) ;
103- if ( currentRect . width !== currentSize . width ||
104- currentRect . height !== currentSize . height ||
105- ( isHorizontalSpace ( props . anchor ) ? currentRect . width : currentRect . height ) !== currentSize . parsedSize ) {
106- setCurrentSize ( {
107- parsedSize : ( isHorizontalSpace ( props . anchor ) ? currentRect . width : currentRect . height ) ,
108- width : currentRect . width ,
109- height : currentRect . height
110- } ) ;
111- }
112- }
113- } , 200 ) , [ ] ) ;
114-
115- React . useEffect ( ( ) => {
116- if ( ! props . trackSize ) {
117- determineCurrentSize ( ) ;
118- }
119- } ) ;
125+ } , [ ] ) ;
120126
121127 onRemove . current = ( ) => {
122128 removeSpace ( parentContext , state . id ) ;
123129 }
124130
125- const space =
126- registerSpace (
127- parentContext ,
128- {
129- id : state . id ,
130- zIndex : currentZIndex ,
131- order : props . order === undefined ? 1 : props . order ,
132- anchorType : props . anchor ,
133- size : props . anchorSize || 0 ,
134- adjustedSize : 0 ,
135- adjustedLeft : 0 ,
136- adjustedTop : 0 ,
137- width : props . anchor && isHorizontalSpace ( props . anchor ) ? props . anchorSize || 0 : props . width ,
138- height : props . anchor && isVerticalSpace ( props . anchor ) ? props . anchorSize || 0 : props . height
139- }
140- ) ;
141-
142131 const currentContext =
143132 createSpaceContext (
144133 state . children ,
145134 ( children ) => setState ( { children : children } ) ,
146135 space ,
147- parentContext ,
148- currentZIndex
136+ parentContext
149137 ) ;
150138
151139 return {
0 commit comments