UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

127 lines (126 loc) 4.54 kB
import { CatchBoundary, ErrorComponent } from "./CatchBoundary.js"; import { useRouter } from "./useRouter.js"; import { nearestMatchContext } from "./matchContext.js"; import { Transitioner } from "./Transitioner.js"; import { SafeFragment } from "./SafeFragment.js"; import { Match } from "./Match.js"; import { replaceEqualDeep, rootRouteId } from "@tanstack/router-core"; import { createComponent, memo } from "@solidjs/web"; import * as Solid from "solid-js"; import { isServer as isServer$1 } from "@tanstack/router-core/isServer"; //#region src/Matches.tsx var NearestMatchContext = nearestMatchContext; function Matches() { const router = useRouter(); const ResolvedSuspense = router.options.disableGlobalCatchBoundary || (isServer$1 ?? router.isServer) || typeof document !== "undefined" && router.ssr ? SafeFragment : Solid.Loading; const rootRoute = () => router.routesById[rootRouteId]; const PendingComponent = rootRoute().options.pendingComponent ?? router.options.defaultPendingComponent; return createComponent(router.options.InnerWrap || SafeFragment, { get children() { return createComponent(ResolvedSuspense, { get fallback() { return PendingComponent ? createComponent(PendingComponent, {}) : null; }, get children() { return [createComponent(Transitioner, {}), createComponent(MatchesInner, {})]; } }); } }); } function MatchesInner() { const router = useRouter(); const matchId = () => router.stores.firstMatchId.state; const routeId = () => matchId() ? rootRouteId : void 0; const match = () => routeId() ? router.stores.getMatchStoreByRouteId(rootRouteId).state : void 0; const hasPendingMatch = () => routeId() ? Boolean(router.stores.pendingRouteIds.state[rootRouteId]) : false; const resetKey = () => router.stores.loadedAt.state; const nearestMatch = { matchId, routeId, match, hasPending: hasPendingMatch }; const matchContent = () => createComponent(Solid.Show, { get when() { return matchId(); }, get children() { return createComponent(Match, { get matchId() { return matchId(); } }); } }); if (router.options.disableGlobalCatchBoundary) return createComponent(NearestMatchContext, { value: nearestMatch, get children() { return matchContent(); } }); return createComponent(NearestMatchContext, { value: nearestMatch, get children() { return createComponent(CatchBoundary, { getResetKey: () => resetKey(), errorComponent: ErrorComponent, get onCatch() { return process.env.NODE_ENV !== "production" ? (error) => { console.warn(`Warning: The following error wasn't caught by any route! At the very least, consider setting an 'errorComponent' in your RootRoute!`); console.warn(`Warning: ${error.message || error.toString()}`); } : void 0; }, get children() { return matchContent(); } }); } }); } function useMatchRoute() { const router = useRouter(); return (opts) => { return Solid.createMemo(() => { const { pending, caseSensitive, fuzzy, includeSearch, ...rest } = opts; router.stores.matchRouteReactivity.state; return router.matchRoute(rest, { pending, caseSensitive, fuzzy, includeSearch }); }); }; } function MatchRoute(props) { const params = useMatchRoute()(props); return memo(Solid.createMemo(() => { const matchedParams = params(); const child = props.children; if (typeof child === "function") return child(matchedParams); return matchedParams ? child : null; })); } function useMatches(opts) { const router = useRouter(); return Solid.createMemo((prev) => { const matches = router.stores.activeMatchesSnapshot.state; const res = opts?.select ? opts.select(matches) : matches; if (prev === void 0) return res; return replaceEqualDeep(prev, res); }); } function useParentMatches(opts) { const contextMatchId = Solid.useContext(nearestMatchContext).matchId; return useMatches({ select: (matches) => { matches = matches.slice(0, matches.findIndex((d) => d.id === contextMatchId())); return opts?.select ? opts.select(matches) : matches; } }); } function useChildMatches(opts) { const contextMatchId = Solid.useContext(nearestMatchContext).matchId; return useMatches({ select: (matches) => { matches = matches.slice(matches.findIndex((d) => d.id === contextMatchId()) + 1); return opts?.select ? opts.select(matches) : matches; } }); } //#endregion export { MatchRoute, Matches, useChildMatches, useMatchRoute, useMatches, useParentMatches }; //# sourceMappingURL=Matches.js.map