-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathExternalLink.astro
More file actions
78 lines (67 loc) · 1.67 KB
/
ExternalLink.astro
File metadata and controls
78 lines (67 loc) · 1.67 KB
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
75
76
77
78
---
import Icon from "./Icon.astro";
import Tooltip from "./Tooltip.astro";
import { faUpRightFromSquare } from "@fortawesome/free-solid-svg-icons";
const {
url,
newTab = true,
class: className,
refer = false,
noIcon,
} = Astro.props;
const hostname = new URL(url).hostname;
let child = await Astro.slots.render("default");
child = child.trim();
export interface Props {
/** The url to link to */
url: string;
/** Whether the linked page should be opened in another tab. Defaults to `true`. */
newTab?: boolean;
/** Whether to [refer](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer) when navigating away. Defaults to `false`. */
refer?: boolean;
/** Don't display external link icon. */
noIcon?: true;
/** Allows you to pass in a class to be applied to the link. */
class?: string;
}
const classes = ["external"];
if (className) {
classes.push(className);
}
if (noIcon) {
classes.push("no-icon");
}
const relationships = ["noopener", "nofollow"];
if (!refer) {
relationships.push("noreferrer");
}
---
<Tooltip content={hostname}>
<span class="external-link" class:list={classes}>
<a
href={url}
target={newTab ? "_blank" : null}
rel={relationships.join(" ")}
set:html={child}
/>{
noIcon !== true && (
<Icon icon={faUpRightFromSquare} classes={["external-indicator"]} />
)
}
</span>
</Tooltip>
<style lang="scss">
.external-link {
display: inline;
color: var(--link-color);
a {
font-weight: 500;
}
}
:global(.external-link .icon.external-indicator) {
display: inline;
margin-right: -0.2em;
vertical-align: text-top;
font-size: 0.5em;
}
</style>