generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLink.tsx
74 lines (65 loc) · 1.95 KB
/
Link.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// this component could potentially be lifted with others into the DSR in a later refactor
// see: https://github.com/cfpb/sbl-frontend/issues/200
import type { LinkProperties as DesignSystemReactLinkProperties } from 'design-system-react';
import {
Link as DesignSystemReactLink,
LinkText,
ListItem,
} from 'design-system-react';
import {
IconExternalLink,
isExternalLinkImplied,
isNewTabImplied,
} from './Link.utils';
interface LinkProperties extends DesignSystemReactLinkProperties {
// design system react's Link component correctly allows undefined values without defaultProps
/* eslint-disable react/require-default-props */
href?: string | undefined;
isRouterLink?: boolean | undefined;
isExternalLink?: boolean | undefined;
isNewTab?: boolean | undefined;
target?: string | undefined;
/* eslint-enable react/require-default-props */
}
export function Link({
children,
href,
isRouterLink,
isNewTab,
className,
isExternalLink,
...others
}: LinkProperties): JSX.Element {
const otherProperties: LinkProperties = { ...others };
let icon;
// Open link in new tab
const openInNewTab = isNewTab ?? isNewTabImplied(href);
if (openInNewTab) otherProperties.target = '_blank';
// Treat as an External link
const treatExternal = isExternalLink ?? isExternalLinkImplied(String(href));
if (treatExternal) {
otherProperties.hasIcon = true; // Underline text, not icon
icon = <IconExternalLink />; // Display icon
}
const asInAppLink = isRouterLink ?? (!treatExternal && !openInNewTab);
return (
<DesignSystemReactLink
href={href}
isRouterLink={asInAppLink}
className={className}
{...otherProperties}
>
<LinkText>{children}</LinkText>
{icon}
</DesignSystemReactLink>
);
}
export function ListLink({ children, ...others }: LinkProperties): JSX.Element {
return (
<ListItem>
<Link {...others} type='list'>
{children}
</Link>
</ListItem>
);
}