Skip to content

Commit 12bc60f

Browse files
authored
[devtools] Added minimum indent size to Component Tree (#33517)
## Summary The devtools Components tab's component tree view currently has a behavior where the indentation of each level of the tree scales based on the available width of the view. If the view is narrow or component names are long, all indentation showing the hierarchy of the tree scales down with the view width until there is no indentation at all. This makes it impossible to see the nesting of the tree, making the tree view much less useful. With long component names and deep hierarchies this issue is particularly egregious. For comparison, the Chrome Dev Tools Elements panel uses a fixed indentation size, so it doesn't suffer from this issue. This PR adds a minimum pixel value for the indentation width, so that even when the window is narrow some indentation will still be visible, maintaining the visual representation of the component tree hierarchy. Alternatively, we could match the behavior of the Chrome Dev Tools and just use a constant indentation width. ## How did you test this change? - tests (yarn test-build-devtools) - tested in browser: - added an alternate left/right split pane layout to react-devtools-shell to test with (#33516) - tested resizing the tree view in different layout modes ### before this change: https://github.com/user-attachments/assets/470991f1-dc05-473f-a2cb-4f7333f6bae4 with a long component name: https://github.com/user-attachments/assets/1568fc64-c7d7-4659-bfb1-9bfc9592fb9d ### after this change: https://github.com/user-attachments/assets/f60bd7fc-97f6-4680-9656-f0db3d155411 with a long component name: https://github.com/user-attachments/assets/6ac3f58c-42ea-4c5a-9a52-c3b397f37b45
1 parent ed023cf commit 12bc60f

File tree

1 file changed

+10
-8
lines changed
  • packages/react-devtools-shared/src/devtools/views/Components

1 file changed

+10
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ import {useExtensionComponentsPanelVisibility} from 'react-devtools-shared/src/f
4141
import {useChangeOwnerAction} from './OwnersListContext';
4242

4343
// Never indent more than this number of pixels (even if we have the room).
44-
const DEFAULT_INDENTATION_SIZE = 12;
44+
const MAX_INDENTATION_SIZE = 12;
45+
const MIN_INDENTATION_SIZE = 4;
4546

4647
export type ItemData = {
4748
isNavigatingWithKeyboard: boolean,
@@ -490,11 +491,11 @@ function updateIndentationSizeVar(
490491

491492
// Reset the max indentation size if the width of the tree has increased.
492493
if (listWidth > prevListWidthRef.current) {
493-
indentationSizeRef.current = DEFAULT_INDENTATION_SIZE;
494+
indentationSizeRef.current = MAX_INDENTATION_SIZE;
494495
}
495496
prevListWidthRef.current = listWidth;
496497

497-
let maxIndentationSize: number = indentationSizeRef.current;
498+
let indentationSize: number = indentationSizeRef.current;
498499

499500
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
500501
for (const child of innerDiv.children) {
@@ -517,12 +518,13 @@ function updateIndentationSizeVar(
517518

518519
const remainingWidth = Math.max(0, listWidth - childWidth);
519520

520-
maxIndentationSize = Math.min(maxIndentationSize, remainingWidth / depth);
521+
indentationSize = Math.min(indentationSize, remainingWidth / depth);
521522
}
522523

523-
indentationSizeRef.current = maxIndentationSize;
524+
indentationSize = Math.max(indentationSize, MIN_INDENTATION_SIZE);
525+
indentationSizeRef.current = indentationSize;
524526

525-
list.style.setProperty('--indentation-size', `${maxIndentationSize}px`);
527+
list.style.setProperty('--indentation-size', `${indentationSize}px`);
526528
}
527529

528530
// $FlowFixMe[missing-local-annot]
@@ -545,7 +547,7 @@ function InnerElementType({children, style}) {
545547
// The user may have resized the window specifically to make more room for DevTools.
546548
// In either case, this should reset our max indentation size logic.
547549
// 2. The second is when the user enters or exits an owner tree.
548-
const indentationSizeRef = useRef<number>(DEFAULT_INDENTATION_SIZE);
550+
const indentationSizeRef = useRef<number>(MAX_INDENTATION_SIZE);
549551
const prevListWidthRef = useRef<number>(0);
550552
const prevOwnerIDRef = useRef<number | null>(ownerID);
551553
const divRef = useRef<HTMLDivElement | null>(null);
@@ -554,7 +556,7 @@ function InnerElementType({children, style}) {
554556
// so when the user opens the "owners tree" view, we should discard the previous width.
555557
if (ownerID !== prevOwnerIDRef.current) {
556558
prevOwnerIDRef.current = ownerID;
557-
indentationSizeRef.current = DEFAULT_INDENTATION_SIZE;
559+
indentationSizeRef.current = MAX_INDENTATION_SIZE;
558560
}
559561

560562
// When we render new content, measure to see if we need to shrink indentation to fit it.

0 commit comments

Comments
 (0)