Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

nextjs-toploader

TheSGJ741.3kMIT3.8.16TypeScript support: included

A Next.js Top Loading Bar component made using nprogress, works with Next.js 15 and Next.js 14 and React.

Next, Next TopLoader, Next.js, Next.js 15, Next.js 14, Next.js 13, Nprogress, React, Top Loading Bar, Progressbar

readme

NextJS TopLoader Next Js TopLoader

  • A Next.js Top Loading Bar component made using nprogress, works with Next.js 14 and React.

NPM NPM Downloads

Install

using npm:

npm install nextjs-toploader

using yarn:

yarn add nextjs-toploader

Usage

import using:

import NextTopLoader from 'nextjs-toploader';

Usage with app/layout.js for app folder structure

For rendering add <NextTopLoader /> to your return() inside the <body></body> of RootLayout():

import NextTopLoader from 'nextjs-toploader';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <NextTopLoader />
        {children}
      </body>
    </html>
  );
}

Usage with pages/_app.js for pages folder structure

For rendering add <PagesTopLoader /> to your return() in MyApp() (Recommended):

import { PagesTopLoader } from 'nextjs-toploader/pages';

export default function MyApp({ Component, pageProps }) {
  return (
    <>
      <PagesTopLoader />
      <Component {...pageProps} />;
    </>
  );
}

You can also use <NextTopLoader /> in pages router, but it's recommended to use <PagesTopLoader /> for useRouter hook support from nextjs-toploader version 2.6.12 onwards

Compatibility with useRouter hook

useRouter hook usage with app/layout.js for app folder structure

For triggering TopLoader when using useRouter hook (app router):

// Import the useRouter hook from nextjs-toploader to trigger the TopLoader

import { useRouter } from 'nextjs-toploader/app';

Then simply use it in your code for example:

const router = useRouter();
router.push('/some-page');

useRouter hook usage with pages/_app.js for pages folder structure

For triggering TopLoader when using useRouter add <PagesTopLoader /> to your return() in MyApp() :

import { PagesTopLoader } from 'nextjs-toploader/pages';

export default function MyApp({ Component, pageProps }) {
  return (
    <>
      <PagesTopLoader />
      <Component {...pageProps} />;
    </>
  );
}

useTopLoader Hook

A custom hook for handling progress indicators using NextTopLoader.

Methods

Name Description
start Starts the progress bar
done Completes the progress bar. Can be forced to complete immediately with an optional force parameter
remove Removes the progress bar element from the DOM
setProgress Manually sets the progress value (between 0.0 and 1.0)
inc Increments the progress bar by a specified amount. If no amount is specified, it makes a small automatic increment
trickle Adds small random increments to the progress bar
isStarted Checks if the progress bar has been started
isRendered Checks if the progress bar is rendered in the DOM
getPositioningCSS Returns the positioning CSS property of the progress bar

Example Usage

'use client';

import React from 'react';
import { useTopLoader } from 'nextjs-toploader';

const Component = () => {
  const loader = useTopLoader();
  return (
    <div>
      <button type="button" onClick={() => loader.start()}>
        Start
      </button>
      <button type="button" onClick={() => loader.setProgress(0.5)}>
        Set Progress
      </button>
    </div>
  );
};

export default Component;

Usage with React, Vite React or any other React Based Framework

For rendering add <NextTopLoader /> to your return() inside the <Router><Router/> component in App() in your App.js:

import NextTopLoader from 'nextjs-toploader';
const App = () => {
  return (
    <div>
      <Router>
        <NextTopLoader />
        <Routes>{/* Your Routes Here */}</Routes>
      </Router>
    </div>
  );
};

export default App;

Default Configuration

If no props are passed to <NextTopLoader />, below is the default configuration applied.

<NextTopLoader
  color="#2299DD"
  initialPosition={0.08}
  crawlSpeed={200}
  height={3}
  crawl={true}
  showSpinner={true}
  easing="ease"
  speed={200}
  shadow="0 0 10px #2299DD,0 0 5px #2299DD"
  template='<div class="bar" role="bar"><div class="peg"></div></div> 
  <div class="spinner" role="spinner"><div class="spinner-icon"></div></div>'
  zIndex={1600}
  showAtBottom={false}
/>
  • color: to change the default color of TopLoader.
  • initialPosition: to change initial position for the TopLoader in percentage, : 0.08 = 8%.
  • crawlSpeed: increment delay speed in ms.
  • speed: animation speed for the TopLoader in ms
  • easing: animation settings using easing (a CSS easing string).
  • height: height of TopLoader in px.
  • crawl: auto incrementing behavior for the TopLoader.
  • showSpinner: to show spinner or not.
  • shadow: a smooth shadow for the TopLoader. (set it to false to disable it)
  • template: to include custom HTML attributes for the TopLoader.
  • zIndex: defines zIndex for the TopLoader.
  • showAtBottom: To show the TopLoader at bottom. (increase height for the TopLoader to ensure it's visibility at the mobile devices)
  • showForHashAnchor: To show for "#" url or not. (set it to false to disable it)

NextTopLoaderProps (props passed to the TopLoader)

Name Type Default Value
color string "#2299DD"
initialPosition number 0.08
crawlSpeed number 200
height number 3
crawl boolean true
showSpinner boolean true
easing string "ease"
speed number 200
shadow string | false "0 0 10px #2299DD,0 0 5px #2299DD"
template string "<div class="bar" role="bar"><div class="peg"></div></div><div class="spinner" role="spinner"><div class="spinner-icon"></div></div>"
zIndex number 1600
showAtBottom boolean false
showForHashAnchor boolean true

Contributors

Code Contributors

This project was made possible thanks to the contributions of its code contributors.

Financial Contribution


UPI ID: thesgj@upi (International UPI ID)

Sponsor me on GitHub

"Buy Me A Coffee"

OpenCollective

changelog

Changelogs

v1.0.0

Added

  • Added next.js version 13.2.3 in package.json peerDependancies
  • Initialized and added propTypes in NextTopLoader, Support for Next.js v13.2.3

v1.0.1

Updated

  • Fixed misspelled Component name in README, added more instructions for the Component for the usage with layout.js in app folder structure

v1.1.1

Added

  • Used React.memo for better performance

Fixed

  • Fix spelling for NextTopLoadersProps to NextTopLoaderProps

Updated

  • Removed unwanted devDependancies to reduce the package size

v1.2.1

Added

  • Added nprogress and @types/nprogress packages for the dependencies

Fixed

  • Fix the newUrl returning as undefined value for few anchor tags , added support to prevent loader from being stuck for anchor navigation for hash urls

Updated

  • Refactor the code in useEffect hook, removed the next/script

v1.2.2

Fixed

  • Fix the Loader gets triggered on navigation to link in another tab

v1.3.2

Added

  • Added support for client side navigations

v1.4.2

Added

  • Added shadow config prop

Fixed

  • Fixed spelling for increment. And updated the README.

v1.5.3

Added

  • Added template option
  • Added the provision to configure z index via props

Fixed

  • Fixed loader infinity when press control click
  • Fixed Unexpected symbol "?" on Safari 12.1 by adding es6 as target
  • Fixed the loader when href is empty
  • Fixed the loader for new URL starts with "blob:"

v1.6.3

Added

  • Added a feature to show the TopLoader at bottom.

v1.6.4

Fixed

  • Fixed the spinner top and bottom margin

v1.6.5

Fixed

  • Fixed the loader when using CMD + Click in macOS

v1.6.6

Fixed

  • Fixed the TopLoader to work with special schemes such as mailto, sms, tel, etc
  • Fixed the TopLoader for "#" hash anchor

v1.6.9

Fixed

  • Resolve progress bar when navigating back from external page
  • Fixed the TopLoader for anchor url having other domain name
  • Moved histry.pushState function outside the MouseEvent, it was triggering for every mouse click
  • Fixed TopLoader getting stuck on popstate events
  • Fixed the use of rest params and passing its type as any
  • Fixed for all Special Schemes, TopLoader will not run on special schemes now onwards
  • Fixed for shift key and alt key

Updated

  • Added Type declarations for few functions
  • Refactor the whole code to make it more readable
  • Updated to add support for the JSR Package Manager
  • Updated to version 1.6.9 to avoid version gap for JSR

v1.6.10

Fixed

  • Fix npm-publish.yml for gh actions to publish with provenance

v1.6.11

Fixed

  • Fix to add use client in jsr usage with nextjs
  • Update README about support for React

v1.6.12

Fixed

  • Fixed indefinite animation when replacing current route
  • Moved @types/nprogress into devDependencies

v2.6.12

Added (major changes)

  • Added PagesTopLoader Component to make useRouter hook supported in pages router

  • Added custom useRouter hook to trigger TopLoader for app router

Updated

  • Updated README.md Documentation made it more readable.

v3.6.12

Added (major changes)

  • Added new import for PagesTopLoader

  • Added new import for custom useRouter hook

Updated

  • Fixing for react js
  • Updated README.md Documentation made it more readable.

v3.6.13

Fixed

  • Fixed imports, PagesTopLoader imported incorrectly like nextjs-toploader/dist/pages and useEffect like nextjs-toploader/dist/app

v3.6.14

Fixed

  • Fixed typo in template value and shadow value in prop type table in README

v3.6.15

Updated

  • Updated copyright year in LICENSE

Fixed

  • Fixed typo in template value in prop type table in README

v3.7.15

Added

  • Added option to control showing of top loader for hash anchors

Updated

  • Updated README.md Documentation for showForHashAnchor

v3.8.15

Added

  • Added useTopLoader hook for managing NextTopLoader

Fixed

  • Fixed target being customizable

v3.8.16

Fixed

  • Fixed target being customizable
  • Fixed Toploader not crawling by reverting the pr #109 that is "fix bug: target is customizable"