Skip to content

Commit f055f54

Browse files
authored
feat: rework links (#280)
now the `href` receivers: - Button - IconButton - HoverableLink do not require additional `onClick` handlers `href` prop just works and `noreferrer noopener` was implemented
1 parent 41a4ba3 commit f055f54

File tree

6 files changed

+85
-40
lines changed

6 files changed

+85
-40
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
},
3636
"peerDependencies": {
3737
"react": "^16.14.0",
38-
"react-dom": "^16.14.0"
38+
"react-dom": "^16.14.0",
39+
"react-router-dom": "^6.3.0"
3940
},
4041
"devDependencies": {
4142
"@babel/core": "^7.17.5",
@@ -82,6 +83,7 @@
8283
"prettier": "^2.5.1",
8384
"react": "^16.14.0",
8485
"react-dom": "^16.14.0",
86+
"react-router-dom": "^6.3.0",
8587
"semantic-release": "^19.0.2",
8688
"storybook-dark-mode": "^1.1.0",
8789
"typescript": "^4.6.2",

src/components/button/index.tsx

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import * as R from 'ramda';
12
import React, { FC } from 'react';
23
import { Button as RebassButton, ButtonProps } from 'rebass';
3-
import * as R from 'ramda';
44

55
// Styles
6-
import styles, { spinnerColor } from './button.styles';
6+
import { Link } from 'react-router-dom';
77
import Spinner from '../spinner';
8+
import styles, { spinnerColor } from './button.styles';
89

9-
import { GetIcon, IconName } from '../icon';
1010
import { Color } from '../../theme/types';
11+
import { GetIcon, IconName } from '../icon';
1112

1213
type Intent = 'primary' | 'secondary' | 'ghost' | 'inline' | 'alert';
1314
export interface QuartzButtonProps extends Omit<ButtonProps, 'css'> {
@@ -27,6 +28,7 @@ const Button: FC<QuartzButtonProps> = ({
2728
disabled,
2829
isLoading,
2930
loadingOnly,
31+
target,
3032
...props
3133
}: QuartzButtonProps) => {
3234
const test = { ...props };
@@ -49,17 +51,16 @@ const Button: FC<QuartzButtonProps> = ({
4951

5052
if (href) {
5153
return (
52-
<a
54+
<Link
5355
style={{
5456
textDecoration: 'none',
5557
}}
56-
onClick={(e) => {
57-
e.preventDefault();
58-
}}
59-
href={href}
58+
to={href}
59+
target={target}
60+
{...(target === '_blank' ? { rel: 'noopener noreferrer' } : {})}
6061
>
6162
{component}
62-
</a>
63+
</Link>
6364
);
6465
}
6566

src/components/icon-button/index.tsx

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { FC } from 'react';
22
import { Button as RebassButton, ButtonProps } from 'rebass';
3+
import { Link } from 'react-router-dom';
34

45
// Components
56
import Tooltip, { TooltipProps } from '../tooltip';
67

7-
// Styles
8+
import { getIcon, IconName } from '../icon/list';
89
import styles from './icon-button.styles';
9-
import { IconName, getIcon } from '../icon/list';
1010

1111
export interface IconButtonProps extends Omit<ButtonProps, 'css'> {
1212
intent?: 'primary' | 'ghost' | 'ghost-white';
@@ -26,6 +26,7 @@ const IconButton: FC<IconButtonProps> = ({
2626
tooltipProps,
2727
href,
2828
onClickIcon,
29+
target,
2930
...props
3031
}: IconButtonProps) => {
3132
let component;
@@ -57,28 +58,20 @@ const IconButton: FC<IconButtonProps> = ({
5758

5859
if (href) {
5960
return (
60-
<a
61+
<Link
6162
style={{
6263
textDecoration: 'none',
6364
}}
6465
onClick={onClickIcon}
65-
href={href}
66+
to={href}
67+
target={target}
68+
{...(target === '_blank' ? { rel: 'noopener noreferrer' } : {})}
6669
>
6770
{component}
68-
</a>
71+
</Link>
6972
);
7073
}
71-
return (
72-
<a
73-
style={{
74-
textDecoration: 'none',
75-
}}
76-
onClick={onClickIcon}
77-
href={href}
78-
>
79-
{component}
80-
</a>
81-
);
74+
return component;
8275
};
8376

8477
export default IconButton;

src/components/navigation/item/index.tsx

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Box } from 'rebass';
1+
import * as R from 'ramda';
22
import React, {
33
FC,
44
memo,
@@ -8,16 +8,17 @@ import React, {
88
useEffect,
99
useMemo,
1010
} from 'react';
11-
import * as R from 'ramda';
11+
import { Box } from 'rebass';
12+
import { Link } from 'react-router-dom';
1213
import { useTheme } from '../../../theme/theme';
1314

1415
// Context
1516
import NavigationContext from '../context/navigation.context';
1617
import { NavigationItemProps } from '../types';
1718
// Styles
18-
import styles from './navigation-item.styles';
1919
import Tooltip from '../../tooltip';
2020
import TooltipPositions from '../../tooltip/positions';
21+
import styles from './navigation-item.styles';
2122

2223
const getVariant = (isDisabled = false, isActive = false): string => {
2324
if (isDisabled) {
@@ -127,17 +128,15 @@ const NavigationItem: FC<NavigationItemProps> = (
127128
}}
128129
>
129130
{href ? (
130-
<a
131+
<Link
131132
style={{
132133
textDecoration: 'none',
133134
}}
134-
onClick={(e) => {
135-
e.preventDefault();
136-
}}
137-
href={href}
135+
onClick={(e) => e.stopPropagation()}
136+
to={href}
138137
>
139138
{component}
140-
</a>
139+
</Link>
141140
) : (
142141
component
143142
)}

src/components/typography/hoverable.tsx

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { FC } from 'react';
2-
import { Text, TextProps, Link, LinkProps } from 'rebass';
2+
import { Link } from 'react-router-dom';
3+
import { Text, TextProps, LinkProps, Link as RebassLink } from 'rebass';
34

45
export interface HoverableTextProps extends Omit<TextProps, 'css'> {}
56
export interface HoverableLinkProps extends Omit<LinkProps, 'css'> {}
@@ -20,10 +21,24 @@ export const HoverableText: FC<HoverableTextProps> = (
2021
return <Text {...props} variant="title" sx={sx} />;
2122
};
2223

23-
export const HoverableLink: FC<HoverableLinkProps> = (
24-
props: HoverableLinkProps,
25-
) => {
24+
export const HoverableLink: FC<HoverableLinkProps> = ({
25+
href,
26+
target,
27+
...props
28+
}: HoverableLinkProps) => {
2629
let { sx } = { ...props };
2730
sx = { ...sx, ...styles };
28-
return <Link {...props} variant="title" sx={sx} />;
31+
32+
return (
33+
<RebassLink
34+
as={Link}
35+
{...props}
36+
// @ts-ignore
37+
to={href}
38+
variant="title"
39+
sx={sx}
40+
target={target}
41+
{...(target === '_blank' ? { rel: 'noopener noreferrer' } : {})}
42+
/>
43+
);
2944
};

yarn.lock

+35
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,7 @@ __metadata:
26832683
react-codemirror2: ^7.2.1
26842684
react-datepicker: ^3.3.0
26852685
react-dom: ^16.14.0
2686+
react-router-dom: ^6.3.0
26862687
react-spring: ^8.0.27
26872688
react-syntax-highlighter: ^15.4.3
26882689
rebass: ^4.0.7
@@ -2694,6 +2695,7 @@ __metadata:
26942695
peerDependencies:
26952696
react: ^16.14.0
26962697
react-dom: ^16.14.0
2698+
react-router-dom: ^6.3.0
26972699
languageName: unknown
26982700
linkType: soft
26992701

@@ -12061,6 +12063,15 @@ __metadata:
1206112063
languageName: node
1206212064
linkType: hard
1206312065

12066+
"history@npm:^5.2.0":
12067+
version: 5.3.0
12068+
resolution: "history@npm:5.3.0"
12069+
dependencies:
12070+
"@babel/runtime": ^7.7.6
12071+
checksum: d73c35df49d19ac172f9547d30a21a26793e83f16a78386d99583b5bf1429cc980799fcf1827eb215d31816a6600684fba9686ce78104e23bd89ec239e7c726f
12072+
languageName: node
12073+
linkType: hard
12074+
1206412075
"hmac-drbg@npm:^1.0.1":
1206512076
version: 1.0.1
1206612077
resolution: "hmac-drbg@npm:1.0.1"
@@ -18751,6 +18762,30 @@ __metadata:
1875118762
languageName: node
1875218763
linkType: hard
1875318764

18765+
"react-router-dom@npm:^6.3.0":
18766+
version: 6.3.0
18767+
resolution: "react-router-dom@npm:6.3.0"
18768+
dependencies:
18769+
history: ^5.2.0
18770+
react-router: 6.3.0
18771+
peerDependencies:
18772+
react: ">=16.8"
18773+
react-dom: ">=16.8"
18774+
checksum: 77603a654f8a8dc7f65535a2e5917a65f8d9ffcb06546d28dd297e52adcc4b8a84377e0115f48dca330b080af2da3e78f29d590c89307094d36927d2b1751ec3
18775+
languageName: node
18776+
linkType: hard
18777+
18778+
"react-router@npm:6.3.0":
18779+
version: 6.3.0
18780+
resolution: "react-router@npm:6.3.0"
18781+
dependencies:
18782+
history: ^5.2.0
18783+
peerDependencies:
18784+
react: ">=16.8"
18785+
checksum: 7be673f5e72104be01e6ab274516bdb932efd93305243170690f6560e3bd1035dd1df3d3c9ce1e0f452638a2529f43a1e77dcf0934fc8031c4783da657be13ca
18786+
languageName: node
18787+
linkType: hard
18788+
1875418789
"react-spring@npm:^8.0.27":
1875518790
version: 8.0.27
1875618791
resolution: "react-spring@npm:8.0.27"

0 commit comments

Comments
 (0)