@depup/tanstack__react-router 1.166.4-depup.0
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 +32 -0
- package/dist/cjs/Asset.cjs +177 -0
- package/dist/cjs/Asset.cjs.map +1 -0
- package/dist/cjs/Asset.d.cts +5 -0
- package/dist/cjs/CatchBoundary.cjs +114 -0
- package/dist/cjs/CatchBoundary.cjs.map +1 -0
- package/dist/cjs/CatchBoundary.d.cts +12 -0
- package/dist/cjs/ClientOnly.cjs +21 -0
- package/dist/cjs/ClientOnly.cjs.map +1 -0
- package/dist/cjs/ClientOnly.d.cts +49 -0
- package/dist/cjs/HeadContent.cjs +15 -0
- package/dist/cjs/HeadContent.cjs.map +1 -0
- package/dist/cjs/HeadContent.d.cts +6 -0
- package/dist/cjs/HeadContent.dev.cjs +41 -0
- package/dist/cjs/HeadContent.dev.cjs.map +1 -0
- package/dist/cjs/HeadContent.dev.d.cts +10 -0
- package/dist/cjs/Match.cjs +255 -0
- package/dist/cjs/Match.cjs.map +1 -0
- package/dist/cjs/Match.d.cts +14 -0
- package/dist/cjs/Matches.cjs +138 -0
- package/dist/cjs/Matches.cjs.map +1 -0
- package/dist/cjs/Matches.d.cts +68 -0
- package/dist/cjs/RouterProvider.cjs +32 -0
- package/dist/cjs/RouterProvider.cjs.map +1 -0
- package/dist/cjs/RouterProvider.d.cts +23 -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 +22 -0
- package/dist/cjs/ScriptOnce.cjs.map +1 -0
- package/dist/cjs/ScriptOnce.d.cts +6 -0
- package/dist/cjs/Scripts.cjs +56 -0
- package/dist/cjs/Scripts.cjs.map +1 -0
- package/dist/cjs/Scripts.d.cts +5 -0
- package/dist/cjs/ScrollRestoration.cjs +38 -0
- package/dist/cjs/ScrollRestoration.cjs.map +1 -0
- package/dist/cjs/ScrollRestoration.d.cts +14 -0
- package/dist/cjs/Transitioner.cjs +119 -0
- package/dist/cjs/Transitioner.cjs.map +1 -0
- package/dist/cjs/Transitioner.d.cts +1 -0
- package/dist/cjs/awaited.cjs +51 -0
- package/dist/cjs/awaited.cjs.map +1 -0
- package/dist/cjs/awaited.d.cts +14 -0
- package/dist/cjs/fileRoute.cjs +109 -0
- package/dist/cjs/fileRoute.cjs.map +1 -0
- package/dist/cjs/fileRoute.d.cts +87 -0
- package/dist/cjs/headContentUtils.cjs +185 -0
- package/dist/cjs/headContentUtils.cjs.map +1 -0
- package/dist/cjs/headContentUtils.d.cts +7 -0
- package/dist/cjs/history.d.cts +8 -0
- package/dist/cjs/index.cjs +241 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +51 -0
- package/dist/cjs/index.dev.cjs +241 -0
- package/dist/cjs/index.dev.cjs.map +1 -0
- package/dist/cjs/index.dev.d.cts +2 -0
- package/dist/cjs/lazyRouteComponent.cjs +70 -0
- package/dist/cjs/lazyRouteComponent.cjs.map +1 -0
- package/dist/cjs/lazyRouteComponent.d.cts +11 -0
- package/dist/cjs/link.cjs +573 -0
- package/dist/cjs/link.cjs.map +1 -0
- package/dist/cjs/link.d.cts +98 -0
- package/dist/cjs/matchContext.cjs +27 -0
- package/dist/cjs/matchContext.cjs.map +1 -0
- package/dist/cjs/matchContext.d.cts +3 -0
- package/dist/cjs/not-found.cjs +38 -0
- package/dist/cjs/not-found.cjs.map +1 -0
- package/dist/cjs/not-found.d.cts +9 -0
- package/dist/cjs/renderRouteNotFound.cjs +22 -0
- package/dist/cjs/renderRouteNotFound.cjs.map +1 -0
- package/dist/cjs/renderRouteNotFound.d.cts +10 -0
- package/dist/cjs/route.cjs +198 -0
- package/dist/cjs/route.cjs.map +1 -0
- package/dist/cjs/route.d.cts +142 -0
- package/dist/cjs/router.cjs +22 -0
- package/dist/cjs/router.cjs.map +1 -0
- package/dist/cjs/router.d.cts +83 -0
- package/dist/cjs/routerContext.cjs +23 -0
- package/dist/cjs/routerContext.cjs.map +1 -0
- package/dist/cjs/routerContext.d.cts +3 -0
- package/dist/cjs/scroll-restoration.cjs +39 -0
- package/dist/cjs/scroll-restoration.cjs.map +1 -0
- package/dist/cjs/scroll-restoration.d.cts +1 -0
- package/dist/cjs/ssr/RouterClient.cjs +25 -0
- package/dist/cjs/ssr/RouterClient.cjs.map +1 -0
- package/dist/cjs/ssr/RouterClient.d.cts +4 -0
- package/dist/cjs/ssr/RouterServer.cjs +9 -0
- package/dist/cjs/ssr/RouterServer.cjs.map +1 -0
- package/dist/cjs/ssr/RouterServer.d.cts +4 -0
- package/dist/cjs/ssr/client.cjs +12 -0
- package/dist/cjs/ssr/client.cjs.map +1 -0
- package/dist/cjs/ssr/client.d.cts +2 -0
- package/dist/cjs/ssr/defaultRenderHandler.cjs +15 -0
- package/dist/cjs/ssr/defaultRenderHandler.cjs.map +1 -0
- package/dist/cjs/ssr/defaultRenderHandler.d.cts +1 -0
- package/dist/cjs/ssr/defaultStreamHandler.cjs +16 -0
- package/dist/cjs/ssr/defaultStreamHandler.cjs.map +1 -0
- package/dist/cjs/ssr/defaultStreamHandler.d.cts +1 -0
- package/dist/cjs/ssr/renderRouterToStream.cjs +73 -0
- package/dist/cjs/ssr/renderRouterToStream.cjs.map +1 -0
- package/dist/cjs/ssr/renderRouterToStream.d.cts +8 -0
- package/dist/cjs/ssr/renderRouterToString.cjs +31 -0
- package/dist/cjs/ssr/renderRouterToString.cjs.map +1 -0
- package/dist/cjs/ssr/renderRouterToString.d.cts +7 -0
- package/dist/cjs/ssr/serializer.d.cts +6 -0
- package/dist/cjs/ssr/server.cjs +20 -0
- package/dist/cjs/ssr/server.cjs.map +1 -0
- package/dist/cjs/ssr/server.d.cts +6 -0
- package/dist/cjs/structuralSharing.d.cts +8 -0
- package/dist/cjs/typePrimitives.d.cts +16 -0
- package/dist/cjs/useBlocker.cjs +171 -0
- package/dist/cjs/useBlocker.cjs.map +1 -0
- package/dist/cjs/useBlocker.d.cts +66 -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 +15 -0
- package/dist/cjs/useLoaderData.cjs.map +1 -0
- package/dist/cjs/useLoaderData.d.cts +19 -0
- package/dist/cjs/useLoaderDeps.cjs +14 -0
- package/dist/cjs/useLoaderDeps.cjs.map +1 -0
- package/dist/cjs/useLoaderDeps.d.cts +19 -0
- package/dist/cjs/useLocation.cjs +10 -0
- package/dist/cjs/useLocation.cjs.map +1 -0
- package/dist/cjs/useLocation.d.cts +18 -0
- package/dist/cjs/useMatch.cjs +47 -0
- package/dist/cjs/useMatch.cjs.map +1 -0
- package/dist/cjs/useMatch.d.cts +14 -0
- package/dist/cjs/useNavigate.cjs +49 -0
- package/dist/cjs/useNavigate.cjs.map +1 -0
- package/dist/cjs/useNavigate.d.cts +28 -0
- package/dist/cjs/useParams.cjs +17 -0
- package/dist/cjs/useParams.cjs.map +1 -0
- package/dist/cjs/useParams.d.cts +21 -0
- package/dist/cjs/useRouteContext.cjs +11 -0
- package/dist/cjs/useRouteContext.cjs.map +1 -0
- package/dist/cjs/useRouteContext.d.cts +3 -0
- package/dist/cjs/useRouter.cjs +32 -0
- package/dist/cjs/useRouter.cjs.map +1 -0
- package/dist/cjs/useRouter.d.cts +14 -0
- package/dist/cjs/useRouterState.cjs +38 -0
- package/dist/cjs/useRouterState.cjs.map +1 -0
- package/dist/cjs/useRouterState.d.cts +20 -0
- package/dist/cjs/useSearch.cjs +16 -0
- package/dist/cjs/useSearch.cjs.map +1 -0
- package/dist/cjs/useSearch.d.cts +21 -0
- package/dist/cjs/utils.cjs +62 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/dist/cjs/utils.d.cts +54 -0
- package/dist/esm/Asset.d.ts +5 -0
- package/dist/esm/Asset.js +160 -0
- package/dist/esm/Asset.js.map +1 -0
- package/dist/esm/CatchBoundary.d.ts +12 -0
- package/dist/esm/CatchBoundary.js +97 -0
- package/dist/esm/CatchBoundary.js.map +1 -0
- package/dist/esm/ClientOnly.d.ts +49 -0
- package/dist/esm/ClientOnly.js +21 -0
- package/dist/esm/ClientOnly.js.map +1 -0
- package/dist/esm/HeadContent.d.ts +6 -0
- package/dist/esm/HeadContent.dev.d.ts +10 -0
- package/dist/esm/HeadContent.dev.js +25 -0
- package/dist/esm/HeadContent.dev.js.map +1 -0
- package/dist/esm/HeadContent.js +15 -0
- package/dist/esm/HeadContent.js.map +1 -0
- package/dist/esm/Match.d.ts +14 -0
- package/dist/esm/Match.js +238 -0
- package/dist/esm/Match.js.map +1 -0
- package/dist/esm/Matches.d.ts +68 -0
- package/dist/esm/Matches.js +121 -0
- package/dist/esm/Matches.js.map +1 -0
- package/dist/esm/RouterProvider.d.ts +23 -0
- package/dist/esm/RouterProvider.js +32 -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 +6 -0
- package/dist/esm/ScriptOnce.js +22 -0
- package/dist/esm/ScriptOnce.js.map +1 -0
- package/dist/esm/Scripts.d.ts +5 -0
- package/dist/esm/Scripts.js +56 -0
- package/dist/esm/Scripts.js.map +1 -0
- package/dist/esm/ScrollRestoration.d.ts +14 -0
- package/dist/esm/ScrollRestoration.js +38 -0
- package/dist/esm/ScrollRestoration.js.map +1 -0
- package/dist/esm/Transitioner.d.ts +1 -0
- package/dist/esm/Transitioner.js +102 -0
- package/dist/esm/Transitioner.js.map +1 -0
- package/dist/esm/awaited.d.ts +14 -0
- package/dist/esm/awaited.js +34 -0
- package/dist/esm/awaited.js.map +1 -0
- package/dist/esm/fileRoute.d.ts +87 -0
- package/dist/esm/fileRoute.js +109 -0
- package/dist/esm/fileRoute.js.map +1 -0
- package/dist/esm/headContentUtils.d.ts +7 -0
- package/dist/esm/headContentUtils.js +168 -0
- package/dist/esm/headContentUtils.js.map +1 -0
- package/dist/esm/history.d.ts +8 -0
- package/dist/esm/index.d.ts +51 -0
- package/dist/esm/index.dev.d.ts +2 -0
- package/dist/esm/index.dev.js +133 -0
- package/dist/esm/index.dev.js.map +1 -0
- package/dist/esm/index.js +133 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lazyRouteComponent.d.ts +11 -0
- package/dist/esm/lazyRouteComponent.js +53 -0
- package/dist/esm/lazyRouteComponent.js.map +1 -0
- package/dist/esm/link.d.ts +98 -0
- package/dist/esm/link.js +556 -0
- package/dist/esm/link.js.map +1 -0
- package/dist/esm/matchContext.d.ts +3 -0
- package/dist/esm/matchContext.js +10 -0
- package/dist/esm/matchContext.js.map +1 -0
- package/dist/esm/not-found.d.ts +9 -0
- package/dist/esm/not-found.js +38 -0
- package/dist/esm/not-found.js.map +1 -0
- package/dist/esm/renderRouteNotFound.d.ts +10 -0
- package/dist/esm/renderRouteNotFound.js +22 -0
- package/dist/esm/renderRouteNotFound.js.map +1 -0
- package/dist/esm/route.d.ts +142 -0
- package/dist/esm/route.js +198 -0
- package/dist/esm/route.js.map +1 -0
- package/dist/esm/router.d.ts +83 -0
- package/dist/esm/router.js +22 -0
- package/dist/esm/router.js.map +1 -0
- package/dist/esm/routerContext.d.ts +3 -0
- package/dist/esm/routerContext.js +6 -0
- package/dist/esm/routerContext.js.map +1 -0
- package/dist/esm/scroll-restoration.d.ts +1 -0
- package/dist/esm/scroll-restoration.js +39 -0
- package/dist/esm/scroll-restoration.js.map +1 -0
- package/dist/esm/ssr/RouterClient.d.ts +4 -0
- package/dist/esm/ssr/RouterClient.js +25 -0
- package/dist/esm/ssr/RouterClient.js.map +1 -0
- package/dist/esm/ssr/RouterServer.d.ts +4 -0
- package/dist/esm/ssr/RouterServer.js +9 -0
- package/dist/esm/ssr/RouterServer.js.map +1 -0
- package/dist/esm/ssr/client.d.ts +2 -0
- package/dist/esm/ssr/client.js +6 -0
- package/dist/esm/ssr/client.js.map +1 -0
- package/dist/esm/ssr/defaultRenderHandler.d.ts +1 -0
- package/dist/esm/ssr/defaultRenderHandler.js +15 -0
- package/dist/esm/ssr/defaultRenderHandler.js.map +1 -0
- package/dist/esm/ssr/defaultStreamHandler.d.ts +1 -0
- package/dist/esm/ssr/defaultStreamHandler.js +16 -0
- package/dist/esm/ssr/defaultStreamHandler.js.map +1 -0
- package/dist/esm/ssr/renderRouterToStream.d.ts +8 -0
- package/dist/esm/ssr/renderRouterToStream.js +73 -0
- package/dist/esm/ssr/renderRouterToStream.js.map +1 -0
- package/dist/esm/ssr/renderRouterToString.d.ts +7 -0
- package/dist/esm/ssr/renderRouterToString.js +31 -0
- package/dist/esm/ssr/renderRouterToString.js.map +1 -0
- package/dist/esm/ssr/serializer.d.ts +6 -0
- package/dist/esm/ssr/server.d.ts +6 -0
- package/dist/esm/ssr/server.js +14 -0
- package/dist/esm/ssr/server.js.map +1 -0
- package/dist/esm/structuralSharing.d.ts +8 -0
- package/dist/esm/typePrimitives.d.ts +16 -0
- package/dist/esm/useBlocker.d.ts +66 -0
- package/dist/esm/useBlocker.js +154 -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 +19 -0
- package/dist/esm/useLoaderData.js +15 -0
- package/dist/esm/useLoaderData.js.map +1 -0
- package/dist/esm/useLoaderDeps.d.ts +19 -0
- package/dist/esm/useLoaderDeps.js +14 -0
- package/dist/esm/useLoaderDeps.js.map +1 -0
- package/dist/esm/useLocation.d.ts +18 -0
- package/dist/esm/useLocation.js +10 -0
- package/dist/esm/useLocation.js.map +1 -0
- package/dist/esm/useMatch.d.ts +14 -0
- package/dist/esm/useMatch.js +30 -0
- package/dist/esm/useMatch.js.map +1 -0
- package/dist/esm/useNavigate.d.ts +28 -0
- package/dist/esm/useNavigate.js +32 -0
- package/dist/esm/useNavigate.js.map +1 -0
- package/dist/esm/useParams.d.ts +21 -0
- package/dist/esm/useParams.js +17 -0
- package/dist/esm/useParams.js.map +1 -0
- package/dist/esm/useRouteContext.d.ts +3 -0
- package/dist/esm/useRouteContext.js +11 -0
- package/dist/esm/useRouteContext.js.map +1 -0
- package/dist/esm/useRouter.d.ts +14 -0
- package/dist/esm/useRouter.js +15 -0
- package/dist/esm/useRouter.js.map +1 -0
- package/dist/esm/useRouterState.d.ts +20 -0
- package/dist/esm/useRouterState.js +38 -0
- package/dist/esm/useRouterState.js.map +1 -0
- package/dist/esm/useSearch.d.ts +21 -0
- package/dist/esm/useSearch.js +16 -0
- package/dist/esm/useSearch.js.map +1 -0
- package/dist/esm/utils.d.ts +54 -0
- package/dist/esm/utils.js +45 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/llms/index.d.ts +3 -0
- package/dist/llms/index.js +43 -0
- package/dist/llms/rules/api.d.ts +2 -0
- package/dist/llms/rules/api.js +4612 -0
- package/dist/llms/rules/guide.d.ts +2 -0
- package/dist/llms/rules/guide.js +10690 -0
- package/dist/llms/rules/installation.d.ts +2 -0
- package/dist/llms/rules/installation.js +1285 -0
- package/dist/llms/rules/routing.d.ts +2 -0
- package/dist/llms/rules/routing.js +1984 -0
- package/dist/llms/rules/setup-and-architecture.d.ts +2 -0
- package/dist/llms/rules/setup-and-architecture.js +920 -0
- package/package.json +142 -0
- package/src/Asset.tsx +219 -0
- package/src/CatchBoundary.tsx +120 -0
- package/src/ClientOnly.tsx +68 -0
- package/src/HeadContent.dev.tsx +46 -0
- package/src/HeadContent.tsx +22 -0
- package/src/Match.tsx +360 -0
- package/src/Matches.tsx +313 -0
- package/src/RouterProvider.tsx +92 -0
- package/src/SafeFragment.tsx +5 -0
- package/src/ScriptOnce.tsx +21 -0
- package/src/Scripts.tsx +80 -0
- package/src/ScrollRestoration.tsx +69 -0
- package/src/Transitioner.tsx +134 -0
- package/src/awaited.tsx +55 -0
- package/src/fileRoute.ts +313 -0
- package/src/headContentUtils.tsx +217 -0
- package/src/history.ts +9 -0
- package/src/index.dev.tsx +6 -0
- package/src/index.tsx +341 -0
- package/src/lazyRouteComponent.tsx +96 -0
- package/src/link.tsx +984 -0
- package/src/matchContext.tsx +8 -0
- package/src/not-found.tsx +43 -0
- package/src/renderRouteNotFound.tsx +35 -0
- package/src/route.tsx +740 -0
- package/src/router.ts +127 -0
- package/src/routerContext.tsx +4 -0
- package/src/scroll-restoration.tsx +45 -0
- package/src/ssr/RouterClient.tsx +22 -0
- package/src/ssr/RouterServer.tsx +9 -0
- package/src/ssr/client.ts +2 -0
- package/src/ssr/defaultRenderHandler.tsx +12 -0
- package/src/ssr/defaultStreamHandler.tsx +13 -0
- package/src/ssr/renderRouterToStream.tsx +90 -0
- package/src/ssr/renderRouterToString.tsx +36 -0
- package/src/ssr/serializer.ts +7 -0
- package/src/ssr/server.ts +6 -0
- package/src/structuralSharing.ts +47 -0
- package/src/typePrimitives.ts +84 -0
- package/src/useBlocker.tsx +320 -0
- package/src/useCanGoBack.ts +5 -0
- package/src/useLoaderData.tsx +91 -0
- package/src/useLoaderDeps.tsx +69 -0
- package/src/useLocation.tsx +52 -0
- package/src/useMatch.tsx +123 -0
- package/src/useNavigate.tsx +78 -0
- package/src/useParams.tsx +107 -0
- package/src/useRouteContext.ts +30 -0
- package/src/useRouter.tsx +25 -0
- package/src/useRouterState.tsx +86 -0
- package/src/useSearch.tsx +105 -0
- package/src/utils.ts +125 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { useLayoutEffect } from './utils'
|
|
3
|
+
import { useRouter } from './useRouter'
|
|
4
|
+
import type {
|
|
5
|
+
AnyRouter,
|
|
6
|
+
FromPathOption,
|
|
7
|
+
NavigateOptions,
|
|
8
|
+
RegisteredRouter,
|
|
9
|
+
UseNavigateResult,
|
|
10
|
+
} from '@tanstack/router-core'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Imperative navigation hook.
|
|
14
|
+
*
|
|
15
|
+
* Returns a stable `navigate(options)` function to change the current location
|
|
16
|
+
* programmatically. Prefer the `Link` component for user-initiated navigation,
|
|
17
|
+
* and use this hook from effects, callbacks, or handlers where imperative
|
|
18
|
+
* navigation is required.
|
|
19
|
+
*
|
|
20
|
+
* Options:
|
|
21
|
+
* - `from`: Optional route base used to resolve relative `to` paths.
|
|
22
|
+
*
|
|
23
|
+
* @returns A function that accepts `NavigateOptions`.
|
|
24
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useNavigateHook
|
|
25
|
+
*/
|
|
26
|
+
export function useNavigate<
|
|
27
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
28
|
+
TDefaultFrom extends string = string,
|
|
29
|
+
>(_defaultOpts?: {
|
|
30
|
+
from?: FromPathOption<TRouter, TDefaultFrom>
|
|
31
|
+
}): UseNavigateResult<TDefaultFrom> {
|
|
32
|
+
const router = useRouter()
|
|
33
|
+
|
|
34
|
+
return React.useCallback(
|
|
35
|
+
(options: NavigateOptions) => {
|
|
36
|
+
return router.navigate({
|
|
37
|
+
...options,
|
|
38
|
+
from: options.from ?? _defaultOpts?.from,
|
|
39
|
+
})
|
|
40
|
+
},
|
|
41
|
+
[_defaultOpts?.from, router],
|
|
42
|
+
) as UseNavigateResult<TDefaultFrom>
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Component that triggers a navigation when rendered. Navigation executes
|
|
47
|
+
* in an effect after mount/update.
|
|
48
|
+
*
|
|
49
|
+
* Props are the same as `NavigateOptions` used by `navigate()`.
|
|
50
|
+
*
|
|
51
|
+
* @returns null
|
|
52
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/navigateComponent
|
|
53
|
+
*/
|
|
54
|
+
export function Navigate<
|
|
55
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
56
|
+
const TFrom extends string = string,
|
|
57
|
+
const TTo extends string | undefined = undefined,
|
|
58
|
+
const TMaskFrom extends string = TFrom,
|
|
59
|
+
const TMaskTo extends string = '',
|
|
60
|
+
>(props: NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>): null {
|
|
61
|
+
const router = useRouter()
|
|
62
|
+
const navigate = useNavigate()
|
|
63
|
+
|
|
64
|
+
const previousPropsRef = React.useRef<NavigateOptions<
|
|
65
|
+
TRouter,
|
|
66
|
+
TFrom,
|
|
67
|
+
TTo,
|
|
68
|
+
TMaskFrom,
|
|
69
|
+
TMaskTo
|
|
70
|
+
> | null>(null)
|
|
71
|
+
useLayoutEffect(() => {
|
|
72
|
+
if (previousPropsRef.current !== props) {
|
|
73
|
+
navigate(props)
|
|
74
|
+
previousPropsRef.current = props
|
|
75
|
+
}
|
|
76
|
+
}, [router, props, navigate])
|
|
77
|
+
return null
|
|
78
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { useMatch } from './useMatch'
|
|
2
|
+
import type {
|
|
3
|
+
StructuralSharingOption,
|
|
4
|
+
ValidateSelected,
|
|
5
|
+
} from './structuralSharing'
|
|
6
|
+
import type {
|
|
7
|
+
AnyRouter,
|
|
8
|
+
RegisteredRouter,
|
|
9
|
+
ResolveUseParams,
|
|
10
|
+
StrictOrFrom,
|
|
11
|
+
ThrowConstraint,
|
|
12
|
+
ThrowOrOptional,
|
|
13
|
+
UseParamsResult,
|
|
14
|
+
} from '@tanstack/router-core'
|
|
15
|
+
|
|
16
|
+
export interface UseParamsBaseOptions<
|
|
17
|
+
TRouter extends AnyRouter,
|
|
18
|
+
TFrom,
|
|
19
|
+
TStrict extends boolean,
|
|
20
|
+
TThrow extends boolean,
|
|
21
|
+
TSelected,
|
|
22
|
+
TStructuralSharing,
|
|
23
|
+
> {
|
|
24
|
+
select?: (
|
|
25
|
+
params: ResolveUseParams<TRouter, TFrom, TStrict>,
|
|
26
|
+
) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
|
|
27
|
+
shouldThrow?: TThrow
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type UseParamsOptions<
|
|
31
|
+
TRouter extends AnyRouter,
|
|
32
|
+
TFrom extends string | undefined,
|
|
33
|
+
TStrict extends boolean,
|
|
34
|
+
TThrow extends boolean,
|
|
35
|
+
TSelected,
|
|
36
|
+
TStructuralSharing,
|
|
37
|
+
> = StrictOrFrom<TRouter, TFrom, TStrict> &
|
|
38
|
+
UseParamsBaseOptions<
|
|
39
|
+
TRouter,
|
|
40
|
+
TFrom,
|
|
41
|
+
TStrict,
|
|
42
|
+
TThrow,
|
|
43
|
+
TSelected,
|
|
44
|
+
TStructuralSharing
|
|
45
|
+
> &
|
|
46
|
+
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>
|
|
47
|
+
|
|
48
|
+
export type UseParamsRoute<out TFrom> = <
|
|
49
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
50
|
+
TSelected = unknown,
|
|
51
|
+
TStructuralSharing extends boolean = boolean,
|
|
52
|
+
>(
|
|
53
|
+
opts?: UseParamsBaseOptions<
|
|
54
|
+
TRouter,
|
|
55
|
+
TFrom,
|
|
56
|
+
/* TStrict */ true,
|
|
57
|
+
/* TThrow */ true,
|
|
58
|
+
TSelected,
|
|
59
|
+
TStructuralSharing
|
|
60
|
+
> &
|
|
61
|
+
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
|
|
62
|
+
) => UseParamsResult<TRouter, TFrom, true, TSelected>
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Access the current route's path parameters with type-safety.
|
|
66
|
+
*
|
|
67
|
+
* Options:
|
|
68
|
+
* - `from`/`strict`: Specify the matched route and whether to enforce strict typing
|
|
69
|
+
* - `select`: Project the params object to a derived value for memoized renders
|
|
70
|
+
* - `structuralSharing`: Enable structural sharing for stable references
|
|
71
|
+
* - `shouldThrow`: Throw if the route is not found in strict contexts
|
|
72
|
+
*
|
|
73
|
+
* @returns The params object (or selected value) for the matched route.
|
|
74
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useParamsHook
|
|
75
|
+
*/
|
|
76
|
+
export function useParams<
|
|
77
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
78
|
+
const TFrom extends string | undefined = undefined,
|
|
79
|
+
TStrict extends boolean = true,
|
|
80
|
+
TThrow extends boolean = true,
|
|
81
|
+
TSelected = unknown,
|
|
82
|
+
TStructuralSharing extends boolean = boolean,
|
|
83
|
+
>(
|
|
84
|
+
opts: UseParamsOptions<
|
|
85
|
+
TRouter,
|
|
86
|
+
TFrom,
|
|
87
|
+
TStrict,
|
|
88
|
+
ThrowConstraint<TStrict, TThrow>,
|
|
89
|
+
TSelected,
|
|
90
|
+
TStructuralSharing
|
|
91
|
+
>,
|
|
92
|
+
): ThrowOrOptional<
|
|
93
|
+
UseParamsResult<TRouter, TFrom, TStrict, TSelected>,
|
|
94
|
+
TThrow
|
|
95
|
+
> {
|
|
96
|
+
return useMatch({
|
|
97
|
+
from: opts.from!,
|
|
98
|
+
shouldThrow: opts.shouldThrow,
|
|
99
|
+
structuralSharing: opts.structuralSharing,
|
|
100
|
+
strict: opts.strict,
|
|
101
|
+
select: (match) => {
|
|
102
|
+
const params = opts.strict === false ? match.params : match._strictParams
|
|
103
|
+
|
|
104
|
+
return opts.select ? opts.select(params) : params
|
|
105
|
+
},
|
|
106
|
+
}) as any
|
|
107
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useMatch } from './useMatch'
|
|
2
|
+
import type {
|
|
3
|
+
AnyRouter,
|
|
4
|
+
RegisteredRouter,
|
|
5
|
+
UseRouteContextBaseOptions,
|
|
6
|
+
UseRouteContextOptions,
|
|
7
|
+
UseRouteContextResult,
|
|
8
|
+
} from '@tanstack/router-core'
|
|
9
|
+
|
|
10
|
+
export type UseRouteContextRoute<out TFrom> = <
|
|
11
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
12
|
+
TSelected = unknown,
|
|
13
|
+
>(
|
|
14
|
+
opts?: UseRouteContextBaseOptions<TRouter, TFrom, true, TSelected>,
|
|
15
|
+
) => UseRouteContextResult<TRouter, TFrom, true, TSelected>
|
|
16
|
+
|
|
17
|
+
export function useRouteContext<
|
|
18
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
19
|
+
const TFrom extends string | undefined = undefined,
|
|
20
|
+
TStrict extends boolean = true,
|
|
21
|
+
TSelected = unknown,
|
|
22
|
+
>(
|
|
23
|
+
opts: UseRouteContextOptions<TRouter, TFrom, TStrict, TSelected>,
|
|
24
|
+
): UseRouteContextResult<TRouter, TFrom, TStrict, TSelected> {
|
|
25
|
+
return useMatch({
|
|
26
|
+
...(opts as any),
|
|
27
|
+
select: (match) =>
|
|
28
|
+
opts.select ? opts.select(match.context) : match.context,
|
|
29
|
+
}) as UseRouteContextResult<TRouter, TFrom, TStrict, TSelected>
|
|
30
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import warning from 'tiny-warning'
|
|
3
|
+
import { routerContext } from './routerContext'
|
|
4
|
+
import type { AnyRouter, RegisteredRouter } from '@tanstack/router-core'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Access the current TanStack Router instance from React context.
|
|
8
|
+
* Must be used within a `RouterProvider`.
|
|
9
|
+
*
|
|
10
|
+
* Options:
|
|
11
|
+
* - `warn`: Log a warning if no router context is found (default: true).
|
|
12
|
+
*
|
|
13
|
+
* @returns The registered router instance.
|
|
14
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useRouterHook
|
|
15
|
+
*/
|
|
16
|
+
export function useRouter<TRouter extends AnyRouter = RegisteredRouter>(opts?: {
|
|
17
|
+
warn?: boolean
|
|
18
|
+
}): TRouter {
|
|
19
|
+
const value = React.useContext(routerContext)
|
|
20
|
+
warning(
|
|
21
|
+
!((opts?.warn ?? true) && !value),
|
|
22
|
+
'useRouter must be used inside a <RouterProvider> component!',
|
|
23
|
+
)
|
|
24
|
+
return value as any
|
|
25
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { useStore } from '@tanstack/react-store'
|
|
2
|
+
import { useRef } from 'react'
|
|
3
|
+
import { replaceEqualDeep } from '@tanstack/router-core'
|
|
4
|
+
import { isServer } from '@tanstack/router-core/isServer'
|
|
5
|
+
import { useRouter } from './useRouter'
|
|
6
|
+
import type {
|
|
7
|
+
AnyRouter,
|
|
8
|
+
RegisteredRouter,
|
|
9
|
+
RouterState,
|
|
10
|
+
} from '@tanstack/router-core'
|
|
11
|
+
import type {
|
|
12
|
+
StructuralSharingOption,
|
|
13
|
+
ValidateSelected,
|
|
14
|
+
} from './structuralSharing'
|
|
15
|
+
|
|
16
|
+
export type UseRouterStateOptions<
|
|
17
|
+
TRouter extends AnyRouter,
|
|
18
|
+
TSelected,
|
|
19
|
+
TStructuralSharing,
|
|
20
|
+
> = {
|
|
21
|
+
router?: TRouter
|
|
22
|
+
select?: (
|
|
23
|
+
state: RouterState<TRouter['routeTree']>,
|
|
24
|
+
) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
|
|
25
|
+
} & StructuralSharingOption<TRouter, TSelected, TStructuralSharing>
|
|
26
|
+
|
|
27
|
+
export type UseRouterStateResult<
|
|
28
|
+
TRouter extends AnyRouter,
|
|
29
|
+
TSelected,
|
|
30
|
+
> = unknown extends TSelected ? RouterState<TRouter['routeTree']> : TSelected
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Subscribe to the router's state store with optional selection and
|
|
34
|
+
* structural sharing for render optimization.
|
|
35
|
+
*
|
|
36
|
+
* Options:
|
|
37
|
+
* - `select`: Project the full router state to a derived slice
|
|
38
|
+
* - `structuralSharing`: Replace-equal semantics for stable references
|
|
39
|
+
* - `router`: Read state from a specific router instance instead of context
|
|
40
|
+
*
|
|
41
|
+
* @returns The selected router state (or the full state by default).
|
|
42
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useRouterStateHook
|
|
43
|
+
*/
|
|
44
|
+
export function useRouterState<
|
|
45
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
46
|
+
TSelected = unknown,
|
|
47
|
+
TStructuralSharing extends boolean = boolean,
|
|
48
|
+
>(
|
|
49
|
+
opts?: UseRouterStateOptions<TRouter, TSelected, TStructuralSharing>,
|
|
50
|
+
): UseRouterStateResult<TRouter, TSelected> {
|
|
51
|
+
const contextRouter = useRouter<TRouter>({
|
|
52
|
+
warn: opts?.router === undefined,
|
|
53
|
+
})
|
|
54
|
+
const router = opts?.router || contextRouter
|
|
55
|
+
|
|
56
|
+
// During SSR we render exactly once and do not need reactivity.
|
|
57
|
+
// Avoid subscribing to the store (and any structural sharing work) on the server.
|
|
58
|
+
const _isServer = isServer ?? router.isServer
|
|
59
|
+
if (_isServer) {
|
|
60
|
+
const state = router.state as RouterState<TRouter['routeTree']>
|
|
61
|
+
return (opts?.select ? opts.select(state) : state) as UseRouterStateResult<
|
|
62
|
+
TRouter,
|
|
63
|
+
TSelected
|
|
64
|
+
>
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const previousResult =
|
|
68
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
69
|
+
useRef<ValidateSelected<TRouter, TSelected, TStructuralSharing>>(undefined)
|
|
70
|
+
|
|
71
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
72
|
+
return useStore(router.__store, (state) => {
|
|
73
|
+
if (opts?.select) {
|
|
74
|
+
if (opts.structuralSharing ?? router.options.defaultStructuralSharing) {
|
|
75
|
+
const newSlice = replaceEqualDeep(
|
|
76
|
+
previousResult.current,
|
|
77
|
+
opts.select(state),
|
|
78
|
+
)
|
|
79
|
+
previousResult.current = newSlice
|
|
80
|
+
return newSlice
|
|
81
|
+
}
|
|
82
|
+
return opts.select(state)
|
|
83
|
+
}
|
|
84
|
+
return state
|
|
85
|
+
}) as UseRouterStateResult<TRouter, TSelected>
|
|
86
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { useMatch } from './useMatch'
|
|
2
|
+
import type {
|
|
3
|
+
StructuralSharingOption,
|
|
4
|
+
ValidateSelected,
|
|
5
|
+
} from './structuralSharing'
|
|
6
|
+
import type {
|
|
7
|
+
AnyRouter,
|
|
8
|
+
RegisteredRouter,
|
|
9
|
+
ResolveUseSearch,
|
|
10
|
+
StrictOrFrom,
|
|
11
|
+
ThrowConstraint,
|
|
12
|
+
ThrowOrOptional,
|
|
13
|
+
UseSearchResult,
|
|
14
|
+
} from '@tanstack/router-core'
|
|
15
|
+
|
|
16
|
+
export interface UseSearchBaseOptions<
|
|
17
|
+
TRouter extends AnyRouter,
|
|
18
|
+
TFrom,
|
|
19
|
+
TStrict extends boolean,
|
|
20
|
+
TThrow extends boolean,
|
|
21
|
+
TSelected,
|
|
22
|
+
TStructuralSharing,
|
|
23
|
+
> {
|
|
24
|
+
select?: (
|
|
25
|
+
state: ResolveUseSearch<TRouter, TFrom, TStrict>,
|
|
26
|
+
) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
|
|
27
|
+
shouldThrow?: TThrow
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type UseSearchOptions<
|
|
31
|
+
TRouter extends AnyRouter,
|
|
32
|
+
TFrom,
|
|
33
|
+
TStrict extends boolean,
|
|
34
|
+
TThrow extends boolean,
|
|
35
|
+
TSelected,
|
|
36
|
+
TStructuralSharing,
|
|
37
|
+
> = StrictOrFrom<TRouter, TFrom, TStrict> &
|
|
38
|
+
UseSearchBaseOptions<
|
|
39
|
+
TRouter,
|
|
40
|
+
TFrom,
|
|
41
|
+
TStrict,
|
|
42
|
+
TThrow,
|
|
43
|
+
TSelected,
|
|
44
|
+
TStructuralSharing
|
|
45
|
+
> &
|
|
46
|
+
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>
|
|
47
|
+
|
|
48
|
+
export type UseSearchRoute<out TFrom> = <
|
|
49
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
50
|
+
TSelected = unknown,
|
|
51
|
+
TStructuralSharing extends boolean = boolean,
|
|
52
|
+
>(
|
|
53
|
+
opts?: UseSearchBaseOptions<
|
|
54
|
+
TRouter,
|
|
55
|
+
TFrom,
|
|
56
|
+
/* TStrict */ true,
|
|
57
|
+
/* TThrow */ true,
|
|
58
|
+
TSelected,
|
|
59
|
+
TStructuralSharing
|
|
60
|
+
> &
|
|
61
|
+
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
|
|
62
|
+
) => UseSearchResult<TRouter, TFrom, true, TSelected>
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Read and select the current route's search parameters with type-safety.
|
|
66
|
+
*
|
|
67
|
+
* Options:
|
|
68
|
+
* - `from`/`strict`: Control which route's search is read and how strictly it's typed
|
|
69
|
+
* - `select`: Map the search object to a derived value for render optimization
|
|
70
|
+
* - `structuralSharing`: Enable structural sharing for stable references
|
|
71
|
+
* - `shouldThrow`: Throw when the route is not found (strict contexts)
|
|
72
|
+
*
|
|
73
|
+
* @returns The search object (or selected value) for the matched route.
|
|
74
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useSearchHook
|
|
75
|
+
*/
|
|
76
|
+
export function useSearch<
|
|
77
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
78
|
+
const TFrom extends string | undefined = undefined,
|
|
79
|
+
TStrict extends boolean = true,
|
|
80
|
+
TThrow extends boolean = true,
|
|
81
|
+
TSelected = unknown,
|
|
82
|
+
TStructuralSharing extends boolean = boolean,
|
|
83
|
+
>(
|
|
84
|
+
opts: UseSearchOptions<
|
|
85
|
+
TRouter,
|
|
86
|
+
TFrom,
|
|
87
|
+
TStrict,
|
|
88
|
+
ThrowConstraint<TStrict, TThrow>,
|
|
89
|
+
TSelected,
|
|
90
|
+
TStructuralSharing
|
|
91
|
+
>,
|
|
92
|
+
): ThrowOrOptional<
|
|
93
|
+
UseSearchResult<TRouter, TFrom, TStrict, TSelected>,
|
|
94
|
+
TThrow
|
|
95
|
+
> {
|
|
96
|
+
return useMatch({
|
|
97
|
+
from: opts.from!,
|
|
98
|
+
strict: opts.strict,
|
|
99
|
+
shouldThrow: opts.shouldThrow,
|
|
100
|
+
structuralSharing: opts.structuralSharing,
|
|
101
|
+
select: (match: any) => {
|
|
102
|
+
return opts.select ? opts.select(match.search) : match.search
|
|
103
|
+
},
|
|
104
|
+
}) as any
|
|
105
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
|
|
3
|
+
// Safe version of React.use() that will not cause compilation errors against
|
|
4
|
+
// React 18 with Webpack, which statically analyzes imports and fails when it
|
|
5
|
+
// sees React.use referenced (since 'use' is not exported from React 18).
|
|
6
|
+
// This uses a dynamic string lookup to avoid the static analysis.
|
|
7
|
+
const REACT_USE = 'use'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* React.use if available (React 19+), undefined otherwise.
|
|
11
|
+
* Use dynamic lookup to avoid Webpack compilation errors with React 18.
|
|
12
|
+
*/
|
|
13
|
+
export const reactUse:
|
|
14
|
+
| (<T>(usable: Promise<T> | React.Context<T>) => T)
|
|
15
|
+
| undefined = (React as any)[REACT_USE]
|
|
16
|
+
|
|
17
|
+
export function useStableCallback<T extends (...args: Array<any>) => any>(
|
|
18
|
+
fn: T,
|
|
19
|
+
): T {
|
|
20
|
+
const fnRef = React.useRef(fn)
|
|
21
|
+
fnRef.current = fn
|
|
22
|
+
|
|
23
|
+
const ref = React.useRef((...args: Array<any>) => fnRef.current(...args))
|
|
24
|
+
return ref.current as T
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const useLayoutEffect =
|
|
28
|
+
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Taken from https://www.developerway.com/posts/implementing-advanced-use-previous-hook#part3
|
|
32
|
+
*/
|
|
33
|
+
export function usePrevious<T>(value: T): T | null {
|
|
34
|
+
// initialise the ref with previous and current values
|
|
35
|
+
const ref = React.useRef<{ value: T; prev: T | null }>({
|
|
36
|
+
value: value,
|
|
37
|
+
prev: null,
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const current = ref.current.value
|
|
41
|
+
|
|
42
|
+
// if the value passed into hook doesn't match what we store as "current"
|
|
43
|
+
// move the "current" to the "previous"
|
|
44
|
+
// and store the passed value as "current"
|
|
45
|
+
if (value !== current) {
|
|
46
|
+
ref.current = {
|
|
47
|
+
value: value,
|
|
48
|
+
prev: current,
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// return the previous value only
|
|
53
|
+
return ref.current.prev
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* React hook to wrap `IntersectionObserver`.
|
|
58
|
+
*
|
|
59
|
+
* This hook will create an `IntersectionObserver` and observe the ref passed to it.
|
|
60
|
+
*
|
|
61
|
+
* When the intersection changes, the callback will be called with the `IntersectionObserverEntry`.
|
|
62
|
+
*
|
|
63
|
+
* @param ref - The ref to observe
|
|
64
|
+
* @param intersectionObserverOptions - The options to pass to the IntersectionObserver
|
|
65
|
+
* @param options - The options to pass to the hook
|
|
66
|
+
* @param callback - The callback to call when the intersection changes
|
|
67
|
+
* @returns The IntersectionObserver instance
|
|
68
|
+
* @example
|
|
69
|
+
* ```tsx
|
|
70
|
+
* const MyComponent = () => {
|
|
71
|
+
* const ref = React.useRef<HTMLDivElement>(null)
|
|
72
|
+
* useIntersectionObserver(
|
|
73
|
+
* ref,
|
|
74
|
+
* (entry) => { doSomething(entry) },
|
|
75
|
+
* { rootMargin: '10px' },
|
|
76
|
+
* { disabled: false }
|
|
77
|
+
* )
|
|
78
|
+
* return <div ref={ref} />
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export function useIntersectionObserver<T extends Element>(
|
|
82
|
+
ref: React.RefObject<T | null>,
|
|
83
|
+
callback: (entry: IntersectionObserverEntry | undefined) => void,
|
|
84
|
+
intersectionObserverOptions: IntersectionObserverInit = {},
|
|
85
|
+
options: { disabled?: boolean } = {},
|
|
86
|
+
) {
|
|
87
|
+
React.useEffect(() => {
|
|
88
|
+
if (
|
|
89
|
+
!ref.current ||
|
|
90
|
+
options.disabled ||
|
|
91
|
+
typeof IntersectionObserver !== 'function'
|
|
92
|
+
) {
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const observer = new IntersectionObserver(([entry]) => {
|
|
97
|
+
callback(entry)
|
|
98
|
+
}, intersectionObserverOptions)
|
|
99
|
+
|
|
100
|
+
observer.observe(ref.current)
|
|
101
|
+
|
|
102
|
+
return () => {
|
|
103
|
+
observer.disconnect()
|
|
104
|
+
}
|
|
105
|
+
}, [callback, intersectionObserverOptions, options.disabled, ref])
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* React hook to take a `React.ForwardedRef` and returns a `ref` that can be used on a DOM element.
|
|
110
|
+
*
|
|
111
|
+
* @param ref - The forwarded ref
|
|
112
|
+
* @returns The inner ref returned by `useRef`
|
|
113
|
+
* @example
|
|
114
|
+
* ```tsx
|
|
115
|
+
* const MyComponent = React.forwardRef((props, ref) => {
|
|
116
|
+
* const innerRef = useForwardedRef(ref)
|
|
117
|
+
* return <div ref={innerRef} />
|
|
118
|
+
* })
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export function useForwardedRef<T>(ref?: React.ForwardedRef<T>) {
|
|
122
|
+
const innerRef = React.useRef<T>(null)
|
|
123
|
+
React.useImperativeHandle(ref, () => innerRef.current!, [])
|
|
124
|
+
return innerRef
|
|
125
|
+
}
|