Skip to content

Commit 44ed118

Browse files
authored
chore: add deprecation tag to 'to' prop on Link primitive (#2265)
* chore: deprecate 'to' prop on Link primitive * chore: update yarn.lock * Create slimy-pugs-travel.md * chore: address comments * chore: add space to warning message to remove LGTM alert
1 parent 74e8c89 commit 44ed118

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

.changeset/slimy-pugs-travel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@aws-amplify/ui-react": patch
3+
---
4+
5+
chore: add deprecation tag to 'to' prop on Link primitive

docs/src/pages/[platform]/components/link/props-table.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ string
106106
string
107107
```
108108
</TableCell>
109-
<TableCell className="props-table__tr-description">A string representation of the URL path</TableCell>
109+
<TableCell className="props-table__tr-description">The Link component's to prop will soon be deprecated. Please see the Amplify UI documentation for using the Link component with routing libraries: https://ui.docs.amplify.aws/react/components/link#routing-libraries A string representation of the URL path</TableCell>
110110
</TableRow>
111111

112112
</TableBody>

docs/src/pages/[platform]/components/link/react.mdx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ To create a Link which opens in a new tab, use the `isExternal` prop. Under the
4747

4848
### Routing Libraries
4949

50-
To use the Link with React routing libraries, pass in routing components to the `as` prop. Below is an example using React Router:
50+
You can use a Link with any React routing library that supports custom components. Below is an example using Link with React Router v5, in which the Link is passed to the `component` prop as a custom navigation component:
5151

5252
<Example>
5353
<ExampleCode>
@@ -61,13 +61,25 @@ To use the Link with React routing libraries, pass in routing components to the
6161
Route,
6262
} from 'react-router-dom';
6363

64+
function Home() {
65+
return <Heading level={2}>Home</Heading>;
66+
}
67+
68+
function About() {
69+
return <Heading level={2}>About</Heading>;
70+
}
71+
72+
function Users() {
73+
return <Heading level={2}>Users</Heading>;
74+
}
75+
6476
function App() {
6577
return (
6678
<Router>
6779
<Flex>
68-
<Link as={ReactRouterLink} to="/">Home</Link>
69-
<Link as={ReactRouterLink} to="/about">About</Link>
70-
<Link as={ReactRouterLink} to="/users">Users</Link>
80+
<ReactRouterLink to="/" component={Link}>Home</ReactRouterLink>
81+
<ReactRouterLink to="/about" component={Link}>About</ReactRouterLink>
82+
<ReactRouterLink to="/users" component={Link}>Users</ReactRouterLink>
7183
</Flex>
7284

7385
<Routes>
@@ -79,18 +91,6 @@ To use the Link with React routing libraries, pass in routing components to the
7991
);
8092
}
8193

82-
function Home() {
83-
return <Heading level={2}>Home</Heading>;
84-
}
85-
86-
function About() {
87-
return <Heading level={2}>About</Heading>;
88-
}
89-
90-
function Users() {
91-
return <Heading level={2}>Users</Heading>;
92-
}
93-
9494
export default App;
9595
```
9696
</ExampleCode>

packages/react/src/primitives/Link/Link.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ import classNames from 'classnames';
44
import { ComponentClassNames } from '../shared';
55
import { LinkProps, Primitive } from '../types';
66
import { View } from '../View';
7+
import { useDeprecationWarning } from '../../hooks/useDeprecationWarning';
78

89
const LinkPrimitive: Primitive<LinkProps, 'a'> = (
910
{ as = 'a', children, className, isExternal, to, ...rest },
1011
ref
1112
) => {
13+
useDeprecationWarning({
14+
shouldWarn: to != null,
15+
message:
16+
"The Link component's to prop will soon be deprecated. " +
17+
'Please see the Amplify UI documentation for using the Link component with routing libraries: ' +
18+
'https://ui.docs.amplify.aws/react/components/link#routing-libraries',
19+
});
1220
return (
1321
<View
1422
as={as}

packages/react/src/primitives/Link/__tests__/Link.test.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import * as React from 'react';
2-
import kebabCase from 'lodash/kebabCase';
32
import { render, screen } from '@testing-library/react';
43
import userEvent from '@testing-library/user-event';
54

65
import { ComponentClassNames } from '../../shared';
7-
import { ComponentPropsToStylePropsMap } from '../../types';
86
import { Link } from '../Link';
97
import { Text } from '../../Text/Text';
108
import { Flex } from '../../Flex';
@@ -117,4 +115,11 @@ describe('Link: ', () => {
117115

118116
expect(screen.getByText(/you are on the about page/i)).toBeInTheDocument();
119117
});
118+
119+
it('should call console.warn if "to" prop is used', async () => {
120+
const spyWarn = jest.spyOn(console, 'warn');
121+
render(<Link to="/test">Test</Link>);
122+
expect(spyWarn).toHaveBeenCalled();
123+
spyWarn.mockRestore();
124+
});
120125
});

packages/react/src/primitives/types/link.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export interface LinkOptions {
1616
isExternal?: boolean;
1717

1818
/**
19+
* @deprecated
20+
* The Link component's to prop will soon be deprecated.
21+
* Please see the Amplify UI documentation for using the Link component with routing libraries:
22+
* https://ui.docs.amplify.aws/react/components/link#routing-libraries
1923
* @description
2024
* A string representation of the URL path
2125
*/

0 commit comments

Comments
 (0)