From f0213788138ff941698c889201cea8c1c40b5588 Mon Sep 17 00:00:00 2001 From: Chuck Dries Date: Sun, 10 Jul 2022 17:56:28 -0700 Subject: [PATCH] Remove location hash when user scrolls page --- src/pages/photogallery/[...].js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/pages/photogallery/[...].js b/src/pages/photogallery/[...].js index c01deeb..78f07cd 100644 --- a/src/pages/photogallery/[...].js +++ b/src/pages/photogallery/[...].js @@ -16,8 +16,21 @@ const SORT_KEYS = { date: [], }; +function removeHash() { + const url = new URL( + typeof window !== "undefined" + ? window.location.href.toString() + : "https://chuckdries.com/photogallery/" + ); + + url.hash = ""; + window.history.replaceState(null, "", url.href.toString()); + window.removeEventListener('wheel', removeHash); +} + const GalleryPage = ({ data }) => { - const hash = typeof window !== "undefined" ? window.location.hash.replace("#", "") : ""; + const hash = + typeof window !== "undefined" ? window.location.hash.replace("#", "") : ""; const [filterKeyword, _setKeyword] = React.useState(null); const [sortKey, _setSortKey] = React.useState("rating"); @@ -74,6 +87,7 @@ const GalleryPage = ({ data }) => { el.scrollIntoView({ block: "center", }); + window.addEventListener("wheel", removeHash); }, [hash]); React.useEffect(() => {