UNPKG

@tanstack/vue-router

Version:

Modern and scalable routing for Vue applications

47 lines (46 loc) 2.27 kB
import { useRouter } from "./useRouter.js"; import { injectDummyPendingMatch, injectPendingMatch, routeIdContext } from "./matchContext.js"; import { invariant } from "@tanstack/router-core"; import * as Vue from "vue"; import { isServer } from "@tanstack/router-core/isServer"; import { useStore } from "@tanstack/vue-store"; //#region src/useMatch.tsx function useMatch(opts) { const router = useRouter(); if (isServer ?? router.isServer) { const nearestRouteId = opts.from ? void 0 : Vue.inject(routeIdContext); const match = (opts.from ?? nearestRouteId ? router.stores.getRouteMatchStore(opts.from ?? nearestRouteId) : void 0)?.get(); if ((opts.shouldThrow ?? true) && !match) { if (process.env.NODE_ENV !== "production") throw new Error(`Invariant failed: Could not find ${opts.from ? `an active match from "${opts.from}"` : "a nearest match!"}`); invariant(); } if (match === void 0) return Vue.ref(void 0); return Vue.ref(opts.select ? opts.select(match) : match); } const hasPendingNearestMatch = opts.from ? injectDummyPendingMatch() : injectPendingMatch(); let match; if (opts.from) match = useStore(router.stores.getRouteMatchStore(opts.from), (value) => value); else { const nearestRouteId = Vue.inject(routeIdContext); if (nearestRouteId) match = useStore(router.stores.getRouteMatchStore(nearestRouteId), (value) => value); else match = Vue.ref(void 0); } const hasPendingRouteMatch = opts.from ? useStore(router.stores.pendingRouteIds, (ids) => ids) : void 0; const isTransitioning = useStore(router.stores.isTransitioning, (value) => value, { equal: Object.is }); const result = Vue.computed(() => { const selectedMatch = match.value; if (selectedMatch === void 0) { if (!(opts.from ? Boolean(hasPendingRouteMatch?.value[opts.from]) : hasPendingNearestMatch.value) && !isTransitioning.value && (opts.shouldThrow ?? true)) { if (process.env.NODE_ENV !== "production") throw new Error(`Invariant failed: Could not find ${opts.from ? `an active match from "${opts.from}"` : "a nearest match!"}`); invariant(); } return; } return opts.select ? opts.select(selectedMatch) : selectedMatch; }); result.value; return result; } //#endregion export { useMatch }; //# sourceMappingURL=useMatch.js.map