Files
scrap/node_modules/next/dist/client/components/navigation.js
2024-09-24 03:52:46 +00:00

268 lines
10 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
ReadonlyURLSearchParams: null,
RedirectType: null,
ServerInsertedHTMLContext: null,
notFound: null,
permanentRedirect: null,
redirect: 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;
},
notFound: function() {
return _navigationreactserver.notFound;
},
permanentRedirect: function() {
return _navigationreactserver.permanentRedirect;
},
redirect: function() {
return _navigationreactserver.redirect;
},
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");
/**
* A [Client Component](https://nextjs.org/docs/app/building-your-application/rendering/client-components) hook
* that lets you *read* the current URL's search parameters.
*
* Learn more about [`URLSearchParams` on MDN](https://developer.mozilla.org/docs/Web/API/URLSearchParams)
*
* @example
* ```ts
* "use client"
* import { useSearchParams } from 'next/navigation'
*
* export default function Page() {
* const searchParams = useSearchParams()
* searchParams.get('foo') // returns 'bar' when ?foo=bar
* // ...
* }
* ```
*
* Read more: [Next.js Docs: `useSearchParams`](https://nextjs.org/docs/app/api-reference/functions/use-search-params)
*/ 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;
}
/**
* A [Client Component](https://nextjs.org/docs/app/building-your-application/rendering/client-components) hook
* that lets you read the current URL's pathname.
*
* @example
* ```ts
* "use client"
* import { usePathname } from 'next/navigation'
*
* export default function Page() {
* const pathname = usePathname() // returns "/dashboard" on /dashboard?foo=bar
* // ...
* }
* ```
*
* Read more: [Next.js Docs: `usePathname`](https://nextjs.org/docs/app/api-reference/functions/use-pathname)
*/ function 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);
}
/**
*
* This hook allows you to programmatically change routes inside [Client Component](https://nextjs.org/docs/app/building-your-application/rendering/client-components).
*
* @example
* ```ts
* "use client"
* import { useRouter } from 'next/navigation'
*
* export default function Page() {
* const router = useRouter()
* // ...
* router.push('/dashboard') // Navigate to /dashboard
* }
* ```
*
* Read more: [Next.js Docs: `useRouter`](https://nextjs.org/docs/app/api-reference/functions/use-router)
*/ function useRouter() {
const router = (0, _react.useContext)(_approutercontextsharedruntime.AppRouterContext);
if (router === null) {
throw new Error("invariant expected app router to be mounted");
}
return router;
}
/**
* A [Client Component](https://nextjs.org/docs/app/building-your-application/rendering/client-components) hook
* that lets you read a route's dynamic params filled in by the current URL.
*
* @example
* ```ts
* "use client"
* import { useParams } from 'next/navigation'
*
* export default function Page() {
* // on /dashboard/[team] where pathname is /dashboard/nextjs
* const { team } = useParams() // team === "nextjs"
* }
* ```
*
* Read more: [Next.js Docs: `useParams`](https://nextjs.org/docs/app/api-reference/functions/use-params)
*/ function useParams() {
return (0, _react.useContext)(_hooksclientcontextsharedruntime.PathParamsContext);
}
/** Get the canonical parameters from the current level to the leaf node. */ 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];
const segmentValue = (0, _getsegmentvalue.getSegmentValue)(segment);
if (!segmentValue || segmentValue.startsWith(_segment.PAGE_SEGMENT_KEY)) {
return segmentPath;
}
segmentPath.push(segmentValue);
return getSelectedLayoutSegmentPath(node, parallelRouteKey, false, segmentPath);
}
/**
* A [Client Component](https://nextjs.org/docs/app/building-your-application/rendering/client-components) hook
* that lets you read the active route segments **below** the Layout it is called from.
*
* @example
* ```ts
* 'use client'
*
* import { useSelectedLayoutSegments } from 'next/navigation'
*
* export default function ExampleClientComponent() {
* const segments = useSelectedLayoutSegments()
*
* return (
* <ul>
* {segments.map((segment, index) => (
* <li key={index}>{segment}</li>
* ))}
* </ul>
* )
* }
* ```
*
* Read more: [Next.js Docs: `useSelectedLayoutSegments`](https://nextjs.org/docs/app/api-reference/functions/use-selected-layout-segments)
*/ function useSelectedLayoutSegments(parallelRouteKey) {
if (parallelRouteKey === void 0) parallelRouteKey = "children";
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.tree, parallelRouteKey);
}
/**
* A [Client Component](https://nextjs.org/docs/app/building-your-application/rendering/client-components) hook
* that lets you read the active route segment **one level below** the Layout it is called from.
*
* @example
* ```ts
* 'use client'
* import { useSelectedLayoutSegment } from 'next/navigation'
*
* export default function ExampleClientComponent() {
* const segment = useSelectedLayoutSegment()
*
* return <p>Active segment: {segment}</p>
* }
* ```
*
* Read more: [Next.js Docs: `useSelectedLayoutSegment`](https://nextjs.org/docs/app/api-reference/functions/use-selected-layout-segment)
*/ function useSelectedLayoutSegment(parallelRouteKey) {
if (parallelRouteKey === void 0) parallelRouteKey = "children";
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