Skip to content

Commit ee479df

Browse files
committed
fix: the Loader gets triggered on navigation to link in another tab (#6) and refactored the code a bit.
1 parent 857b3ed commit ee479df

File tree

4 files changed

+58
-49
lines changed

4 files changed

+58
-49
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@
4040
### Updated
4141

4242
- Refactor the code in `useEffect` hook, removed the `next/script`
43+
44+
## v1.2.2
45+
46+
### Fixed
47+
48+
- Fix the Loader gets triggered on navigation to link in another tab

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,6 @@ If no props are passed to `<NextTopLoader />`, below is the default configuratio
8686
- `crawl`: auto increamenting behaviour for the TopLoader.
8787
- `showSpinner`: to show spinner or not.
8888

89-
After passing the props reload the your next.js page once in the browser, to see changes for `<NextTopLoader />` ( This is because NextTopLoader uses built-in history api in browser for indicating progress. )
89+
[![Sponsor me on GitHub](https://img.shields.io/badge/Sponsor%20me%20on-GitHub-brightgreen)](https://github.com/sponsors/TheSGJ)
90+
91+
[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/thesgj)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nextjs-toploader",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "A Next.js Top Loading Bar component made using nprogress, works with Next.js 13.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.tsx

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface NextTopLoaderProps {
2727
*/
2828
crawlSpeed?: number;
2929
/**
30-
* The height for the TopLoader.
30+
* The height for the TopLoader in pixels (px).
3131
* @default 3
3232
*/
3333
height?: number;
@@ -53,74 +53,75 @@ export interface NextTopLoaderProps {
5353
speed?: number;
5454
}
5555

56-
const NextTopLoader = (props: NextTopLoaderProps) => {
57-
const color = '#29d';
58-
const height = 3;
56+
const NextTopLoader = ({
57+
color,
58+
height,
59+
showSpinner,
60+
crawl,
61+
crawlSpeed,
62+
initialPosition,
63+
easing,
64+
speed,
65+
}: NextTopLoaderProps) => {
66+
const defaultColor = '#29d';
67+
const defaultHeight = 3;
5968

6069
const styles = (
6170
<style>
6271
{`#nprogress{pointer-events:none}#nprogress .bar{background:${
63-
props.color ? props.color : color
72+
color ?? defaultColor
6473
};position:fixed;z-index:1031;top:0;left:0;width:100%;height:${
65-
props.height ? props.height : height
74+
height ?? defaultHeight
6675
}px}#nprogress .peg{display:block;position:absolute;right:0;width:100px;height:100%;box-shadow:0 0 10px ${
67-
props.color ? props.color : color
76+
color ?? defaultColor
6877
},0 0 5px ${
69-
props.color ? props.color : color
78+
color ?? defaultColor
7079
};opacity:1;-webkit-transform:rotate(3deg) translate(0px,-4px);-ms-transform:rotate(3deg) translate(0px,-4px);transform:rotate(3deg) translate(0px,-4px)}#nprogress .spinner{display:block;position:fixed;z-index:1031;top:15px;right:15px}#nprogress .spinner-icon{width:18px;height:18px;box-sizing:border-box;border:2px solid transparent;border-top-color:${
71-
props.color ? props.color : color
80+
color ?? defaultColor
7281
};border-left-color:${
73-
props.color ? props.color : color
82+
color ?? defaultColor
7483
};border-radius:50%;-webkit-animation:nprogress-spinner 400ms linear infinite;animation:nprogress-spinner 400ms linear infinite}.nprogress-custom-parent{overflow:hidden;position:relative}.nprogress-custom-parent #nprogress .bar,.nprogress-custom-parent #nprogress .spinner{position:absolute}@-webkit-keyframes nprogress-spinner{0%{-webkit-transform:rotate(0deg)}100%{-webkit-transform:rotate(360deg)}}@keyframes nprogress-spinner{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}`}
7584
</style>
7685
);
7786

7887
React.useEffect(() => {
79-
if (props.showSpinner !== undefined) {
80-
NProgress.configure({ showSpinner: props.showSpinner });
81-
}
82-
if (props.crawl !== undefined) {
83-
NProgress.configure({ trickle: props.crawl });
84-
}
85-
if (props.crawlSpeed !== undefined) {
86-
NProgress.configure({ trickleSpeed: props.crawlSpeed });
87-
}
88-
if (props.initialPosition !== undefined) {
89-
NProgress.configure({ minimum: props.initialPosition });
90-
}
91-
if (props.easing !== undefined) {
92-
NProgress.configure({ easing: props.easing });
93-
}
94-
if (props.speed !== undefined) {
95-
NProgress.configure({ speed: props.speed });
88+
NProgress.configure({
89+
showSpinner: showSpinner ?? true,
90+
trickle: crawl ?? true,
91+
trickleSpeed: crawlSpeed ?? 200,
92+
minimum: initialPosition ?? 0.08,
93+
easing: easing ?? 'ease',
94+
speed: speed ?? 200,
95+
});
96+
97+
function isAnchorOfCurrentUrl(currentUrl: string, newUrl: string) {
98+
const currentUrlObj = new URL(currentUrl);
99+
const newUrlObj = new URL(newUrl);
100+
// Compare hostname, pathname, and search parameters
101+
if (
102+
currentUrlObj.hostname === newUrlObj.hostname &&
103+
currentUrlObj.pathname === newUrlObj.pathname &&
104+
currentUrlObj.search === newUrlObj.search
105+
) {
106+
// Check if the new URL is just an anchor of the current URL page
107+
const currentHash = currentUrlObj.hash;
108+
const newHash = newUrlObj.hash;
109+
return (
110+
currentHash !== newHash && currentUrlObj.href.replace(currentHash, '') === newUrlObj.href.replace(newHash, '')
111+
);
112+
}
113+
return false;
96114
}
115+
97116
// eslint-disable-next-line no-var
98117
var npgclass = document.querySelectorAll('html');
99118
let navLinks = document.querySelectorAll('a');
100119
navLinks.forEach((navLink) => {
101120
navLink.addEventListener('click', (event: MouseEvent) => {
102121
let currentUrl = window.location.href;
103122
let newUrl = (event.currentTarget as HTMLAnchorElement).href;
104-
let isExternalLink = (event.currentTarget as HTMLAnchorElement).target === "_blank";
105-
function isAnchorOfCurrentUrl(currentUrl: string, newUrl: string) {
106-
const currentUrlObj = new URL(currentUrl);
107-
const newUrlObj = new URL(newUrl);
108-
// Compare hostname, pathname, and search parameters
109-
if (
110-
currentUrlObj.hostname === newUrlObj.hostname &&
111-
currentUrlObj.pathname === newUrlObj.pathname &&
112-
currentUrlObj.search === newUrlObj.search
113-
) {
114-
// Check if the new URL is just an anchor of the current URL page
115-
const currentHash = currentUrlObj.hash;
116-
const newHash = newUrlObj.hash;
117-
return (
118-
currentHash !== newHash &&
119-
currentUrlObj.href.replace(currentHash, '') === newUrlObj.href.replace(newHash, '')
120-
);
121-
}
122-
return false;
123-
}
123+
const isExternalLink = (event.currentTarget as HTMLAnchorElement).target === '_blank';
124+
124125
const isAnchor = isAnchorOfCurrentUrl(currentUrl, newUrl);
125126
if (newUrl === currentUrl || isAnchor || isExternalLink) {
126127
NProgress.start();

0 commit comments

Comments
 (0)