one
Version:
One is a new React Framework that makes Vite serve both native and web.
147 lines (146 loc) • 6.3 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { BaseNavigationContainer, getActionFromState, getPathFromState, getStateFromPath, ThemeProvider, validatePathConfig } from "@react-navigation/core";
import { DefaultTheme, LinkingContext, LocaleDirContext, UNSTABLE_UnhandledLinkingContext as UnhandledLinkingContext } from "@react-navigation/native";
import * as React from "react";
import { I18nManager } from "react-native";
import useLatestCallback from "use-latest-callback";
import { useBackButton } from "./useBackButton.native.js";
import { useDocumentTitle } from "./useDocumentTitle.native.js";
import { useLinking } from "./useLinking.native.js";
import { useThenable } from "./useThenable.native.js";
import { SSRNavigationContainer } from "./SSRNavigationContainer.native.js";
globalThis.REACT_NAVIGATION_DEVTOOLS = /* @__PURE__ */new WeakMap();
function NavigationContainerInner(props, ref) {
if (typeof window === "undefined") {
var {
theme = DefaultTheme,
initialState,
linking,
children
} = props;
return /* @__PURE__ */_jsx(SSRNavigationContainer, {
initialState,
theme,
linking,
children
});
}
return /* @__PURE__ */_jsx(NavigationContainerClientInner, {
...props,
forwardedRef: ref
});
}
function NavigationContainerClientInner(param) {
var {
forwardedRef,
direction = I18nManager.getConstants().isRTL ? "rtl" : "ltr",
theme = DefaultTheme,
linking,
fallback = null,
documentTitle,
onReady,
onStateChange,
...rest
} = param;
var ref = forwardedRef;
var isLinkingEnabled = linking ? linking.enabled !== false : false;
if (linking === null || linking === void 0 ? void 0 : linking.config) {
validatePathConfig(linking.config);
}
var refContainer = React.useRef(null);
useBackButton(refContainer);
useDocumentTitle(refContainer, documentTitle);
var [lastUnhandledLink, setLastUnhandledLink] = React.useState();
var {
getInitialState
} = useLinking(refContainer, {
enabled: isLinkingEnabled,
prefixes: [],
...linking
}, setLastUnhandledLink);
var linkingContext = React.useMemo(function () {
return {
options: linking
};
}, [linking]);
var unhandledLinkingContext = React.useMemo(function () {
return {
lastUnhandledLink,
setLastUnhandledLink
};
}, [lastUnhandledLink, setLastUnhandledLink]);
var onReadyForLinkingHandling = useLatestCallback(function () {
var _refContainer_current_getCurrentRoute, _refContainer_current;
var path = (_refContainer_current = refContainer.current) === null || _refContainer_current === void 0 ? void 0 : (_refContainer_current_getCurrentRoute = _refContainer_current.getCurrentRoute()) === null || _refContainer_current_getCurrentRoute === void 0 ? void 0 : _refContainer_current_getCurrentRoute.path;
setLastUnhandledLink(function (previousLastUnhandledLink) {
if (previousLastUnhandledLink === path) {
return void 0;
}
return previousLastUnhandledLink;
});
onReady === null || onReady === void 0 ? void 0 : onReady();
});
var onStateChangeForLinkingHandling = useLatestCallback(function (state) {
var _refContainer_current_getCurrentRoute, _refContainer_current;
var path = (_refContainer_current = refContainer.current) === null || _refContainer_current === void 0 ? void 0 : (_refContainer_current_getCurrentRoute = _refContainer_current.getCurrentRoute()) === null || _refContainer_current_getCurrentRoute === void 0 ? void 0 : _refContainer_current_getCurrentRoute.path;
setLastUnhandledLink(function (previousLastUnhandledLink) {
if (previousLastUnhandledLink === path) {
return void 0;
}
return previousLastUnhandledLink;
});
onStateChange === null || onStateChange === void 0 ? void 0 : onStateChange(state);
});
React.useEffect(function () {
if (refContainer.current) {
REACT_NAVIGATION_DEVTOOLS.set(refContainer.current, {
get linking() {
var _ref, _ref1, _ref2, _ref3;
return {
...linking,
enabled: isLinkingEnabled,
prefixes: (_ref = linking === null || linking === void 0 ? void 0 : linking.prefixes) !== null && _ref !== void 0 ? _ref : [],
getStateFromPath: (_ref1 = linking === null || linking === void 0 ? void 0 : linking.getStateFromPath) !== null && _ref1 !== void 0 ? _ref1 : getStateFromPath,
getPathFromState: (_ref2 = linking === null || linking === void 0 ? void 0 : linking.getPathFromState) !== null && _ref2 !== void 0 ? _ref2 : getPathFromState,
getActionFromState: (_ref3 = linking === null || linking === void 0 ? void 0 : linking.getActionFromState) !== null && _ref3 !== void 0 ? _ref3 : getActionFromState
};
}
});
}
});
var [isResolved, initialState] = useThenable(getInitialState);
if (process.env.ONE_DEBUG_ROUTER) {
console.info(`[one] \u{1F3E0} NavigationContainer isResolved=${isResolved} initialState=`, JSON.stringify(initialState, null, 2));
console.info(`[one] \u{1F3E0} NavigationContainer rest.initialState=`, rest.initialState);
}
React.useImperativeHandle(ref, function () {
return refContainer.current;
});
var isLinkingReady = rest.initialState != null || !isLinkingEnabled || isResolved;
if (!isLinkingReady) {
return /* @__PURE__ */_jsx(ThemeProvider, {
value: theme,
children: fallback
});
}
return /* @__PURE__ */_jsx(LocaleDirContext.Provider, {
value: direction,
children: /* @__PURE__ */_jsx(UnhandledLinkingContext.Provider, {
value: unhandledLinkingContext,
children: /* @__PURE__ */_jsx(LinkingContext.Provider, {
value: linkingContext,
children: /* @__PURE__ */_jsx(BaseNavigationContainer, {
...rest,
theme,
onReady: onReadyForLinkingHandling,
onStateChange: onStateChangeForLinkingHandling,
initialState: rest.initialState == null ? initialState : rest.initialState,
ref: refContainer
})
})
})
});
}
var NavigationContainer = /* @__PURE__ */React.forwardRef(NavigationContainerInner);
export { NavigationContainer };
//# sourceMappingURL=NavigationContainer.native.js.map