UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

100 lines 3.46 kB
import warning from 'tiny-warning'; import { createRoute } from './route'; import { useMatch } from './useMatch'; import { useLoaderDeps } from './useLoaderDeps'; import { useLoaderData } from './useLoaderData'; import { useSearch } from './useSearch'; import { useParams } from './useParams'; import { useNavigate } from './useNavigate'; import { useRouter } from './useRouter'; import { useRouteContext } from './useRouteContext'; export function createFileRoute(path) { if (typeof path === 'object') { return new FileRoute(path, { silent: true, }).createRoute(path); } return new FileRoute(path, { silent: true, }).createRoute; } /** @deprecated It's no longer recommended to use the `FileRoute` class directly. Instead, use `createFileRoute('/path/to/file')(options)` to create a file route. */ export class FileRoute { constructor(path, _opts) { this.path = path; this.createRoute = (options) => { if (process.env.NODE_ENV !== 'production') { warning(this.silent, 'FileRoute is deprecated and will be removed in the next major version. Use the createFileRoute(path)(options) function instead.'); } const route = createRoute(options); route.isRoot = false; return route; }; this.silent = _opts?.silent; } } /** @deprecated It's recommended not to split loaders into separate files. Instead, place the loader function in the the main route file, inside the `createFileRoute('/path/to/file)(options)` options. */ export function FileRouteLoader(_path) { if (process.env.NODE_ENV !== 'production') { warning(false, `FileRouteLoader is deprecated and will be removed in the next major version. Please place the loader function in the the main route file, inside the \`createFileRoute('/path/to/file')(options)\` options`); } return (loaderFn) => loaderFn; } export class LazyRoute { constructor(opts) { this.useMatch = (opts) => { return useMatch({ select: opts?.select, from: this.options.id, }); }; this.useRouteContext = (opts) => { return useRouteContext({ ...opts, from: this.options.id }); }; this.useSearch = (opts) => { return useSearch({ select: opts?.select, from: this.options.id, }); }; this.useParams = (opts) => { return useParams({ select: opts?.select, from: this.options.id, }); }; this.useLoaderDeps = (opts) => { return useLoaderDeps({ ...opts, from: this.options.id }); }; this.useLoaderData = (opts) => { return useLoaderData({ ...opts, from: this.options.id }); }; this.useNavigate = () => { const router = useRouter(); return useNavigate({ from: router.routesById[this.options.id].fullPath }); }; this.options = opts; } } export function createLazyRoute(id) { return (opts) => { return new LazyRoute({ id: id, ...opts, }); }; } export function createLazyFileRoute(id) { if (typeof id === 'object') { return new LazyRoute(id); } return (opts) => new LazyRoute({ id, ...opts }); } //# sourceMappingURL=fileRoute.js.map