UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

113 lines (112 loc) 3.57 kB
import { useRouter } from "./useRouter.js"; import { getLocationChangeInfo, trimPathRight } from "@tanstack/router-core"; import * as Solid from "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 = useRouter(); let mountLoadForRouter = { router, mounted: false }; const isLoading = Solid.createMemo(() => router.stores.isLoading.state); const [isSolidTransitioning] = [() => false]; const hasPendingMatches = Solid.createMemo(() => router.stores.hasPendingMatches.state); const isAnyPending = Solid.createMemo(() => isLoading() || isSolidTransitioning() || hasPendingMatches()); const isPagePending = Solid.createMemo(() => isLoading() || hasPendingMatches()); router.startTransition = (fn) => { Solid.runWithOwner(null, fn); try { Solid.flush(); } catch {} }; Solid.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 (trimPathRight(router.latestLocation.publicHref) !== trimPathRight(nextLocation.publicHref)) router.commitLocation({ ...nextLocation, replace: true }); return () => { unsub(); }; }); Solid.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.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", ...getLocationChangeInfo(loc, resolvedLoc) }); if (previousIsPagePending && !currentIsPagePending) router.emit({ type: "onBeforeRouteMount", ...getLocationChangeInfo(loc, resolvedLoc) }); if (previousIsAnyPending && !currentIsAnyPending) { const changeInfo = getLocationChangeInfo(loc, resolvedLoc); router.emit({ type: "onResolved", ...changeInfo }); Solid.runWithOwner(null, () => { router.batch(() => { router.stores.status.setState(() => "idle"); router.stores.resolvedLocation.setState(() => loc); }); }); if (changeInfo.hrefChanged) handleHashScrollWithLocation(router, loc); } }); return null; } //#endregion export { Transitioner }; //# sourceMappingURL=Transitioner.js.map