Skip to content

Commit 224a46c

Browse files
committed
Resize and track size fixes
1 parent 31eaf17 commit 224a46c

File tree

11 files changed

+275
-284
lines changed

11 files changed

+275
-284
lines changed

demo/src/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react';
22
import './App.scss';
33
import ReactGA from 'react-ga';
4-
import { Test } from './Test';
5-
// import { UI } from './ui-demo/UI';
4+
// import { Test } from './Test';
5+
import { UI } from './ui-demo/UI';
66

77
ReactGA.initialize("UA-144490437-1");
88
ReactGA.pageview(window.location.pathname + window.location.search);
99

1010
const App: React.FC = () => {
11-
return <Test />
11+
return <UI />
1212
}
1313

1414
export default App;

demo/src/Test.tsx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,42 @@ const COLLAPSED_SIZE = 20;
77
const MINIMUM_SIZE = 200;
88

99
export const Test : React.FC = () => {
10-
const [ size, setSize ] = React.useState(300);
10+
const [ size, setSize ] = React.useState("25%");
1111

1212
const onResizeStart = () => {
1313
};
1414

1515
const onResizeEnd = (width: number) => {
16-
setSize(width);
16+
// setSize(width);
1717
}
1818

1919
return (
2020
<Space.ViewPort className="test">
2121

22-
<Space.LeftResizable
22+
<Space.Left
2323
style={{ backgroundColor: 'yellow', padding: 10 }}
2424
size={size}
25-
handleSize={60}
26-
maximumSize={600}
27-
minimumSize={100}
28-
onResizeStart={onResizeStart}
29-
onResizeEnd={onResizeEnd}
30-
trackSize={true}
25+
trackSize={false}
3126
order={1}>
3227
<Space.Info>
3328
{(info) => <span>Hello<br />{info.width} x {info.height}</span>}
3429
</Space.Info>
35-
</Space.LeftResizable>
30+
<div>Something else</div>
31+
</Space.Left>
3632

37-
<Space.LeftResizable
33+
<Space.Left
3834
style={{ backgroundColor: 'navy', color: 'white', padding: 10 }}
3935
size={200}
40-
trackSize={true}
36+
trackSize={false}
4137
order={2}>
4238
<Space.Info>
4339
{(info) => <span>Something<br />{info.width} x {info.height}</span>}
4440
</Space.Info>
45-
</Space.LeftResizable>
41+
</Space.Left>
4642

4743
<Space.Fill
4844
style={{ backgroundColor: 'red', padding: 10 }}
49-
trackSize={true}>
45+
trackSize={false}>
5046
<Space.Info>
5147
{(info) => <span>World<br />{info.width} x {info.height}</span>}
5248
</Space.Info>

react-spaces/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-spaces",
3-
"version": "0.1.19",
3+
"version": "0.1.20-alpha.3",
44
"main": "dist/index.js",
55
"module": "dist/es/index.js",
66
"types": "dist/index.d.ts",
@@ -58,7 +58,8 @@
5858
"scripts": {
5959
"start": "rollup -c -w",
6060
"build": "npm run build:prod",
61-
"build:prod": "rollup -c"
61+
"build:prod": "rollup -c",
62+
"publish:alpha": "npm publish --tag alpha"
6263
},
6364
"eslintConfig": {
6465
"extends": "react-app"

react-spaces/src/Globals/ISpaceContext.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,26 @@ const recalcSpaces = (spaces: ISpace[]) => {
4444
const adjustedSize = t.adjustedSize !== 0 ? ` + ${getSizeString(t.adjustedSize)}` : ``;
4545
if (space.anchorType === undefined)
4646
{
47-
if (t.anchorType === AnchorType.Top && t.size) {
48-
adjustedTop.push(`${getSizeString(t.size)}${adjustedSize}`);
49-
} else if (t.anchorType === AnchorType.Left && t.size) {
50-
adjustedLeft.push(`${getSizeString(t.size)}${adjustedSize}`);
51-
} else if (t.anchorType === AnchorType.Bottom && t.size) {
52-
adjustedBottom.push(`${getSizeString(t.size)}${adjustedSize}`);
53-
} else if (t.anchorType === AnchorType.Right && t.size) {
54-
adjustedRight.push(`${getSizeString(t.size)}${adjustedSize}`);
47+
if (t.anchorType === AnchorType.Top && t.anchorSize) {
48+
adjustedTop.push(`${getSizeString(t.anchorSize)}${adjustedSize}`);
49+
} else if (t.anchorType === AnchorType.Left && t.anchorSize) {
50+
adjustedLeft.push(`${getSizeString(t.anchorSize)}${adjustedSize}`);
51+
} else if (t.anchorType === AnchorType.Bottom && t.anchorSize) {
52+
adjustedBottom.push(`${getSizeString(t.anchorSize)}${adjustedSize}`);
53+
} else if (t.anchorType === AnchorType.Right && t.anchorSize) {
54+
adjustedRight.push(`${getSizeString(t.anchorSize)}${adjustedSize}`);
5555
}
5656
}
5757
else
5858
{
59-
if (t.anchorType === AnchorType.Top && t.size && space.top !== undefined) {
60-
adjustedTop.push(`${getSizeString(t.size)}${adjustedSize}`);
61-
} else if (t.anchorType === AnchorType.Left && t.size && space.left !== undefined) {
62-
adjustedLeft.push(`${getSizeString(t.size)}${adjustedSize}`);
63-
} else if (t.anchorType === AnchorType.Bottom && t.size && space.bottom !== undefined) {
64-
adjustedBottom.push(`${getSizeString(t.size)}${adjustedSize}`);
65-
} else if (t.anchorType === AnchorType.Right && t.size && space.right !== undefined) {
66-
adjustedRight.push(`${getSizeString(t.size)}${adjustedSize}`);
59+
if (t.anchorType === AnchorType.Top && t.anchorSize && space.top !== undefined) {
60+
adjustedTop.push(`${getSizeString(t.anchorSize)}${adjustedSize}`);
61+
} else if (t.anchorType === AnchorType.Left && t.anchorSize && space.left !== undefined) {
62+
adjustedLeft.push(`${getSizeString(t.anchorSize)}${adjustedSize}`);
63+
} else if (t.anchorType === AnchorType.Bottom && t.anchorSize && space.bottom !== undefined) {
64+
adjustedBottom.push(`${getSizeString(t.anchorSize)}${adjustedSize}`);
65+
} else if (t.anchorType === AnchorType.Right && t.anchorSize && space.right !== undefined) {
66+
adjustedRight.push(`${getSizeString(t.anchorSize)}${adjustedSize}`);
6767
}
6868
}
6969
} else {
@@ -94,10 +94,12 @@ export const registerSpace = (context: ISpaceContext, space: ISpace) => {
9494
if (!existing) {
9595
context.updateChildren(recalcSpaces([ ...context.children, space ]));
9696
return space;
97-
} else {
97+
}
98+
else
99+
{
98100
existing.order = space.order;
99101
existing.anchorType = space.anchorType;
100-
existing.size = space.size;
102+
existing.anchorSize = space.anchorSize;
101103
return existing;
102104
}
103105
}

react-spaces/src/Globals/Resize.tsx

Lines changed: 0 additions & 45 deletions
This file was deleted.

react-spaces/src/Globals/Types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ export interface IState {
138138
}
139139

140140
export interface ISize {
141-
parsedSize: number | undefined,
142141
width: number,
143142
height: number
144143
}
@@ -150,7 +149,7 @@ export interface ISpace {
150149
order: number,
151150
zIndex: number,
152151
anchorType: AnchorType | undefined,
153-
size: number | string,
152+
anchorSize: number | string,
154153
adjustedSize: number,
155154
left?: number | string,
156155
top?: number | string,

react-spaces/src/HeadStyles.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export const HeadStyles : React.FC<{ spaces: ISpace[] }> = (props) => {
1717
top: (space.top !== undefined ? cssValue(space.top, space.adjustedTop) : undefined),
1818
right: (space.right !== undefined ? cssValue(space.right, space.adjustedLeft) : undefined),
1919
bottom: (space.bottom !== undefined ? cssValue(space.bottom, space.adjustedTop) : undefined),
20-
width: isHorizontalSpace(space.anchorType) ? cssValue(space.size, space.adjustedSize) : space.width,
21-
height: isVerticalSpace(space.anchorType) ? cssValue(space.size, space.adjustedSize) : space.height,
20+
width: isHorizontalSpace(space.anchorType) ? cssValue(space.anchorSize, space.adjustedSize) : space.width,
21+
height: isVerticalSpace(space.anchorType) ? cssValue(space.anchorSize, space.adjustedSize) : space.height,
2222
zIndex: space.zIndex
2323
};
2424
return (

react-spaces/src/Hooks/useSpace.ts

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,56 @@ import { initialState, isHorizontalSpace, isVerticalSpace } from 'src/Globals/Ut
44
import { ISpaceContext, updateSpace, removeSpace, registerSpace, createSpaceContext } from 'src/Globals/ISpaceContext';
55
import { SpaceLayerContext, SpaceContext } from 'src/Globals/Contexts';
66
import { ResizeSensor } from 'css-element-queries';
7-
import { throttle } from 'src/Globals/Throttle';
87

98
export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<HTMLElement | undefined>) => {
109

11-
const [ state, changeState ] = React.useState<IState>(initialState(props));
12-
const [ currentSize, setCurrentSize ] = React.useState<ISize | null>(null);
13-
const resizeSensor = React.useRef<ResizeSensor>();
14-
const onRemove = React.useRef<(() => void)>();
15-
16-
const setState = (stateDelta: Partial<IState>) => changeState(prev => ({...prev, ...stateDelta}));
17-
1810
const parentContext = React.useContext(SpaceContext) as ISpaceContext;
1911

2012
if (!parentContext) {
2113
throw new Error("No top-level space: There must be a <ViewPort /> or <Fixed /> ancestor space");
2214
}
2315

16+
const [ state, changeState ] = React.useState<IState>(initialState(props));
17+
const [ currentSize, setCurrentSize ] = React.useState<ISize | null>(null);
18+
const resizeSensor = React.useRef<ResizeSensor>();
19+
const onRemove = React.useRef<(() => void)>();
20+
const setState = (stateDelta: Partial<IState>) => changeState(prev => ({...prev, ...stateDelta}));
2421
const layerContext = React.useContext(SpaceLayerContext);
2522
const currentZIndex = props.zIndex || layerContext || 0;
2623

24+
const updateCurrentSize = React.useCallback(() => {
25+
if (divElementRef.current) {
26+
const rect = divElementRef.current.getBoundingClientRect();
27+
setCurrentSize({
28+
width: rect.width,
29+
height: rect.height
30+
});
31+
}
32+
}, []);
33+
34+
const space =
35+
registerSpace(
36+
parentContext,
37+
{
38+
id: state.id,
39+
zIndex: currentZIndex,
40+
order: props.order === undefined ? 1 : props.order,
41+
anchorType: props.anchor,
42+
anchorSize: props.anchorSize || 0,
43+
adjustedSize: 0,
44+
adjustedLeft: 0,
45+
adjustedTop: 0,
46+
width: props.anchor && isHorizontalSpace(props.anchor) ? props.anchorSize || 0 : props.width,
47+
height: props.anchor && isVerticalSpace(props.anchor) ? props.anchorSize || 0 : props.height
48+
}
49+
);
50+
51+
React.useEffect(() => {
52+
setCurrentSize(null);
53+
}, [ parentContext.children.length ]);
54+
2755
// Deal with property changes to size / zIndex / anchoring
2856
React.useEffect(() => {
29-
setCurrentSize(prev => ({
30-
parsedSize: typeof props.anchorSize === "string" ? 0 : props.anchorSize as number | undefined,
31-
width: prev ? prev.width : 0,
32-
height: prev ? prev.height : 0
33-
}));
3457
updateSpace(
3558
parentContext,
3659
state.id,
@@ -43,11 +66,6 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
4366
}, [ props.anchorSize ]);
4467

4568
React.useEffect(() => {
46-
setCurrentSize(prev => ({
47-
parsedSize: typeof props.anchorSize === "string" ? 0 : props.anchorSize as number | undefined,
48-
width: prev ? prev.width : 0,
49-
height: prev ? prev.height : 0
50-
}));
5169
updateSpace(
5270
parentContext,
5371
state.id,
@@ -71,51 +89,24 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
7189
}
7290
);
7391
}, [ 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-
);
9192

9293
// 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), []);
10394

10495
React.useEffect(() => {
10596
if (divElementRef.current) {
10697
if (props.trackSize) {
10798
resizeSensor.current = new ResizeSensor(
10899
divElementRef.current,
109-
(size) => setCurrentSize({
110-
parsedSize: (isHorizontalSpace(props.anchor) ? size.width : size.height),
100+
(size) => setCurrentSize({
111101
width: size.width,
112102
height: size.height
113103
})
114104
);
115105
}
116-
determineCurrentSize();
117106
}
118107

108+
updateCurrentSize();
109+
119110
const cleanup = () => {
120111
resizeSensor.current && resizeSensor.current.detach();
121112
onRemove.current && onRemove.current();
@@ -124,9 +115,16 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
124115
return cleanup;
125116
}, []);
126117

127-
onRemove.current = () => {
118+
React.useEffect(() => {
119+
if (!currentSize)
120+
{
121+
updateCurrentSize();
122+
}
123+
})
124+
125+
onRemove.current = React.useCallback(() => {
128126
removeSpace(parentContext, state.id);
129-
}
127+
}, []);
130128

131129
const currentContext =
132130
createSpaceContext(
@@ -138,8 +136,8 @@ export const useSpace = (props: AllProps, divElementRef: React.MutableRefObject<
138136

139137
return {
140138
space,
141-
currentSize,
142139
parentContext,
143-
currentContext
140+
currentContext,
141+
currentSize: currentSize || { width: 0, height: 0 }
144142
}
145143
}

0 commit comments

Comments
 (0)