Skip to content

Commit 6463342

Browse files
committed
Adding custom errors
1 parent 9a587e0 commit 6463342

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ yarn-debug.log*
66
yarn-error.log*
77
lerna-debug.log*
88

9+
#Builded files
10+
cjs/
11+
12+
# package json
13+
package-lock.json
14+
915
# Diagnostic reports (https://nodejs.org/api/report.html)
1016
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
1117

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"@rollup/plugin-babel": "^5.3.0",
4141
"@rollup/plugin-commonjs": "^19.0.2",
4242
"@rollup/plugin-replace": "^3.1.0",
43-
"standard-version": "^9.5.0"
43+
"standard-version": "^9.5.0",
4444
},
4545
"peerDependencies": {
4646
"react-router-dom": "^6.4.2"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default class UseScrollNavigateCoordinateError extends Error {
2+
3+
constructor(message) {
4+
super(message);
5+
this.name = "UseScrollNavigateCoordinateError"
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default class UseScrollNavigatePathError extends Error {
2+
3+
constructor(message) {
4+
super(message);
5+
this.name = "UseScrollNavigatePathError"
6+
}
7+
}

src/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import {useNavigate} from "react-router-dom";
2+
import UseScrollNavigatePathError from "./errors/useScrollNavigatePathError";
3+
import UseScrollNavigateCoordinateError from "./errors/useScrollNavigateCoordinateError";
4+
25

36
/**
47
*
@@ -12,18 +15,21 @@ import {useNavigate} from "react-router-dom";
1215
export default function useScrollNavigate(path, scrollX = 0 , scrollY = 0 ) {
1316

1417
if (typeof scrollX !== "number" || typeof scrollY !== "number") {
15-
throw new Error("scrollX and scrollY must be numbers");
18+
throw new UseScrollNavigateCoordinateError("scrollX and scrollY must be numbers");
1619
}
1720
if (typeof path !== "string") {
18-
throw new Error("path must be a string");
21+
throw new UseScrollNavigatePathError("path must be a string") ;
1922
}
2023
if(path === ""){
21-
throw new Error("path must not be empty");
24+
throw new UseScrollNavigatePathError("path must not be empty");
2225
}
2326

2427
const navigate = useNavigate();
2528
return () => {
26-
window.scrollTo(scrollX, scrollY);
29+
window.scroll({left: scrollX, top: scrollY, behavior: "smooth" });
2730
navigate(path);
2831
}
29-
}
32+
}
33+
34+
35+

0 commit comments

Comments
 (0)