use-react-native-navigation
Version:
A utility library for a easier use of react-native-navigation library.
112 lines • 4.77 kB
JavaScript
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var navigation_store_1 = require("./navigation.store");
var useNavigationEvent_1 = require("./useNavigationEvent");
/** A list of commands to ignore for ComponentDidAppear event. */
var componentDidAppearCommandBlacklist = [
'MANUAL_DISMISS_ALL_MODALS',
'MANUAL_DISMISS_MODAL',
'DISMISS_OVERLAY',
];
/** A list of commands to ignore for ModalDismissed event. */
var modalDismissedCommandBlacklist = [
'MANUAL_DISMISS_MODAL',
'MANUAL_DISMISS_ALL_MODALS',
];
/**
* ### Use Track Navigation
*
* Set up the event Navigation event listeners to track the current componentId.
*/
exports.useTrackNavigation = function (passedComponentId) {
var _a = navigation_store_1.useNavigationStore(), status = _a.status, updateNavigationStatus = _a.updateNavigationStatus, onPreviousStackComponentShown = _a.onPreviousStackComponentShown;
/**
* Component appear event listener.
*
* Run:
* 1. Set previous componentId to the currently set componentId.
* 2. Set current componentId to the incoming componentId.
*/
useNavigationEvent_1.useComponentDidAppear(function (_a) {
var componentId = _a.componentId, componentType = _a.componentType;
if (componentType !== 'Component')
return;
var incomingComponentId = componentId;
var currentComponentId = status.currentComponentId;
if (componentDidAppearCommandBlacklist.indexOf(status.commandType) >= 0) {
return;
}
/**
* When Modal or Overlay is shown, need to add the previous componentId to the previousStackComponentIds.
*/
if (status.commandType === 'SHOW_MODAL' || status.commandType === 'SHOW_OVERLAY') {
updateNavigationStatus({
currentComponentId: incomingComponentId,
previousComponentId: currentComponentId,
updating: false,
previousStackComponentIds: __spreadArrays(status.previousStackComponentIds, [currentComponentId]).filter(function (i) { return !!i; }),
commandType: null,
});
return;
}
updateNavigationStatus({
currentComponentId: incomingComponentId,
previousComponentId: currentComponentId,
updating: false,
commandType: null,
});
}, passedComponentId);
/**
* Component disappear event listener.
*
* ONLY RUNS when dismissing an overlay as dismissing overlay does not trigger appear event of
* the current screen.
*
* This procedure assumes that the overlay is only a single layout.
*
* Run:
* 1. Set current componentId to the previously set componentId.
* 2. Set previous componentId to null.
*
*/
useNavigationEvent_1.useComponentDidDisappear(function (_a) {
var componentType = _a.componentType;
if (componentType !== 'Component')
return;
if (status.commandType === 'DISMISS_OVERLAY') {
var _b = onPreviousStackComponentShown(status.previousStackComponentIds), lastStackComponentId = _b.lastStackComponentId, updatedStackList = _b.updatedStackList;
updateNavigationStatus({
currentComponentId: lastStackComponentId !== null && lastStackComponentId !== void 0 ? lastStackComponentId : null,
previousStackComponentIds: updatedStackList,
previousComponentId: null,
updating: false,
commandType: null,
});
}
}, passedComponentId);
/**
* Modal dismiss event listener.
*/
useNavigationEvent_1.useModalDismissed(function () {
if (modalDismissedCommandBlacklist.indexOf(status.commandType) >= 0) {
return;
}
// This will only run when Android hardware back button is pressed.
var _a = onPreviousStackComponentShown(status.previousStackComponentIds), lastStackComponentId = _a.lastStackComponentId, updatedStackList = _a.updatedStackList;
updateNavigationStatus({
currentComponentId: lastStackComponentId,
previousStackComponentIds: updatedStackList,
previousComponentId: null,
updating: false,
commandType: null,
});
}, passedComponentId);
};
//# sourceMappingURL=useTrackNavigation.js.map
;