From d3acd77af0d05309c73b3111d47354ef041057f5 Mon Sep 17 00:00:00 2001 From: Mo Beigi Date: Thu, 3 Oct 2024 15:25:41 +1000 Subject: [PATCH] Add option to control showing of top loader for hash anchors --- src/index.tsx | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 7dce0a8..bda04ea 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -80,6 +80,12 @@ export type NextTopLoaderProps = { * */ showAtBottom?: boolean; + /** + * To show the TopLoader for hash anchors. + * @default true + * + */ + showForHashAnchor?: boolean; }; /** @@ -104,6 +110,7 @@ const NextTopLoader = ({ template, zIndex = 1600, showAtBottom = false, + showForHashAnchor = true, }: NextTopLoaderProps): React.JSX.Element => { const defaultColor = '#29d'; const defaultHeight = 3; @@ -241,21 +248,26 @@ const NextTopLoader = ({ newUrl.startsWith(scheme) ); - const isAnchor: boolean = isAnchorOfCurrentUrl(currentUrl, newUrl); const notSameHost = !isSameHostName(window.location.href, anchor.href); if (notSameHost) { return; } + + const isAnchorOrHashAnchor = + isAnchorOfCurrentUrl(currentUrl, newUrl) || isHashAnchor(window.location.href, anchor.href); + if (!showForHashAnchor && isAnchorOrHashAnchor) { + return; + } + if ( newUrl === currentUrl || - isAnchor || isExternalLink || isSpecialScheme || + isAnchorOrHashAnchor || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey || - isHashAnchor(window.location.href, anchor.href) || !toAbsoluteURL(anchor.href).startsWith('http') ) { NProgress.start(); @@ -288,10 +300,10 @@ const NextTopLoader = ({ })((window as Window).history); /** - * Complete TopLoader Progress on replacing current entry of history stack - * @param {History} - * @returns {void} - */ + * Complete TopLoader Progress on replacing current entry of history stack + * @param {History} + * @returns {void} + */ ((history: History): void => { const replaceState = history.replaceState; history.replaceState = (...args) => {