Skip to content

Commit

Permalink
fix: Accept ref for next-intl/link (#300)
Browse files Browse the repository at this point in the history
Fixes #299
  • Loading branch information
amannn authored May 22, 2023
1 parent 720e6ac commit 4d7cc17
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/next-intl/src/link/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {ComponentProps} from 'react';
import React, {ComponentProps, forwardRef} from 'react';
import useClientLocale from '../client/useClientLocale';
import BaseLink from '../shared/BaseLink';

Expand Down Expand Up @@ -26,7 +26,9 @@ type Props = Omit<ComponentProps<typeof BaseLink>, 'locale'> & {
* the `set-cookie` response header would cause the locale cookie on the current
* page to be overwritten before the user even decides to change the locale.
*/
export default function Link({locale, ...rest}: Props) {
function Link({locale, ...rest}: Props, ref: Props['ref']) {
const defaultLocale = useClientLocale();
return <BaseLink locale={locale || defaultLocale} {...rest} />;
return <BaseLink ref={ref} locale={locale || defaultLocale} {...rest} />;
}

export default forwardRef(Link);
17 changes: 17 additions & 0 deletions packages/next-intl/test/link/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ describe('unprefixed routing', () => {
'https://example.com/test'
);
});

it('can receive a ref', () => {
let ref;

render(
<Link
ref={(node) => {
ref = node;
}}
href="/test"
>
Test
</Link>
);

expect(ref).toBeDefined();
});
});

describe('prefixed routing', () => {
Expand Down

2 comments on commit 4d7cc17

@vercel
Copy link

@vercel vercel bot commented on 4d7cc17 May 22, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

example-next-13-next-auth – ./examples/example-next-13-next-auth

example-next-13-next-auth.vercel.app
example-next-13-next-auth-next-intl.vercel.app
example-next-13-next-auth-git-main-next-intl.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4d7cc17 May 22, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

next-intl-example-next-13 – ./examples/example-next-13

next-intl-example-next-13-git-main-next-intl.vercel.app
next-intl-example-next-13.vercel.app
next-intl-example-next-13-next-intl.vercel.app

Please sign in to comment.