UNPKG

@tanstack/vue-router

Version:

Modern and scalable routing for Vue applications

45 lines (44 loc) 1.94 kB
import { useRouter } from "./useRouter.js"; import { 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.getMatchStore(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); } let match; const nearestRouteId = Vue.inject(routeIdContext); if (opts.from) match = useStore(router.stores.getMatchStore(opts.from)); else if (nearestRouteId) match = useStore(router.stores.getMatchStore(nearestRouteId)); else match = Vue.ref(void 0); const ownsMatch = nearestRouteId !== void 0 && (opts.from ?? nearestRouteId) === nearestRouteId; let lastOwnedMatch; const result = Vue.computed(() => { if (match.value !== void 0 && ownsMatch) lastOwnedMatch = match.value; const selectedMatch = match.value ?? lastOwnedMatch; if (selectedMatch === void 0) { if (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