@adamjanicki/ui 1.3.7 → 1.3.8
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 +11 -2
- package/hooks/useScrollToHash.js +11 -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,15 @@
|
|
|
1
|
+
type UseScrollToHashConfig = {
|
|
2
|
+
/**
|
|
3
|
+
* Whether or not to scroll to the hash
|
|
4
|
+
*/
|
|
5
|
+
active: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* The scroll behavior to use
|
|
8
|
+
*/
|
|
9
|
+
behavior?: ScrollBehavior;
|
|
10
|
+
};
|
|
1
11
|
/**
|
|
2
12
|
* A hook for scrolling to a hash on the page.
|
|
3
|
-
* @returns a function that scrolls to the hash on the page
|
|
4
13
|
*/
|
|
5
|
-
declare const useScrollToHash: (
|
|
14
|
+
declare const useScrollToHash: ({ active, behavior, }: UseScrollToHashConfig) => void;
|
|
6
15
|
export default useScrollToHash;
|
package/hooks/useScrollToHash.js
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
function (behavior) {
|
|
6
|
+
var useScrollToHash = function (_a) {
|
|
7
|
+
var active = _a.active, _b = _a.behavior, behavior = _b === void 0 ? "smooth" : _b;
|
|
8
|
+
useEffect(function () {
|
|
9
|
+
if (!active)
|
|
10
|
+
return;
|
|
13
11
|
var hash = window.location.hash;
|
|
14
|
-
if (hash) {
|
|
15
|
-
var
|
|
16
|
-
|
|
17
|
-
element.scrollIntoView({ behavior: behavior });
|
|
18
|
-
}
|
|
12
|
+
if ((hash === null || hash === void 0 ? void 0 : hash.length) > 1) {
|
|
13
|
+
var id = hash.substring(1);
|
|
14
|
+
scrollToId(id, behavior);
|
|
19
15
|
}
|
|
20
|
-
}, []);
|
|
16
|
+
}, [active, behavior]);
|
|
21
17
|
};
|
|
22
18
|
export default useScrollToHash;
|