-
Notifications
You must be signed in to change notification settings - Fork 170
/
link.ts
195 lines (186 loc) · 5.29 KB
/
link.ts
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import type { AnchorHTMLAttributes, CSSProperties, MouseEvent, MutableRefObject, PropsWithChildren } from "react";
import { createElement, useCallback, useEffect, useMemo, useRef } from "react";
import util from "../../shared/util.ts";
import events from "../core/events.ts";
import { redirect } from "../core/redirect.ts";
import { useRouter } from "./router.ts";
const prefetched = new Set<string>();
export type LinkProps = PropsWithChildren<
{
to: string;
replace?: boolean;
prefetch?: boolean;
innerRef?: MutableRefObject<HTMLAnchorElement | null>;
} & Omit<AnchorHTMLAttributes<Record<never, never>>, "herf" | "hrefLang">
>;
/**
* The `<Link>` component to link between pages.
*/
export function Link(props: LinkProps) {
const {
to,
prefetch: propPrefetch,
replace,
className,
style,
onClick: propOnClick,
onMouseEnter: propOnMouseEnter,
onMouseLeave: propOnMouseLeave,
["aria-current"]: propAriaCurrent,
children,
innerRef,
...rest
} = props;
const { url: { pathname } } = useRouter();
const href = useMemo(() => {
if (!util.isFilledString(to)) {
throw new Error("<Link>: prop `to` is required.");
}
if (util.isLikelyHttpURL(to)) {
return to;
}
let [p, q] = util.splitBy(to, "?");
if (p.startsWith("/")) {
p = util.cleanPath(p);
} else {
p = util.cleanPath(pathname + "/" + p);
}
return [p, q].filter(Boolean).join("?");
}, [pathname, to]);
const ariaCurrent = useMemo(() => {
if (util.isFilledString(propAriaCurrent)) {
return propAriaCurrent;
}
if (href.startsWith("/")) {
return "page";
}
return undefined;
}, [href, propAriaCurrent]);
const prefetch = useCallback(() => {
if (!util.isLikelyHttpURL(href) && !prefetched.has(href)) {
events.emit("moduleprefetch", { href });
prefetched.add(href);
}
}, [href]);
const timerRef = useRef<number | null>(null);
const onMouseEnter = (e: MouseEvent) => {
if (typeof propOnMouseEnter === "function") {
propOnMouseEnter(e);
}
if (e.defaultPrevented) {
return;
}
if (!timerRef.current && !prefetched.has(href)) {
timerRef.current = setTimeout(() => {
timerRef.current = null;
prefetch();
}, 150);
}
};
const onMouseLeave = (e: MouseEvent) => {
if (typeof propOnMouseLeave === "function") {
propOnMouseLeave(e);
}
if (e.defaultPrevented) {
return;
}
if (timerRef.current) {
clearTimeout(timerRef.current);
timerRef.current = null;
}
};
const onClick = (e: MouseEvent) => {
if (typeof propOnClick === "function") {
propOnClick(e);
}
if (e.defaultPrevented || isModifiedEvent(e)) {
return;
}
e.preventDefault();
redirect(href, replace);
};
useEffect(() => {
if (propPrefetch) {
prefetch();
}
return () => {
if (timerRef.current) {
clearTimeout(timerRef.current);
timerRef.current = null;
}
};
}, [propPrefetch, prefetch]);
return createElement(
"a",
{
...rest,
className,
style,
href,
onClick,
onMouseEnter,
onMouseLeave,
"aria-current": ariaCurrent,
ref: innerRef,
},
children,
);
}
export type NavLinkProps = LinkProps & {
exact?: boolean;
activeClassName?: string;
activeStyle?: CSSProperties;
};
/**
* A special version of the `<Link>` that will add styling attributes to the rendered element when it matches the current URL.
*/
export function NavLink(props: NavLinkProps) {
const { to, exact, className: propClassName, style: propStyle, activeStyle, activeClassName, ...rest } = props;
const { url } = useRouter();
const isActivated = useMemo(() => {
if (!util.isFilledString(to)) {
return false;
}
const [p, q] = util.splitBy(to, "?");
const currentPathname = util.trimSuffix(url.pathname, "/");
let pathname: string;
if (p.startsWith("/")) {
pathname = util.cleanPath(p);
} else {
pathname = util.cleanPath(currentPathname + "/" + p);
}
if (!exact) {
return pathname === currentPathname || currentPathname.startsWith(pathname + "/");
}
return pathname === currentPathname && q === url.searchParams.toString();
}, [url.pathname, url.searchParams, to, exact]);
const className = useMemo(() => {
if (!isActivated || !activeClassName) {
return propClassName;
}
return [propClassName, activeClassName].filter(util.isFilledString).join(" ");
}, [propClassName, activeClassName, isActivated]);
const style = useMemo(() => {
if (!isActivated || !activeStyle) {
return propStyle;
}
return Object.assign({}, propStyle, activeStyle);
}, [propStyle, activeStyle, isActivated]);
const linkProps: LinkProps = { ...rest, to, className, style };
if (isActivated) {
Object.assign(linkProps, { "data-active": "true" });
}
return createElement(Link, linkProps);
}
function isModifiedEvent(event: MouseEvent): boolean {
const { target } = event.currentTarget as HTMLAnchorElement;
const nativeEvent = event.nativeEvent;
return (
(target && target !== "_self") ||
event.metaKey ||
event.ctrlKey ||
event.shiftKey ||
event.altKey || // triggers resource download
(nativeEvent && nativeEvent.which === 2)
);
}