-
Notifications
You must be signed in to change notification settings - Fork 0
/
next-app-nprogress.tsx
89 lines (79 loc) · 2.41 KB
/
next-app-nprogress.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Inspired by:
// https://github.com/apal21/nextjs-progressbar/issues/86#issuecomment-1447977706
// https://github.com/vercel/next.js/discussions/41745#discussioncomment-4208449
"use client";
import NProgress from "nprogress";
import * as React from "react";
type PushStateInput = [
data: any,
unused: string,
url?: string | URL | null | undefined,
];
export default function NextAppNprogress({
color = "#29d",
height = "2px",
}: {
color?: string | undefined;
height?: string | number | undefined;
}) {
const styles = (
<style>
{/* Source: https://github.com/rstacruz/nprogress/blob/e1a8b7fb6e059085df5f83c45d3c2308a147ca18/nprogress.css */}
{`
#nprogress {
pointer-events: none;
}
#nprogress .bar {
background: ${color};
position: fixed;
z-index: 99999;
top: 0;
left: 0;
width: 100%;
height: ${typeof height === `string` ? height : `${height}px`};
}
#nprogress .peg {
display: block;
position: absolute;
right: 0px;
width: 100px;
height: 100%;
box-shadow: 0 0 10px ${color}, 0 0 5px ${color};
opacity: 1.0;
-webkit-transform: rotate(3deg) translate(0px, -4px);
-ms-transform: rotate(3deg) translate(0px, -4px);
transform: rotate(3deg) translate(0px, -4px);
}
`}
</style>
);
React.useEffect(() => {
NProgress.configure({ showSpinner: false });
const handleAnchorClick = (event: MouseEvent) => {
const targetUrl = (event.currentTarget as HTMLAnchorElement).href;
const currentUrl = location.href;
if (targetUrl !== currentUrl) {
NProgress.start();
}
};
const handleMutation = () => {
const anchorElements = document.querySelectorAll("a");
for (const anchor of anchorElements) {
anchor.addEventListener("click", handleAnchorClick);
}
};
const mutationObserver = new MutationObserver(handleMutation);
mutationObserver.observe(document, { childList: true, subtree: true });
handleMutation();
window.history.pushState = new Proxy(
window.history.pushState.bind(window.history),
{
apply: (target, thisArg, argArray: PushStateInput) => {
NProgress.done();
target.apply(thisArg, argArray);
},
},
);
});
return styles;
}