UNPKG

@tanstack/react-router

Version:

Modern and scalable routing for React applications

235 lines (234 loc) • 6.85 kB
import invariant from "tiny-invariant"; import { rootRouteId, trimPathLeft, joinPaths } from "@tanstack/router-core"; import { useLoaderData } from "./useLoaderData.js"; import { useLoaderDeps } from "./useLoaderDeps.js"; import { useParams } from "./useParams.js"; import { useSearch } from "./useSearch.js"; import { notFound } from "./not-found.js"; import { useNavigate } from "./useNavigate.js"; import { useMatch } from "./useMatch.js"; import { useRouter } from "./useRouter.js"; function getRouteApi(id) { return new RouteApi({ id }); } class RouteApi { /** * @deprecated Use the `getRouteApi` function instead. */ constructor({ id }) { this.useMatch = (opts) => { return useMatch({ select: opts == null ? void 0 : opts.select, from: this.id, structuralSharing: opts == null ? void 0 : opts.structuralSharing }); }; this.useRouteContext = (opts) => { return useMatch({ from: this.id, select: (d) => (opts == null ? void 0 : opts.select) ? opts.select(d.context) : d.context }); }; this.useSearch = (opts) => { return useSearch({ select: opts == null ? void 0 : opts.select, structuralSharing: opts == null ? void 0 : opts.structuralSharing, from: this.id }); }; this.useParams = (opts) => { return useParams({ select: opts == null ? void 0 : opts.select, structuralSharing: opts == null ? void 0 : opts.structuralSharing, from: this.id }); }; this.useLoaderDeps = (opts) => { return useLoaderDeps({ ...opts, from: this.id, strict: false }); }; this.useLoaderData = (opts) => { return useLoaderData({ ...opts, from: this.id, strict: false }); }; this.useNavigate = () => { const router = useRouter(); return useNavigate({ from: router.routesById[this.id].fullPath }); }; this.notFound = (opts) => { return notFound({ routeId: this.id, ...opts }); }; this.id = id; } } class Route { /** * @deprecated Use the `createRoute` function instead. */ constructor(options) { this.init = (opts) => { var _a, _b; this.originalIndex = opts.originalIndex; const options2 = this.options; const isRoot = !(options2 == null ? void 0 : options2.path) && !(options2 == null ? void 0 : options2.id); this.parentRoute = (_b = (_a = this.options).getParentRoute) == null ? void 0 : _b.call(_a); if (isRoot) { this._path = rootRouteId; } else { invariant( this.parentRoute, `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.` ); } let path = isRoot ? rootRouteId : options2.path; if (path && path !== "/") { path = trimPathLeft(path); } const customId = (options2 == null ? void 0 : options2.id) || path; let id = isRoot ? rootRouteId : joinPaths([ this.parentRoute.id === rootRouteId ? "" : this.parentRoute.id, customId ]); if (path === rootRouteId) { path = "/"; } if (id !== rootRouteId) { id = joinPaths(["/", id]); } const fullPath = id === rootRouteId ? "/" : joinPaths([this.parentRoute.fullPath, path]); this._path = path; this._id = id; this._fullPath = fullPath; this._to = fullPath; this._ssr = (options2 == null ? void 0 : options2.ssr) ?? opts.defaultSsr ?? true; }; this.addChildren = (children) => { return this._addFileChildren(children); }; this._addFileChildren = (children) => { if (Array.isArray(children)) { this.children = children; } if (typeof children === "object" && children !== null) { this.children = Object.values(children); } return this; }; this._addFileTypes = () => { return this; }; this.updateLoader = (options2) => { Object.assign(this.options, options2); return this; }; this.update = (options2) => { Object.assign(this.options, options2); return this; }; this.lazy = (lazyFn) => { this.lazyFn = lazyFn; return this; }; this.useMatch = (opts) => { return useMatch({ select: opts == null ? void 0 : opts.select, from: this.id, structuralSharing: opts == null ? void 0 : opts.structuralSharing }); }; this.useRouteContext = (opts) => { return useMatch({ ...opts, from: this.id, select: (d) => (opts == null ? void 0 : opts.select) ? opts.select(d.context) : d.context }); }; this.useSearch = (opts) => { return useSearch({ select: opts == null ? void 0 : opts.select, structuralSharing: opts == null ? void 0 : opts.structuralSharing, from: this.id }); }; this.useParams = (opts) => { return useParams({ select: opts == null ? void 0 : opts.select, structuralSharing: opts == null ? void 0 : opts.structuralSharing, from: this.id }); }; this.useLoaderDeps = (opts) => { return useLoaderDeps({ ...opts, from: this.id }); }; this.useLoaderData = (opts) => { return useLoaderData({ ...opts, from: this.id }); }; this.useNavigate = () => { return useNavigate({ from: this.fullPath }); }; this.options = options || {}; this.isRoot = !(options == null ? void 0 : options.getParentRoute); invariant( !((options == null ? void 0 : options.id) && (options == null ? void 0 : options.path)), `Route cannot have both an 'id' and a 'path' option.` ); this.$$typeof = Symbol.for("react.memo"); } get to() { return this._to; } get id() { return this._id; } get path() { return this._path; } get fullPath() { return this._fullPath; } get ssr() { return this._ssr; } } function createRoute(options) { return new Route(options); } function createRootRouteWithContext() { return (options) => { return createRootRoute(options); }; } const rootRouteWithContext = createRootRouteWithContext; class RootRoute extends Route { /** * @deprecated `RootRoute` is now an internal implementation detail. Use `createRootRoute()` instead. */ constructor(options) { super(options); } } function createRootRoute(options) { return new RootRoute(options); } function createRouteMask(opts) { return opts; } class NotFoundRoute extends Route { constructor(options) { super({ ...options, id: "404" }); } } export { NotFoundRoute, RootRoute, Route, RouteApi, createRootRoute, createRootRouteWithContext, createRoute, createRouteMask, getRouteApi, rootRouteWithContext }; //# sourceMappingURL=route.js.map