homeflowjs 1.0.70 → 1.0.72
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/use-scroll-to.hook.js +12 -6
- package/package.json +1 -1
@@ -79,15 +79,15 @@ const useScrollTo = (options) => {
|
|
79
79
|
const [currentTarget, setCurrentTarget] = useState(null);
|
80
80
|
const scrollAbortControllerRef = useRef(null);
|
81
81
|
|
82
|
-
const handleScrollTo = () => {
|
83
|
-
const { target, yOffsetFromURLParams } = getURLParams(window.location.hash);
|
84
|
-
|
85
|
-
|
86
|
-
if(!targetElement) return;
|
82
|
+
const handleScrollTo = (hash) => {
|
83
|
+
const { target, yOffsetFromURLParams } = getURLParams(hash || window.location.hash);
|
84
|
+
if (!target) return;
|
87
85
|
|
86
|
+
const targetElement = getTargetElementHelper(target);
|
88
87
|
const targetObj = {
|
89
88
|
targetElement,
|
90
89
|
additionalYOffset: yOffsetFromURLParams,
|
90
|
+
...(hash && { hash }), // Add hash to the target object so onIntersect can update the URL
|
91
91
|
...options,
|
92
92
|
}
|
93
93
|
|
@@ -126,6 +126,7 @@ const useScrollTo = (options) => {
|
|
126
126
|
scrollAbortControllerRef.current.abort(); // Stop the scroll
|
127
127
|
scrollAbortControllerRef.current = null; // Reset the controller
|
128
128
|
scrollToTarget({ ...currentTarget }); // Fire the scrollToTarget function again
|
129
|
+
if (currentTarget?.hash) window.location.hash = currentTarget.hash; // Update the URL hash
|
129
130
|
}
|
130
131
|
}
|
131
132
|
});
|
@@ -142,8 +143,13 @@ const useScrollTo = (options) => {
|
|
142
143
|
el.addEventListener(
|
143
144
|
'click',
|
144
145
|
(e) => {
|
146
|
+
|
147
|
+
/**
|
148
|
+
* Prevent default behavior, hash change is handled onIntersect,
|
149
|
+
* otherwise it will cause the scroll to be instant
|
150
|
+
*/
|
145
151
|
e.preventDefault();
|
146
|
-
handleScrollTo();
|
152
|
+
handleScrollTo(e.currentTarget.getAttribute('href'));
|
147
153
|
},
|
148
154
|
{ signal: clickEventController.signal }
|
149
155
|
);
|