@tanstack/solid-router 1.20.3-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +29 -0
- package/dist/cjs/Asset.cjs +49 -0
- package/dist/cjs/Asset.cjs.map +1 -0
- package/dist/cjs/Asset.d.cts +2 -0
- package/dist/cjs/CatchBoundary.cjs +92 -0
- package/dist/cjs/CatchBoundary.cjs.map +1 -0
- package/dist/cjs/CatchBoundary.d.cts +11 -0
- package/dist/cjs/ClientOnly.cjs +36 -0
- package/dist/cjs/ClientOnly.cjs.map +1 -0
- package/dist/cjs/ClientOnly.d.cts +49 -0
- package/dist/cjs/HeadContent.cjs +146 -0
- package/dist/cjs/HeadContent.cjs.map +1 -0
- package/dist/cjs/HeadContent.d.cts +8 -0
- package/dist/cjs/Match.cjs +339 -0
- package/dist/cjs/Match.cjs.map +1 -0
- package/dist/cjs/Match.d.cts +8 -0
- package/dist/cjs/Matches.cjs +151 -0
- package/dist/cjs/Matches.cjs.map +1 -0
- package/dist/cjs/Matches.d.cts +24 -0
- package/dist/cjs/RouterProvider.cjs +45 -0
- package/dist/cjs/RouterProvider.cjs.map +1 -0
- package/dist/cjs/RouterProvider.d.cts +10 -0
- package/dist/cjs/SafeFragment.cjs +8 -0
- package/dist/cjs/SafeFragment.cjs.map +1 -0
- package/dist/cjs/SafeFragment.d.cts +1 -0
- package/dist/cjs/ScriptOnce.cjs +23 -0
- package/dist/cjs/ScriptOnce.cjs.map +1 -0
- package/dist/cjs/ScriptOnce.d.cts +5 -0
- package/dist/cjs/Scripts.cjs +48 -0
- package/dist/cjs/Scripts.cjs.map +1 -0
- package/dist/cjs/Scripts.d.cts +1 -0
- package/dist/cjs/ScrollRestoration.cjs +37 -0
- package/dist/cjs/ScrollRestoration.cjs.map +1 -0
- package/dist/cjs/ScrollRestoration.d.cts +14 -0
- package/dist/cjs/Transitioner.cjs +124 -0
- package/dist/cjs/Transitioner.cjs.map +1 -0
- package/dist/cjs/Transitioner.d.cts +1 -0
- package/dist/cjs/awaited.cjs +49 -0
- package/dist/cjs/awaited.cjs.map +1 -0
- package/dist/cjs/awaited.d.cts +11 -0
- package/dist/cjs/fileRoute.cjs +103 -0
- package/dist/cjs/fileRoute.cjs.map +1 -0
- package/dist/cjs/fileRoute.d.cts +54 -0
- package/dist/cjs/history.d.cts +8 -0
- package/dist/cjs/index.cjs +289 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +52 -0
- package/dist/cjs/lazyRouteComponent.cjs +73 -0
- package/dist/cjs/lazyRouteComponent.cjs.map +1 -0
- package/dist/cjs/lazyRouteComponent.d.cts +2 -0
- package/dist/cjs/link.cjs +278 -0
- package/dist/cjs/link.cjs.map +1 -0
- package/dist/cjs/link.d.cts +44 -0
- package/dist/cjs/matchContext.cjs +25 -0
- package/dist/cjs/matchContext.cjs.map +1 -0
- package/dist/cjs/matchContext.d.cts +3 -0
- package/dist/cjs/not-found.cjs +42 -0
- package/dist/cjs/not-found.cjs.map +1 -0
- package/dist/cjs/not-found.d.cts +8 -0
- package/dist/cjs/renderRouteNotFound.cjs +23 -0
- package/dist/cjs/renderRouteNotFound.cjs.map +1 -0
- package/dist/cjs/renderRouteNotFound.d.cts +2 -0
- package/dist/cjs/route.cjs +236 -0
- package/dist/cjs/route.cjs.map +1 -0
- package/dist/cjs/route.d.cts +95 -0
- package/dist/cjs/router.cjs +22 -0
- package/dist/cjs/router.cjs.map +1 -0
- package/dist/cjs/router.d.cts +69 -0
- package/dist/cjs/routerContext.cjs +33 -0
- package/dist/cjs/routerContext.cjs.map +1 -0
- package/dist/cjs/routerContext.d.cts +8 -0
- package/dist/cjs/scroll-restoration.cjs +23 -0
- package/dist/cjs/scroll-restoration.cjs.map +1 -0
- package/dist/cjs/scroll-restoration.d.cts +1 -0
- package/dist/cjs/typePrimitives.d.cts +10 -0
- package/dist/cjs/useBlocker.cjs +165 -0
- package/dist/cjs/useBlocker.cjs.map +1 -0
- package/dist/cjs/useBlocker.d.cts +67 -0
- package/dist/cjs/useCanGoBack.cjs +8 -0
- package/dist/cjs/useCanGoBack.cjs.map +1 -0
- package/dist/cjs/useCanGoBack.d.cts +1 -0
- package/dist/cjs/useLoaderData.cjs +14 -0
- package/dist/cjs/useLoaderData.cjs.map +1 -0
- package/dist/cjs/useLoaderData.d.cts +8 -0
- package/dist/cjs/useLoaderDeps.cjs +17 -0
- package/dist/cjs/useLoaderDeps.cjs.map +1 -0
- package/dist/cjs/useLoaderDeps.d.cts +7 -0
- package/dist/cjs/useLocation.cjs +10 -0
- package/dist/cjs/useLocation.cjs.map +1 -0
- package/dist/cjs/useLocation.d.cts +7 -0
- package/dist/cjs/useMatch.cjs +39 -0
- package/dist/cjs/useMatch.cjs.map +1 -0
- package/dist/cjs/useMatch.d.cts +10 -0
- package/dist/cjs/useNavigate.cjs +46 -0
- package/dist/cjs/useNavigate.cjs.map +1 -0
- package/dist/cjs/useNavigate.d.cts +5 -0
- package/dist/cjs/useParams.cjs +15 -0
- package/dist/cjs/useParams.cjs.map +1 -0
- package/dist/cjs/useParams.d.cts +9 -0
- package/dist/cjs/useRouteContext.cjs +11 -0
- package/dist/cjs/useRouteContext.cjs.map +1 -0
- package/dist/cjs/useRouteContext.d.cts +4 -0
- package/dist/cjs/useRouter.cjs +29 -0
- package/dist/cjs/useRouter.cjs.map +1 -0
- package/dist/cjs/useRouter.d.cts +4 -0
- package/dist/cjs/useRouterState.cjs +16 -0
- package/dist/cjs/useRouterState.cjs.map +1 -0
- package/dist/cjs/useRouterState.d.cts +8 -0
- package/dist/cjs/useSearch.cjs +15 -0
- package/dist/cjs/useSearch.cjs.map +1 -0
- package/dist/cjs/useSearch.d.cts +9 -0
- package/dist/cjs/utils.cjs +58 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/dist/cjs/utils.d.cts +34 -0
- package/dist/esm/Asset.d.ts +2 -0
- package/dist/esm/Asset.js +49 -0
- package/dist/esm/Asset.js.map +1 -0
- package/dist/esm/CatchBoundary.d.ts +11 -0
- package/dist/esm/CatchBoundary.js +75 -0
- package/dist/esm/CatchBoundary.js.map +1 -0
- package/dist/esm/ClientOnly.d.ts +49 -0
- package/dist/esm/ClientOnly.js +19 -0
- package/dist/esm/ClientOnly.js.map +1 -0
- package/dist/esm/HeadContent.d.ts +8 -0
- package/dist/esm/HeadContent.js +129 -0
- package/dist/esm/HeadContent.js.map +1 -0
- package/dist/esm/Match.d.ts +8 -0
- package/dist/esm/Match.js +322 -0
- package/dist/esm/Match.js.map +1 -0
- package/dist/esm/Matches.d.ts +24 -0
- package/dist/esm/Matches.js +134 -0
- package/dist/esm/Matches.js.map +1 -0
- package/dist/esm/RouterProvider.d.ts +10 -0
- package/dist/esm/RouterProvider.js +45 -0
- package/dist/esm/RouterProvider.js.map +1 -0
- package/dist/esm/SafeFragment.d.ts +1 -0
- package/dist/esm/SafeFragment.js +8 -0
- package/dist/esm/SafeFragment.js.map +1 -0
- package/dist/esm/ScriptOnce.d.ts +5 -0
- package/dist/esm/ScriptOnce.js +23 -0
- package/dist/esm/ScriptOnce.js.map +1 -0
- package/dist/esm/Scripts.d.ts +1 -0
- package/dist/esm/Scripts.js +48 -0
- package/dist/esm/Scripts.js.map +1 -0
- package/dist/esm/ScrollRestoration.d.ts +14 -0
- package/dist/esm/ScrollRestoration.js +37 -0
- package/dist/esm/ScrollRestoration.js.map +1 -0
- package/dist/esm/Transitioner.d.ts +1 -0
- package/dist/esm/Transitioner.js +107 -0
- package/dist/esm/Transitioner.js.map +1 -0
- package/dist/esm/awaited.d.ts +11 -0
- package/dist/esm/awaited.js +32 -0
- package/dist/esm/awaited.js.map +1 -0
- package/dist/esm/fileRoute.d.ts +54 -0
- package/dist/esm/fileRoute.js +103 -0
- package/dist/esm/fileRoute.js.map +1 -0
- package/dist/esm/history.d.ts +8 -0
- package/dist/esm/index.d.ts +52 -0
- package/dist/esm/index.js +151 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lazyRouteComponent.d.ts +2 -0
- package/dist/esm/lazyRouteComponent.js +73 -0
- package/dist/esm/lazyRouteComponent.js.map +1 -0
- package/dist/esm/link.d.ts +44 -0
- package/dist/esm/link.js +261 -0
- package/dist/esm/link.js.map +1 -0
- package/dist/esm/matchContext.d.ts +3 -0
- package/dist/esm/matchContext.js +8 -0
- package/dist/esm/matchContext.js.map +1 -0
- package/dist/esm/not-found.d.ts +8 -0
- package/dist/esm/not-found.js +42 -0
- package/dist/esm/not-found.js.map +1 -0
- package/dist/esm/renderRouteNotFound.d.ts +2 -0
- package/dist/esm/renderRouteNotFound.js +23 -0
- package/dist/esm/renderRouteNotFound.js.map +1 -0
- package/dist/esm/route.d.ts +95 -0
- package/dist/esm/route.js +236 -0
- package/dist/esm/route.js.map +1 -0
- package/dist/esm/router.d.ts +69 -0
- package/dist/esm/router.js +22 -0
- package/dist/esm/router.js.map +1 -0
- package/dist/esm/routerContext.d.ts +8 -0
- package/dist/esm/routerContext.js +16 -0
- package/dist/esm/routerContext.js.map +1 -0
- package/dist/esm/scroll-restoration.d.ts +1 -0
- package/dist/esm/scroll-restoration.js +23 -0
- package/dist/esm/scroll-restoration.js.map +1 -0
- package/dist/esm/typePrimitives.d.ts +10 -0
- package/dist/esm/useBlocker.d.ts +67 -0
- package/dist/esm/useBlocker.js +148 -0
- package/dist/esm/useBlocker.js.map +1 -0
- package/dist/esm/useCanGoBack.d.ts +1 -0
- package/dist/esm/useCanGoBack.js +8 -0
- package/dist/esm/useCanGoBack.js.map +1 -0
- package/dist/esm/useLoaderData.d.ts +8 -0
- package/dist/esm/useLoaderData.js +14 -0
- package/dist/esm/useLoaderData.js.map +1 -0
- package/dist/esm/useLoaderDeps.d.ts +7 -0
- package/dist/esm/useLoaderDeps.js +17 -0
- package/dist/esm/useLoaderDeps.js.map +1 -0
- package/dist/esm/useLocation.d.ts +7 -0
- package/dist/esm/useLocation.js +10 -0
- package/dist/esm/useLocation.js.map +1 -0
- package/dist/esm/useMatch.d.ts +10 -0
- package/dist/esm/useMatch.js +22 -0
- package/dist/esm/useMatch.js.map +1 -0
- package/dist/esm/useNavigate.d.ts +5 -0
- package/dist/esm/useNavigate.js +29 -0
- package/dist/esm/useNavigate.js.map +1 -0
- package/dist/esm/useParams.d.ts +9 -0
- package/dist/esm/useParams.js +15 -0
- package/dist/esm/useParams.js.map +1 -0
- package/dist/esm/useRouteContext.d.ts +4 -0
- package/dist/esm/useRouteContext.js +11 -0
- package/dist/esm/useRouteContext.js.map +1 -0
- package/dist/esm/useRouter.d.ts +4 -0
- package/dist/esm/useRouter.js +12 -0
- package/dist/esm/useRouter.js.map +1 -0
- package/dist/esm/useRouterState.d.ts +8 -0
- package/dist/esm/useRouterState.js +16 -0
- package/dist/esm/useRouterState.js.map +1 -0
- package/dist/esm/useSearch.d.ts +9 -0
- package/dist/esm/useSearch.js +15 -0
- package/dist/esm/useSearch.js.map +1 -0
- package/dist/esm/utils.d.ts +34 -0
- package/dist/esm/utils.js +41 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/source/Asset.d.ts +2 -0
- package/dist/source/Asset.jsx +23 -0
- package/dist/source/Asset.jsx.map +1 -0
- package/dist/source/CatchBoundary.d.ts +11 -0
- package/dist/source/CatchBoundary.jsx +43 -0
- package/dist/source/CatchBoundary.jsx.map +1 -0
- package/dist/source/ClientOnly.d.ts +49 -0
- package/dist/source/ClientOnly.jsx +51 -0
- package/dist/source/ClientOnly.jsx.map +1 -0
- package/dist/source/HeadContent.d.ts +8 -0
- package/dist/source/HeadContent.jsx +143 -0
- package/dist/source/HeadContent.jsx.map +1 -0
- package/dist/source/Match.d.ts +8 -0
- package/dist/source/Match.jsx +255 -0
- package/dist/source/Match.jsx.map +1 -0
- package/dist/source/Matches.d.ts +24 -0
- package/dist/source/Matches.jsx +103 -0
- package/dist/source/Matches.jsx.map +1 -0
- package/dist/source/RouterProvider.d.ts +10 -0
- package/dist/source/RouterProvider.jsx +27 -0
- package/dist/source/RouterProvider.jsx.map +1 -0
- package/dist/source/SafeFragment.d.ts +1 -0
- package/dist/source/SafeFragment.jsx +4 -0
- package/dist/source/SafeFragment.jsx.map +1 -0
- package/dist/source/ScriptOnce.d.ts +5 -0
- package/dist/source/ScriptOnce.jsx +17 -0
- package/dist/source/ScriptOnce.jsx.map +1 -0
- package/dist/source/Scripts.d.ts +1 -0
- package/dist/source/Scripts.jsx +49 -0
- package/dist/source/Scripts.jsx.map +1 -0
- package/dist/source/ScrollRestoration.d.ts +14 -0
- package/dist/source/ScrollRestoration.jsx +37 -0
- package/dist/source/ScrollRestoration.jsx.map +1 -0
- package/dist/source/Transitioner.d.ts +1 -0
- package/dist/source/Transitioner.jsx +102 -0
- package/dist/source/Transitioner.jsx.map +1 -0
- package/dist/source/awaited.d.ts +11 -0
- package/dist/source/awaited.jsx +19 -0
- package/dist/source/awaited.jsx.map +1 -0
- package/dist/source/fileRoute.d.ts +54 -0
- package/dist/source/fileRoute.js +98 -0
- package/dist/source/fileRoute.js.map +1 -0
- package/dist/source/history.d.ts +8 -0
- package/dist/source/history.js +2 -0
- package/dist/source/history.js.map +1 -0
- package/dist/source/index.d.ts +52 -0
- package/dist/source/index.jsx +43 -0
- package/dist/source/index.jsx.map +1 -0
- package/dist/source/lazyRouteComponent.d.ts +2 -0
- package/dist/source/lazyRouteComponent.jsx +83 -0
- package/dist/source/lazyRouteComponent.jsx.map +1 -0
- package/dist/source/link.d.ts +44 -0
- package/dist/source/link.jsx +335 -0
- package/dist/source/link.jsx.map +1 -0
- package/dist/source/matchContext.d.ts +3 -0
- package/dist/source/matchContext.jsx +5 -0
- package/dist/source/matchContext.jsx.map +1 -0
- package/dist/source/not-found.d.ts +8 -0
- package/dist/source/not-found.jsx +30 -0
- package/dist/source/not-found.jsx.map +1 -0
- package/dist/source/renderRouteNotFound.d.ts +2 -0
- package/dist/source/renderRouteNotFound.jsx +15 -0
- package/dist/source/renderRouteNotFound.jsx.map +1 -0
- package/dist/source/route.d.ts +96 -0
- package/dist/source/route.jsx +179 -0
- package/dist/source/route.jsx.map +1 -0
- package/dist/source/router.d.ts +70 -0
- package/dist/source/router.js +21 -0
- package/dist/source/router.js.map +1 -0
- package/dist/source/routerContext.d.ts +8 -0
- package/dist/source/routerContext.jsx +13 -0
- package/dist/source/routerContext.jsx.map +1 -0
- package/dist/source/scroll-restoration.d.ts +1 -0
- package/dist/source/scroll-restoration.jsx +16 -0
- package/dist/source/scroll-restoration.jsx.map +1 -0
- package/dist/source/typePrimitives.d.ts +10 -0
- package/dist/source/typePrimitives.js +2 -0
- package/dist/source/typePrimitives.js.map +1 -0
- package/dist/source/useBlocker.d.ts +67 -0
- package/dist/source/useBlocker.jsx +136 -0
- package/dist/source/useBlocker.jsx.map +1 -0
- package/dist/source/useCanGoBack.d.ts +1 -0
- package/dist/source/useCanGoBack.js +5 -0
- package/dist/source/useCanGoBack.js.map +1 -0
- package/dist/source/useLoaderData.d.ts +8 -0
- package/dist/source/useLoaderData.jsx +11 -0
- package/dist/source/useLoaderData.jsx.map +1 -0
- package/dist/source/useLoaderDeps.d.ts +7 -0
- package/dist/source/useLoaderDeps.jsx +11 -0
- package/dist/source/useLoaderDeps.jsx.map +1 -0
- package/dist/source/useLocation.d.ts +7 -0
- package/dist/source/useLocation.jsx +7 -0
- package/dist/source/useLocation.jsx.map +1 -0
- package/dist/source/useMatch.d.ts +10 -0
- package/dist/source/useMatch.jsx +19 -0
- package/dist/source/useMatch.jsx.map +1 -0
- package/dist/source/useNavigate.d.ts +5 -0
- package/dist/source/useNavigate.jsx +18 -0
- package/dist/source/useNavigate.jsx.map +1 -0
- package/dist/source/useParams.d.ts +9 -0
- package/dist/source/useParams.jsx +12 -0
- package/dist/source/useParams.jsx.map +1 -0
- package/dist/source/useRouteContext.d.ts +4 -0
- package/dist/source/useRouteContext.js +8 -0
- package/dist/source/useRouteContext.js.map +1 -0
- package/dist/source/useRouter.d.ts +4 -0
- package/dist/source/useRouter.jsx +9 -0
- package/dist/source/useRouter.jsx.map +1 -0
- package/dist/source/useRouterState.d.ts +8 -0
- package/dist/source/useRouterState.jsx +14 -0
- package/dist/source/useRouterState.jsx.map +1 -0
- package/dist/source/useSearch.d.ts +9 -0
- package/dist/source/useSearch.jsx +12 -0
- package/dist/source/useSearch.jsx.map +1 -0
- package/dist/source/utils.d.ts +34 -0
- package/dist/source/utils.js +59 -0
- package/dist/source/utils.js.map +1 -0
- package/package.json +80 -0
- package/src/Asset.tsx +24 -0
- package/src/CatchBoundary.tsx +78 -0
- package/src/ClientOnly.tsx +65 -0
- package/src/HeadContent.tsx +174 -0
- package/src/Match.tsx +357 -0
- package/src/Matches.tsx +243 -0
- package/src/RouterProvider.tsx +79 -0
- package/src/SafeFragment.tsx +3 -0
- package/src/ScriptOnce.tsx +30 -0
- package/src/Scripts.tsx +65 -0
- package/src/ScrollRestoration.tsx +69 -0
- package/src/Transitioner.tsx +140 -0
- package/src/awaited.tsx +40 -0
- package/src/fileRoute.ts +256 -0
- package/src/history.ts +9 -0
- package/src/index.tsx +374 -0
- package/src/lazyRouteComponent.tsx +110 -0
- package/src/link.tsx +597 -0
- package/src/matchContext.tsx +10 -0
- package/src/not-found.tsx +42 -0
- package/src/renderRouteNotFound.tsx +26 -0
- package/src/route.tsx +550 -0
- package/src/router.ts +112 -0
- package/src/routerContext.tsx +26 -0
- package/src/scroll-restoration.tsx +29 -0
- package/src/typePrimitives.ts +74 -0
- package/src/useBlocker.tsx +305 -0
- package/src/useCanGoBack.ts +5 -0
- package/src/useLoaderData.tsx +50 -0
- package/src/useLoaderDeps.tsx +46 -0
- package/src/useLocation.tsx +30 -0
- package/src/useMatch.tsx +96 -0
- package/src/useNavigate.tsx +40 -0
- package/src/useParams.tsx +71 -0
- package/src/useRouteContext.ts +31 -0
- package/src/useRouter.tsx +15 -0
- package/src/useRouterState.tsx +36 -0
- package/src/useSearch.tsx +71 -0
- package/src/utils.ts +79 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Dynamic } from 'solid-js/web'
|
|
2
|
+
import { createResource } from 'solid-js'
|
|
3
|
+
import { Outlet } from './Match'
|
|
4
|
+
import { ClientOnly } from './ClientOnly'
|
|
5
|
+
import type { AsyncRouteComponent } from './route'
|
|
6
|
+
|
|
7
|
+
// If the load fails due to module not found, it may mean a new version of
|
|
8
|
+
// the build was deployed and the user's browser is still using an old version.
|
|
9
|
+
// If this happens, the old version in the user's browser would have an outdated
|
|
10
|
+
// URL to the lazy module.
|
|
11
|
+
// In that case, we want to attempt one window refresh to get the latest.
|
|
12
|
+
function isModuleNotFoundError(error: any): boolean {
|
|
13
|
+
return (
|
|
14
|
+
typeof error?.message === 'string' &&
|
|
15
|
+
/Failed to fetch dynamically imported module/.test(error.message)
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function lazyRouteComponent<
|
|
20
|
+
T extends Record<string, any>,
|
|
21
|
+
TKey extends keyof T = 'default',
|
|
22
|
+
>(
|
|
23
|
+
importer: () => Promise<T>,
|
|
24
|
+
exportName?: TKey,
|
|
25
|
+
ssr?: () => boolean,
|
|
26
|
+
): T[TKey] extends (props: infer TProps) => any
|
|
27
|
+
? AsyncRouteComponent<TProps>
|
|
28
|
+
: never {
|
|
29
|
+
let loadPromise: Promise<any> | undefined
|
|
30
|
+
let comp: T[TKey] | T['default']
|
|
31
|
+
let error: any
|
|
32
|
+
|
|
33
|
+
const load = () => {
|
|
34
|
+
if (typeof document === 'undefined' && ssr?.() === false) {
|
|
35
|
+
comp = (() => null) as any
|
|
36
|
+
return Promise.resolve(comp)
|
|
37
|
+
}
|
|
38
|
+
if (!loadPromise) {
|
|
39
|
+
loadPromise = importer()
|
|
40
|
+
.then((res) => {
|
|
41
|
+
loadPromise = undefined
|
|
42
|
+
comp = res[exportName ?? 'default']
|
|
43
|
+
return comp
|
|
44
|
+
})
|
|
45
|
+
.catch((err) => {
|
|
46
|
+
error = err
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return loadPromise
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const lazyComp = function Lazy(props: any) {
|
|
54
|
+
// Now that we're out of preload and into actual render path,
|
|
55
|
+
// throw the error if it was a module not found error during preload
|
|
56
|
+
if (error) {
|
|
57
|
+
if (isModuleNotFoundError(error)) {
|
|
58
|
+
// We don't want an error thrown from preload in this case, because
|
|
59
|
+
// there's nothing we want to do about module not found during preload.
|
|
60
|
+
// Record the error, recover the promise with a null return,
|
|
61
|
+
// and we will attempt module not found resolution during the render path.
|
|
62
|
+
|
|
63
|
+
if (
|
|
64
|
+
error instanceof Error &&
|
|
65
|
+
typeof window !== 'undefined' &&
|
|
66
|
+
typeof sessionStorage !== 'undefined'
|
|
67
|
+
) {
|
|
68
|
+
// Again, we want to reload one time on module not found error and not enter
|
|
69
|
+
// a reload loop if there is some other issue besides an old deploy.
|
|
70
|
+
// That's why we store our reload attempt in sessionStorage.
|
|
71
|
+
// Use error.message as key because it contains the module path that failed.
|
|
72
|
+
const storageKey = `tanstack_router_reload:${error.message}`
|
|
73
|
+
if (!sessionStorage.getItem(storageKey)) {
|
|
74
|
+
sessionStorage.setItem(storageKey, '1')
|
|
75
|
+
window.location.reload()
|
|
76
|
+
|
|
77
|
+
// Return empty component while we wait for window to reload
|
|
78
|
+
return {
|
|
79
|
+
default: () => null,
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Otherwise, just throw the error
|
|
86
|
+
throw error
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!comp) {
|
|
90
|
+
const [compResource] = createResource(load, {
|
|
91
|
+
initialValue: comp,
|
|
92
|
+
ssrLoadFrom: 'initial',
|
|
93
|
+
})
|
|
94
|
+
return <>{compResource()}</>
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (ssr?.() === false) {
|
|
98
|
+
return (
|
|
99
|
+
<ClientOnly fallback={<Outlet />}>
|
|
100
|
+
<Dynamic component={comp} {...props} />
|
|
101
|
+
</ClientOnly>
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
return <Dynamic component={comp} {...props} />
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
;(lazyComp as any).preload = load
|
|
108
|
+
|
|
109
|
+
return lazyComp as any
|
|
110
|
+
}
|
package/src/link.tsx
ADDED
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
import * as Solid from 'solid-js'
|
|
2
|
+
|
|
3
|
+
import { mergeRefs } from '@solid-primitives/refs'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
deepEqual,
|
|
7
|
+
exactPathTest,
|
|
8
|
+
functionalUpdate,
|
|
9
|
+
preloadWarning,
|
|
10
|
+
removeTrailingSlash,
|
|
11
|
+
} from '@tanstack/router-core'
|
|
12
|
+
import { Dynamic } from 'solid-js/web'
|
|
13
|
+
import { useRouterState } from './useRouterState'
|
|
14
|
+
import { useRouter } from './useRouter'
|
|
15
|
+
|
|
16
|
+
import { useIntersectionObserver } from './utils'
|
|
17
|
+
|
|
18
|
+
import { useMatches } from './Matches'
|
|
19
|
+
import type {
|
|
20
|
+
AnyRouter,
|
|
21
|
+
Constrain,
|
|
22
|
+
LinkCurrentTargetElement,
|
|
23
|
+
LinkOptions,
|
|
24
|
+
RegisteredRouter,
|
|
25
|
+
RoutePaths,
|
|
26
|
+
} from '@tanstack/router-core'
|
|
27
|
+
import type {
|
|
28
|
+
ValidateLinkOptions,
|
|
29
|
+
ValidateLinkOptionsArray,
|
|
30
|
+
} from './typePrimitives'
|
|
31
|
+
|
|
32
|
+
export function useLinkProps<
|
|
33
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
34
|
+
TFrom extends RoutePaths<TRouter['routeTree']> | string = string,
|
|
35
|
+
TTo extends string = '',
|
|
36
|
+
TMaskFrom extends RoutePaths<TRouter['routeTree']> | string = TFrom,
|
|
37
|
+
TMaskTo extends string = '',
|
|
38
|
+
>(
|
|
39
|
+
options: UseLinkPropsOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>,
|
|
40
|
+
): Solid.ComponentProps<'a'> {
|
|
41
|
+
const router = useRouter()
|
|
42
|
+
const [isTransitioning, setIsTransitioning] = Solid.createSignal(false)
|
|
43
|
+
let hasRenderFetched = false
|
|
44
|
+
|
|
45
|
+
const [local, rest] = Solid.splitProps(
|
|
46
|
+
Solid.mergeProps(
|
|
47
|
+
{
|
|
48
|
+
activeProps: () => ({ class: 'active' }),
|
|
49
|
+
inactiveProps: () => ({}),
|
|
50
|
+
},
|
|
51
|
+
options,
|
|
52
|
+
),
|
|
53
|
+
[
|
|
54
|
+
'activeProps',
|
|
55
|
+
'inactiveProps',
|
|
56
|
+
'activeOptions',
|
|
57
|
+
'to',
|
|
58
|
+
'preload',
|
|
59
|
+
'preloadDelay',
|
|
60
|
+
'hashScrollIntoView',
|
|
61
|
+
'replace',
|
|
62
|
+
'startTransition',
|
|
63
|
+
'resetScroll',
|
|
64
|
+
'viewTransition',
|
|
65
|
+
'children',
|
|
66
|
+
'target',
|
|
67
|
+
'disabled',
|
|
68
|
+
'style',
|
|
69
|
+
'class',
|
|
70
|
+
'onClick',
|
|
71
|
+
'onFocus',
|
|
72
|
+
'onMouseEnter',
|
|
73
|
+
'onMouseLeave',
|
|
74
|
+
'onMouseOver',
|
|
75
|
+
'onMouseOut',
|
|
76
|
+
'onTouchStart',
|
|
77
|
+
'ignoreBlocker',
|
|
78
|
+
],
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
// const {
|
|
82
|
+
// // custom props
|
|
83
|
+
// activeProps = () => ({ class: 'active' }),
|
|
84
|
+
// inactiveProps = () => ({}),
|
|
85
|
+
// activeOptions,
|
|
86
|
+
// to,
|
|
87
|
+
// preload: userPreload,
|
|
88
|
+
// preloadDelay: userPreloadDelay,
|
|
89
|
+
// hashScrollIntoView,
|
|
90
|
+
// replace,
|
|
91
|
+
// startTransition,
|
|
92
|
+
// resetScroll,
|
|
93
|
+
// viewTransition,
|
|
94
|
+
// // element props
|
|
95
|
+
// children,
|
|
96
|
+
// target,
|
|
97
|
+
// disabled,
|
|
98
|
+
// style,
|
|
99
|
+
// class,
|
|
100
|
+
// onClick,
|
|
101
|
+
// onFocus,
|
|
102
|
+
// onMouseEnter,
|
|
103
|
+
// onMouseLeave,
|
|
104
|
+
// onTouchStart,
|
|
105
|
+
// ignoreBlocker,
|
|
106
|
+
// ...rest
|
|
107
|
+
// } = options
|
|
108
|
+
|
|
109
|
+
const [_, propsSafeToSpread] = Solid.splitProps(rest, [
|
|
110
|
+
'params',
|
|
111
|
+
'search',
|
|
112
|
+
'hash',
|
|
113
|
+
'state',
|
|
114
|
+
'mask',
|
|
115
|
+
'reloadDocument',
|
|
116
|
+
])
|
|
117
|
+
|
|
118
|
+
// If this link simply reloads the current route,
|
|
119
|
+
// make sure it has a new key so it will trigger a data refresh
|
|
120
|
+
|
|
121
|
+
// If this `to` is a valid external URL, return
|
|
122
|
+
// null for LinkUtils
|
|
123
|
+
|
|
124
|
+
const type: Solid.Accessor<'internal' | 'external'> = () => {
|
|
125
|
+
try {
|
|
126
|
+
new URL(`${local.to}`)
|
|
127
|
+
return 'external'
|
|
128
|
+
} catch {}
|
|
129
|
+
return 'internal'
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const currentSearch = useRouterState({
|
|
133
|
+
select: (s) => s.location.searchStr,
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
// when `from` is not supplied, use the leaf route of the current matches as the `from` location
|
|
137
|
+
// so relative routing works as expected
|
|
138
|
+
const from = useMatches({
|
|
139
|
+
select: (matches) => options.from ?? matches[matches.length - 1]?.fullPath,
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
const _options = () => ({
|
|
143
|
+
...options,
|
|
144
|
+
from: from(),
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
const next = Solid.createMemo(() => {
|
|
148
|
+
currentSearch()
|
|
149
|
+
return router.buildLocation(_options() as any)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
const preload = Solid.createMemo(() => {
|
|
153
|
+
if (_options().reloadDocument) {
|
|
154
|
+
return false
|
|
155
|
+
}
|
|
156
|
+
return local.preload ?? router.options.defaultPreload
|
|
157
|
+
})
|
|
158
|
+
const preloadDelay = () =>
|
|
159
|
+
local.preloadDelay ?? router.options.defaultPreloadDelay ?? 0
|
|
160
|
+
|
|
161
|
+
const isActive = useRouterState({
|
|
162
|
+
select: (s) => {
|
|
163
|
+
if (local.activeOptions?.exact) {
|
|
164
|
+
const testExact = exactPathTest(
|
|
165
|
+
s.location.pathname,
|
|
166
|
+
next().pathname,
|
|
167
|
+
router.basepath,
|
|
168
|
+
)
|
|
169
|
+
if (!testExact) {
|
|
170
|
+
return false
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
const currentPathSplit = removeTrailingSlash(
|
|
174
|
+
s.location.pathname,
|
|
175
|
+
router.basepath,
|
|
176
|
+
).split('/')
|
|
177
|
+
const nextPathSplit = removeTrailingSlash(
|
|
178
|
+
next()?.pathname,
|
|
179
|
+
router.basepath,
|
|
180
|
+
)?.split('/')
|
|
181
|
+
|
|
182
|
+
const pathIsFuzzyEqual = nextPathSplit?.every(
|
|
183
|
+
(d, i) => d === currentPathSplit[i],
|
|
184
|
+
)
|
|
185
|
+
if (!pathIsFuzzyEqual) {
|
|
186
|
+
return false
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (local.activeOptions?.includeSearch ?? true) {
|
|
191
|
+
const searchTest = deepEqual(s.location.search, next().search, {
|
|
192
|
+
partial: !local.activeOptions?.exact,
|
|
193
|
+
ignoreUndefined: !local.activeOptions?.explicitUndefined,
|
|
194
|
+
})
|
|
195
|
+
if (!searchTest) {
|
|
196
|
+
return false
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (local.activeOptions?.includeHash) {
|
|
201
|
+
return s.location.hash === next().hash
|
|
202
|
+
}
|
|
203
|
+
return true
|
|
204
|
+
},
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
const doPreload = () =>
|
|
208
|
+
router.preloadRoute(_options() as any).catch((err: any) => {
|
|
209
|
+
console.warn(err)
|
|
210
|
+
console.warn(preloadWarning)
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
const preloadViewportIoCallback = (
|
|
214
|
+
entry: IntersectionObserverEntry | undefined,
|
|
215
|
+
) => {
|
|
216
|
+
if (entry?.isIntersecting) {
|
|
217
|
+
doPreload()
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const [ref, setRef] = Solid.createSignal<Element | null>(null)
|
|
222
|
+
|
|
223
|
+
useIntersectionObserver(
|
|
224
|
+
ref,
|
|
225
|
+
preloadViewportIoCallback,
|
|
226
|
+
{ rootMargin: '100px' },
|
|
227
|
+
{ disabled: !!local.disabled || !(preload() === 'viewport') },
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
Solid.createEffect(() => {
|
|
231
|
+
if (hasRenderFetched) {
|
|
232
|
+
return
|
|
233
|
+
}
|
|
234
|
+
if (!local.disabled && preload() === 'render') {
|
|
235
|
+
doPreload()
|
|
236
|
+
hasRenderFetched = true
|
|
237
|
+
}
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
if (type() === 'external') {
|
|
241
|
+
return Solid.mergeProps(
|
|
242
|
+
propsSafeToSpread,
|
|
243
|
+
{
|
|
244
|
+
ref,
|
|
245
|
+
get type() {
|
|
246
|
+
return type()
|
|
247
|
+
},
|
|
248
|
+
get href() {
|
|
249
|
+
return local.to
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
Solid.splitProps(local, [
|
|
253
|
+
'children',
|
|
254
|
+
'target',
|
|
255
|
+
'disabled',
|
|
256
|
+
'style',
|
|
257
|
+
'class',
|
|
258
|
+
'onClick',
|
|
259
|
+
'onFocus',
|
|
260
|
+
'onMouseEnter',
|
|
261
|
+
'onMouseLeave',
|
|
262
|
+
'onMouseOut',
|
|
263
|
+
'onMouseOver',
|
|
264
|
+
'onTouchStart',
|
|
265
|
+
])[0],
|
|
266
|
+
) as any
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// The click handler
|
|
270
|
+
const handleClick = (e: MouseEvent) => {
|
|
271
|
+
if (
|
|
272
|
+
!local.disabled &&
|
|
273
|
+
!isCtrlEvent(e) &&
|
|
274
|
+
!e.defaultPrevented &&
|
|
275
|
+
(!local.target || local.target === '_self') &&
|
|
276
|
+
e.button === 0
|
|
277
|
+
) {
|
|
278
|
+
e.preventDefault()
|
|
279
|
+
|
|
280
|
+
setIsTransitioning(true)
|
|
281
|
+
|
|
282
|
+
const unsub = router.subscribe('onResolved', () => {
|
|
283
|
+
unsub()
|
|
284
|
+
setIsTransitioning(false)
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
// All is well? Navigate!
|
|
288
|
+
// N.B. we don't call `router.commitLocation(next) here because we want to run `validateSearch` before committing
|
|
289
|
+
return router.navigate({
|
|
290
|
+
..._options(),
|
|
291
|
+
replace: local.replace,
|
|
292
|
+
resetScroll: local.resetScroll,
|
|
293
|
+
hashScrollIntoView: local.hashScrollIntoView,
|
|
294
|
+
startTransition: local.startTransition,
|
|
295
|
+
viewTransition: local.viewTransition,
|
|
296
|
+
ignoreBlocker: local.ignoreBlocker,
|
|
297
|
+
} as any)
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// The click handler
|
|
302
|
+
const handleFocus = (_: MouseEvent) => {
|
|
303
|
+
if (local.disabled) return
|
|
304
|
+
if (preload()) {
|
|
305
|
+
doPreload()
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const handleTouchStart = handleFocus
|
|
310
|
+
|
|
311
|
+
const handleEnter = (e: MouseEvent) => {
|
|
312
|
+
if (local.disabled) return
|
|
313
|
+
const eventTarget = (e.target || {}) as LinkCurrentTargetElement
|
|
314
|
+
|
|
315
|
+
if (preload()) {
|
|
316
|
+
if (eventTarget.preloadTimeout) {
|
|
317
|
+
return
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
eventTarget.preloadTimeout = setTimeout(() => {
|
|
321
|
+
eventTarget.preloadTimeout = null
|
|
322
|
+
doPreload()
|
|
323
|
+
}, preloadDelay())
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const handleLeave = (e: MouseEvent) => {
|
|
328
|
+
if (local.disabled) return
|
|
329
|
+
const eventTarget = (e.target || {}) as LinkCurrentTargetElement
|
|
330
|
+
|
|
331
|
+
if (eventTarget.preloadTimeout) {
|
|
332
|
+
clearTimeout(eventTarget.preloadTimeout)
|
|
333
|
+
eventTarget.preloadTimeout = null
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** Call a JSX.EventHandlerUnion with the event. */
|
|
338
|
+
function callHandler<T, TEvent extends Event>(
|
|
339
|
+
event: TEvent & { currentTarget: T; target: Element },
|
|
340
|
+
handler: Solid.JSX.EventHandlerUnion<T, TEvent> | undefined,
|
|
341
|
+
) {
|
|
342
|
+
if (handler) {
|
|
343
|
+
if (typeof handler === 'function') {
|
|
344
|
+
handler(event)
|
|
345
|
+
} else {
|
|
346
|
+
handler[0](handler[1], event)
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return event.defaultPrevented
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function composeEventHandlers<T>(
|
|
354
|
+
handlers: Array<Solid.JSX.EventHandlerUnion<T, any> | undefined>,
|
|
355
|
+
) {
|
|
356
|
+
return (event: any) => {
|
|
357
|
+
for (const handler of handlers) {
|
|
358
|
+
callHandler(event, handler)
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Get the active props
|
|
364
|
+
const resolvedActiveProps: () => Omit<Solid.ComponentProps<'a'>, 'style'> & {
|
|
365
|
+
style?: Solid.JSX.CSSProperties
|
|
366
|
+
} = () =>
|
|
367
|
+
isActive() ? (functionalUpdate(local.activeProps as any, {}) ?? {}) : {}
|
|
368
|
+
|
|
369
|
+
// Get the inactive props
|
|
370
|
+
const resolvedInactiveProps: () => Omit<
|
|
371
|
+
Solid.ComponentProps<'a'>,
|
|
372
|
+
'style'
|
|
373
|
+
> & { style?: Solid.JSX.CSSProperties } = () =>
|
|
374
|
+
isActive() ? {} : functionalUpdate(local.inactiveProps, {})
|
|
375
|
+
|
|
376
|
+
const resolvedClassName = () =>
|
|
377
|
+
[local.class, resolvedActiveProps().class, resolvedInactiveProps().class]
|
|
378
|
+
.filter(Boolean)
|
|
379
|
+
.join(' ')
|
|
380
|
+
|
|
381
|
+
const resolvedStyle = () => ({
|
|
382
|
+
...local.style,
|
|
383
|
+
...resolvedActiveProps().style,
|
|
384
|
+
...resolvedInactiveProps().style,
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
const href = Solid.createMemo(() => {
|
|
388
|
+
const nextLocation = next()
|
|
389
|
+
const maskedLocation = nextLocation?.maskedLocation
|
|
390
|
+
|
|
391
|
+
return _options().disabled
|
|
392
|
+
? undefined
|
|
393
|
+
: maskedLocation
|
|
394
|
+
? router.history.createHref(maskedLocation.href)
|
|
395
|
+
: router.history.createHref(nextLocation?.href)
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
return Solid.mergeProps(
|
|
399
|
+
propsSafeToSpread,
|
|
400
|
+
resolvedActiveProps,
|
|
401
|
+
resolvedInactiveProps,
|
|
402
|
+
() => {
|
|
403
|
+
return {
|
|
404
|
+
href: href(),
|
|
405
|
+
ref: mergeRefs(setRef, _options().ref),
|
|
406
|
+
onClick: composeEventHandlers([local.onClick, handleClick]),
|
|
407
|
+
onFocus: composeEventHandlers([local.onFocus, handleFocus]),
|
|
408
|
+
onMouseEnter: composeEventHandlers([local.onMouseEnter, handleEnter]),
|
|
409
|
+
onMouseOver: composeEventHandlers([local.onMouseOver, handleEnter]),
|
|
410
|
+
onMouseLeave: composeEventHandlers([local.onMouseLeave, handleLeave]),
|
|
411
|
+
onMouseOut: composeEventHandlers([local.onMouseOut, handleLeave]),
|
|
412
|
+
onTouchStart: composeEventHandlers([
|
|
413
|
+
local.onTouchStart,
|
|
414
|
+
handleTouchStart,
|
|
415
|
+
]),
|
|
416
|
+
disabled: !!local.disabled,
|
|
417
|
+
target: local.target,
|
|
418
|
+
...(Object.keys(resolvedStyle).length && { style: resolvedStyle }),
|
|
419
|
+
...(resolvedClassName() && { class: resolvedClassName() }),
|
|
420
|
+
...(local.disabled && {
|
|
421
|
+
role: 'link',
|
|
422
|
+
'aria-disabled': true,
|
|
423
|
+
}),
|
|
424
|
+
...(isActive() && { 'data-status': 'active', 'aria-current': 'page' }),
|
|
425
|
+
...(isTransitioning() && { 'data-transitioning': 'transitioning' }),
|
|
426
|
+
}
|
|
427
|
+
},
|
|
428
|
+
) as any
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export type UseLinkPropsOptions<
|
|
432
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
433
|
+
TFrom extends RoutePaths<TRouter['routeTree']> | string = string,
|
|
434
|
+
TTo extends string | undefined = '.',
|
|
435
|
+
TMaskFrom extends RoutePaths<TRouter['routeTree']> | string = TFrom,
|
|
436
|
+
TMaskTo extends string = '.',
|
|
437
|
+
> = ActiveLinkOptions<'a', TRouter, TFrom, TTo, TMaskFrom, TMaskTo> &
|
|
438
|
+
Omit<Solid.ComponentProps<'a'>, 'style'> & { style?: Solid.JSX.CSSProperties }
|
|
439
|
+
|
|
440
|
+
export type ActiveLinkOptions<
|
|
441
|
+
TComp = 'a',
|
|
442
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
443
|
+
TFrom extends string = string,
|
|
444
|
+
TTo extends string | undefined = '.',
|
|
445
|
+
TMaskFrom extends string = TFrom,
|
|
446
|
+
TMaskTo extends string = '.',
|
|
447
|
+
> = LinkOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> &
|
|
448
|
+
ActiveLinkOptionProps<TComp>
|
|
449
|
+
|
|
450
|
+
type ActiveLinkProps<TComp> = Partial<
|
|
451
|
+
LinkComponentSolidProps<TComp> & {
|
|
452
|
+
[key: `data-${string}`]: unknown
|
|
453
|
+
}
|
|
454
|
+
>
|
|
455
|
+
|
|
456
|
+
export interface ActiveLinkOptionProps<TComp = 'a'> {
|
|
457
|
+
/**
|
|
458
|
+
* A function that returns additional props for the `active` state of this link.
|
|
459
|
+
* These props override other props passed to the link (`style`'s are merged, `class`'s are concatenated)
|
|
460
|
+
*/
|
|
461
|
+
activeProps?: ActiveLinkProps<TComp> | (() => ActiveLinkProps<TComp>)
|
|
462
|
+
/**
|
|
463
|
+
* A function that returns additional props for the `inactive` state of this link.
|
|
464
|
+
* These props override other props passed to the link (`style`'s are merged, `class`'s are concatenated)
|
|
465
|
+
*/
|
|
466
|
+
inactiveProps?: ActiveLinkProps<TComp> | (() => ActiveLinkProps<TComp>)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export type LinkProps<
|
|
470
|
+
TComp = 'a',
|
|
471
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
472
|
+
TFrom extends string = string,
|
|
473
|
+
TTo extends string | undefined = '.',
|
|
474
|
+
TMaskFrom extends string = TFrom,
|
|
475
|
+
TMaskTo extends string = '.',
|
|
476
|
+
> = ActiveLinkOptions<TComp, TRouter, TFrom, TTo, TMaskFrom, TMaskTo> &
|
|
477
|
+
LinkPropsChildren
|
|
478
|
+
|
|
479
|
+
export interface LinkPropsChildren {
|
|
480
|
+
// 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
|
|
481
|
+
children?:
|
|
482
|
+
| Solid.JSX.Element
|
|
483
|
+
| ((state: {
|
|
484
|
+
isActive: boolean
|
|
485
|
+
isTransitioning: boolean
|
|
486
|
+
}) => Solid.JSX.Element)
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
type LinkComponentSolidProps<TComp> = TComp extends Solid.ValidComponent
|
|
490
|
+
? Omit<Solid.ComponentProps<TComp>, keyof CreateLinkProps>
|
|
491
|
+
: never
|
|
492
|
+
|
|
493
|
+
export type LinkComponentProps<
|
|
494
|
+
TComp = 'a',
|
|
495
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
496
|
+
TFrom extends string = string,
|
|
497
|
+
TTo extends string | undefined = '.',
|
|
498
|
+
TMaskFrom extends string = TFrom,
|
|
499
|
+
TMaskTo extends string = '.',
|
|
500
|
+
> = LinkComponentSolidProps<TComp> &
|
|
501
|
+
LinkProps<TComp, TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
|
|
502
|
+
|
|
503
|
+
export type CreateLinkProps = LinkProps<
|
|
504
|
+
any,
|
|
505
|
+
any,
|
|
506
|
+
string,
|
|
507
|
+
string,
|
|
508
|
+
string,
|
|
509
|
+
string
|
|
510
|
+
>
|
|
511
|
+
|
|
512
|
+
export type LinkComponent<
|
|
513
|
+
in out TComp,
|
|
514
|
+
in out TDefaultFrom extends string = string,
|
|
515
|
+
> = <
|
|
516
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
517
|
+
const TFrom extends string = TDefaultFrom,
|
|
518
|
+
const TTo extends string | undefined = undefined,
|
|
519
|
+
const TMaskFrom extends string = TFrom,
|
|
520
|
+
const TMaskTo extends string = '',
|
|
521
|
+
>(
|
|
522
|
+
props: LinkComponentProps<TComp, TRouter, TFrom, TTo, TMaskFrom, TMaskTo>,
|
|
523
|
+
) => Solid.JSX.Element
|
|
524
|
+
|
|
525
|
+
export interface LinkComponentRoute<
|
|
526
|
+
in out TDefaultFrom extends string = string,
|
|
527
|
+
> {
|
|
528
|
+
defaultFrom: TDefaultFrom
|
|
529
|
+
<
|
|
530
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
531
|
+
const TTo extends string | undefined = undefined,
|
|
532
|
+
const TMaskTo extends string = '',
|
|
533
|
+
>(
|
|
534
|
+
props: LinkComponentProps<
|
|
535
|
+
'a',
|
|
536
|
+
TRouter,
|
|
537
|
+
this['defaultFrom'],
|
|
538
|
+
TTo,
|
|
539
|
+
this['defaultFrom'],
|
|
540
|
+
TMaskTo
|
|
541
|
+
>,
|
|
542
|
+
): Solid.JSX.Element
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
export function createLink<const TComp>(
|
|
546
|
+
Comp: Constrain<TComp, any, (props: CreateLinkProps) => Solid.JSX.Element>,
|
|
547
|
+
): LinkComponent<TComp> {
|
|
548
|
+
return (props) => <Link {...(props as any)} _asChild={Comp} />
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
export const Link: LinkComponent<'a'> = (props: any) => {
|
|
552
|
+
const [local, rest] = Solid.splitProps(props, ['_asChild'])
|
|
553
|
+
|
|
554
|
+
const [_, linkProps] = Solid.splitProps(
|
|
555
|
+
useLinkProps(rest as unknown as any),
|
|
556
|
+
['type', 'children'],
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
const children = () =>
|
|
560
|
+
typeof rest.children === 'function'
|
|
561
|
+
? rest.children({
|
|
562
|
+
get isActive() {
|
|
563
|
+
return (linkProps as any)['data-status'] === 'active'
|
|
564
|
+
},
|
|
565
|
+
})
|
|
566
|
+
: rest.children
|
|
567
|
+
|
|
568
|
+
return (
|
|
569
|
+
<Dynamic component={local._asChild ? local._asChild : 'a'} {...linkProps}>
|
|
570
|
+
{children}
|
|
571
|
+
</Dynamic>
|
|
572
|
+
)
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function isCtrlEvent(e: MouseEvent) {
|
|
576
|
+
return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey)
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
export type LinkOptionsFnOptions<
|
|
580
|
+
TOptions,
|
|
581
|
+
TComp,
|
|
582
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
583
|
+
> =
|
|
584
|
+
TOptions extends ReadonlyArray<any>
|
|
585
|
+
? ValidateLinkOptionsArray<TRouter, TOptions, string, TComp>
|
|
586
|
+
: ValidateLinkOptions<TRouter, TOptions, string, TComp>
|
|
587
|
+
|
|
588
|
+
export type LinkOptionsFn<TComp> = <
|
|
589
|
+
const TOptions,
|
|
590
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
591
|
+
>(
|
|
592
|
+
options: LinkOptionsFnOptions<TOptions, TComp, TRouter>,
|
|
593
|
+
) => TOptions
|
|
594
|
+
|
|
595
|
+
export const linkOptions: LinkOptionsFn<'a'> = (options) => {
|
|
596
|
+
return options as any
|
|
597
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as Solid from 'solid-js'
|
|
2
|
+
|
|
3
|
+
export const matchContext = Solid.createContext<
|
|
4
|
+
Solid.Accessor<string | undefined>
|
|
5
|
+
>(() => undefined)
|
|
6
|
+
|
|
7
|
+
// N.B. this only exists so we can conditionally call useContext on it when we are not interested in the nearest match
|
|
8
|
+
export const dummyMatchContext = Solid.createContext<
|
|
9
|
+
Solid.Accessor<string | undefined>
|
|
10
|
+
>(() => undefined)
|