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

[core] Use router specific Link components #4661

Merged
merged 11 commits into from
Feb 24, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function Content({ router }) {

Content.propTypes = {
router: PropTypes.shape({
Link: PropTypes.func,
navigate: PropTypes.func.isRequired,
pathname: PropTypes.string.isRequired,
searchParams: PropTypes.instanceOf(URLSearchParams).isRequired,
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/toolpad/core/api/app-provider.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"router": {
"type": {
"name": "shape",
"description": "{ navigate: func, pathname: string, searchParams: URLSearchParams }"
"description": "{ Link?: func, navigate: func, pathname: string, searchParams: URLSearchParams }"
},
"default": "null"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/toolpad-core/src/AppProvider/AppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
RouterContext,
WindowContext,
} from '../shared/context';
import type { LinkProps } from '../shared/Link';
import { AppThemeProvider } from './AppThemeProvider';

export interface NavigateOptions {
Expand All @@ -27,6 +28,7 @@ export interface Router {
pathname: string;
searchParams: URLSearchParams;
navigate: Navigate;
Link?: React.ComponentType<LinkProps>;
}

export interface Branding {
Expand Down Expand Up @@ -242,6 +244,7 @@ AppProvider.propTypes /* remove-proptypes */ = {
* @default null
*/
router: PropTypes.shape({
Link: PropTypes.func,
navigate: PropTypes.func.isRequired,
pathname: PropTypes.string.isRequired,
searchParams: PropTypes.instanceOf(URLSearchParams).isRequired,
Expand Down
2 changes: 2 additions & 0 deletions packages/toolpad-core/src/nextjs/NextAppProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ vi.mock('next/router', () => ({ useRouter: () => null }));

vi.mock('next/compat/router', () => ({ useRouter: () => null }));

vi.mock('next/link', () => ({ default: () => null }));

interface RouterTestProps {
children: React.ReactNode;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/toolpad-core/src/nextjs/NextAppProviderApp.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import Link from 'next/link';
import { usePathname, useSearchParams, useRouter } from 'next/navigation';
import { AppProvider } from '../AppProvider';
import type { AppProviderProps, Navigate, Router } from '../AppProvider';
Expand Down Expand Up @@ -29,6 +30,7 @@ export function NextAppProviderApp(props: AppProviderProps) {
pathname,
searchParams,
navigate,
Link,
}),
[pathname, navigate, searchParams],
);
Expand Down
2 changes: 2 additions & 0 deletions packages/toolpad-core/src/nextjs/NextAppProviderPages.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import Link from 'next/link';
import { asArray } from '@toolpad/utils/collections';
import { useRouter } from 'next/router';
import { AppProvider } from '../AppProvider';
Expand Down Expand Up @@ -41,6 +42,7 @@ export function NextAppProviderPages(props: AppProviderProps) {
pathname,
searchParams,
navigate,
Link,
Copy link
Member

Choose a reason for hiding this comment

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

I believe this needs to handle the history prop?

}),
[navigate, pathname, searchParams],
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
'use client';
import * as React from 'react';
import { useSearchParams, useLocation, useNavigate } from 'react-router';
import { useSearchParams, useLocation, useNavigate, Link as ReactRouterLink } from 'react-router';
import { AppProvider, type AppProviderProps, Navigate, Router } from '../AppProvider/AppProvider';
import { LinkProps } from '../shared/Link';

const Link = React.forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => {
const { href, ...rest } = props;
Copy link
Member

Choose a reason for hiding this comment

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

I believe this should also handle the history prop?

return <ReactRouterLink ref={ref} to={href} {...rest} />;
});

function ReactRouterAppProvider(props: AppProviderProps) {
const { pathname } = useLocation();
Expand All @@ -26,6 +32,7 @@ function ReactRouterAppProvider(props: AppProviderProps) {
pathname,
searchParams,
navigate: navigateImpl,
Link,
}),
[pathname, searchParams, navigateImpl],
);
Expand Down
27 changes: 24 additions & 3 deletions packages/toolpad-core/src/shared/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { RouterContext } from './context';
* @ignore - internal component.
*/

export interface LinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
export interface DefaultLinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
Copy link
Member

Choose a reason for hiding this comment

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

Can't this just be the LinkProps that we use both for DefaultLink and Link?

Suggested change
export interface DefaultLinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
export interface LinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {

history?: 'auto' | 'push' | 'replace';
href: string;
}

export const Link = React.forwardRef(function Link(
props: LinkProps,
export const DefaultLink = React.forwardRef(function Link(
props: DefaultLinkProps,
ref: React.ForwardedRef<HTMLAnchorElement>,
) {
const { children, href, onClick, history, ...rest } = props;
Expand All @@ -34,3 +35,23 @@ export const Link = React.forwardRef(function Link(
</a>
);
});

export interface LinkProps {
href: string;
children: React.ReactNode;
onClick?: React.MouseEventHandler<HTMLAnchorElement>;
style?: React.CSSProperties;
}

export const Link = React.forwardRef(function Link(
props: LinkProps,
ref: React.ForwardedRef<HTMLAnchorElement>,
) {
const routerContext = React.useContext(RouterContext);
const LinkComponent = routerContext?.Link ?? DefaultLink;
return (
<LinkComponent ref={ref} {...props}>
{props.children}
</LinkComponent>
);
});