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

[FEATURE] Added delayMs to prevent showing the progressbar on fast pa… #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ export default function MyApp({ Component, pageProps }) {
If no props are passed to `<NextNProgress />`, below is the default configuration applied.

```jsx
<NextNProgress color="#29D" startPosition={0.3} stopDelayMs={200} height={3} showOnShallow={true} />
<NextNProgress color="#29D" startPosition={0.3} startDelayMs={150} stopDelayMs={200} height={3} showOnShallow={true} />
```

- `color`: to change the default color of progressbar. You can also use `rgb(,,)` or `rgba(,,,)`.
- `startPosition`: to set the default starting position : `0.3 = 30%`.
- `startDelayMs`: time for delay to start progressbar in `ms`.
- `stopDelayMs`: time for delay to stop progressbar in `ms`.
- `height`: height of progressbar in `px`.
- `showOnShallow`: You can choose whether you want the progressbar to be displayed if you're using shallow routing. It takes a boolean. Learn more about shallow routing [in Next.js docs](https://nextjs.org/docs/routing/shallow-routing).
Expand Down
15 changes: 13 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
*/
startPosition?: number;
/**
* The start delay in milliseconds.
* @default 250
*/
startDelayMs?: number; /**
* The stop delay in milliseconds.
* @default 200
*/
Expand Down Expand Up @@ -57,6 +61,7 @@
const NextNProgress = ({
color = '#29D',
startPosition = 0.3,
startDelayMs = 250,
stopDelayMs = 200,
height = 3,
showOnShallow = true,
Expand All @@ -69,6 +74,7 @@
),
}: NextNProgressProps) => {
let timer: NodeJS.Timeout | null = null;
let timeout: NodeJS.Timeout | null = null;

React.useEffect(() => {
if (options) {
Expand All @@ -85,17 +91,17 @@
}, []);

const routeChangeStart = (
_: string,

Check failure on line 94 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Linting

'_' is defined but never used
{

Check failure on line 95 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Compile source code

'shallow' is declared but its value is never read.
shallow,

Check failure on line 96 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Linting

'shallow' is defined but never used
}: {
shallow: boolean;
},
) => {
if (!shallow || showOnShallow) {
timeout = setTimeout(() => {
NProgress.set(startPosition);
NProgress.start();
}
}, startDelayMs);
};

const routeChangeEnd = (
Expand All @@ -106,6 +112,10 @@
shallow: boolean;
},
) => {
if (timeout) clearTimeout(timeout);
if (!NProgress.isStarted()) {
return;
}
if (!shallow || showOnShallow) {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
Expand Down Expand Up @@ -204,6 +214,7 @@
NextNProgress.propTypes = {
color: PropTypes.string,
startPosition: PropTypes.number,
startDelayMs: PropTypes.number,
stopDelayMs: PropTypes.number,
height: PropTypes.number,
showOnShallow: PropTypes.bool,
Expand Down
Loading