Skip to content

feat: add bindings for Route.Link and routeApi.Link #4095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,12 @@ export type CreateLinkProps = LinkProps<
string
>

export type LinkComponent<TComp> = <
export type LinkComponent<
in out TComp,
in out TDefaultFrom extends string = string,
> = <
TRouter extends AnyRouter = RegisteredRouter,
const TFrom extends string = string,
const TFrom extends string = TDefaultFrom,
const TTo extends string | undefined = undefined,
const TMaskFrom extends string = TFrom,
const TMaskTo extends string = '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
BaseRouteApi,
notFound,
} from '@tanstack/router-core'
import React from 'react'
import { useLoaderData } from './useLoaderData'
import { useLoaderDeps } from './useLoaderDeps'
import { useParams } from './useParams'
import { useSearch } from './useSearch'
import { useNavigate } from './useNavigate'
import { useMatch } from './useMatch'
import { useRouter } from './useRouter'
import { Link } from './link'
import type {
AnyContext,
AnyRoute,
Expand Down Expand Up @@ -39,8 +41,8 @@ import type { UseMatchRoute } from './useMatch'
import type { UseLoaderDepsRoute } from './useLoaderDeps'
import type { UseParamsRoute } from './useParams'
import type { UseSearchRoute } from './useSearch'
import type * as React from 'react'
import type { UseRouteContextRoute } from './useRouteContext'
import type { LinkComponent } from './link'

declare module '@tanstack/router-core' {
export interface UpdatableRouteOptionsExtensions {
Expand All @@ -61,6 +63,7 @@ declare module '@tanstack/router-core' {
useLoaderDeps: UseLoaderDepsRoute<TId>
useLoaderData: UseLoaderDataRoute<TId>
useNavigate: () => UseNavigateResult<TFullPath>
Link: LinkComponent<'a', TFullPath>
}
}

Expand Down Expand Up @@ -133,6 +136,16 @@ export class RouteApi<
notFound = (opts?: NotFoundError) => {
return notFound({ routeId: this.id as string, ...opts })
}

Link: LinkComponent<'a', RouteTypesById<TRouter, TId>['fullPath']> =
React.forwardRef((props, ref: React.ForwardedRef<HTMLAnchorElement>) => {
const router = useRouter()
const fullPath = router.routesById[this.id as string].fullPath
return <Link ref={ref} from={fullPath as never} {...props} />
}) as unknown as LinkComponent<
'a',
RouteTypesById<TRouter, TId>['fullPath']
>
}

export class Route<
Expand Down Expand Up @@ -241,6 +254,19 @@ export class Route<
useNavigate = (): UseNavigateResult<TFullPath> => {
return useNavigate({ from: this.fullPath })
}

Link: LinkComponent<'a', TFullPath> = React.forwardRef(
(props, ref: React.ForwardedRef<HTMLAnchorElement>) => {
const router = useRouter()
return (
<Link
ref={ref}
from={router.routesById[this.id].fullPath as never}
{...props}
/>
)
},
) as unknown as LinkComponent<'a', TFullPath>
}

export function createRoute<
Expand Down Expand Up @@ -426,6 +452,19 @@ export class RootRoute<
useNavigate = (): UseNavigateResult<'/'> => {
return useNavigate({ from: this.fullPath })
}

Link: LinkComponent<'a', '/'> = React.forwardRef(
(props, ref: React.ForwardedRef<HTMLAnchorElement>) => {
const router = useRouter()
return (
<Link
ref={ref}
from={router.routesById[this.id].fullPath as never}
{...props}
/>
)
},
) as unknown as LinkComponent<'a', '/'>
}

export function createRootRoute<
Expand Down
8 changes: 7 additions & 1 deletion packages/react-router/tests/routeApi.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expectTypeOf, test } from 'vitest'
import { createRootRoute, createRoute, createRouter, getRouteApi } from '../src'
import type { MakeRouteMatch, UseNavigateResult } from '../src'
import type { LinkComponent, MakeRouteMatch, UseNavigateResult } from '../src'

const rootRoute = createRootRoute()

Expand Down Expand Up @@ -87,6 +87,12 @@ describe('getRouteApi', () => {
MakeRouteMatch<typeof routeTree, '/invoices/$invoiceId'>
>()
})
test('Link', () => {
const Link = invoiceRouteApi.Link
expectTypeOf(Link).toEqualTypeOf<
LinkComponent<'a', '/invoices/$invoiceId'>
>()
})
})

describe('createRoute', () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/solid-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,12 @@ export type CreateLinkProps = LinkProps<
string
>

export type LinkComponent<TComp> = <
export type LinkComponent<
in out TComp,
in out TDefaultFrom extends string = string,
> = <
TRouter extends AnyRouter = RegisteredRouter,
const TFrom extends string = string,
const TFrom extends string = TDefaultFrom,
const TTo extends string | undefined = undefined,
const TMaskFrom extends string = TFrom,
const TMaskTo extends string = '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BaseRouteApi,
notFound,
} from '@tanstack/router-core'
import { Link } from './link'
import { useLoaderData } from './useLoaderData'
import { useLoaderDeps } from './useLoaderDeps'
import { useParams } from './useParams'
Expand Down Expand Up @@ -41,6 +42,7 @@ import type { UseParamsRoute } from './useParams'
import type { UseSearchRoute } from './useSearch'
import type * as Solid from 'solid-js'
import type { UseRouteContextRoute } from './useRouteContext'
import type { LinkComponent } from './link'

declare module '@tanstack/router-core' {
export interface UpdatableRouteOptionsExtensions {
Expand All @@ -61,6 +63,7 @@ declare module '@tanstack/router-core' {
useLoaderDeps: UseLoaderDepsRoute<TId>
useLoaderData: UseLoaderDataRoute<TId>
useNavigate: () => UseNavigateResult<TFullPath>
Link: LinkComponent<'a', TFullPath>
}
}

Expand Down Expand Up @@ -128,6 +131,14 @@ export class RouteApi<
notFound = (opts?: NotFoundError) => {
return notFound({ routeId: this.id as string, ...opts })
}

Link: LinkComponent<'a', RouteTypesById<TRouter, TId>['fullPath']> = (
props,
) => {
const router = useRouter()
const fullPath = router.routesById[this.id as string].fullPath
return <Link from={fullPath as never} {...props} />
}
}

export class Route<
Expand Down Expand Up @@ -230,6 +241,12 @@ export class Route<
useNavigate = (): UseNavigateResult<TFullPath> => {
return useNavigate({ from: this.fullPath })
}

Link: LinkComponent<'a', TFullPath> = (props) => {
const router = useRouter()
const fullPath = router.routesById[this.id as string].fullPath
return <Link from={fullPath as never} {...props} />
}
}

export function createRoute<
Expand Down Expand Up @@ -411,6 +428,12 @@ export class RootRoute<
useNavigate = (): UseNavigateResult<'/'> => {
return useNavigate({ from: this.fullPath })
}

Link: LinkComponent<'a', '/'> = (props) => {
const router = useRouter()
const fullPath = router.routesById[this.id as string].fullPath
return <Link from={fullPath as never} {...props} />
}
}

export function createRouteMask<
Expand Down
7 changes: 7 additions & 0 deletions packages/solid-router/tests/routeApi.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expectTypeOf, test } from 'vitest'
import { createRootRoute, createRoute, createRouter, getRouteApi } from '../src'
import type { LinkComponent } from '../src'
import type { Accessor } from 'solid-js'
import type { MakeRouteMatch, UseNavigateResult } from '@tanstack/router-core'

Expand Down Expand Up @@ -96,6 +97,12 @@ describe('getRouteApi', () => {
Accessor<MakeRouteMatch<typeof routeTree, '/invoices/$invoiceId'>>
>()
})
test('Link', () => {
const Link = invoiceRouteApi.Link
expectTypeOf(Link).toEqualTypeOf<
LinkComponent<'a', '/invoices/$invoiceId'>
>()
})
})

describe('createRoute', () => {
Expand Down