UNPKG

use-react-native-navigation

Version:

A utility library for a easier use of react-native-navigation library.

109 lines 4.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = require("react"); var react_native_navigation_1 = require("react-native-navigation"); /** * ### ComponentDidAppear event listener. * * if targetId is provided, the effect will only run when targetId matches the incoming componentId. */ function useComponentDidAppear(effect, targetId) { react_1.useLayoutEffect(function () { var didAppearEvent = react_native_navigation_1.Navigation.events().registerComponentDidAppearListener( // @ts-ignore runEffectOnScreenTarget(effect, targetId)); return function () { return didAppearEvent.remove(); }; }, [effect, targetId]); } exports.useComponentDidAppear = useComponentDidAppear; /** * ### ComponentDidDisappear event listener. * * if targetId is provided, the effect will only run when targetId matches the incoming componentId. */ function useComponentDidDisappear(effect, targetId) { react_1.useLayoutEffect(function () { var didDisappearEvent = react_native_navigation_1.Navigation.events().registerComponentDidDisappearListener( // @ts-ignore runEffectOnScreenTarget(effect, targetId)); return function () { return didDisappearEvent.remove(); }; }, [effect, targetId]); } exports.useComponentDidDisappear = useComponentDidDisappear; /** * ### ModalDismissed event listener. * * if targetId is provided, the effect will only run when targetId matches the incoming componentId. */ function useModalDismissed(effect, targetId) { react_1.useLayoutEffect(function () { var modalDismissedEvent = react_native_navigation_1.Navigation.events().registerModalDismissedListener( // @ts-ignore runEffectOnScreenTarget(effect, targetId)); return function () { return modalDismissedEvent.remove(); }; }, [effect, targetId]); } exports.useModalDismissed = useModalDismissed; /** * ### Modal */ function useModalAttemptedToDismiss(effect, targetId) { react_1.useLayoutEffect(function () { var modalAttemptedToDismissEvent = react_native_navigation_1.Navigation.events().registerModalAttemptedToDismissListener( // @ts-ignore runEffectOnScreenTarget(effect, targetId)); return function () { return modalAttemptedToDismissEvent.remove(); }; }); } exports.useModalAttemptedToDismiss = useModalAttemptedToDismiss; /** * ### NavigationButton event listener. * * if targetButtonId is provided, the effect will only run when targetButtonId matches the incoming buttonId. */ function useNavigationButtonPressed(effect, targetButtonId) { react_1.useLayoutEffect(function () { var navButtonPressedEvent = react_native_navigation_1.Navigation.events().registerNavigationButtonPressedListener(runEffectOnComponentTarget(effect, targetButtonId)); return function () { return navButtonPressedEvent.remove(); }; }); } exports.useNavigationButtonPressed = useNavigationButtonPressed; /** * Run Effect on Screen Target * * If targetId is provided, the effect will only run when the targetId matches the event's componentId. * Otherwise, the effect will run every time. */ function runEffectOnScreenTarget(effect, targetId) { return function (event) { if (!targetId) { effect(event); return; } if (targetId && event.componentId === targetId) { effect(event); } }; } /** * ### Run Effect on Component target. * * If targetButtonId is provided, the effect will only run when the targetButtonId matches the event's buttonId. * Otherwise, the effect will run every time. */ function runEffectOnComponentTarget(effect, targetButtonId) { return function (event) { if (!targetButtonId) { effect(event); return; } /** * If buttonId exists, the event is NavigationButtonPressedEvent. */ if (targetButtonId && 'buttonId' in event && targetButtonId === event.buttonId) { effect(event); return; } }; } //# sourceMappingURL=useNavigationEvent.js.map