@samitouri / QOS-React-1 / commits / d5f3c50f58

chore[DevTools/Tree]: don't pre-select root element and remove unused code (#32015)

In this PR: 1. Removed unused code in `Tree.js` 2. Removed logic for pre-selecting first element in the tree by default. This is a bit clowny, because it steals focus and resets scroll, when user attempts to expand / collapse some subtree. 3. Updated comments around https://github.com/facebook/react/commit/1c381c588aed1ed6814f1be04fbe42cd069ce174. To expand on 3-rd point, for someone who might be reading this in the future: We can't guarantee focus of RDT browser extension panels, because they are hosted in an `iframe`. Attempting to fire any events won't have any result, user action with the corresponding `iframe` is required in order for this `iframe` to obtain focus. The only reason why built-in Elements panel in Chrome works correctly is because it is supported natively somewhere in Chrome / Chrome DevTools. Also, when you select an element on the application page, Chrome will make sure that Elements panel opened, which technically guarantees focus inside DevTools window and Elements panel subview. As of today, we can't navigate user to third-party extensions panels, there is no API for this, hence no ability to guarantee focused RDT panels.

Ruslan Lesiutin committed Jan 9, 2025 at 18:00 UTC d5f3c50f584ab0212e472fc4f4d599194a0286a7
1 file changed +5 -30
packages/react-devtools-shared/src/devtools/views/Components/Tree.js
+5 -30
@@ -42,16 +42,12 @@ import {logEvent} from 'react-devtools-shared/src/Logger';
42 const DEFAULT_INDENTATION_SIZE = 12;
43
44 export type ItemData = {
45 - numElements: number,
45 isNavigatingWithKeyboard: boolean,
47 - lastScrolledIDRef: {current: number | null, ...},
46 onElementMouseEnter: (id: number) => void,
47 treeFocused: boolean,
48 };
49
52 -type Props = {};
53 -
54 -export default function Tree(props: Props): React.Node {
50 +export default function Tree(): React.Node {
51 const dispatch = useContext(TreeDispatcherContext);
52 const {
53 numElements,
@@ -96,7 +92,8 @@ export default function Tree(props: Props): React.Node {
92 );
93
94 // Picking an element in the inspector should put focus into the tree.
99 - // This ensures that keyboard navigation works right after picking a node.
95 + // If possible, navigation works right after picking a node.
96 + // NOTE: This is not guaranteed to work, because browser extension panels are hosted inside an iframe.
97 useEffect(() => {
98 function handleStopInspectingHost(didSelectNode: boolean) {
99 if (didSelectNode && focusTargetRef.current !== null) {
@@ -112,11 +109,6 @@ export default function Tree(props: Props): React.Node {
109 bridge.removeListener('stopInspectingHost', handleStopInspectingHost);
110 }, [bridge]);
111
115 - // This ref is passed down the context to elements.
116 - // It lets them avoid autoscrolling to the same item many times
117 - // when a selected virtual row goes in and out of the viewport.
118 - const lastScrolledIDRef = useRef<number | null>(null);
119 -
112 // Navigate the tree with up/down arrow keys.
113 useEffect(() => {
114 if (treeRef.current === null) {
@@ -214,16 +206,7 @@ export default function Tree(props: Props): React.Node {
206
207 // Focus management.
208 const handleBlur = useCallback(() => setTreeFocused(false), []);
217 - const handleFocus = useCallback(() => {
218 - setTreeFocused(true);
219 -
220 - if (selectedElementIndex === null && numElements > 0) {
221 - dispatch({
222 - type: 'SELECT_ELEMENT_AT_INDEX',
223 - payload: 0,
224 - });
225 - }
226 - }, [dispatch, numElements, selectedElementIndex]);
209 + const handleFocus = useCallback(() => setTreeFocused(true), []);
210
211 const handleKeyPress = useCallback(
212 (event: $FlowFixMe) => {
@@ -294,19 +277,11 @@ export default function Tree(props: Props): React.Node {
277 // This includes the owner context, since it controls a filtered view of the tree.
278 const itemData = useMemo<ItemData>(
279 () => ({
297 - numElements,
280 isNavigatingWithKeyboard,
281 onElementMouseEnter: handleElementMouseEnter,
300 - lastScrolledIDRef,
282 treeFocused,
283 }),
303 - [
304 - numElements,
305 - isNavigatingWithKeyboard,
306 - handleElementMouseEnter,
307 - lastScrolledIDRef,
308 - treeFocused,
309 - ],
284 + [isNavigatingWithKeyboard, handleElementMouseEnter, treeFocused],
285 );
286
287 const itemKey = useCallback(