UNPKG

next

Version:

The React Framework

181 lines (179 loc) • 7.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { ReadonlyURLSearchParams: null, RedirectType: null, ServerInsertedHTMLContext: null, forbidden: null, notFound: null, permanentRedirect: null, redirect: null, unauthorized: null, unstable_rethrow: null, useParams: null, usePathname: null, useRouter: null, useSearchParams: null, useSelectedLayoutSegment: null, useSelectedLayoutSegments: null, useServerInsertedHTML: null }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { ReadonlyURLSearchParams: function() { return _navigationreactserver.ReadonlyURLSearchParams; }, RedirectType: function() { return _navigationreactserver.RedirectType; }, ServerInsertedHTMLContext: function() { return _serverinsertedhtmlsharedruntime.ServerInsertedHTMLContext; }, forbidden: function() { return _navigationreactserver.forbidden; }, notFound: function() { return _navigationreactserver.notFound; }, permanentRedirect: function() { return _navigationreactserver.permanentRedirect; }, redirect: function() { return _navigationreactserver.redirect; }, unauthorized: function() { return _navigationreactserver.unauthorized; }, unstable_rethrow: function() { return _navigationreactserver.unstable_rethrow; }, useParams: function() { return useParams; }, usePathname: function() { return usePathname; }, useRouter: function() { return useRouter; }, useSearchParams: function() { return useSearchParams; }, useSelectedLayoutSegment: function() { return useSelectedLayoutSegment; }, useSelectedLayoutSegments: function() { return useSelectedLayoutSegments; }, useServerInsertedHTML: function() { return _serverinsertedhtmlsharedruntime.useServerInsertedHTML; } }); const _react = require("react"); const _approutercontextsharedruntime = require("../../shared/lib/app-router-context.shared-runtime"); const _hooksclientcontextsharedruntime = require("../../shared/lib/hooks-client-context.shared-runtime"); const _getsegmentvalue = require("./router-reducer/reducers/get-segment-value"); const _segment = require("../../shared/lib/segment"); const _navigationreactserver = require("./navigation.react-server"); const _serverinsertedhtmlsharedruntime = require("../../shared/lib/server-inserted-html.shared-runtime"); const useDynamicRouteParams = typeof window === 'undefined' ? require('../../server/app-render/dynamic-rendering').useDynamicRouteParams : undefined; function useSearchParams() { const searchParams = (0, _react.useContext)(_hooksclientcontextsharedruntime.SearchParamsContext); // In the case where this is `null`, the compat types added in // `next-env.d.ts` will add a new overload that changes the return type to // include `null`. const readonlySearchParams = (0, _react.useMemo)(()=>{ if (!searchParams) { // When the router is not ready in pages, we won't have the search params // available. return null; } return new _navigationreactserver.ReadonlyURLSearchParams(searchParams); }, [ searchParams ]); if (typeof window === 'undefined') { // AsyncLocalStorage should not be included in the client bundle. const { bailoutToClientRendering } = require('./bailout-to-client-rendering'); // TODO-APP: handle dynamic = 'force-static' here and on the client bailoutToClientRendering('useSearchParams()'); } return readonlySearchParams; } function usePathname() { useDynamicRouteParams == null ? void 0 : useDynamicRouteParams('usePathname()'); // In the case where this is `null`, the compat types added in `next-env.d.ts` // will add a new overload that changes the return type to include `null`. return (0, _react.useContext)(_hooksclientcontextsharedruntime.PathnameContext); } function useRouter() { const router = (0, _react.useContext)(_approutercontextsharedruntime.AppRouterContext); if (router === null) { throw Object.defineProperty(new Error('invariant expected app router to be mounted'), "__NEXT_ERROR_CODE", { value: "E238", enumerable: false, configurable: true }); } return router; } function useParams() { useDynamicRouteParams == null ? void 0 : useDynamicRouteParams('useParams()'); return (0, _react.useContext)(_hooksclientcontextsharedruntime.PathParamsContext); } /** Get the canonical parameters from the current level to the leaf node. */ // Client components API function getSelectedLayoutSegmentPath(tree, parallelRouteKey, first, segmentPath) { if (first === void 0) first = true; if (segmentPath === void 0) segmentPath = []; let node; if (first) { // Use the provided parallel route key on the first parallel route node = tree[1][parallelRouteKey]; } else { // After first parallel route prefer children, if there's no children pick the first parallel route. const parallelRoutes = tree[1]; var _parallelRoutes_children; node = (_parallelRoutes_children = parallelRoutes.children) != null ? _parallelRoutes_children : Object.values(parallelRoutes)[0]; } if (!node) return segmentPath; const segment = node[0]; let segmentValue = (0, _getsegmentvalue.getSegmentValue)(segment); if (!segmentValue || segmentValue.startsWith(_segment.PAGE_SEGMENT_KEY)) { return segmentPath; } segmentPath.push(segmentValue); return getSelectedLayoutSegmentPath(node, parallelRouteKey, false, segmentPath); } function useSelectedLayoutSegments(parallelRouteKey) { if (parallelRouteKey === void 0) parallelRouteKey = 'children'; useDynamicRouteParams == null ? void 0 : useDynamicRouteParams('useSelectedLayoutSegments()'); const context = (0, _react.useContext)(_approutercontextsharedruntime.LayoutRouterContext); // @ts-expect-error This only happens in `pages`. Type is overwritten in navigation.d.ts if (!context) return null; return getSelectedLayoutSegmentPath(context.parentTree, parallelRouteKey); } function useSelectedLayoutSegment(parallelRouteKey) { if (parallelRouteKey === void 0) parallelRouteKey = 'children'; useDynamicRouteParams == null ? void 0 : useDynamicRouteParams('useSelectedLayoutSegment()'); const selectedLayoutSegments = useSelectedLayoutSegments(parallelRouteKey); if (!selectedLayoutSegments || selectedLayoutSegments.length === 0) { return null; } const selectedLayoutSegment = parallelRouteKey === 'children' ? selectedLayoutSegments[0] : selectedLayoutSegments[selectedLayoutSegments.length - 1]; // if the default slot is showing, we return null since it's not technically "selected" (it's a fallback) // and returning an internal value like `__DEFAULT__` would be confusing. return selectedLayoutSegment === _segment.DEFAULT_SEGMENT_KEY ? null : selectedLayoutSegment; } if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') { Object.defineProperty(exports.default, '__esModule', { value: true }); Object.assign(exports.default, exports); module.exports = exports.default; } //# sourceMappingURL=navigation.js.map