@adamjanicki/ui 1.3.7 → 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.
- package/functions/index.d.ts +2 -1
- package/functions/index.js +2 -1
- package/functions/scrollToId.d.ts +6 -0
- package/functions/scrollToId.js +17 -0
- package/hooks/useScrollToHash.d.ts +16 -2
- package/hooks/useScrollToHash.js +14 -15
- package/package.json +1 -1
package/functions/index.d.ts
CHANGED
package/functions/index.js
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scroll to the element with the given id
|
|
3
|
+
* @param id the id of the element to scroll to
|
|
4
|
+
* @param behavior behavior of the scroll
|
|
5
|
+
*/
|
|
6
|
+
export default function scrollToId(id, behavior) {
|
|
7
|
+
try {
|
|
8
|
+
// catch cases where hash is not a valid id
|
|
9
|
+
var element = document.getElementById(id);
|
|
10
|
+
if (element) {
|
|
11
|
+
element.scrollIntoView({ behavior: behavior });
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
catch (_a) {
|
|
15
|
+
// do nothing
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -1,6 +1,20 @@
|
|
|
1
|
+
type UseScrollToHashConfig = {
|
|
2
|
+
/**
|
|
3
|
+
* Whether or not to scroll to the hash
|
|
4
|
+
* @default true
|
|
5
|
+
*/
|
|
6
|
+
active?: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* The scroll behavior to use
|
|
9
|
+
*/
|
|
10
|
+
behavior?: ScrollBehavior;
|
|
11
|
+
/**
|
|
12
|
+
* Delay in ms to set using setTimeout
|
|
13
|
+
*/
|
|
14
|
+
delay?: number;
|
|
15
|
+
};
|
|
1
16
|
/**
|
|
2
17
|
* A hook for scrolling to a hash on the page.
|
|
3
|
-
* @returns a function that scrolls to the hash on the page
|
|
4
18
|
*/
|
|
5
|
-
declare const useScrollToHash: (
|
|
19
|
+
declare const useScrollToHash: (config: UseScrollToHashConfig) => void;
|
|
6
20
|
export default useScrollToHash;
|
package/hooks/useScrollToHash.js
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import scrollToId from "../functions/scrollToId";
|
|
2
3
|
/**
|
|
3
4
|
* A hook for scrolling to a hash on the page.
|
|
4
|
-
* @returns a function that scrolls to the hash on the page
|
|
5
5
|
*/
|
|
6
|
-
var useScrollToHash = function () {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* Scrolls to the hash on the page.
|
|
10
|
-
* @param behavior the behavior of the scroll
|
|
11
|
-
*/
|
|
12
|
-
function (behavior) {
|
|
6
|
+
var useScrollToHash = function (config) {
|
|
7
|
+
var _a = config.active, active = _a === void 0 ? true : _a, behavior = config.behavior, delay = config.delay;
|
|
8
|
+
useEffect(function () {
|
|
13
9
|
var hash = window.location.hash;
|
|
14
|
-
if (hash)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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); };
|
|
19
17
|
}
|
|
20
|
-
|
|
18
|
+
scrollToId(id, behavior);
|
|
19
|
+
}, [active, behavior, delay]);
|
|
21
20
|
};
|
|
22
21
|
export default useScrollToHash;
|