|
1 | 1 | //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"; |
4 | 4 |
|
5 | 5 | //Utils |
6 | | -import { |
7 | | - DEFAULT_SCROLL_BEHAVIOR, |
8 | | - DEFAULT_SCROLL_POSITION, |
9 | | -} from "../Utils/constants"; |
10 | | -import { DEFAULT_SCROLL_FUNC } from "../Utils/functions"; |
11 | 6 | import { BaseScrollOptions } from "../Utils/types"; |
12 | 7 |
|
13 | 8 | export interface HashScrollProps extends Partial<BaseScrollOptions> { |
@@ -37,42 +32,17 @@ export interface HashScrollProps extends Partial<BaseScrollOptions> { |
37 | 32 | const HashScroll: FC<HashScrollProps> = ({ |
38 | 33 | hash, |
39 | 34 | children, |
40 | | - behavior = DEFAULT_SCROLL_BEHAVIOR, |
41 | | - position = DEFAULT_SCROLL_POSITION, |
| 35 | + behavior, |
| 36 | + position, |
42 | 37 | requiredPathname, |
43 | | - scrollFunc = DEFAULT_SCROLL_FUNC, |
| 38 | + scrollFunc, |
44 | 39 | }) => { |
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, { |
73 | 41 | behavior, |
74 | 42 | position, |
75 | | - ]); |
| 43 | + requiredPathname, |
| 44 | + scrollFunc, |
| 45 | + }); |
76 | 46 |
|
77 | 47 | return cloneElement(children, { |
78 | 48 | ref: childRef, |
|
0 commit comments