-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathnext.tsx
More file actions
60 lines (56 loc) · 1.71 KB
/
next.tsx
File metadata and controls
60 lines (56 loc) · 1.71 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
"use client"
import React from 'react'
import { startTransition } from "react";
import NextLink from "next/link";
import { useRouter } from "next/navigation";
import { useProgress } from ".";
import { formatUrl } from './format-url';
// Copied from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/link.tsx#L180-L191
function isModifiedEvent(event: React.MouseEvent): boolean {
const eventTarget = event.currentTarget as HTMLAnchorElement | SVGAElement;
const target = eventTarget.getAttribute("target");
return (
(target && target !== "_self") ||
event.metaKey ||
event.ctrlKey ||
event.shiftKey ||
event.altKey || // triggers resource download
(event.nativeEvent && event.nativeEvent.which === 2)
);
}
/**
* A custom Link component that wraps Next.js's next/link component.
*/
export function Link({
href,
children,
replace,
scroll,
onClick,
...rest
}: Parameters<typeof NextLink>[0]) {
const router = useRouter();
const startProgress = useProgress()
return (
<NextLink
href={href}
onClick={(e) => {
onClick?.(e);
if (isModifiedEvent(e)) return;
e.preventDefault();
startTransition(() => {
startProgress()
const url = typeof href === 'string' ? href : formatUrl(href)
if (replace) {
router.replace(url, { scroll })
} else {
router.push(url, { scroll })
}
})
}}
{...rest}
>
{children}
</NextLink>
);
}