Skip to content

Commit 40968f1

Browse files
authored
Merge pull request #114 from farukozdemirz/feature/use-top-loader-hook
feat: add useTopLoader hook for managing NextTopLoader
2 parents 0f4508b + f83d0bd commit 40968f1

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,51 @@ export default function MyApp({ Component, pageProps }) {
103103

104104
---
105105

106+
## useTopLoader Hook
107+
108+
A custom hook for handling progress indicators using NextTopLoader.
109+
110+
## Methods
111+
112+
| Name | Description |
113+
| ----------------- | ------------------------------------------------------------------------------------------------------------------ |
114+
| start | Starts the progress bar |
115+
| done | Completes the progress bar. Can be forced to complete immediately with an optional force parameter |
116+
| remove | Removes the progress bar element from the DOM |
117+
| setProgress | Manually sets the progress value (between 0.0 and 1.0) |
118+
| inc | Increments the progress bar by a specified amount. If no amount is specified, it makes a small automatic increment |
119+
| trickle | Adds small random increments to the progress bar |
120+
| isStarted | Checks if the progress bar has been started |
121+
| isRendered | Checks if the progress bar is rendered in the DOM |
122+
| getPositioningCSS | Returns the positioning CSS property of the progress bar |
123+
124+
## Example Usage
125+
126+
```js
127+
'use client';
128+
129+
import React from 'react';
130+
import { useTopLoader } from 'nextjs-toploader';
131+
132+
const Component = () => {
133+
const loader = useTopLoader();
134+
return (
135+
<div>
136+
<button type="button" onClick={() => loader.start()}>
137+
Start
138+
</button>
139+
<button type="button" onClick={() => loader.setProgress(0.5)}>
140+
Set Progress
141+
</button>
142+
</div>
143+
);
144+
};
145+
146+
export default Component;
147+
```
148+
149+
---
150+
106151
### Usage with React, Vite React or any other React Based Framework
107152

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

src/hooks/useTopLoader.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as NProgress from 'nprogress';
2+
3+
interface TopLoaderActions {
4+
start: () => NProgress.NProgress;
5+
done: (force?: boolean) => NProgress.NProgress;
6+
remove: () => void;
7+
setProgress: (value: number) => NProgress.NProgress;
8+
inc: (amount?: number) => NProgress.NProgress;
9+
trickle: () => NProgress.NProgress;
10+
isStarted: () => boolean;
11+
isRendered: () => boolean;
12+
getPositioningCSS: () => "translate3d" | "translate" | "margin";
13+
}
14+
15+
export const useTopLoader = (): TopLoaderActions => {
16+
const actions: TopLoaderActions = {
17+
start: () => NProgress.start(),
18+
done: (force?: boolean) => NProgress.done(force),
19+
remove: () => NProgress.remove(),
20+
setProgress: (value: number) => NProgress.set(value),
21+
inc: (amount?: number) => NProgress.inc(amount),
22+
trickle: () => NProgress.trickle(),
23+
isStarted: () => NProgress.isStarted(),
24+
isRendered: () => NProgress.isRendered(),
25+
getPositioningCSS: () => NProgress.getPositioningCSS(),
26+
};
27+
28+
return actions;
29+
};

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import * as PropTypes from 'prop-types';
88
import * as React from 'react';
99
import * as NProgress from 'nprogress';
10+
export { useTopLoader } from './hooks/useTopLoader';
1011

1112
// @deno-types ="npm:preact@10.19.6"
1213

0 commit comments

Comments
 (0)