xref: /expo/packages/expo-router/build/ExpoRoot.js (revision da3c31f4)
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3    if (k2 === undefined) k2 = k;
4    var desc = Object.getOwnPropertyDescriptor(m, k);
5    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6      desc = { enumerable: true, get: function() { return m[k]; } };
7    }
8    Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10    if (k2 === undefined) k2 = k;
11    o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14    Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16    o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19    if (mod && mod.__esModule) return mod;
20    var result = {};
21    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22    __setModuleDefault(result, mod);
23    return result;
24};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26    return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.ExpoRoot = void 0;
30const expo_constants_1 = __importDefault(require("expo-constants"));
31const expo_status_bar_1 = require("expo-status-bar");
32const react_1 = __importStar(require("react"));
33const react_native_1 = require("react-native");
34const react_native_safe_area_context_1 = require("react-native-safe-area-context");
35const NavigationContainer_1 = __importDefault(require("./fork/NavigationContainer"));
36const router_store_1 = require("./global-state/router-store");
37const Splash_1 = require("./views/Splash");
38const isTestEnv = process.env.NODE_ENV === 'test';
39const INITIAL_METRICS = react_native_1.Platform.OS === 'web' || isTestEnv
40    ? {
41        frame: { x: 0, y: 0, width: 0, height: 0 },
42        insets: { top: 0, left: 0, right: 0, bottom: 0 },
43    }
44    : undefined;
45const hasViewControllerBasedStatusBarAppearance = react_native_1.Platform.OS === 'ios' &&
46    !!expo_constants_1.default.expoConfig?.ios?.infoPlist?.UIViewControllerBasedStatusBarAppearance;
47function ExpoRoot({ wrapper: ParentWrapper = react_1.Fragment, ...props }) {
48    /*
49     * Due to static rendering we need to wrap these top level views in second wrapper
50     * View's like <SafeAreaProvider /> generate a <div> so if the parent wrapper
51     * is a HTML document, we need to ensure its inside the <body>
52     */
53    const wrapper = ({ children }) => {
54        return (react_1.default.createElement(ParentWrapper, null,
55            react_1.default.createElement(react_native_safe_area_context_1.SafeAreaProvider
56            // SSR support
57            , {
58                // SSR support
59                initialMetrics: INITIAL_METRICS },
60                children,
61                !hasViewControllerBasedStatusBarAppearance && react_1.default.createElement(expo_status_bar_1.StatusBar, { style: "auto" }))));
62    };
63    return react_1.default.createElement(ContextNavigator, { ...props, wrapper: wrapper });
64}
65exports.ExpoRoot = ExpoRoot;
66const initialUrl = react_native_1.Platform.OS === 'web' && typeof window !== 'undefined'
67    ? new URL(window.location.href)
68    : undefined;
69function ContextNavigator({ context, location: initialLocation = initialUrl, wrapper: WrapperComponent = react_1.Fragment, }) {
70    const store = (0, router_store_1.useInitializeExpoRouter)(context, initialLocation);
71    if (store.shouldShowTutorial()) {
72        Splash_1.SplashScreen.hideAsync();
73        if (process.env.NODE_ENV === 'development') {
74            const Tutorial = require('./onboard/Tutorial').Tutorial;
75            return (react_1.default.createElement(WrapperComponent, null,
76                react_1.default.createElement(Tutorial, null)));
77        }
78        else {
79            // Ensure tutorial styles are stripped in production.
80            return null;
81        }
82    }
83    const Component = store.rootComponent;
84    return (react_1.default.createElement(NavigationContainer_1.default, { ref: store.navigationRef, initialState: store.initialState, linking: store.linking, onUnhandledAction: onUnhandledAction, documentTitle: {
85            enabled: false,
86        } },
87        react_1.default.createElement(WrapperComponent, null,
88            react_1.default.createElement(Component, null))));
89}
90let onUnhandledAction;
91if (process.env.NODE_ENV !== 'production') {
92    onUnhandledAction = (action) => {
93        const payload = action.payload;
94        let message = `The action '${action.type}'${payload ? ` with payload ${JSON.stringify(action.payload)}` : ''} was not handled by any navigator.`;
95        switch (action.type) {
96            case 'NAVIGATE':
97            case 'PUSH':
98            case 'REPLACE':
99            case 'JUMP_TO':
100                if (payload?.name) {
101                    message += `\n\nDo you have a route named '${payload.name}'?`;
102                }
103                else {
104                    message += `\n\nYou need to pass the name of the screen to navigate to. This may be a bug.`;
105                }
106                break;
107            case 'GO_BACK':
108            case 'POP':
109            case 'POP_TO_TOP':
110                message += `\n\nIs there any screen to go back to?`;
111                break;
112            case 'OPEN_DRAWER':
113            case 'CLOSE_DRAWER':
114            case 'TOGGLE_DRAWER':
115                message += `\n\nIs your screen inside a Drawer navigator?`;
116                break;
117        }
118        message += `\n\nThis is a development-only warning and won't be shown in production.`;
119        if (process.env.NODE_ENV === 'test') {
120            throw new Error(message);
121        }
122        console.error(message);
123    };
124}
125else {
126    onUnhandledAction = function () { };
127}
128//# sourceMappingURL=ExpoRoot.js.map