Skip to content

Commit 5cd20f8

Browse files
committed
[feat]: allowing default next link component
1 parent 25bdc83 commit 5cd20f8

File tree

12 files changed

+92
-36
lines changed

12 files changed

+92
-36
lines changed

.eslintrc

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
{
2-
"plugins": ["@typescript-eslint"],
2+
"plugins": [
3+
"@typescript-eslint"
4+
],
35
"extends": [
46
"eslint:recommended",
57
"plugin:@typescript-eslint/eslint-recommended",
68
"plugin:@typescript-eslint/recommended"
79
],
810
"rules": {
9-
"quotes": ["error", "single"],
10-
"semi": ["error", "always"],
11-
"eol-last": ["error", "always"],
11+
"quotes": [
12+
"error",
13+
"single"
14+
],
15+
"semi": [
16+
"error",
17+
"always"
18+
],
19+
"eol-last": [
20+
"error",
21+
"always"
22+
],
1223
"comma-dangle": [
1324
"error",
1425
{
@@ -19,13 +30,22 @@
1930
"functions": "never"
2031
}
2132
],
22-
"arrow-parens": ["error", "always"],
23-
"no-trailing-spaces": ["error", { "skipBlankLines": true }],
33+
"arrow-parens": [
34+
"error",
35+
"always"
36+
],
37+
"no-trailing-spaces": [
38+
"error",
39+
{
40+
"skipBlankLines": true
41+
}
42+
],
2443
"prefer-const": "warn",
2544
"accessor-pairs": "off",
2645
"no-async-promise-executor": "off",
2746
"no-unused-vars": "off",
2847
"node/no-missing-require": "off",
48+
"@typescript-eslint/no-empty-function": "off",
2949
"no-console": "warn"
3050
}
31-
}
51+
}

.vscode/extensions.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"recommendations": [
3+
"streetsidesoftware.code-spell-checker",
4+
"dbaeumer.vscode-eslint",
5+
"esbenp.prettier-vscode"
6+
]
7+
}

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ export default function RootLayout({ children }: { children: React.ReactNode })
4444
}
4545
```
4646

47-
Use `Link` component form `next13-progressbar` in your entire app instead of native `next/link` to navigate with progressbar
47+
By default all `a` tags are handled by `next/link` so you don't need to do anything.
4848

4949
```jsx
50-
'use client';
51-
import { Link } from 'next13-progressbar';
50+
import Link from 'next/link';
5251

5352
export default function NavBar() {
5453
return (
@@ -61,8 +60,18 @@ export default function NavBar() {
6160
}
6261
```
6362

63+
But if you want to use `Link` component from `next13-progressbar` you can do it by importing it from `next13-progressbar` and use it as you would use `next/link` component.
64+
65+
```jsx
66+
'use client';
67+
import { Link } from 'next13-progressbar';
68+
69+
// use it as you would use next/link
70+
```
71+
6472
And you are done. You can now customize you progress bar as you need!
65-
⚠️ Note: You must use `Link` component inside client components only.
73+
74+
⚠️ Note: You must use `next13-progressbar` `Link` component inside client components only.
6675

6776
### Default Config
6877

example/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
"eslint": "8.43.0",
1616
"eslint-config-next": "13.4.7",
1717
"next": "13.4.7",
18-
"next13-progressbar": "^0.1.0",
18+
"next13-progressbar": "^0.1.2",
1919
"react": "18.2.0",
2020
"react-dom": "18.2.0",
2121
"typescript": "5.1.5"
22-
},
23-
"devDependencies": {
24-
"husky": "^8.0.3"
2522
}
2623
}

example/src/components/NavBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
2-
import { Link } from 'next13-progressbar';
32
import React from 'react';
3+
import Link from 'next/link';
44

55
const NavBar = () => {
66
return (

example/yarn.lock

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,11 +1158,6 @@ human-signals@^4.3.0:
11581158
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-4.3.1.tgz#ab7f811e851fca97ffbd2c1fe9a958964de321b2"
11591159
integrity sha512-nZXjEF2nbo7lIw3mgYjItAfgQXog3OjJogSbKa2CQIIvSGWcKgeJnQlNXip6NglNzYH45nSRiEVimMvYL8DDqQ==
11601160

1161-
husky@^8.0.3:
1162-
version "8.0.3"
1163-
resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.3.tgz#4936d7212e46d1dea28fef29bb3a108872cd9184"
1164-
integrity sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg==
1165-
11661161
ignore@^5.2.0:
11671162
version "5.2.4"
11681163
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.2.4.tgz#a291c0c6178ff1b960befe47fcdec301674a6324"
@@ -1511,10 +1506,10 @@ natural-compare@^1.4.0:
15111506
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
15121507
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==
15131508

1514-
next13-progressbar@^0.1.0:
1515-
version "0.1.0"
1516-
resolved "https://registry.yarnpkg.com/next13-progressbar/-/next13-progressbar-0.1.0.tgz#81310c50104aebefd0e379fcd53fa538c797fa65"
1517-
integrity sha512-BK7E3VM4JvDy0KSKT4sgcss+LIN8UmfW2CzuN2Q3n/mE5K/f3Jv5BKLDuY1a+3HCjhHrEfhL1izgFghzlk86aw==
1509+
next13-progressbar@^0.1.2:
1510+
version "0.1.2"
1511+
resolved "https://registry.yarnpkg.com/next13-progressbar/-/next13-progressbar-0.1.2.tgz#4b7c846908c548dcb6948bb33120d81ef65c589c"
1512+
integrity sha512-6r3J/GFeTcVLkkBd615sutRUR1oLP341VLbXv+54S+7qzoB5q66pBMNq2VI5cT8hXjSKvVvHeSadUCpomFG4pw==
15181513
dependencies:
15191514
"@types/nprogress" "^0.2.0"
15201515
nprogress "^0.2.0"

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "next13-progressbar",
3-
"version": "0.1.0",
3+
"version": "0.1.2",
44
"description": "A ProgressBar for next.js 13 with app directory ",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -25,6 +25,7 @@
2525
"@types/react": "^18.2.14",
2626
"@typescript-eslint/eslint-plugin": "^5.60.1",
2727
"eslint": "^8.43.0",
28+
"husky": "^8.0.3",
2829
"next": "^13.4.7",
2930
"prettier": "^2.8.8",
3031
"react": "^18.2.0",

src/CLink.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Link, { LinkProps } from 'next/link';
33
import { usePathname } from 'next/navigation';
44
import React from 'react';
5-
import { useApp } from './NProvider';
5+
import { useNProgress } from './NProvider';
66

77
export interface PLinkProps extends LinkProps {
88
children: React.ReactNode;
@@ -14,7 +14,7 @@ export interface PLinkProps extends LinkProps {
1414
*/
1515
export const CLink = (props: PLinkProps) => {
1616
const pathname = usePathname();
17-
const { setShowProgressBar } = useApp();
17+
const { setShowProgressBar } = useNProgress();
1818

1919
const handleShowProgressBar = () => {
2020
if (pathname !== props.href) {

src/NProvider.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import ProgressBar, { Next13ProgressProps } from './ProgressBar';
33
import { NavigationEvents } from './NavigationEvents';
4+
import { usePathname } from 'next/navigation';
45
// import { usePathname } from 'next/navigation';
56

67
export interface NProviderProps extends Next13ProgressProps {
@@ -14,13 +15,23 @@ interface NProviderState {
1415

1516
const NPContext = React.createContext<NProviderState>({
1617
showProgressBar: false,
17-
// eslint-disable-next-line @typescript-eslint/no-empty-function
1818
setShowProgressBar: () => {},
1919
});
2020

2121
export const NProvider = (props: NProviderProps) => {
2222
const [showProgressBar, setShowProgressBar] = React.useState(false);
23-
// const pathname = usePathname();
23+
const pathname = usePathname();
24+
25+
useEffect(() => {
26+
const anchors = document.querySelectorAll('a');
27+
anchors.forEach((anchor) => {
28+
anchor.addEventListener('click', () => {
29+
if (pathname !== anchor.href) {
30+
setShowProgressBar(true);
31+
}
32+
});
33+
});
34+
}, []);
2435

2536
const progressProps = { ...props };
2637
delete progressProps.children;
@@ -33,4 +44,4 @@ export const NProvider = (props: NProviderProps) => {
3344
);
3445
};
3546

36-
export const useApp = () => React.useContext(NPContext);
47+
export const useNProgress = () => React.useContext(NPContext);

src/NavigationEvents.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use client';
22
import { useEffect } from 'react';
33
import { usePathname } from 'next/navigation';
4-
import { useApp } from './NProvider';
4+
import { useNProgress } from './NProvider';
55

66
export function NavigationEvents() {
77
const pathname = usePathname();
8-
const { setShowProgressBar } = useApp();
8+
const { setShowProgressBar } = useNProgress();
99

1010
useEffect(() => {
1111
setShowProgressBar(false);

0 commit comments

Comments
 (0)