Skip to content

Commit 3d983f6

Browse files
author
spoeck
committed
add use resize observer
1 parent f5c395f commit 3d983f6

File tree

5 files changed

+19
-89
lines changed

5 files changed

+19
-89
lines changed

package-lock.json

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"@material-ui/core": "4.9.12",
4141
"@material-ui/styles": "^4.9.10",
4242
"react-spring": "^8.0.27",
43-
"react-use-gesture": "^7.0.15"
43+
"react-use-gesture": "^7.0.15",
44+
"use-resize-observer": "^6.1.0"
4445
},
4546
"peerDependencies": {
4647
"react": ">=16.12.0",

src/components/Planet.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import { ClickAwayListener, makeStyles } from '@material-ui/core';
33
import { CreateCSSProperties, CSSProperties } from "@material-ui/styles";
44
import * as React from "react";
5-
import { ReactElement, useLayoutEffect, useRef } from "react";
5+
import { ReactElement } from "react";
6+
import useResizeObserver from "use-resize-observer";
67
import { DragableContainer } from "./DragableContainer";
78
import { Orbit } from "./Orbit";
89
import { Satellite } from "./Satellite";
@@ -70,22 +71,13 @@ export function Planet(props: Props) {
7071
satelliteOrientation,
7172
} = props;
7273
const classes = useStyles(props);
73-
const targetRef = useRef<any>();
74-
const [width, setWidth] = React.useState(0);
75-
const [height, setHeight] = React.useState(0);
74+
const { ref, height = 0, width = 0 } = useResizeObserver();
7675
const [_open, setOpen] = React.useState(!!open);
7776

7877
React.useEffect(() => {
7978
setOpen(!!open);
8079
}, [open]);
8180

82-
useLayoutEffect(() => {
83-
if (targetRef && targetRef.current) {
84-
setWidth(targetRef.current.offsetWidth);
85-
setHeight(targetRef.current.offsetHeight);
86-
}
87-
}, [targetRef]);
88-
8981
var satellites: ReactElement<any>[] = [];
9082
var satelliteCount = React.Children.count(children);
9183
React.Children.forEach(children, (c, i) => {
@@ -165,7 +157,7 @@ export function Planet(props: Props) {
165157
bounceOnClose={(bounce && !!!bounceOnOpen) || bounceOnClose}
166158
bounceDirection={bounceDirection}
167159
>
168-
<div ref={targetRef}>{centerContent}</div>
160+
<div ref={ref as any}>{centerContent}</div>
169161
</DragableContainer>
170162
</div>
171163
</div>

src/components/Satellite.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// prettier-ignore
22
import { makeStyles } from '@material-ui/core';
33
import * as React from "react";
4-
import { useLayoutEffect, useRef } from "react";
54
import { animated, useSpring } from "react-spring";
5+
import useResizeObserver from "use-resize-observer";
66
import { DragableContainer } from "./DragableContainer";
77

88
interface Props {
@@ -40,9 +40,7 @@ export function Satellite(props: Props) {
4040
orientation,
4141
} = props;
4242
const classes = useStyles(props);
43-
const targetRef = useRef<any>();
44-
const [width, setWidth] = React.useState(0);
45-
const [height, setHeight] = React.useState(0);
43+
const { ref, height = 0, width = 0 } = useResizeObserver();
4644
const position = useSpring({
4745
reverse: !open,
4846
from: getInitalSatellitePosition(width, height, planetWidth, planetHeight),
@@ -57,24 +55,17 @@ export function Satellite(props: Props) {
5755
rotation,
5856
orientation
5957
),
60-
config: { mass, tension, friction, clamp: true },
58+
config: { mass, tension, friction },
6159
});
6260

63-
useLayoutEffect(() => {
64-
if (targetRef && targetRef.current) {
65-
setWidth(targetRef.current.offsetWidth);
66-
setHeight(targetRef.current.offsetHeight);
67-
}
68-
}, [targetRef]);
69-
7061
return (
7162
<animated.div className={classes.root} style={position}>
7263
<DragableContainer
7364
on={dragable}
7465
dragRadius={dragRadius}
7566
dragable={dragable}
7667
>
77-
<div ref={targetRef}>{children}</div>
68+
<div ref={ref as any}>{children}</div>
7869
</DragableContainer>
7970
</animated.div>
8071
);

src/stories/ScreenEdge.stories.tsx

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

0 commit comments

Comments
 (0)