Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou must be signed in to change notification settings

TheSGJ/nextjs-toploader

Repository files navigation

NextJS TopLoader Next Js TopLoader

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

NPMNPM Downloads

Install

using npm:

npm install nextjs-toploader

using yarn:

yarn add nextjs-toploader

Usage

import using:

importNextTopLoaderfrom'nextjs-toploader';

Usage withapp/layout.js forapp folder structure

For rendering add<NextTopLoader /> to yourreturn() inside the<body></body> ofRootLayout():

importNextTopLoaderfrom'nextjs-toploader';exportdefaultfunctionRootLayout({ children}){return(<htmllang="en"><body><NextTopLoader/>{children}</body></html>);}

Usage withpages/_app.js forpages folder structure

For rendering add<PagesTopLoader /> to yourreturn() inMyApp() (Recommended):

import{PagesTopLoader}from'nextjs-toploader/pages';exportdefaultfunctionMyApp({ Component, pageProps}){return(<><PagesTopLoader/><Component{...pageProps}/>;</>);}

You can also use<NextTopLoader /> inpages router, but it's recommended to use<PagesTopLoader /> foruseRouter hook support fromnextjs-toploader version 2.6.12 onwards

Compatibility withuseRouter hook

useRouter hook usage withapp/layout.js forapp folder structure

For triggering TopLoader when usinguseRouter hook (app router):

// Import the useRouter hook from nextjs-toploader to trigger the TopLoaderimport{useRouter}from'nextjs-toploader/app';

Then simply use it in your code for example:

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

useRouter hook usage withpages/_app.js forpages folder structure

For triggering TopLoader when usinguseRouter add<PagesTopLoader /> to yourreturn() inMyApp() :

import{PagesTopLoader}from'nextjs-toploader/pages';exportdefaultfunctionMyApp({ Component, pageProps}){return(<><PagesTopLoader/><Component{...pageProps}/>;</>);}

useTopLoader Hook

A custom hook for handling progress indicators using NextTopLoader.

Methods

NameDescription
startStarts the progress bar
doneCompletes the progress bar. Can be forced to complete immediately with an optional force parameter
removeRemoves the progress bar element from the DOM
setProgressManually sets the progress value (between 0.0 and 1.0)
incIncrements the progress bar by a specified amount. If no amount is specified, it makes a small automatic increment
trickleAdds small random increments to the progress bar
isStartedChecks if the progress bar has been started
isRenderedChecks if the progress bar is rendered in the DOM
getPositioningCSSReturns the positioning CSS property of the progress bar

Example Usage

'use client';importReactfrom'react';import{useTopLoader}from'nextjs-toploader';constComponent=()=>{constloader=useTopLoader();return(<div><buttontype="button"onClick={()=>loader.start()}>        Start</button><buttontype="button"onClick={()=>loader.setProgress(0.5)}>        Set Progress</button></div>);};exportdefaultComponent;

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

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

importNextTopLoaderfrom'nextjs-toploader';constApp=()=>{return(<div><Router><NextTopLoader/><Routes>{/* Your Routes Here */}</Routes></Router></div>);};exportdefaultApp;

Default Configuration

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

<NextTopLoadercolor="#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 role="bar"><div></div></div>  <div role="spinner"><div></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 inms.
  • speed: animation speed for the TopLoader inms
  • easing: animation settings using easing (a CSS easing string).
  • height: height of TopLoader inpx.
  • crawl: auto incrementing behavior for the TopLoader.
  • showSpinner: to show spinner or not.
  • shadow: a smooth shadow for the TopLoader. (set it tofalse 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 tofalse to disable it)

NextTopLoaderProps (props passed to the TopLoader)

NameTypeDefault Value
colorstring"#2299DD"
initialPositionnumber0.08
crawlSpeednumber200
heightnumber3
crawlbooleantrue
showSpinnerbooleantrue
easingstring"ease"
speednumber200
shadowstring | false"0 0 10px #2299DD,0 0 5px #2299DD"
templatestring"<div role="bar"><div></div></div><div role="spinner"><div></div></div>"
zIndexnumber1600
showAtBottombooleanfalse
showForHashAnchorbooleantrue

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


[8]ページ先頭

©2009-2025 Movatter.jp