Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to control showing of top loader for hash anchors (fixes #92) #93

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
TheSGJ marked this conversation as resolved.
Show resolved Hide resolved
};

/**
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);
TheSGJ marked this conversation as resolved.
Show resolved Hide resolved
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}
*/
TheSGJ marked this conversation as resolved.
Show resolved Hide resolved
((history: History): void => {
const replaceState = history.replaceState;
history.replaceState = (...args) => {
Expand Down
Loading