8
8
*/
9
9
10
10
import * as React from 'react' ;
11
- import { Fragment , useContext } from 'react' ;
11
+ import { Fragment , useContext , useEffect , useRef } from 'react' ;
12
12
import WhatChanged from './WhatChanged' ;
13
13
import { ProfilerContext } from './ProfilerContext' ;
14
14
import { formatDuration , formatTime } from './utils' ;
@@ -31,12 +31,48 @@ export default function SidebarSelectedFiberInfo(_: Props) {
31
31
selectFiber,
32
32
} = useContext ( ProfilerContext ) ;
33
33
const { profilingCache} = profilerStore ;
34
+ const selectedListItemRef = useRef < HTMLElement | null > ( null ) ;
34
35
35
36
const commitIndices = profilingCache . getFiberCommits ( {
36
37
fiberID : ( ( selectedFiberID : any ) : number ) ,
37
38
rootID : ( ( rootID : any ) : number ) ,
38
39
} ) ;
39
40
41
+ const handleKeyDown = event => {
42
+ switch ( event . key ) {
43
+ case 'ArrowUp' :
44
+ if ( selectedCommitIndex !== null ) {
45
+ const prevIndex = commitIndices . indexOf ( selectedCommitIndex ) ;
46
+ const nextIndex =
47
+ prevIndex > 0 ? prevIndex - 1 : commitIndices . length - 1 ;
48
+ selectCommitIndex ( commitIndices [ nextIndex ] ) ;
49
+ }
50
+ event . preventDefault ( ) ;
51
+ break ;
52
+ case 'ArrowDown' :
53
+ if ( selectedCommitIndex !== null ) {
54
+ const prevIndex = commitIndices . indexOf ( selectedCommitIndex ) ;
55
+ const nextIndex =
56
+ prevIndex < commitIndices . length - 1 ? prevIndex + 1 : 0 ;
57
+ selectCommitIndex ( commitIndices [ nextIndex ] ) ;
58
+ }
59
+ event . preventDefault ( ) ;
60
+ break ;
61
+ default :
62
+ break ;
63
+ }
64
+ } ;
65
+
66
+ useEffect ( ( ) => {
67
+ const selectedElement = selectedListItemRef . current ;
68
+ if (
69
+ selectedElement !== null &&
70
+ typeof selectedElement . scrollIntoView === 'function'
71
+ ) {
72
+ selectedElement . scrollIntoView ( { block : 'nearest' , inline : 'nearest' } ) ;
73
+ }
74
+ } , [ selectedCommitIndex ] ) ;
75
+
40
76
const listItems = [ ] ;
41
77
let i = 0 ;
42
78
for ( i = 0 ; i < commitIndices . length ; i ++ ) {
@@ -50,6 +86,7 @@ export default function SidebarSelectedFiberInfo(_: Props) {
50
86
listItems . push (
51
87
< button
52
88
key = { commitIndex }
89
+ ref = { selectedCommitIndex === commitIndex ? selectedListItemRef : null }
53
90
className = {
54
91
selectedCommitIndex === commitIndex
55
92
? styles . CurrentCommit
@@ -75,7 +112,7 @@ export default function SidebarSelectedFiberInfo(_: Props) {
75
112
< ButtonIcon type = "close" />
76
113
</ Button >
77
114
</ div >
78
- < div className = { styles . Content } >
115
+ < div className = { styles . Content } onKeyDown = { handleKeyDown } tabIndex = { 0 } >
79
116
< WhatChanged fiberID = { ( ( selectedFiberID : any ) : number ) } />
80
117
{ listItems . length > 0 && (
81
118
< Fragment >
0 commit comments