UNPKG

3.61 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = useFocusEvents;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _NavigationContext = _interopRequireDefault(require("./NavigationContext"));
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14function _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
16function _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 * Hook to take care of emitting `focus` and `blur` events.
20 */
21function 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; // When the parent screen changes its focus state, we also need to change child's focus
29 // Coz the child screen can't be focused if the parent screen is out of focus
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; // We wouldn't have `lastFocusedKey` on initial mount
48 // Fire focus event for the current route on mount if there's no parent navigator
49
50 if (lastFocusedKey === undefined && !navigation) {
51 emitter.emit({
52 type: 'focus',
53 target: currentFocusedKey
54 });
55 } // We should only emit events when the focused key changed and navigator is focused
56 // When navigator is not focused, screens inside shouldn't receive focused status either
57
58
59 if (lastFocusedKey === currentFocusedKey || !(navigation ? navigation.isFocused() : true)) {
60 return;
61 }
62
63 if (lastFocusedKey === undefined) {
64 // Only fire events after initial mount
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//# sourceMappingURL=useFocusEvents.js.map
\No newline at end of file