@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
97 lines (96 loc) • 3.57 kB
JavaScript
const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
const require_useRouter = require("./useRouter.cjs");
let _tanstack_router_core = require("@tanstack/router-core");
let solid_js = require("solid-js");
solid_js = require_runtime.__toESM(solid_js);
let _tanstack_router_core_isServer = require("@tanstack/router-core/isServer");
//#region src/Transitioner.tsx
function Transitioner() {
const router = require_useRouter.useRouter();
let mountLoadForRouter = {
router,
mounted: false
};
const isLoading = solid_js.createMemo(() => router.stores.isLoading.state);
if (_tanstack_router_core_isServer.isServer ?? router.isServer) return null;
const [isSolidTransitioning, startSolidTransition] = solid_js.useTransition();
const hasPendingMatches = solid_js.createMemo(() => router.stores.hasPendingMatches.state);
const isAnyPending = solid_js.createMemo(() => isLoading() || isSolidTransitioning() || hasPendingMatches());
const isPagePending = solid_js.createMemo(() => isLoading() || hasPendingMatches());
router.startTransition = (fn) => {
solid_js.startTransition(() => {
startSolidTransition(fn);
});
};
solid_js.onMount(() => {
const unsub = router.history.subscribe(router.load);
const nextLocation = router.buildLocation({
to: router.latestLocation.pathname,
search: true,
params: true,
hash: true,
state: true,
_includeValidateSearch: true
});
if ((0, _tanstack_router_core.trimPathRight)(router.latestLocation.publicHref) !== (0, _tanstack_router_core.trimPathRight)(nextLocation.publicHref)) router.commitLocation({
...nextLocation,
replace: true
});
solid_js.onCleanup(() => {
unsub();
});
});
solid_js.createRenderEffect(() => {
solid_js.untrack(() => {
if (typeof window !== "undefined" && router.ssr || mountLoadForRouter.router === router && mountLoadForRouter.mounted) return;
mountLoadForRouter = {
router,
mounted: true
};
const tryLoad = async () => {
try {
await router.load();
} catch (err) {
console.error(err);
}
};
tryLoad();
});
});
solid_js.createRenderEffect((previousIsLoading = false) => {
const currentIsLoading = isLoading();
if (previousIsLoading && !currentIsLoading) router.emit({
type: "onLoad",
...(0, _tanstack_router_core.getLocationChangeInfo)(router.stores.location.state, router.stores.resolvedLocation.state)
});
return currentIsLoading;
});
solid_js.createComputed((previousIsPagePending = false) => {
const currentIsPagePending = isPagePending();
if (previousIsPagePending && !currentIsPagePending) router.emit({
type: "onBeforeRouteMount",
...(0, _tanstack_router_core.getLocationChangeInfo)(router.stores.location.state, router.stores.resolvedLocation.state)
});
return currentIsPagePending;
});
solid_js.createRenderEffect((previousIsAnyPending = false) => {
const currentIsAnyPending = isAnyPending();
if (previousIsAnyPending && !currentIsAnyPending) {
const changeInfo = (0, _tanstack_router_core.getLocationChangeInfo)(router.stores.location.state, router.stores.resolvedLocation.state);
router.emit({
type: "onResolved",
...changeInfo
});
solid_js.batch(() => {
router.stores.status.setState(() => "idle");
router.stores.resolvedLocation.setState(() => router.stores.location.state);
});
if (changeInfo.hrefChanged) (0, _tanstack_router_core.handleHashScroll)(router);
}
return currentIsAnyPending;
});
return null;
}
//#endregion
exports.Transitioner = Transitioner;
//# sourceMappingURL=Transitioner.cjs.map