Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

Transitioner.tsx

144 lines3.6 KB

1import * as Solid from 'solid-js'
2import {
3 getLocationChangeInfo,
4 handleHashScroll,
5 trimPathRight,
6} from '@tanstack/router-core'
7import { useRouter } from './useRouter'
8import { useRouterState } from './useRouterState'
9import { usePrevious } from './utils'
10​
11export function Transitioner() {
12 const router = useRouter()
13 let mountLoadForRouter = { router, mounted: false }
14 const isLoading = useRouterState({
15 select: ({ isLoading }) => isLoading,
16 })
17​
18 if (router.isServer) {
19 return null
20 }
21​
22 const [isTransitioning, setIsTransitioning] = Solid.createSignal(false)
23 // Track pending state changes
24 const hasPendingMatches = useRouterState({
25 select: (s) => s.matches.some((d) => d.status === 'pending'),
26 })
27​
28 const previousIsLoading = usePrevious(isLoading)
29​
30 const isAnyPending = () =>
31 isLoading() || isTransitioning() || hasPendingMatches()
32 const previousIsAnyPending = usePrevious(isAnyPending)
33​
34 const isPagePending = () => isLoading() || hasPendingMatches()
35 const previousIsPagePending = usePrevious(isPagePending)
36​
37 router.startTransition = async (fn: () => void | Promise<void>) => {
38 setIsTransitioning(true)
39 await fn()
40 setIsTransitioning(false)
41 }
42​
43 // Subscribe to location changes
44 // and try to load the new location
45 Solid.onMount(() => {
46 const unsub = router.history.subscribe(router.load)
47​
48 const nextLocation = router.buildLocation({
49 to: router.latestLocation.pathname,
50 search: true,
51 params: true,
52 hash: true,
53 state: true,
54 _includeValidateSearch: true,
55 })
56​
57 if (
58 trimPathRight(router.latestLocation.href) !==
59 trimPathRight(nextLocation.href)
60 ) {
61 router.commitLocation({ ...nextLocation, replace: true })
62 }
63​
64 Solid.onCleanup(() => {
65 unsub()
66 })
67 })
68​
69 // Try to load the initial location
70 Solid.createRenderEffect(() => {
71 Solid.untrack(() => {
72 if (
73 // if we are hydrating from SSR, loading is triggered in ssr-client
74 (typeof window !== 'undefined' && router.ssr) ||
75 (mountLoadForRouter.router === router && mountLoadForRouter.mounted)
76 ) {
77 return
78 }
79 mountLoadForRouter = { router, mounted: true }
80 const tryLoad = async () => {
81 try {
82 await router.load()
83 } catch (err) {
84 console.error(err)
85 }
86 }
87 tryLoad()
88 })
89 })
90​
91 Solid.createRenderEffect(
92 Solid.on(
93 [previousIsLoading, isLoading],
94 ([previousIsLoading, isLoading]) => {
95 if (previousIsLoading.previous && !isLoading) {
96 router.emit({
97 type: 'onLoad',
98 ...getLocationChangeInfo(router.state),
99 })
100 }
101 },
102 ),
103 )
104​
105 Solid.createRenderEffect(
106 Solid.on(
107 [isPagePending, previousIsPagePending],
108 ([isPagePending, previousIsPagePending]) => {
109 // emit onBeforeRouteMount
110 if (previousIsPagePending.previous && !isPagePending) {
111 router.emit({
112 type: 'onBeforeRouteMount',
113 ...getLocationChangeInfo(router.state),
114 })
115 }
116 },
117 ),
118 )
119​
120 Solid.createRenderEffect(
121 Solid.on(
122 [isAnyPending, previousIsAnyPending],
123 ([isAnyPending, previousIsAnyPending]) => {
124 // The router was pending and now it's not
125 if (previousIsAnyPending.previous && !isAnyPending) {
126 router.emit({
127 type: 'onResolved',
128 ...getLocationChangeInfo(router.state),
129 })
130​
131 router.__store.setState((s) => ({
132 ...s,
133 status: 'idle',
134 resolvedLocation: s.location,
135 }))
136​
137 handleHashScroll(router)
138 }
139 },
140 ),
141 )
142​
143 return null