@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
41 lines (40 loc) • 1.72 kB
JavaScript
const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
const require_useRouter = require("./useRouter.cjs");
const require_matchContext = require("./matchContext.cjs");
let _tanstack_router_core = require("@tanstack/router-core");
let solid_js = require("solid-js");
solid_js = require_runtime.__toESM(solid_js);
//#region src/useMatch.tsx
function useMatch(opts) {
const router = require_useRouter.useRouter();
const nearestMatch = opts.from ? void 0 : solid_js.useContext(require_matchContext.nearestMatchContext);
const match = () => {
if (opts.from) {
const ids = router.stores.matchesId.state;
for (const id of ids) {
const matchStore = router.stores.activeMatchStoresById.get(id);
if (matchStore?.routeId === opts.from) return matchStore.state;
}
return;
}
return nearestMatch?.match();
};
return solid_js.createMemo((prev) => {
const selectedMatch = match();
if (selectedMatch === void 0) {
const hasPendingMatch = opts.from ? Boolean(router.stores.pendingRouteIds.state[opts.from]) : nearestMatch?.hasPending() ?? false;
const isTransitioning = router.stores.isTransitioning.state;
if (!hasPendingMatch && !isTransitioning && (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!"}`);
(0, _tanstack_router_core.invariant)();
}
return;
}
const res = opts.select ? opts.select(selectedMatch) : selectedMatch;
if (prev === void 0) return res;
return (0, _tanstack_router_core.replaceEqualDeep)(prev, res);
});
}
//#endregion
exports.useMatch = useMatch;
//# sourceMappingURL=useMatch.cjs.map