Skip to content

Commit 3752cf3

Browse files
committed
Split effects to separate different types of updates to props
1 parent f8b2980 commit 3752cf3

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

react-spaces/src/Globals/Hooks.ts

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
1414
const setState = (stateDelta: Partial<IState>) => changeState(prev => ({...prev, ...stateDelta}));
1515

1616
const parentContext = React.useContext(SpaceContext);
17-
const layerContext = React.useContext(SpaceLayerContext);
17+
const layerContext = React.useContext(SpaceLayerContext);
1818
const currentZIndex = props.zIndex || layerContext || 0;
19-
const previouszIndex = usePrevious(currentZIndex);
2019

2120
// Deal with property changes to size / anchoring
21+
React.useEffect(() => {
22+
setState({
23+
parsedSize: typeof props.anchorSize === "string" ? 0 : props.anchorSize as number | undefined,
24+
width: isHorizontalSpace(props) ? props.anchorSize || 0 : props.width,
25+
height: isVerticalSpace(props) ? props.anchorSize || 0 : props.height,
26+
adjustedSize: 0
27+
});
28+
parentContext && parentContext.updateSpaceTakerAdjustedSize(state.id, 0);
29+
}, [ props.anchorSize ]);
30+
2231
React.useEffect(() => {
2332
setState({
2433
parsedSize: typeof props.anchorSize === "string" ? 0 : props.anchorSize as number | undefined,
@@ -27,18 +36,14 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
2736
right: props.anchor !== AnchorType.Left ? props.right || 0 : undefined,
2837
bottom: props.anchor !== AnchorType.Top ? props.bottom || 0 : undefined,
2938
width: isHorizontalSpace(props) ? props.anchorSize || 0 : props.width,
30-
height: isVerticalSpace(props) ? props.anchorSize || 0 : props.height,
31-
debug: props.debug !== undefined ? props.debug : false,
32-
adjustedSize: 0
33-
})
34-
if (parentContext) {
35-
parentContext.updateSpaceTakerAdjustedSize(state.id, 0);
36-
if (currentZIndex !== previouszIndex) {
37-
parentContext.updateSpaceTakerLayer(state.id, currentZIndex);
38-
}
39-
}
40-
}, [ currentZIndex, props.left, props.top, props.bottom, props.right, props.width, props.height, props.anchor, props.anchorSize, props.debug ]);
39+
height: isVerticalSpace(props) ? props.anchorSize || 0 : props.height
40+
});
41+
}, [ props.left, props.top, props.bottom, props.right, props.width, props.height, props.anchor ]);
4142

43+
React.useEffect(() => {
44+
parentContext && parentContext.updateSpaceTakerLayer(state.id, currentZIndex);
45+
}, [ currentZIndex ]);
46+
4247
// Setup / cleanup
4348
React.useEffect(() => {
4449
if (divElementRef.current) {
@@ -103,25 +108,25 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
103108
const adjustedSize = t.adjustedSize !== 0 ? ` + ${getSizeString(t.adjustedSize)}` : ``;
104109
if (isFilledSpace(props))
105110
{
106-
if (t.anchorType === AnchorType.Top) {
111+
if (t.anchorType === AnchorType.Top && t.size) {
107112
adjustedTop.push(`${getSizeString(t.size)}${adjustedSize}`);
108-
} else if (t.anchorType === AnchorType.Left) {
113+
} else if (t.anchorType === AnchorType.Left && t.size) {
109114
adjustedLeft.push(`${getSizeString(t.size)}${adjustedSize}`);
110-
} else if (t.anchorType === AnchorType.Bottom) {
115+
} else if (t.anchorType === AnchorType.Bottom && t.size) {
111116
adjustedBottom.push(`${getSizeString(t.size)}${adjustedSize}`);
112-
} else if (t.anchorType === AnchorType.Right) {
117+
} else if (t.anchorType === AnchorType.Right && t.size) {
113118
adjustedRight.push(`${getSizeString(t.size)}${adjustedSize}`);
114119
}
115120
}
116121
else
117122
{
118-
if (t.anchorType === AnchorType.Top && outerStyle.top !== undefined) {
123+
if (t.anchorType === AnchorType.Top && t.size && outerStyle.top !== undefined) {
119124
adjustedTop.push(`${getSizeString(t.size)}${adjustedSize}`);
120-
} else if (t.anchorType === AnchorType.Left && outerStyle.left !== undefined) {
125+
} else if (t.anchorType === AnchorType.Left && t.size && outerStyle.left !== undefined) {
121126
adjustedLeft.push(`${getSizeString(t.size)}${adjustedSize}`);
122-
} else if (t.anchorType === AnchorType.Bottom && outerStyle.bottom !== undefined) {
127+
} else if (t.anchorType === AnchorType.Bottom && t.size && outerStyle.bottom !== undefined) {
123128
adjustedBottom.push(`${getSizeString(t.size)}${adjustedSize}`);
124-
} else if (t.anchorType === AnchorType.Right && outerStyle.right !== undefined) {
129+
} else if (t.anchorType === AnchorType.Right && t.size && outerStyle.right !== undefined) {
125130
adjustedRight.push(`${getSizeString(t.size)}${adjustedSize}`);
126131
}
127132
}
@@ -178,7 +183,6 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
178183
[
179184
...[
180185
"spaces-space",
181-
resize.resizeType || undefined,
182186
props.scrollable ? (resize.resizeHandle ? "scrollable" : "scrollable-a") : undefined,
183187
debug ? 'debug' : undefined
184188
],
@@ -204,20 +208,6 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
204208
}
205209
}
206210

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-
221211
export const useParentSpace = () => {
222212
const parentContext = React.useContext(SpaceContext);
223213

0 commit comments

Comments
 (0)