Skip to content

Commit f235171

Browse files
committed
attempting to fix for hash anchor
1 parent fed8625 commit f235171

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/index.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,24 @@ const NextTopLoader = ({
111111
// Check if to show at bottom
112112
const positionStyle = showAtBottom ? 'bottom: 0;' : 'top: 0;';
113113
const spinnerPositionStyle = showAtBottom ? 'bottom: 15px;' : 'top: 15px;';
114-
114+
115115
const styles = (
116116
<style>
117117
{`#nprogress{pointer-events:none}#nprogress .bar{background:${color};position:fixed;z-index:${zIndex};${positionStyle}left:0;width:100%;height:${height}px}#nprogress .peg{display:block;position:absolute;right:0;width:100px;height:100%;${boxShadow};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:${zIndex};${spinnerPositionStyle}right:15px}#nprogress .spinner-icon{width:18px;height:18px;box-sizing:border-box;border:2px solid transparent;border-top-color:${color};border-left-color:${color};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)}}`}
118118
</style>
119119
);
120120

121+
// Convert the url to Absolute URL based on the current window location.
122+
const toAbsoluteURL = (url: string): string => {
123+
return new URL(url, window.location.href).href;
124+
};
125+
126+
// Check if it is hash anchor or same page anchor
127+
const isHashAnchor = (currentUrl: string, newUrl: string): boolean => {
128+
const current = new URL(toAbsoluteURL(currentUrl));
129+
const next = new URL(toAbsoluteURL(newUrl));
130+
return current.href.split('#')[0] === next.href.split('#')[0];
131+
};
121132
React.useEffect(() => {
122133
NProgress.configure({
123134
showSpinner: showSpinner ?? true,
@@ -167,9 +178,21 @@ const NextTopLoader = ({
167178
const currentUrl = window.location.href;
168179
// const newUrl = (anchor as HTMLAnchorElement).href;
169180
const isExternalLink = (anchor as HTMLAnchorElement).target === '_blank';
170-
const isSpecialScheme = ['tel:', 'mailto:', 'sms:', 'blob:', 'download:'].some((scheme) => newUrl.startsWith(scheme));
181+
182+
// Check for Special Schemes
183+
const isSpecialScheme = ['tel:', 'mailto:', 'sms:', 'blob:', 'download:'].some((scheme) =>
184+
newUrl.startsWith(scheme)
185+
);
171186
const isAnchor = isAnchorOfCurrentUrl(currentUrl, newUrl);
172-
if (newUrl === currentUrl || isAnchor || isExternalLink || isSpecialScheme || event.ctrlKey|| event.metaKey) {
187+
if (
188+
newUrl === currentUrl ||
189+
isAnchor ||
190+
isExternalLink ||
191+
isSpecialScheme ||
192+
event.ctrlKey ||
193+
event.metaKey ||
194+
isHashAnchor(window.location.href, anchor.href)
195+
) {
173196
NProgress.start();
174197
NProgress.done();
175198
[].forEach.call(npgclass, function (el: Element) {

0 commit comments

Comments
 (0)