homeflowjs 1.0.15 → 1.0.16
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/hooks/index.js +3 -1
- package/hooks/use-scroll-to.hook.js +67 -0
- package/package.json +1 -1
- package/utils/index.js +4 -0
package/hooks/index.js
CHANGED
@@ -6,6 +6,7 @@ import usePropertyInfiniteScroll from './use-property-inifinite-scroll';
|
|
6
6
|
import useLoadBranches from './use-load-branches';
|
7
7
|
import useLoadPreviousProperties from './use-load-previous-properties';
|
8
8
|
import useRecaptcha from './use-recaptcha';
|
9
|
+
import useScrollTo from './use-scroll-to.hook';
|
9
10
|
|
10
11
|
export {
|
11
12
|
useDefaultSort,
|
@@ -15,5 +16,6 @@ export {
|
|
15
16
|
usePropertyInfiniteScroll,
|
16
17
|
useLoadBranches,
|
17
18
|
useLoadPreviousProperties,
|
18
|
-
useRecaptcha
|
19
|
+
useRecaptcha,
|
20
|
+
useScrollTo,
|
19
21
|
};
|
@@ -0,0 +1,67 @@
|
|
1
|
+
import { useEffect } from 'react';
|
2
|
+
import { isFunction } from '../utils';
|
3
|
+
|
4
|
+
const scrollToTarget = (target, { yOffset = 0 } = {}) => {
|
5
|
+
// If target is chunk ID
|
6
|
+
let targetElement;
|
7
|
+
if (!isNaN(target)) {
|
8
|
+
targetElement = document.querySelector(
|
9
|
+
`[data-content-id="${target}"]`,
|
10
|
+
);
|
11
|
+
}
|
12
|
+
|
13
|
+
// Check for ID or class name
|
14
|
+
if (!targetElement) targetElement = document.querySelector(`.${target}`);
|
15
|
+
if (!targetElement) targetElement = document.querySelector(`#${target}`);
|
16
|
+
|
17
|
+
if (targetElement) {
|
18
|
+
let verticalOffset = 0;
|
19
|
+
if (typeof yOffset === 'number') {
|
20
|
+
verticalOffset = yOffset;
|
21
|
+
} else if (isFunction(yOffset)) {
|
22
|
+
const value = yOffset();
|
23
|
+
if (typeof value === 'number') {
|
24
|
+
verticalOffset = value;
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
const y = targetElement.getBoundingClientRect().top
|
29
|
+
+ window.pageYOffset + verticalOffset;
|
30
|
+
|
31
|
+
window.scrollTo({ top: y, behavior: 'smooth' });
|
32
|
+
history.scrollRestoration = 'manual';
|
33
|
+
}
|
34
|
+
};
|
35
|
+
|
36
|
+
const getTarget = (hash) => {
|
37
|
+
const [, search] = hash.split('?');
|
38
|
+
const { target } = Object.fromEntries(new URLSearchParams(search));
|
39
|
+
return target;
|
40
|
+
};
|
41
|
+
|
42
|
+
function handleScrollToClick(options) {
|
43
|
+
// eslint-disable-next-line func-names
|
44
|
+
return function () {
|
45
|
+
const target = getTarget(this.hash);
|
46
|
+
if (target) {
|
47
|
+
scrollToTarget(target, options);
|
48
|
+
}
|
49
|
+
};
|
50
|
+
}
|
51
|
+
|
52
|
+
const useScrollTo = (options) => {
|
53
|
+
useEffect(() => {
|
54
|
+
const scrollToEls = document.querySelectorAll('[href^="#scroll-to"]');
|
55
|
+
scrollToEls.forEach((el) => {
|
56
|
+
el.addEventListener(
|
57
|
+
'click',
|
58
|
+
handleScrollToClick(options),
|
59
|
+
);
|
60
|
+
});
|
61
|
+
|
62
|
+
const target = getTarget(window.location.hash);
|
63
|
+
scrollToTarget(target, options);
|
64
|
+
}, []);
|
65
|
+
};
|
66
|
+
|
67
|
+
export default useScrollTo;
|
package/package.json
CHANGED
package/utils/index.js
CHANGED
@@ -153,3 +153,7 @@ export function JSON_to_URLEncoded(element,key,list){
|
|
153
153
|
}
|
154
154
|
return encodeURI(list.join('&'));
|
155
155
|
}
|
156
|
+
|
157
|
+
export function isFunction(functionToCheck) {
|
158
|
+
return functionToCheck && Object.prototype.toString.call(functionToCheck) === '[object Function]';
|
159
|
+
}
|