@@ -3,6 +3,7 @@ import { Resizable, ResizeType } from '../Resizable';
33import { Guid } from 'guid-typescript' ;
44import { CenteredVertically , Centered } from '../Centered' ;
55import { AnchorType , AllProps , IState , ISpaceContext , ISpaceTaker , CenterType } from './Types' ;
6+ import { SpaceContext , SpaceLayerContext } from './Contexts' ;
67
78const getSizeString =
89 ( size : string | number ) => typeof ( size ) === "string" ? size : `${ size } px` ;
@@ -33,10 +34,16 @@ export const initialState = (props: AllProps) => ({
3334 debug : props . debug !== undefined ? props . debug : false ,
3435} )
3536
36- const createContext = ( state : IState , setState : ( stateDelta : Partial < IState > ) => void , parent : ISpaceContext | null ) => {
37+ const createContext = (
38+ state : IState ,
39+ setState : ( stateDelta : Partial < IState > ) => void ,
40+ parent : ISpaceContext | null ,
41+ zIndex : number ) => {
42+
3743 return {
3844 debug : parent ? ( parent . debug ? true : state . debug ) : state . debug ,
3945 level : parent ? parent . level + 1 : 0 ,
46+ zIndex : zIndex ,
4047 width : state . currentWidth ,
4148 height : state . currentHeight ,
4249 spaceTakers : state . spaceTakers ,
@@ -83,6 +90,7 @@ const createContext = (state: IState, setState: (stateDelta: Partial<IState>) =>
8390 } )
8491 }
8592 }
93+
8694}
8795
8896const applyResize = ( props : AllProps , state : IState , setState : ( stateDelta : Partial < IState > ) => void , parentContext : ISpaceContext | null , handleSize : number ) => {
@@ -131,13 +139,24 @@ const applyResize = (props: AllProps, state: IState, setState: (stateDelta: Part
131139 return { resizeHandle : null , resizeType : null } ;
132140}
133141
134- export const calculateSpace = ( props : AllProps , state : IState , setState : ( stateDelta : Partial < IState > ) => void , registerOnRemove : ( handler : ( ) => void ) => void , parentContext : ISpaceContext | null ) => {
142+ const anchorTypes = [
143+ AnchorType . Left ,
144+ AnchorType . Right ,
145+ AnchorType . Bottom ,
146+ AnchorType . Top
147+ ] ;
148+
149+ export const useSpace = ( props : AllProps , state : IState , setState : ( stateDelta : Partial < IState > ) => void , registerOnRemove : ( handler : ( ) => void ) => void ) => {
150+
151+ const parentContext = React . useContext ( SpaceContext ) ;
152+ const currentZIndex = React . useContext ( SpaceLayerContext ) || 0 ;
135153
136154 const outerStyle = {
137155 left : ( state . left !== undefined ? `calc(${ state . left } px)` : undefined ) as string | undefined ,
138156 top : ( state . top !== undefined ? `calc(${ state . top } px)` : undefined ) as string ,
139157 right : ( state . right !== undefined ? `calc(${ state . right } px)` : undefined ) as string ,
140158 bottom : ( state . bottom !== undefined ? `calc(${ state . bottom } px)` : undefined ) as string ,
159+ zIndex : currentZIndex ,
141160 width :
142161 isHorizontalSpace ( props ) ?
143162 `calc(${ getSizeString ( props . anchorSize || 0 ) } + ${ state . adjustedSize } px)`
@@ -158,50 +177,47 @@ export const calculateSpace = (props: AllProps, state: IState, setState: (stateD
158177 let adjustedRight : string [ ] = [ ] ;
159178 let adjustedBottom : string [ ] = [ ] ;
160179
161- [ AnchorType . Left ,
162- AnchorType . Right ,
163- AnchorType . Bottom ,
164- AnchorType . Top ]
165- . forEach ( a => {
166- const spaceTakers =
167- parentContext . spaceTakers
168- . filter ( t => t . anchorType === a )
169- . sort ( ( a , b ) => a . order - b . order ) ;
180+ for ( let at = 0 ; at < anchorTypes . length ; at ++ )
181+ {
182+ const spaceTakers =
183+ parentContext . spaceTakers
184+ . filter ( t => t . zIndex === currentZIndex && t . anchorType === anchorTypes [ at ] )
185+ . sort ( ( a , b ) => a . order - b . order ) ;
170186
171- for ( let i = 0 ; i < spaceTakers . length ; i ++ )
172- {
173- const t = spaceTakers [ i ] ;
174- if ( t . id !== state . id ) {
175- const adjustedSize = t . adjustedSize !== 0 ?` + ${ t . adjustedSize } px` : `` ;
176- if ( isFilledSpace ( props ) )
177- {
178- if ( t . anchorType === AnchorType . Top ) {
179- adjustedTop . push ( `${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
180- } else if ( t . anchorType === AnchorType . Left ) {
181- adjustedLeft . push ( `${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
182- } else if ( t . anchorType === AnchorType . Bottom ) {
183- adjustedBottom . push ( `${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
184- } else if ( t . anchorType === AnchorType . Right ) {
185- adjustedRight . push ( `${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
186- }
187+ for ( let i = 0 ; i < spaceTakers . length ; i ++ )
188+ {
189+ const t = spaceTakers [ i ] ;
190+ if ( t . id !== state . id ) {
191+ const adjustedSize = t . adjustedSize !== 0 ?` + ${ t . adjustedSize } px` : `` ;
192+ if ( isFilledSpace ( props ) )
193+ {
194+ if ( t . anchorType === AnchorType . Top ) {
195+ adjustedTop . push ( `${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
196+ } else if ( t . anchorType === AnchorType . Left ) {
197+ adjustedLeft . push ( `${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
198+ } else if ( t . anchorType === AnchorType . Bottom ) {
199+ adjustedBottom . push ( `${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
200+ } else if ( t . anchorType === AnchorType . Right ) {
201+ adjustedRight . push ( `${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
187202 }
188- else
189- {
190- if ( t . anchorType === AnchorType . Top && outerStyle . top !== undefined ) {
191- adjustedTop . push ( ` ${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
192- } else if ( t . anchorType === AnchorType . Left && outerStyle . left !== undefined ) {
193- adjustedLeft . push ( ` ${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
194- } else if ( t . anchorType === AnchorType . Bottom && outerStyle . bottom !== undefined ) {
195- adjustedBottom . push ( ` ${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
196- } else if ( t . anchorType === AnchorType . Right && outerStyle . right !== undefined ) {
197- adjustedRight . push ( ` ${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
198- }
203+ }
204+ else
205+ {
206+ if ( t . anchorType === AnchorType . Top && outerStyle . top !== undefined ) {
207+ adjustedTop . push ( ` ${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
208+ } else if ( t . anchorType === AnchorType . Left && outerStyle . left !== undefined ) {
209+ adjustedLeft . push ( ` ${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
210+ } else if ( t . anchorType === AnchorType . Bottom && outerStyle . bottom !== undefined ) {
211+ adjustedBottom . push ( ` ${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
212+ } else if ( t . anchorType === AnchorType . Right && outerStyle . right !== undefined ) {
213+ adjustedRight . push ( ` ${ getSizeString ( t . size ) } ${ adjustedSize } ` ) ;
199214 }
200- } else {
201- break ;
202215 }
216+ } else {
217+ break ;
203218 }
204- } ) ;
219+ }
220+ }
205221
206222 [
207223 { adjusted : adjustedTop , setter : ( value : string ) => outerStyle . top = value } ,
@@ -213,6 +229,7 @@ export const calculateSpace = (props: AllProps, state: IState, setState: (stateD
213229 if ( props . anchor ) {
214230 parentContext . registerSpaceTaker ( {
215231 id : state . id ,
232+ zIndex : currentZIndex ,
216233 order : props . order || 1 ,
217234 anchorType : props . anchor ,
218235 size : props . anchorSize || 0 ,
@@ -221,7 +238,7 @@ export const calculateSpace = (props: AllProps, state: IState, setState: (stateD
221238 }
222239 }
223240
224- const currentContext = createContext ( state , setState , parentContext ) ;
241+ const currentContext = createContext ( state , setState , parentContext , currentZIndex ) ;
225242 const handleSize = props . handleSize || 5 ;
226243 const overlayHandle = props . overlayHandle !== undefined ? props . overlayHandle : true ;
227244 let children = props . children ;
@@ -245,12 +262,17 @@ export const calculateSpace = (props: AllProps, state: IState, setState: (stateD
245262 }
246263 } ;
247264
265+ const debug =
266+ parentContext ? parentContext . debug : false ||
267+ props . debug !== undefined ? props . debug : false ;
268+
248269 return {
249270 currentContext,
250271 resizeHandle : resize . resizeHandle ,
251272 resizeType : resize . resizeType ,
252273 outerStyle,
253274 innerStyle,
254- children
275+ children,
276+ debug
255277 }
256278}
0 commit comments