Skip to content

Commit d7dbe4a

Browse files
feat: DevTools - Removed Use Memo.
1 parent f8005f0 commit d7dbe4a

File tree

1 file changed

+38
-30
lines changed
  • packages/react-devtools-shared/src/devtools/views/Components

1 file changed

+38
-30
lines changed

packages/react-devtools-shared/src/devtools/views/Components/Components.js

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import React, {
1212
Fragment,
1313
useRef,
1414
useState,
15-
useMemo,
15+
useLayoutEffect,
1616
useCallback,
1717
} from 'react';
1818
import Tree from './Tree';
@@ -68,39 +68,30 @@ function Components(_: {||}) {
6868
);
6969
}
7070

71-
const resizeDirections = {
71+
const RESIZE_DIRECTIONS = {
7272
HORIZONTAL: 'HORIZONTAL',
7373
VERTICAL: 'VERTICAL',
7474
};
75+
const LOCAL_STORAGE_RESIZE_ELEMENT_PERCENTAGE_HORIZONTAL_KEY = `React::DevTools::resizedElementPercentage::${RESIZE_DIRECTIONS.HORIZONTAL}`;
76+
const LOCAL_STORAGE_RESIZE_ELEMENT_PERCENTAGE_VERTICAL_KEY = `React::DevTools::resizedElementPercentage::${RESIZE_DIRECTIONS.VERTICAL}`;
7577

7678
function ComponentResizer({children}): {|children: Function|} {
77-
const [isResizing, setIsResizing] = useState(false);
79+
const [isResizing, setIsResizing] = useState<boolean>(false);
7880
const [
7981
horizontalPercentage,
8082
setHorizontalPercentage,
8183
] = useLocalStorage<number>(
82-
`React::DevTools::resizedElementPercentage::${resizeDirections.HORIZONTAL}`,
84+
LOCAL_STORAGE_RESIZE_ELEMENT_PERCENTAGE_HORIZONTAL_KEY,
8385
65,
8486
);
8587
const [verticalPercentage, setVerticalPercentage] = useLocalStorage<number>(
86-
`React::DevTools::resizedElementPercentage::${resizeDirections.VERTICAL}`,
88+
LOCAL_STORAGE_RESIZE_ELEMENT_PERCENTAGE_VERTICAL_KEY,
8789
50,
8890
);
89-
const updateLocalStorageTimeoutId = useRef(null);
90-
const componentsWrapperRef = useRef(null);
91-
const resizeElementRef = useRef(null);
92-
93-
// TODO: We might be saving the localStorage values,
94-
// TODO: but window.innerWidth might be bellow 600 so that why it's broken.
95-
// TODO: OR we can't access the property when building the extension. :(
96-
const resizeElementStyles = useMemo(
97-
() => ({
98-
flexBasis: `${
99-
window.innerWidth > 600 ? horizontalPercentage : verticalPercentage
100-
}%`,
101-
}),
102-
[horizontalPercentage, verticalPercentage],
103-
);
91+
const updateLocalStorageTimeoutId = useRef<number>(null);
92+
const componentsWrapperRef = useRef<HTMLDivElement>(null);
93+
const resizeElementRef = useRef<HTMLElement>(null);
94+
const [resizeElementStyles, setResizeElementStyles] = useState<Object>({});
10495

10596
const onResizeStart = useCallback(() => {
10697
setIsResizing(true);
@@ -115,7 +106,7 @@ function ComponentResizer({children}): {|children: Function|} {
115106
if (
116107
!isResizing ||
117108
componentsWrapperRef.current === null ||
118-
resizeElementRef.current === null
109+
resizeElementRef.current === null
119110
) {
120111
return;
121112
}
@@ -129,26 +120,29 @@ function ComponentResizer({children}): {|children: Function|} {
129120
top,
130121
} = componentsWrapperRef.current.getBoundingClientRect();
131122
const resizeDirection =
132-
width > 600 ? resizeDirections.HORIZONTAL : resizeDirections.VERTICAL;
133-
const currentMousePosition =
134-
resizeDirection === resizeDirections.HORIZONTAL
123+
width > 600 ? RESIZE_DIRECTIONS.HORIZONTAL : RESIZE_DIRECTIONS.VERTICAL;
124+
const currentMousePosition: number =
125+
resizeDirection === RESIZE_DIRECTIONS.HORIZONTAL
135126
? e.clientX - left
136127
: e.clientY - top;
137-
const boundary = {
128+
const boundary: {|
129+
min: number,
130+
max: number,
131+
|} = {
138132
min: 40,
139133
max:
140-
resizeDirection === resizeDirections.HORIZONTAL
134+
resizeDirection === RESIZE_DIRECTIONS.HORIZONTAL
141135
? width - 40
142136
: height - 40,
143137
};
144-
const mousePositionInBounds =
138+
const mousePositionInBounds: boolean =
145139
currentMousePosition > boundary.min &&
146140
currentMousePosition < boundary.max;
147141

148142
if (mousePositionInBounds) {
149-
const updatedFlexBasisValue =
143+
const updatedFlexBasisValue: number =
150144
(currentMousePosition /
151-
(resizeDirection === resizeDirections.HORIZONTAL
145+
(resizeDirection === RESIZE_DIRECTIONS.HORIZONTAL
152146
? width
153147
: height)) *
154148
100;
@@ -158,7 +152,7 @@ function ComponentResizer({children}): {|children: Function|} {
158152
clearTimeout(updateLocalStorageTimeoutId.current);
159153

160154
updateLocalStorageTimeoutId.current = setTimeout(() => {
161-
if (resizeDirection === resizeDirections.HORIZONTAL) {
155+
if (resizeDirection === RESIZE_DIRECTIONS.HORIZONTAL) {
162156
setHorizontalPercentage(updatedFlexBasisValue);
163157
} else {
164158
setVerticalPercentage(updatedFlexBasisValue);
@@ -169,6 +163,20 @@ function ComponentResizer({children}): {|children: Function|} {
169163
[componentsWrapperRef, resizeElementRef, isResizing],
170164
);
171165

166+
useLayoutEffect(() => {
167+
if (componentsWrapperRef.current !== null) {
168+
if (componentsWrapperRef.current.getBoundingClientRect().width > 600) {
169+
setResizeElementStyles({
170+
flexBasis: `${horizontalPercentage}%`,
171+
});
172+
} else {
173+
setResizeElementStyles({
174+
flexBasis: `${verticalPercentage}%`,
175+
});
176+
}
177+
}
178+
}, [componentsWrapperRef, horizontalPercentage, verticalPercentage]);
179+
172180
return (
173181
<div
174182
ref={componentsWrapperRef}

0 commit comments

Comments
 (0)