make it work on nav

This commit is contained in:
Simon Knott 2024-11-20 10:14:50 +01:00
parent 4276d45ee9
commit 00d3b41c9f
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -134,13 +134,14 @@ export function useAnchor(id: AnchorID, onReveal: () => void) {
const listener = () => { const listener = () => {
const params = new URLSearchParams(window.location.hash.slice(1)); const params = new URLSearchParams(window.location.hash.slice(1));
if (!params.has('anchor')) if (params.has('anchor') && matchesAnchor(id, params.get('anchor')!))
return;
const anchor = params.get('anchor');
if (matchesAnchor(id, anchor!))
onReveal(); onReveal();
}; };
window.addEventListener('popstate', listener); window.addEventListener('popstate', listener);
// check if we're already on the anchor. required to make it work on page load.
listener();
return () => window.removeEventListener('popstate', listener); return () => window.removeEventListener('popstate', listener);
}, [id, onReveal]); }, [id, onReveal]);
} }