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