Skip to content

Commit d45dc32

Browse files
authored
Merge pull request #44 from aeagle/fix/42-zindex-change
Ensure zIndex changes on space refreshes related spaces in same parent
2 parents 9722809 + 367550b commit d45dc32

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

react-spaces/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-spaces",
3-
"version": "0.1.18",
3+
"version": "0.1.19",
44
"main": "dist/index.js",
55
"module": "dist/es/index.js",
66
"types": "dist/index.d.ts",

react-spaces/src/Globals/Hooks.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
1616
const parentContext = React.useContext(SpaceContext);
1717
const layerContext = React.useContext(SpaceLayerContext);
1818
const currentZIndex = props.zIndex || layerContext || 0;
19+
const previouszIndex = usePrevious(currentZIndex);
1920

2021
// Deal with property changes to size / anchoring
2122
React.useEffect(() => {
@@ -32,8 +33,11 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
3233
})
3334
if (parentContext) {
3435
parentContext.updateSpaceTakerAdjustedSize(state.id, 0);
36+
if (currentZIndex !== previouszIndex) {
37+
parentContext.updateSpaceTakerLayer(state.id, currentZIndex);
38+
}
3539
}
36-
}, [ props.zIndex, props.left, props.top, props.bottom, props.right, props.width, props.height, props.anchor, props.anchorSize, props.debug ]);
40+
}, [ currentZIndex, props.left, props.top, props.bottom, props.right, props.width, props.height, props.anchor, props.anchorSize, props.debug ]);
3741

3842
// Setup / cleanup
3943
React.useEffect(() => {
@@ -200,6 +204,20 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
200204
}
201205
}
202206

207+
function usePrevious<T>(value: T) {
208+
// The ref object is a generic container whose current property is mutable ...
209+
// ... and can hold any value, similar to an instance property on a class
210+
const ref = React.useRef<T>();
211+
212+
// Store current value in ref
213+
React.useEffect(() => {
214+
ref.current = value;
215+
}, [value]); // Only re-run if value changes
216+
217+
// Return previous value (happens before update in useEffect above)
218+
return ref.current;
219+
}
220+
203221
export const useParentSpace = () => {
204222
const parentContext = React.useContext(SpaceContext);
205223

react-spaces/src/Globals/Types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export interface ISpaceContext {
162162
registerSpaceTaker: (spaceTaker: ISpaceTaker) => void,
163163
removeSpaceTaker: (id: string) => void,
164164
updateSpaceTakerAdjustedSize: (id: string, adjustedSize: number) => void,
165+
updateSpaceTakerLayer: (id: string, zIndex: number) => void,
165166
updateDebug: (id: string, debug: boolean) => void,
166167
startDrag: (e: React.MouseEvent) => void
167168
}

react-spaces/src/Globals/Utils.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ export const createContext = (
7171
updateSpaceTakerAdjustedSize:
7272
(id: string, adjustedSize: number) =>
7373
setState({ spaceTakers: state.spaceTakers.map(t => t.id === id ? {...t, ...{ adjustedSize: adjustedSize }} : t) }),
74+
updateSpaceTakerLayer:
75+
(id: string, zIndex: number) =>
76+
setState({ spaceTakers: state.spaceTakers.map(t => t.id === id ? {...t, ...{ zIndex: zIndex }} : t) }),
7477
updateDebug:
7578
(id: string, debug: boolean) =>
7679
setState({ spaceTakers: state.spaceTakers.map(t => t.id === id ? {...t, ...{ debug: debug }} : t) }),

0 commit comments

Comments
 (0)