1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = useFocusEvents;
|
7 |
|
8 | var React = _interopRequireWildcard(require("react"));
|
9 |
|
10 | var _NavigationContext = _interopRequireDefault(require("./NavigationContext"));
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
15 |
|
16 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function useFocusEvents(_ref) {
|
22 | let {
|
23 | state,
|
24 | emitter
|
25 | } = _ref;
|
26 | const navigation = React.useContext(_NavigationContext.default);
|
27 | const lastFocusedKeyRef = React.useRef();
|
28 | const currentFocusedKey = state.routes[state.index].key;
|
29 |
|
30 |
|
31 | React.useEffect(() => navigation === null || navigation === void 0 ? void 0 : navigation.addListener('focus', () => {
|
32 | lastFocusedKeyRef.current = currentFocusedKey;
|
33 | emitter.emit({
|
34 | type: 'focus',
|
35 | target: currentFocusedKey
|
36 | });
|
37 | }), [currentFocusedKey, emitter, navigation]);
|
38 | React.useEffect(() => navigation === null || navigation === void 0 ? void 0 : navigation.addListener('blur', () => {
|
39 | lastFocusedKeyRef.current = undefined;
|
40 | emitter.emit({
|
41 | type: 'blur',
|
42 | target: currentFocusedKey
|
43 | });
|
44 | }), [currentFocusedKey, emitter, navigation]);
|
45 | React.useEffect(() => {
|
46 | const lastFocusedKey = lastFocusedKeyRef.current;
|
47 | lastFocusedKeyRef.current = currentFocusedKey;
|
48 |
|
49 |
|
50 | if (lastFocusedKey === undefined && !navigation) {
|
51 | emitter.emit({
|
52 | type: 'focus',
|
53 | target: currentFocusedKey
|
54 | });
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 | if (lastFocusedKey === currentFocusedKey || !(navigation ? navigation.isFocused() : true)) {
|
60 | return;
|
61 | }
|
62 |
|
63 | if (lastFocusedKey === undefined) {
|
64 |
|
65 | return;
|
66 | }
|
67 |
|
68 | emitter.emit({
|
69 | type: 'blur',
|
70 | target: lastFocusedKey
|
71 | });
|
72 | emitter.emit({
|
73 | type: 'focus',
|
74 | target: currentFocusedKey
|
75 | });
|
76 | }, [currentFocusedKey, emitter, navigation]);
|
77 | }
|
78 |
|
\ | No newline at end of file |