@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
115 lines (114 loc) • 3.88 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);
//#region src/Transitioner.tsx
/**
* Inline version of handleHashScroll that accepts a pre-captured location
* to avoid reading router.stores.location.state inside an effect callback
* (which would trigger a Solid v2 reactive warning).
*/
function handleHashScrollWithLocation(_router, location) {
if (typeof document !== "undefined" && document.querySelector) {
const hashScrollIntoViewOptions = location.state.__hashScrollIntoViewOptions ?? true;
if (hashScrollIntoViewOptions && location.hash !== "") {
const el = document.getElementById(location.hash);
if (el) el.scrollIntoView(hashScrollIntoViewOptions);
}
}
}
function Transitioner() {
const router = require_useRouter.useRouter();
let mountLoadForRouter = {
router,
mounted: false
};
const isLoading = solid_js.createMemo(() => router.stores.isLoading.state);
const [isSolidTransitioning] = [() => false];
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.runWithOwner(null, fn);
try {
solid_js.flush();
} catch {}
};
solid_js.onSettled(() => {
const unsub = router.history.subscribe(() => {
queueMicrotask(() => router.load());
});
router.updateLatestLocation();
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
});
return () => {
unsub();
};
});
solid_js.onSettled(() => {
if (typeof window !== "undefined" && router.ssr || mountLoadForRouter.router === router && mountLoadForRouter.mounted) return;
mountLoadForRouter = {
router,
mounted: true
};
queueMicrotask(() => {
const tryLoad = async () => {
try {
await router.load();
} catch (err) {
console.error(err);
}
};
tryLoad();
});
});
solid_js.createRenderEffect(() => [
isLoading(),
isPagePending(),
isAnyPending(),
router.stores.location.state,
router.stores.resolvedLocation.state
], ([currentIsLoading, currentIsPagePending, currentIsAnyPending, loc, resolvedLoc], prev) => {
if (!loc) return;
const previousIsLoading = prev?.[0];
const previousIsPagePending = prev?.[1];
const previousIsAnyPending = prev?.[2];
if (previousIsLoading && !currentIsLoading) router.emit({
type: "onLoad",
...(0, _tanstack_router_core.getLocationChangeInfo)(loc, resolvedLoc)
});
if (previousIsPagePending && !currentIsPagePending) router.emit({
type: "onBeforeRouteMount",
...(0, _tanstack_router_core.getLocationChangeInfo)(loc, resolvedLoc)
});
if (previousIsAnyPending && !currentIsAnyPending) {
const changeInfo = (0, _tanstack_router_core.getLocationChangeInfo)(loc, resolvedLoc);
router.emit({
type: "onResolved",
...changeInfo
});
solid_js.runWithOwner(null, () => {
router.batch(() => {
router.stores.status.setState(() => "idle");
router.stores.resolvedLocation.setState(() => loc);
});
});
if (changeInfo.hrefChanged) handleHashScrollWithLocation(router, loc);
}
});
return null;
}
//#endregion
exports.Transitioner = Transitioner;
//# sourceMappingURL=Transitioner.cjs.map