Skip to content

Commit 063d2bb

Browse files
authored
fix bug: target is customizable
1 parent 461a78d commit 063d2bb

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

src/index.tsx

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
'use client';
1+
"use client";
22
// deno-ts-ignore-file
33
// deno-lint-ignore-file
44
/* eslint-disable no-var */
55
/* eslint-disable max-len */
66
/* eslint-disable prefer-const */
7-
import * as PropTypes from 'prop-types';
8-
import * as React from 'react';
9-
import * as NProgress from 'nprogress';
7+
import * as PropTypes from "prop-types";
8+
import * as React from "react";
9+
import * as NProgress from "nprogress";
1010

1111
// @deno-types ="npm:preact@10.19.6"
1212

@@ -112,7 +112,7 @@ const NextTopLoader = ({
112112
showAtBottom = false,
113113
showForHashAnchor = true,
114114
}: NextTopLoaderProps): React.JSX.Element => {
115-
const defaultColor = '#29d';
115+
const defaultColor = "#29d";
116116
const defaultHeight = 3;
117117

118118
const color = propColor ?? defaultColor;
@@ -121,14 +121,14 @@ const NextTopLoader = ({
121121
// Any falsy (except undefined) will disable the shadow
122122
const boxShadow =
123123
!shadow && shadow !== undefined
124-
? ''
124+
? ""
125125
: shadow
126-
? `box-shadow:${shadow}`
127-
: `box-shadow:0 0 10px ${color},0 0 5px ${color}`;
126+
? `box-shadow:${shadow}`
127+
: `box-shadow:0 0 10px ${color},0 0 5px ${color}`;
128128

129129
// Check if to show at bottom
130-
const positionStyle = showAtBottom ? 'bottom: 0;' : 'top: 0;';
131-
const spinnerPositionStyle = showAtBottom ? 'bottom: 15px;' : 'top: 15px;';
130+
const positionStyle = showAtBottom ? "bottom: 0;" : "top: 0;";
131+
const spinnerPositionStyle = showAtBottom ? "bottom: 15px;" : "top: 15px;";
132132

133133
/**
134134
* CSS Styles for the NextTopLoader
@@ -157,7 +157,7 @@ const NextTopLoader = ({
157157
const isHashAnchor = (currentUrl: string, newUrl: string): boolean => {
158158
const current = new URL(toAbsoluteURL(currentUrl));
159159
const next = new URL(toAbsoluteURL(newUrl));
160-
return current.href.split('#')[0] === next.href.split('#')[0];
160+
return current.href.split("#")[0] === next.href.split("#")[0];
161161
};
162162

163163
/**
@@ -169,7 +169,10 @@ const NextTopLoader = ({
169169
const isSameHostName = (currentUrl: string, newUrl: string): boolean => {
170170
const current = new URL(toAbsoluteURL(currentUrl));
171171
const next = new URL(toAbsoluteURL(newUrl));
172-
return current.hostname.replace(/^www\./, '') === next.hostname.replace(/^www\./, '');
172+
return (
173+
current.hostname.replace(/^www\./, "") ===
174+
next.hostname.replace(/^www\./, "")
175+
);
173176
};
174177

175178
React.useEffect((): ReturnType<React.EffectCallback> => {
@@ -178,7 +181,7 @@ const NextTopLoader = ({
178181
trickle: crawl ?? true,
179182
trickleSpeed: crawlSpeed ?? 200,
180183
minimum: initialPosition ?? 0.08,
181-
easing: easing ?? 'ease',
184+
easing: easing ?? "ease",
182185
speed: speed ?? 200,
183186
template:
184187
template ??
@@ -204,25 +207,32 @@ const NextTopLoader = ({
204207
const currentHash = currentUrlObj.hash;
205208
const newHash = newUrlObj.hash;
206209
return (
207-
currentHash !== newHash && currentUrlObj.href.replace(currentHash, '') === newUrlObj.href.replace(newHash, '')
210+
currentHash !== newHash &&
211+
currentUrlObj.href.replace(currentHash, "") ===
212+
newUrlObj.href.replace(newHash, "")
208213
);
209214
}
210215
return false;
211216
}
212217

213218
// deno-lint-ignore no-var
214-
var nProgressClass: NodeListOf<HTMLHtmlElement> = document.querySelectorAll('html');
219+
var nProgressClass: NodeListOf<HTMLHtmlElement> =
220+
document.querySelectorAll("html");
215221

216222
const removeNProgressClass = (): void =>
217-
nProgressClass.forEach((el: Element) => el.classList.remove('nprogress-busy'));
223+
nProgressClass.forEach((el: Element) =>
224+
el.classList.remove("nprogress-busy")
225+
);
218226

219227
/**
220228
* Find the closest anchor to trigger
221229
* @param element {HTMLElement | null}
222230
* @returns element {Element}
223231
*/
224-
function findClosestAnchor(element: HTMLElement | null): HTMLAnchorElement | null {
225-
while (element && element.tagName.toLowerCase() !== 'a') {
232+
function findClosestAnchor(
233+
element: HTMLElement | null
234+
): HTMLAnchorElement | null {
235+
while (element && element.tagName.toLowerCase() !== "a") {
226236
element = element.parentElement;
227237
}
228238
return element as HTMLAnchorElement;
@@ -241,20 +251,28 @@ const NextTopLoader = ({
241251
if (newUrl) {
242252
const currentUrl = window.location.href;
243253
// const newUrl = (anchor as HTMLAnchorElement).href;
244-
const isExternalLink = (anchor as HTMLAnchorElement).target === '_blank';
254+
const isExternalLink = !!(anchor as HTMLAnchorElement).target;
245255

246256
// Check for Special Schemes
247-
const isSpecialScheme = ['tel:', 'mailto:', 'sms:', 'blob:', 'download:'].some((scheme) =>
248-
newUrl.startsWith(scheme)
249-
);
257+
const isSpecialScheme = [
258+
"tel:",
259+
"mailto:",
260+
"sms:",
261+
"blob:",
262+
"download:",
263+
].some((scheme) => newUrl.startsWith(scheme));
250264

251-
const notSameHost = !isSameHostName(window.location.href, anchor.href);
265+
const notSameHost = !isSameHostName(
266+
window.location.href,
267+
anchor.href
268+
);
252269
if (notSameHost) {
253270
return;
254271
}
255272

256273
const isAnchorOrHashAnchor =
257-
isAnchorOfCurrentUrl(currentUrl, newUrl) || isHashAnchor(window.location.href, anchor.href);
274+
isAnchorOfCurrentUrl(currentUrl, newUrl) ||
275+
isHashAnchor(window.location.href, anchor.href);
258276
if (!showForHashAnchor && isAnchorOrHashAnchor) {
259277
return;
260278
}
@@ -268,7 +286,7 @@ const NextTopLoader = ({
268286
event.metaKey ||
269287
event.shiftKey ||
270288
event.altKey ||
271-
!toAbsoluteURL(anchor.href).startsWith('http')
289+
!toAbsoluteURL(anchor.href).startsWith("http")
272290
) {
273291
NProgress.start();
274292
NProgress.done();
@@ -327,15 +345,15 @@ const NextTopLoader = ({
327345
}
328346

329347
// Add the global click event listener
330-
window.addEventListener('popstate', handleBackAndForth);
331-
document.addEventListener('click', handleClick);
332-
window.addEventListener('pagehide', handlePageHide);
348+
window.addEventListener("popstate", handleBackAndForth);
349+
document.addEventListener("click", handleClick);
350+
window.addEventListener("pagehide", handlePageHide);
333351

334352
// Clean up the global click event listener when the component is unmounted
335353
return (): void => {
336-
document.removeEventListener('click', handleClick);
337-
window.removeEventListener('pagehide', handlePageHide);
338-
window.removeEventListener('popstate', handleBackAndForth);
354+
document.removeEventListener("click", handleClick);
355+
window.removeEventListener("pagehide", handlePageHide);
356+
window.removeEventListener("popstate", handleBackAndForth);
339357
};
340358
}, []);
341359

0 commit comments

Comments
 (0)