UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

172 lines 6.79 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var NavigationContainer_exports = {}; __export(NavigationContainer_exports, { NavigationContainer: () => NavigationContainer }); module.exports = __toCommonJS(NavigationContainer_exports); var import_core = require("@react-navigation/core"); var import_native = require("@react-navigation/native"); var React = __toESM(require("react"), 1); var import_react_native = require("react-native-web"); var import_use_latest_callback = __toESM(require("use-latest-callback"), 1); var import_useBackButton = require("./useBackButton.cjs"); var import_useDocumentTitle = require("./useDocumentTitle.cjs"); var import_useLinking = require("./useLinking.cjs"); var import_useThenable = require("./useThenable.cjs"); var import_SSRNavigationContainer = require("./SSRNavigationContainer.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); globalThis.REACT_NAVIGATION_DEVTOOLS = /* @__PURE__ */new WeakMap(); function NavigationContainerInner(props, ref) { if (typeof window === "undefined") { const { theme = import_native.DefaultTheme, initialState, linking, children } = props; return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_SSRNavigationContainer.SSRNavigationContainer, { initialState, theme, linking, children }); } return /* @__PURE__ */(0, import_jsx_runtime.jsx)(NavigationContainerClientInner, { ...props, forwardedRef: ref }); } function NavigationContainerClientInner({ forwardedRef, direction = import_react_native.I18nManager.getConstants().isRTL ? "rtl" : "ltr", theme = import_native.DefaultTheme, linking, fallback = null, documentTitle, onReady, onStateChange, ...rest }) { const ref = forwardedRef; const isLinkingEnabled = linking ? linking.enabled !== false : false; if (linking?.config) { (0, import_core.validatePathConfig)(linking.config); } const refContainer = React.useRef(null); (0, import_useBackButton.useBackButton)(refContainer); (0, import_useDocumentTitle.useDocumentTitle)(refContainer, documentTitle); const [lastUnhandledLink, setLastUnhandledLink] = React.useState(); const { getInitialState } = (0, import_useLinking.useLinking)(refContainer, { enabled: isLinkingEnabled, prefixes: [], ...linking }, setLastUnhandledLink); const linkingContext = React.useMemo(() => ({ options: linking }), [linking]); const unhandledLinkingContext = React.useMemo(() => ({ lastUnhandledLink, setLastUnhandledLink }), [lastUnhandledLink, setLastUnhandledLink]); const onReadyForLinkingHandling = (0, import_use_latest_callback.default)(() => { const path = refContainer.current?.getCurrentRoute()?.path; setLastUnhandledLink(previousLastUnhandledLink => { if (previousLastUnhandledLink === path) { return void 0; } return previousLastUnhandledLink; }); onReady?.(); }); const onStateChangeForLinkingHandling = (0, import_use_latest_callback.default)(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 ?? import_core.getStateFromPath, getPathFromState: linking?.getPathFromState ?? import_core.getPathFromState, getActionFromState: linking?.getActionFromState ?? import_core.getActionFromState }; } }); } }); const [isResolved, initialState] = (0, import_useThenable.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__ */(0, import_jsx_runtime.jsx)(import_core.ThemeProvider, { value: theme, children: fallback }); } return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_native.LocaleDirContext.Provider, { value: direction, children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_native.UNSTABLE_UnhandledLinkingContext.Provider, { value: unhandledLinkingContext, children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_native.LinkingContext.Provider, { value: linkingContext, children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_core.BaseNavigationContainer, { ...rest, theme, onReady: onReadyForLinkingHandling, onStateChange: onStateChangeForLinkingHandling, initialState: rest.initialState == null ? initialState : rest.initialState, ref: refContainer }) }) }) }); } const NavigationContainer = React.forwardRef(NavigationContainerInner);