Skip to content

Commit

Permalink
Add option to control showing of top loader for hash anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
mobeigi committed Oct 3, 2024
1 parent 2e46bf9 commit d3acd77
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ export type NextTopLoaderProps = {
*
*/
showAtBottom?: boolean;
/**
* To show the TopLoader for hash anchors.
* @default true
*
*/
showForHashAnchor?: boolean;
};

/**
Expand All @@ -104,6 +110,7 @@ const NextTopLoader = ({
template,
zIndex = 1600,
showAtBottom = false,
showForHashAnchor = true,
}: NextTopLoaderProps): React.JSX.Element => {
const defaultColor = '#29d';
const defaultHeight = 3;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit d3acd77

Please sign in to comment.