Files
med-notes/.pnpm-store/v10/files/c5/1fa676e23a668942b980a3403bc55a3a315a317ef3a68a3145e227d287c0c80278e2fce27535f816e673ce190ceba496ae0e6402ee09b4b930682a8f0604da
2025-05-09 05:30:08 +02:00

80 lines
1.9 KiB
Plaintext

import * as React from 'react'
import { Matches } from './Matches'
import { getRouterContext } from './routerContext'
import type {
AnyRouter,
RegisteredRouter,
RouterOptions,
} from '@tanstack/router-core'
export function RouterContextProvider<
TRouter extends AnyRouter = RegisteredRouter,
TDehydrated extends Record<string, any> = Record<string, any>,
>({
router,
children,
...rest
}: RouterProps<TRouter, TDehydrated> & {
children: React.ReactNode
}) {
// Allow the router to update options on the router instance
router.update({
...router.options,
...rest,
context: {
...router.options.context,
...rest.context,
},
} as any)
const routerContext = getRouterContext()
const provider = (
<routerContext.Provider value={router as AnyRouter}>
{children}
</routerContext.Provider>
)
if (router.options.Wrap) {
return <router.options.Wrap>{provider}</router.options.Wrap>
}
return provider
}
export function RouterProvider<
TRouter extends AnyRouter = RegisteredRouter,
TDehydrated extends Record<string, any> = Record<string, any>,
>({ router, ...rest }: RouterProps<TRouter, TDehydrated>) {
return (
<RouterContextProvider router={router} {...rest}>
<Matches />
</RouterContextProvider>
)
}
export type RouterProps<
TRouter extends AnyRouter = RegisteredRouter,
TDehydrated extends Record<string, any> = Record<string, any>,
> = Omit<
RouterOptions<
TRouter['routeTree'],
NonNullable<TRouter['options']['trailingSlash']>,
NonNullable<TRouter['options']['defaultStructuralSharing']>,
TRouter['history'],
TDehydrated
>,
'context'
> & {
router: TRouter
context?: Partial<
RouterOptions<
TRouter['routeTree'],
NonNullable<TRouter['options']['trailingSlash']>,
NonNullable<TRouter['options']['defaultStructuralSharing']>,
TRouter['history'],
TDehydrated
>['context']
>
}