fix hash nav stuff

This commit is contained in:
Chuck Dries 2023-03-09 21:23:23 -08:00
parent f21fc5efd1
commit fa2a639dbb

View File

@ -90,6 +90,9 @@ const GalleryPage = ({
); );
const removeHash = React.useCallback(() => { const removeHash = React.useCallback(() => {
if (!hash.length) {
return;
}
const url = new URL( const url = new URL(
typeof window !== "undefined" typeof window !== "undefined"
? window.location.href.toString() ? window.location.href.toString()
@ -97,9 +100,9 @@ const GalleryPage = ({
); );
url.hash = ""; url.hash = "";
navigate(url.href.toString()); window.history.replaceState(null, "", url.href.toString());
window.removeEventListener("wheel", removeHash); window.removeEventListener("wheel", removeHash);
}, []); }, [hash]);
React.useEffect(() => { React.useEffect(() => {
window.addEventListener("wheel", removeHash); window.addEventListener("wheel", removeHash);
@ -110,7 +113,7 @@ const GalleryPage = ({
// hacky but it works for now // hacky but it works for now
requestAnimationFrame(() => { requestAnimationFrame(() => {
// don't scroll into view if user got here with back button or if we just cleared it // don't scroll into view if user got here with back button or if we just cleared it
if (!hash || !hash.length) { if (!hash.length) {
return; return;
} }
const el = document.getElementById(hash); const el = document.getElementById(hash);
@ -118,6 +121,7 @@ const GalleryPage = ({
console.log("⚠failed to find hash"); console.log("⚠failed to find hash");
return; return;
} }
console.log('scrolling into view manually')
el.scrollIntoView({ el.scrollIntoView({
block: hash.startsWith("all") ? "start" : "center", block: hash.startsWith("all") ? "start" : "center",
}); });