homeflowjs 1.0.17 → 1.0.18

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,7 +1,8 @@
1
1
  import { useEffect } from 'react';
2
2
  import { isFunction } from '../utils';
3
3
 
4
- const scrollToTarget = (target, { yOffset = 0 } = {}) => {
4
+ const scrollToTarget = (target, { yOffset = 0, additionalYOffset = 0 } = {}) => {
5
+
5
6
  // If target is chunk ID
6
7
  let targetElement;
7
8
  if (!isNaN(target)) {
@@ -25,33 +26,46 @@ const scrollToTarget = (target, { yOffset = 0 } = {}) => {
25
26
  }
26
27
  }
27
28
 
28
- const y = targetElement.getBoundingClientRect().top
29
- + window.pageYOffset + verticalOffset;
29
+ if(additionalYOffset) {
30
+ verticalOffset = verticalOffset + parseInt(additionalYOffset, 10);
31
+ }
32
+
33
+ let y = 0;
34
+ const targetTop = targetElement.getBoundingClientRect().top;
35
+ if(targetTop > 0) y =
36
+ targetElement.getBoundingClientRect().top +
37
+ window.pageYOffset +
38
+ verticalOffset;
30
39
 
31
40
  window.scrollTo({ top: y, behavior: 'smooth' });
32
41
  history.scrollRestoration = 'manual';
33
42
  }
34
43
  };
35
44
 
36
- const getTarget = (hash) => {
45
+ const getURLParams = (hash) => {
37
46
  const [, search] = hash.split('?');
38
- const { target } = Object.fromEntries(new URLSearchParams(search));
39
- return target;
47
+ const { target, yOffset } = Object.fromEntries(new URLSearchParams(search));
48
+ return { target, yOffsetFromURLParams: yOffset };
40
49
  };
41
50
 
42
51
  function handleScrollToClick(options) {
43
52
  // eslint-disable-next-line func-names
44
53
  return function () {
45
- const target = getTarget(this.hash);
54
+ const { target, yOffsetFromURLParams } = getURLParams(this.hash);
46
55
  if (target) {
47
- scrollToTarget(target, options);
56
+ scrollToTarget(target, {
57
+ ...options,
58
+ additionalYOffset: yOffsetFromURLParams,
59
+ });
48
60
  }
49
61
  };
50
62
  }
51
63
 
52
64
  const useScrollTo = (options) => {
53
65
  useEffect(() => {
54
- const scrollToEls = document.querySelectorAll('[href^="#scroll-to"]');
66
+ const scrollToEls = document.querySelectorAll(
67
+ '[href^="#scroll-to"], [href^="#/scroll-to"]'
68
+ );
55
69
  scrollToEls.forEach((el) => {
56
70
  el.addEventListener(
57
71
  'click',
@@ -59,8 +73,11 @@ const useScrollTo = (options) => {
59
73
  );
60
74
  });
61
75
 
62
- const target = getTarget(window.location.hash);
63
- scrollToTarget(target, options);
76
+ const { target, yOffsetFromURLParams } = getURLParams(window.location.hash);
77
+ scrollToTarget(target, {
78
+ ...options,
79
+ additionalYOffset: yOffsetFromURLParams,
80
+ });
64
81
  }, []);
65
82
  };
66
83
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homeflowjs",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "sideEffects": [
5
5
  "modal/**/*",
6
6
  "user/default-profile/**/*",