-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseTranscriptNavigation.ts
More file actions
53 lines (41 loc) · 1.1 KB
/
useTranscriptNavigation.ts
File metadata and controls
53 lines (41 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { hooks } from 'botframework-webchat';
import { useCallback, type KeyboardEvent } from 'react';
const { useScrollDown, useScrollUp } = hooks;
export default function useTranscriptNavigation() {
const scrollDown = useScrollDown();
const scrollUp = useScrollUp();
return useCallback(
(event: KeyboardEvent<unknown>) => {
if (event.target instanceof HTMLTextAreaElement && event.target.value) {
return;
}
const { ctrlKey, metaKey, shiftKey } = event;
if (ctrlKey || metaKey || shiftKey) {
return;
}
let handled = true;
switch (event.key) {
case 'End':
scrollDown({ displacement: Infinity });
break;
case 'Home':
scrollUp({ displacement: Infinity });
break;
case 'PageDown':
scrollDown();
break;
case 'PageUp':
scrollUp();
break;
default:
handled = false;
break;
}
if (handled) {
event.preventDefault();
event.stopPropagation();
}
},
[scrollDown, scrollUp]
);
}