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 "delay" component prop #18

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 25 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export type NextTopLoaderProps = {
* @ you can disable it by setting it to `false`
*/
shadow?: string | false;
/**
* Minimum duration in ms until the TopLoader starts showing.
* @default 200
*/
delay?: number;
}

const NextTopLoader = ({
Expand All @@ -72,6 +77,7 @@ const NextTopLoader = ({
easing,
speed,
shadow,
delay,
}: NextTopLoaderProps) => {
const defaultColor = '#29d';
const defaultHeight = 3;
Expand Down Expand Up @@ -103,6 +109,8 @@ const NextTopLoader = ({
);

React.useEffect(() => {
let progressBarTimeout: NodeJS.Timeout | undefined = undefined;

NProgress.configure({
showSpinner: showSpinner ?? true,
trickle: crawl ?? true,
Expand All @@ -112,6 +120,16 @@ const NextTopLoader = ({
speed: speed ?? 200,
});

function startProgressBar() {
clearTimeout(progressBarTimeout);
progressBarTimeout = setTimeout(NProgress.start, delay ?? 200);
}

function stopProgressBar() {
clearTimeout(progressBarTimeout);
NProgress.done();
}

function isAnchorOfCurrentUrl(currentUrl: string, newUrl: string) {
const currentUrlObj = new URL(currentUrl);
const newUrlObj = new URL(newUrl);
Expand Down Expand Up @@ -149,17 +167,17 @@ const NextTopLoader = ({
const isExternalLink = (anchor as HTMLAnchorElement).target === "_blank";
const isAnchor = isAnchorOfCurrentUrl(currentUrl, newUrl);
if (newUrl === currentUrl || isAnchor || isExternalLink) {
NProgress.start();
NProgress.done();
startProgressBar();
stopProgressBar();
[].forEach.call(npgclass, function (el: Element) {
el.classList.remove("nprogress-busy");
});
} else {
NProgress.start();
startProgressBar();
(function (history) {
const pushState = history.pushState;
history.pushState = function () {
NProgress.done();
stopProgressBar();
[].forEach.call(npgclass, function (el: Element) {
el.classList.remove("nprogress-busy");
});
Expand All @@ -172,8 +190,8 @@ const NextTopLoader = ({
} catch (err) {
// Log the error in development only!
// console.log('NextTopLoader error: ', err);
NProgress.start();
NProgress.done();
startProgressBar();
stopProgressBar();
}
}

Expand All @@ -199,6 +217,7 @@ NextTopLoader.propTypes = {
initialPosition: PropTypes.number,
easing: PropTypes.string,
speed: PropTypes.number,
delay: PropTypes.number,
shadow: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
Expand Down