|
| 1 | +"use strict"; |
| 2 | +var __assign = (this && this.__assign) || function () { |
| 3 | + __assign = Object.assign || function(t) { |
| 4 | + for (var s, i = 1, n = arguments.length; i < n; i++) { |
| 5 | + s = arguments[i]; |
| 6 | + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) |
| 7 | + t[p] = s[p]; |
| 8 | + } |
| 9 | + return t; |
| 10 | + }; |
| 11 | + return __assign.apply(this, arguments); |
| 12 | +}; |
| 13 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 14 | +var react_1 = require("react"); |
| 15 | +var core_1 = require("@react-navigation/core"); |
| 16 | +function useNavigation() { |
| 17 | + return react_1.useContext(core_1.NavigationContext); |
| 18 | +} |
| 19 | +exports.useNavigation = useNavigation; |
| 20 | +function useNavigationParam(paramName) { |
| 21 | + return useNavigation().getParam(paramName); |
| 22 | +} |
| 23 | +exports.useNavigationParam = useNavigationParam; |
| 24 | +function useNavigationState() { |
| 25 | + return useNavigation().state; |
| 26 | +} |
| 27 | +exports.useNavigationState = useNavigationState; |
| 28 | +function useNavigationKey() { |
| 29 | + return useNavigation().state.key; |
| 30 | +} |
| 31 | +exports.useNavigationKey = useNavigationKey; |
| 32 | +function useNavigationEvents(handleEvt) { |
| 33 | + var navigation = useNavigation(); |
| 34 | + react_1.useEffect(function () { |
| 35 | + var subsA = navigation.addListener('action', handleEvt); |
| 36 | + var subsWF = navigation.addListener('willFocus', handleEvt); |
| 37 | + var subsDF = navigation.addListener('didFocus', handleEvt); |
| 38 | + var subsWB = navigation.addListener('willBlur', handleEvt); |
| 39 | + var subsDB = navigation.addListener('didBlur', handleEvt); |
| 40 | + return function () { |
| 41 | + subsA.remove(); |
| 42 | + subsWF.remove(); |
| 43 | + subsDF.remove(); |
| 44 | + subsWB.remove(); |
| 45 | + subsDB.remove(); |
| 46 | + }; |
| 47 | + }, |
| 48 | + // For TODO consideration: If the events are tied to the navigation object and the key |
| 49 | + // identifies the nav object, then we should probably pass [navigation.state.key] here, to |
| 50 | + // make sure react doesn't needlessly detach and re-attach this effect. In practice this |
| 51 | + // seems to cause troubles |
| 52 | + undefined); |
| 53 | +} |
| 54 | +exports.useNavigationEvents = useNavigationEvents; |
| 55 | +var emptyFocusState = { |
| 56 | + isFocused: false, |
| 57 | + isBlurring: false, |
| 58 | + isBlurred: false, |
| 59 | + isFocusing: false, |
| 60 | +}; |
| 61 | +var didFocusState = __assign({}, emptyFocusState, { isFocused: true }); |
| 62 | +var willBlurState = __assign({}, emptyFocusState, { isBlurring: true }); |
| 63 | +var didBlurState = __assign({}, emptyFocusState, { isBlurred: true }); |
| 64 | +var willFocusState = __assign({}, emptyFocusState, { isFocusing: true }); |
| 65 | +var getInitialFocusState = function (isFocused) { |
| 66 | + return isFocused ? didFocusState : didBlurState; |
| 67 | +}; |
| 68 | +function focusStateOfEvent(eventName) { |
| 69 | + switch (eventName) { |
| 70 | + case 'didFocus': |
| 71 | + return didFocusState; |
| 72 | + case 'willFocus': |
| 73 | + return willFocusState; |
| 74 | + case 'willBlur': |
| 75 | + return willBlurState; |
| 76 | + case 'didBlur': |
| 77 | + return didBlurState; |
| 78 | + default: |
| 79 | + return null; |
| 80 | + } |
| 81 | +} |
| 82 | +function useFocusState() { |
| 83 | + var navigation = useNavigation(); |
| 84 | + var isFocused = navigation.isFocused(); |
| 85 | + var _a = react_1.useState(getInitialFocusState(isFocused)), focusState = _a[0], setFocusState = _a[1]; |
| 86 | + function handleEvt(e) { |
| 87 | + var newState = focusStateOfEvent(e.type); |
| 88 | + newState && setFocusState(newState); |
| 89 | + } |
| 90 | + useNavigationEvents(handleEvt); |
| 91 | + return focusState; |
| 92 | +} |
| 93 | +exports.useFocusState = useFocusState; |
0 commit comments