@@ -52,6 +52,7 @@ export default function Tree(props: Props) {
52
52
// $FlowFixMe https://github.com/facebook/flow/issues/7341
53
53
const listRef = useRef < FixedSizeList < ItemData > | null > ( null ) ;
54
54
const treeRef = useRef < HTMLDivElement | null > ( null ) ;
55
+ const focusTargetRef = useRef < HTMLDivElement | null > ( null ) ;
55
56
56
57
const [ treeFocused , setTreeFocused ] = useState < boolean > ( false ) ;
57
58
@@ -69,6 +70,19 @@ export default function Tree(props: Props) {
69
70
}
70
71
} , [ listRef , selectedElementIndex ] ) ;
71
72
73
+ // Picking an element in the inspector should put focus into the tree.
74
+ // This ensures that keyboard navigation works right after picking a node.
75
+ useEffect ( ( ) => {
76
+ function handleStopInspectingDOM ( didSelectNode ) {
77
+ if ( didSelectNode && focusTargetRef . current !== null ) {
78
+ focusTargetRef . current . focus ( ) ;
79
+ }
80
+ }
81
+ bridge . addListener ( 'stopInspectingDOM' , handleStopInspectingDOM ) ;
82
+ return ( ) =>
83
+ bridge . removeListener ( 'stopInspectingDOM' , handleStopInspectingDOM ) ;
84
+ } , [ bridge ] ) ;
85
+
72
86
// This ref is passed down the context to elements.
73
87
// It lets them avoid autoscrolling to the same item many times
74
88
// when a selected virtual row goes in and out of the viewport.
@@ -204,6 +218,7 @@ export default function Tree(props: Props) {
204
218
onFocus = { handleFocus }
205
219
onKeyPress = { handleKeyPress }
206
220
onMouseLeave = { handleMouseLeave }
221
+ ref = { focusTargetRef }
207
222
tabIndex = { 0 }
208
223
>
209
224
< AutoSizer >
0 commit comments