@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.
@@ -1,2 +1,3 @@
1
1
  import classNames from "./classNames";
2
- export { classNames };
2
+ import scrollToId from "./scrollToId";
3
+ export { classNames, scrollToId };
@@ -1,2 +1,3 @@
1
1
  import classNames from "./classNames";
2
- export { classNames };
2
+ import scrollToId from "./scrollToId";
3
+ export { classNames, scrollToId };
@@ -0,0 +1,6 @@
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: string, behavior?: ScrollBehavior): void;
@@ -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: () => (behavior?: ScrollBehavior) => void;
14
+ declare const useScrollToHash: ({ active, behavior, }: UseScrollToHashConfig) => void;
6
15
  export default useScrollToHash;
@@ -1,22 +1,18 @@
1
- import { useCallback } from "react";
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
- return useCallback(
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 (_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 element = document.getElementById(hash.substring(1));
16
- if (element) {
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamjanicki/ui",
3
- "version": "1.3.7",
3
+ "version": "1.3.8",
4
4
  "description": "Basic UI components and hooks for React in TypeScript",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",