Open History to see the last commit
1import * as Solid from 'solid-js'
2import warning from 'tiny-warning'
3import { rootRouteId } from '@tanstack/router-core'
4import { CatchBoundary, ErrorComponent } from './CatchBoundary'
5import { useRouterState } from './useRouterState'
6import { useRouter } from './useRouter'
7import { Transitioner } from './Transitioner'
8import { matchContext } from './matchContext'
9import { SafeFragment } from './SafeFragment'
10import { Match } from './Match'
11import type {
12 AnyRoute,
13 AnyRouter,
14 DeepPartial,
15 Expand,
16 MakeOptionalPathParams,
17 MakeOptionalSearchParams,
18 MakeRouteMatchUnion,
19 MaskOptions,
20 MatchRouteOptions,
21 NoInfer,
22 RegisteredRouter,
23 ResolveRelativePath,
24 ResolveRoute,
25 RouteByPath,
26 RouterState,
27 ToSubOptionsProps,
28} from '@tanstack/router-core'
29β
30declare module '@tanstack/router-core' {
31 export interface RouteMatchExtensions {
32 meta?: Array<Solid.JSX.IntrinsicElements['meta'] | undefined>
33 links?: Array<Solid.JSX.IntrinsicElements['link'] | undefined>
34 scripts?: Array<Solid.JSX.IntrinsicElements['script'] | undefined>
35 styles?: Array<Solid.JSX.IntrinsicElements['style'] | undefined>
36 headScripts?: Array<Solid.JSX.IntrinsicElements['script'] | undefined>
37 }
38}
39β
40export function Matches() {
41 const router = useRouter()
42β
43 const ResolvedSuspense =
44 router.isServer || (typeof document !== 'undefined' && router.ssr)
45 ? SafeFragment
46 : Solid.Suspense
47β
48 const rootRoute: () => AnyRoute = () => router.routesById[rootRouteId]
49 const PendingComponent =
50 rootRoute().options.pendingComponent ??
51 router.options.defaultPendingComponent
52β
53 const OptionalWrapper = router.options.InnerWrap || SafeFragment
54β
55 return (
56 <OptionalWrapper>
57 <ResolvedSuspense
58 fallback={PendingComponent ? <PendingComponent /> : null}
59 >
60 <Transitioner />
61 <MatchesInner />
62 </ResolvedSuspense>
63 </OptionalWrapper>
64 )
65}
66β
67function MatchesInner() {
68 const router = useRouter()
69 const matchId = useRouterState({
70 select: (s) => {
71 return s.matches[0]?.id
72 },
73 })
74β
75 const resetKey = useRouterState({
76 select: (s) => s.loadedAt,
77 })
78β
79 const matchComponent = () => {
80 return (
81 <Solid.Show when={matchId()}>
82 <Match matchId={matchId()!} />
83 </Solid.Show>
84 )
85 }
86β
87 return (
88 <matchContext.Provider value={matchId}>
89 {router.options.disableGlobalCatchBoundary ? (
90 matchComponent()
91 ) : (
92 <CatchBoundary
93 getResetKey={() => resetKey()}
94 errorComponent={ErrorComponent}
95 onCatch={(error) => {
96 warning(
97 false,
98 `The following error wasn't caught by any route! At the very leas
99 t, consider setting an 'errorComponent' in your RootRoute!`,
100 )
101 warning(false, error.message || error.toString())
102 }}
103 >
104 {matchComponent()}
105 </CatchBoundary>
106 )}
107 </matchContext.Provider>
108 )
109}
110β
111export type UseMatchRouteOptions<
112 TRouter extends AnyRouter = RegisteredRouter,
113 TFrom extends string = string,
114 TTo extends string | undefined = undefined,
115 TMaskFrom extends string = TFrom,
116 TMaskTo extends string = '',
117> = ToSubOptionsProps<TRouter, TFrom, TTo> &
118 DeepPartial<MakeOptionalSearchParams<TRouter, TFrom, TTo>> &
119 DeepPartial<MakeOptionalPathParams<TRouter, TFrom, TTo>> &
120 MaskOptions<TRouter, TMaskFrom, TMaskTo> &
121 MatchRouteOptions
122β
123export function useMatchRoute<TRouter extends AnyRouter = RegisteredRouter>() {
124 const router = useRouter()
125β
126 const status = useRouterState({
127 select: (s) => s.status,
128 })
129β
130 return <
131 const TFrom extends string = string,
132 const TTo extends string | undefined = undefined,
133 const TMaskFrom extends string = TFrom,
134 const TMaskTo extends string = '',
135 >(
136 opts: UseMatchRouteOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>,
137 ): Solid.Accessor<
138 false | Expand<ResolveRoute<TRouter, TFrom, TTo>['types']['allParams']>
139 > => {
140 const { pending, caseSensitive, fuzzy, includeSearch, ...rest } = opts
141β
142 const matchRoute = Solid.createMemo(() => {
143 status()
144 return router.matchRoute(rest as any, {
145 pending,
146 caseSensitive,
147 fuzzy,
148 includeSearch,
149 })
150 })
151β
152 return matchRoute
153 }
154}
155β
156export type MakeMatchRouteOptions<
157 TRouter extends AnyRouter = RegisteredRouter,
158 TFrom extends string = string,
159 TTo extends string | undefined = undefined,
160 TMaskFrom extends string = TFrom,
161 TMaskTo extends string = '',
162> = UseMatchRouteOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & {
163 // If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
164 children?:
165 | ((
166 params?: RouteByPath<
167 TRouter['routeTree'],
168 ResolveRelativePath<TFrom, NoInfer<TTo>>
169 >['types']['allParams'],
170 ) => Solid.JSX.Element)
171 | Solid.JSX.Element
172}
173β
174export function MatchRoute<
175 TRouter extends AnyRouter = RegisteredRouter,
176 const TFrom extends string = string,
177 const TTo extends string | undefined = undefined,
178 const TMaskFrom extends string = TFrom,
179 const TMaskTo extends string = '',
180>(props: MakeMatchRouteOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>): any {
181 const status = useRouterState({
182 select: (s) => s.status,
183 })
184β
185 return (
186 <Solid.Show when={status()} keyed>
187 {(_) => {
188 const matchRoute = useMatchRoute()
189 const params = matchRoute(props as any)() as boolean
190 const child = props.children
191 if (typeof child === 'function') {
192 return (child as any)(params)
193 }
194β
195 return params ? child : null
196 }}
197 </Solid.Show>
198 )
199}
200β
201export interface UseMatchesBaseOptions<TRouter extends AnyRouter, TSelected> {
202 select?: (matches: Array<MakeRouteMatchUnion<TRouter>>) => TSelected
203}
204β
205export type UseMatchesResult<
206 TRouter extends AnyRouter,
207 TSelected,
208> = unknown extends TSelected ? Array<MakeRouteMatchUnion<TRouter>> : TSelected
209β
210export function useMatches<
211 TRouter extends AnyRouter = RegisteredRouter,
212 TSelected = unknown,
213>(
214 opts?: UseMatchesBaseOptions<TRouter, TSelected>,
215): Solid.Accessor<UseMatchesResult<TRouter, TSelected>> {
216 return useRouterState({
217 select: (state: RouterState<TRouter['routeTree']>) => {
218 const matches = state.matches
219 return opts?.select
220 ? opts.select(matches as Array<MakeRouteMatchUnion<TRouter>>)
221 : matches
222 },
223 } as any) as Solid.Accessor<UseMatchesResult<TRouter, TSelected>>
224}
225β
226export function useParentMatches<
227 TRouter extends AnyRouter = RegisteredRouter,
228 TSelected = unknown,
229>(
230 opts?: UseMatchesBaseOptions<TRouter, TSelected>,
231): Solid.Accessor<UseMatchesResult<TRouter, TSelected>> {
232 const contextMatchId = Solid.useContext(matchContext)
233β
234 return useMatches({
235 select: (matches: Array<MakeRouteMatchUnion<TRouter>>) => {
236 matches = matches.slice(
237 0,
238 matches.findIndex((d) => d.id === contextMatchId()),
239 )
240 return opts?.select ? opts.select(matches) : matches
241 },
242 } as any)
243}
244β
245export function useChildMatches<
246 TRouter extends AnyRouter = RegisteredRouter,
247 TSelected = unknown,
248>(
249 opts?: UseMatchesBaseOptions<TRouter, TSelected>,
250): Solid.Accessor<UseMatchesResult<TRouter, TSelected>> {
251 const contextMatchId = Solid.useContext(matchContext)
252β
253 return useMatches({
254 select: (matches: Array<MakeRouteMatchUnion<TRouter>>) => {
255 matches = matches.slice(
256 matches.findIndex((d) => d.id === contextMatchId()) + 1,
257 )
258 return opts?.select ? opts.select(matches) : matches
259 },
260 } as any)
261}