@adamjanicki/ui 1.3.8 → 1.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
type UseScrollToHashConfig = {
|
|
2
2
|
/**
|
|
3
3
|
* Whether or not to scroll to the hash
|
|
4
|
+
* @default true
|
|
4
5
|
*/
|
|
5
|
-
active
|
|
6
|
+
active?: boolean;
|
|
6
7
|
/**
|
|
7
8
|
* The scroll behavior to use
|
|
8
9
|
*/
|
|
9
10
|
behavior?: ScrollBehavior;
|
|
11
|
+
/**
|
|
12
|
+
* Delay in ms to set using setTimeout
|
|
13
|
+
*/
|
|
14
|
+
delay?: number;
|
|
10
15
|
};
|
|
11
16
|
/**
|
|
12
17
|
* A hook for scrolling to a hash on the page.
|
|
13
18
|
*/
|
|
14
|
-
declare const useScrollToHash: (
|
|
19
|
+
declare const useScrollToHash: (config: UseScrollToHashConfig) => void;
|
|
15
20
|
export default useScrollToHash;
|
package/hooks/useScrollToHash.js
CHANGED
|
@@ -3,16 +3,19 @@ import scrollToId from "../functions/scrollToId";
|
|
|
3
3
|
/**
|
|
4
4
|
* A hook for scrolling to a hash on the page.
|
|
5
5
|
*/
|
|
6
|
-
var useScrollToHash = function (
|
|
7
|
-
var
|
|
6
|
+
var useScrollToHash = function (config) {
|
|
7
|
+
var _a = config.active, active = _a === void 0 ? true : _a, behavior = config.behavior, delay = config.delay;
|
|
8
8
|
useEffect(function () {
|
|
9
|
-
if (!active)
|
|
10
|
-
return;
|
|
11
9
|
var hash = window.location.hash;
|
|
12
|
-
if ((hash === null || hash === void 0 ? void 0 : hash.length)
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
if (!active || (hash === null || hash === void 0 ? void 0 : hash.length) <= 1)
|
|
11
|
+
return;
|
|
12
|
+
var id = hash.substring(1);
|
|
13
|
+
scrollToId(id, behavior);
|
|
14
|
+
if (delay !== undefined) {
|
|
15
|
+
var timeout_1 = setTimeout(function () { return scrollToId(id, behavior); }, delay);
|
|
16
|
+
return function () { return clearTimeout(timeout_1); };
|
|
15
17
|
}
|
|
16
|
-
|
|
18
|
+
scrollToId(id, behavior);
|
|
19
|
+
}, [active, behavior, delay]);
|
|
17
20
|
};
|
|
18
21
|
export default useScrollToHash;
|