@weser/hooks 0.0.11 → 0.0.13
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/dist/useRouteChange.js +15 -2
- package/package.json +2 -2
package/dist/useRouteChange.js
CHANGED
|
@@ -9,11 +9,24 @@ export default function useRouteChange(onRouteChange, pathname) {
|
|
|
9
9
|
useEffect(() => {
|
|
10
10
|
const onClick = (e) => {
|
|
11
11
|
const target = e.target;
|
|
12
|
-
|
|
12
|
+
const anchor = getContainingAnchor(e.target);
|
|
13
|
+
if ((target.tagName === 'A' && target.href === pathname) ||
|
|
14
|
+
(anchor !== null && anchor?.pathname === pathname)) {
|
|
13
15
|
onRouteChange(pathname);
|
|
14
16
|
}
|
|
15
17
|
};
|
|
16
18
|
window.addEventListener('click', onClick);
|
|
17
19
|
return () => window.removeEventListener('click', onClick);
|
|
18
|
-
}, []);
|
|
20
|
+
}, [pathname]);
|
|
21
|
+
}
|
|
22
|
+
function getContainingAnchor(node) {
|
|
23
|
+
if (!node || !(node instanceof Node))
|
|
24
|
+
return null;
|
|
25
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
26
|
+
return node.closest('a');
|
|
27
|
+
}
|
|
28
|
+
// For text/other nodes, start at the nearest Element ancestor
|
|
29
|
+
const parent = node.parentElement ||
|
|
30
|
+
node.parentNode;
|
|
31
|
+
return parent?.closest('a');
|
|
19
32
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weser/hooks",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"description": "React hooks for building apps",
|
|
5
5
|
"author": "Robin Weser <robin@weser.io>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
"rimraf": "^3.0.2",
|
|
53
53
|
"typescript": "^5.4.5"
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "2e5430bd1d2f421a9ab85dff0918b89023f8039b"
|
|
56
56
|
}
|