1 | import * as React from 'react';
|
2 | import NavigationContext from './NavigationContext';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | export default function useFocusEvents(_ref) {
|
8 | let {
|
9 | state,
|
10 | emitter
|
11 | } = _ref;
|
12 | const navigation = React.useContext(NavigationContext);
|
13 | const lastFocusedKeyRef = React.useRef();
|
14 | const currentFocusedKey = state.routes[state.index].key;
|
15 |
|
16 |
|
17 | React.useEffect(() => navigation === null || navigation === void 0 ? void 0 : navigation.addListener('focus', () => {
|
18 | lastFocusedKeyRef.current = currentFocusedKey;
|
19 | emitter.emit({
|
20 | type: 'focus',
|
21 | target: currentFocusedKey
|
22 | });
|
23 | }), [currentFocusedKey, emitter, navigation]);
|
24 | React.useEffect(() => navigation === null || navigation === void 0 ? void 0 : navigation.addListener('blur', () => {
|
25 | lastFocusedKeyRef.current = undefined;
|
26 | emitter.emit({
|
27 | type: 'blur',
|
28 | target: currentFocusedKey
|
29 | });
|
30 | }), [currentFocusedKey, emitter, navigation]);
|
31 | React.useEffect(() => {
|
32 | const lastFocusedKey = lastFocusedKeyRef.current;
|
33 | lastFocusedKeyRef.current = currentFocusedKey;
|
34 |
|
35 |
|
36 | if (lastFocusedKey === undefined && !navigation) {
|
37 | emitter.emit({
|
38 | type: 'focus',
|
39 | target: currentFocusedKey
|
40 | });
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 | if (lastFocusedKey === currentFocusedKey || !(navigation ? navigation.isFocused() : true)) {
|
46 | return;
|
47 | }
|
48 |
|
49 | if (lastFocusedKey === undefined) {
|
50 |
|
51 | return;
|
52 | }
|
53 |
|
54 | emitter.emit({
|
55 | type: 'blur',
|
56 | target: lastFocusedKey
|
57 | });
|
58 | emitter.emit({
|
59 | type: 'focus',
|
60 | target: currentFocusedKey
|
61 | });
|
62 | }, [currentFocusedKey, emitter, navigation]);
|
63 | }
|
64 |
|
\ | No newline at end of file |