one
Version:
One is a new React Framework that makes Vite serve both native and web.
137 lines (136 loc) • 4.89 kB
JavaScript
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-web";
import useLatestCallback from "use-latest-callback";
import { useBackButton } from "./useBackButton.mjs";
import { useDocumentTitle } from "./useDocumentTitle.mjs";
import { useLinking } from "./useLinking.mjs";
import { useThenable } from "./useThenable.mjs";
import { SSRNavigationContainer } from "./SSRNavigationContainer.mjs";
import { jsx } from "react/jsx-runtime";
globalThis.REACT_NAVIGATION_DEVTOOLS = /* @__PURE__ */new WeakMap();
function NavigationContainerInner(props, ref) {
if (typeof window === "undefined") {
const {
theme = DefaultTheme,
initialState,
linking,
children
} = props;
return /* @__PURE__ */jsx(SSRNavigationContainer, {
initialState,
theme,
linking,
children
});
}
return /* @__PURE__ */jsx(NavigationContainerClientInner, {
...props,
forwardedRef: ref
});
}
function NavigationContainerClientInner({
forwardedRef,
direction = I18nManager.getConstants().isRTL ? "rtl" : "ltr",
theme = DefaultTheme,
linking,
fallback = null,
documentTitle,
onReady,
onStateChange,
...rest
}) {
const ref = forwardedRef;
const isLinkingEnabled = linking ? linking.enabled !== false : false;
if (linking?.config) {
validatePathConfig(linking.config);
}
const refContainer = React.useRef(null);
useBackButton(refContainer);
useDocumentTitle(refContainer, documentTitle);
const [lastUnhandledLink, setLastUnhandledLink] = React.useState();
const {
getInitialState
} = useLinking(refContainer, {
enabled: isLinkingEnabled,
prefixes: [],
...linking
}, setLastUnhandledLink);
const linkingContext = React.useMemo(() => ({
options: linking
}), [linking]);
const unhandledLinkingContext = React.useMemo(() => ({
lastUnhandledLink,
setLastUnhandledLink
}), [lastUnhandledLink, setLastUnhandledLink]);
const onReadyForLinkingHandling = useLatestCallback(() => {
const path = refContainer.current?.getCurrentRoute()?.path;
setLastUnhandledLink(previousLastUnhandledLink => {
if (previousLastUnhandledLink === path) {
return void 0;
}
return previousLastUnhandledLink;
});
onReady?.();
});
const onStateChangeForLinkingHandling = useLatestCallback(state => {
const path = refContainer.current?.getCurrentRoute()?.path;
setLastUnhandledLink(previousLastUnhandledLink => {
if (previousLastUnhandledLink === path) {
return void 0;
}
return previousLastUnhandledLink;
});
onStateChange?.(state);
});
React.useEffect(() => {
if (refContainer.current) {
REACT_NAVIGATION_DEVTOOLS.set(refContainer.current, {
get linking() {
return {
...linking,
enabled: isLinkingEnabled,
prefixes: linking?.prefixes ?? [],
getStateFromPath: linking?.getStateFromPath ?? getStateFromPath,
getPathFromState: linking?.getPathFromState ?? getPathFromState,
getActionFromState: linking?.getActionFromState ?? getActionFromState
};
}
});
}
});
const [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, () => refContainer.current);
const 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
})
})
})
});
}
const NavigationContainer = React.forwardRef(NavigationContainerInner);
export { NavigationContainer };
//# sourceMappingURL=NavigationContainer.mjs.map