Skip to content
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

fix: allow activeProps to infer component props #2347

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
63 changes: 33 additions & 30 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,9 @@ export function useLinkProps<
}

return {
...rest,
...resolvedActiveProps,
...resolvedInactiveProps,
...rest,
href: disabled
? undefined
: next.maskedLocation
Expand All @@ -846,50 +846,65 @@ export function useLinkProps<
}
}

type UseLinkReactProps<TComp> = TComp extends keyof JSX.IntrinsicElements
? JSX.IntrinsicElements[TComp]
: React.PropsWithoutRef<
TComp extends React.ComponentType<infer TProps> ? TProps : never
> &
React.RefAttributes<
TComp extends
| React.FC<{ ref: infer TRef }>
| React.Component<{ ref: infer TRef }>
? TRef
: never
>

export type UseLinkPropsOptions<
TRouter extends AnyRouter = RegisteredRouter,
TFrom extends RoutePaths<TRouter['routeTree']> | string = string,
TTo extends string | undefined = '.',
TMaskFrom extends RoutePaths<TRouter['routeTree']> | string = TFrom,
TMaskTo extends string = '.',
> = ActiveLinkOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> &
React.AnchorHTMLAttributes<HTMLAnchorElement>
> = ActiveLinkOptions<'a', TRouter, TFrom, TTo, TMaskFrom, TMaskTo> &
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

about the 'a': please see this comment #2092 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works with 'a' properly now ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool!
@mirague check this out

UseLinkReactProps<'a'>

export type ActiveLinkOptions<
TComp = 'a',
TRouter extends AnyRouter = RegisteredRouter,
TFrom extends string = string,
TTo extends string | undefined = '.',
TMaskFrom extends string = TFrom,
TMaskTo extends string = '.',
> = LinkOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & ActiveLinkOptionProps
> = LinkOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> &
ActiveLinkOptionProps<TComp>

type ActiveLinkAnchorProps = Omit<
React.AnchorHTMLAttributes<HTMLAnchorElement> & {
type ActiveLinkProps<TComp> = Partial<
LinkComponentReactProps<TComp> & {
[key: `data-${string}`]: unknown
},
'children'
}
>

export interface ActiveLinkOptionProps {
export interface ActiveLinkOptionProps<TComp = 'a'> {
/**
* A function that returns additional props for the `active` state of this link.
* These props override other props passed to the link (`style`'s are merged, `className`'s are concatenated)
*/
activeProps?: ActiveLinkAnchorProps | (() => ActiveLinkAnchorProps)
activeProps?: ActiveLinkProps<TComp> | (() => ActiveLinkProps<TComp>)
/**
* A function that returns additional props for the `inactive` state of this link.
* These props override other props passed to the link (`style`'s are merged, `className`'s are concatenated)
*/
inactiveProps?: ActiveLinkAnchorProps | (() => ActiveLinkAnchorProps)
inactiveProps?: ActiveLinkProps<TComp> | (() => ActiveLinkProps<TComp>)
}

export type LinkProps<
TComp = 'a',
TRouter extends AnyRouter = RegisteredRouter,
TFrom extends string = string,
TTo extends string | undefined = '.',
TMaskFrom extends string = TFrom,
TMaskTo extends string = '.',
> = ActiveLinkOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> &
> = ActiveLinkOptions<TComp, TRouter, TFrom, TTo, TMaskFrom, TMaskTo> &
LinkPropsChildren

export interface LinkPropsChildren {
Expand All @@ -902,32 +917,20 @@ export interface LinkPropsChildren {
}) => React.ReactNode)
}

type LinkComponentReactProps<TComp> = React.PropsWithoutRef<
TComp extends React.FC<infer TProps> | React.Component<infer TProps>
? TProps
: TComp extends keyof React.JSX.IntrinsicElements
? Omit<React.HTMLProps<TComp>, 'children' | 'preload'>
: never
> &
React.RefAttributes<
TComp extends
| React.FC<{ ref: infer TRef }>
| React.Component<{ ref: infer TRef }>
? TRef
: TComp extends keyof React.JSX.IntrinsicElements
? React.ComponentRef<TComp>
: never
>
type LinkComponentReactProps<TComp> = Omit<
UseLinkReactProps<TComp>,
'children' | 'preload'
>

export type LinkComponentProps<
TComp,
TComp = 'a',
TRouter extends AnyRouter = RegisteredRouter,
TFrom extends string = string,
TTo extends string | undefined = '.',
TMaskFrom extends string = TFrom,
TMaskTo extends string = '.',
> = LinkComponentReactProps<TComp> &
LinkProps<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
LinkProps<TComp, TRouter, TFrom, TTo, TMaskFrom, TMaskTo>

export type LinkComponent<TComp> = <
TRouter extends RegisteredRouter = RegisteredRouter,
Expand Down
89 changes: 89 additions & 0 deletions packages/react-router/tests/link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3710,4 +3710,93 @@ describe('createLink', () => {
expect(customElement.hasAttribute('foo')).toBe(true)
expect(customElement.getAttribute('foo')).toBe('bar')
})

it('should pass activeProps and inactiveProps to the custom link', async () => {
const Button: React.FC<
React.PropsWithChildren<{
active?: boolean
foo?: boolean
overrideMeIfYouWant: string
}>
> = ({ active, foo, children, ...props }) => (
<button {...props}>
active: {active ? 'yes' : 'no'} - foo: {foo ? 'yes' : 'no'} - {children}
</button>
)

const ButtonLink = createLink(Button)

const rootRoute = createRootRoute()
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => (
<>
<ButtonLink
to="/"
overrideMeIfYouWant="Button1"
activeProps={{
active: true,
'data-hello': 'world',
overrideMeIfYouWant: 'overridden-by-activeProps',
}}
inactiveProps={{ foo: true }}
>
Button1
</ButtonLink>
<ButtonLink
to="/posts"
overrideMeIfYouWant="Button2"
activeProps={{
active: true,
'data-hello': 'world',
}}
inactiveProps={{
foo: true,
'data-hello': 'void',
overrideMeIfYouWant: 'overridden-by-inactiveProps',
}}
>
Button2
</ButtonLink>
<ButtonLink
to="/posts"
overrideMeIfYouWant="Button3"
activeProps={{
active: true,
}}
inactiveProps={{
active: false,
}}
>
Button3
</ButtonLink>
</>
),
})
const postsRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/posts',
})
const router = createRouter({
routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
})

render(<RouterProvider router={router} />)

const button1 = await screen.findByText('active: yes - foo: no - Button1')
expect(button1.getAttribute('data-hello')).toBe('world')
expect(button1.getAttribute('overrideMeIfYouWant')).toBe(
'overridden-by-activeProps',
)

const button2 = await screen.findByText('active: no - foo: yes - Button2')
expect(button2.getAttribute('data-hello')).toBe('void')
expect(button2.getAttribute('overrideMeIfYouWant')).toBe(
'overridden-by-inactiveProps',
)

const button3 = await screen.findByText('active: no - foo: no - Button3')
expect(button3.getAttribute('overrideMeIfYouWant')).toBe('Button3')
})
})