Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit 73581b9

Browse files
committed
HashScroll uses useHashScroll
1 parent 120f764 commit 73581b9

File tree

2 files changed

+9
-41
lines changed

2 files changed

+9
-41
lines changed

scripts/postcommit.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ const postcommit = async () => {
1616
if ((await getBranches()).current.includes("main")) {
1717
const { stdout: changed } = await getFilesChangedInCommit("HEAD");
1818

19-
console.log(changed);
20-
2119
const docsDir = join(ROOT_DIR, "docs");
2220

2321
const docsPath = "docs/";

src/Components/HashScroll.tsx

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
//React Imports
2-
import { useRef, FC, ReactElement, cloneElement, useEffect } from "react";
3-
import { useLocation } from "react-router-dom";
2+
import { FC, ReactElement, cloneElement } from "react";
3+
import useHashScroll from "../Hooks/useHashScroll";
44

55
//Utils
6-
import {
7-
DEFAULT_SCROLL_BEHAVIOR,
8-
DEFAULT_SCROLL_POSITION,
9-
} from "../Utils/constants";
10-
import { DEFAULT_SCROLL_FUNC } from "../Utils/functions";
116
import { BaseScrollOptions } from "../Utils/types";
127

138
export interface HashScrollProps extends Partial<BaseScrollOptions> {
@@ -37,42 +32,17 @@ export interface HashScrollProps extends Partial<BaseScrollOptions> {
3732
const HashScroll: FC<HashScrollProps> = ({
3833
hash,
3934
children,
40-
behavior = DEFAULT_SCROLL_BEHAVIOR,
41-
position = DEFAULT_SCROLL_POSITION,
35+
behavior,
36+
position,
4237
requiredPathname,
43-
scrollFunc = DEFAULT_SCROLL_FUNC,
38+
scrollFunc,
4439
}) => {
45-
const { hash: urlHash, pathname } = useLocation();
46-
47-
const childRef = useRef<HTMLElement>(null);
48-
49-
if (hash.charAt(0) !== "#") {
50-
hash = "#" + hash;
51-
}
52-
53-
if (typeof requiredPathname === "string") {
54-
requiredPathname = [requiredPathname];
55-
}
56-
57-
useEffect(() => {
58-
if (
59-
urlHash === hash &&
60-
(requiredPathname === undefined || requiredPathname.includes(pathname))
61-
) {
62-
if (childRef.current) {
63-
scrollFunc(childRef, behavior, position);
64-
}
65-
}
66-
}, [
67-
urlHash,
68-
childRef,
69-
hash,
70-
requiredPathname,
71-
pathname,
72-
scrollFunc,
40+
const childRef = useHashScroll(hash, {
7341
behavior,
7442
position,
75-
]);
43+
requiredPathname,
44+
scrollFunc,
45+
});
7646

7747
return cloneElement(children, {
7848
ref: childRef,

0 commit comments

Comments
 (0)