simplify menu to avoid breaking anchor link navigation

This commit is contained in:
2024-08-15 00:58:43 +02:00
parent 1b62a7f637
commit 5976d51dda
2 changed files with 21 additions and 32 deletions

View File

@ -9,49 +9,32 @@ import { useInView } from "react-intersection-observer";
import { getSearchPath } from "@/lib/common";
export const Header = () => {
/*
This component depends on the following CSS rule in base.css to prevent scrolling
the body while the menu is open:
html:has(header[data-show=true]) {
overflow: hidden;
}
*/
const { ref: observer, inView: isInView } = useInView({
triggerOnce: false,
initialInView: true,
});
const { replace } = useRouter();
const pathname = usePathname();
const [showMenu, setShowMenu] = useState(false);
function toggleMenu() {
setShowMenu(!showMenu);
}
// remove ability to scroll when opening menu + keep previous scroll position when closing menu
useEffect(() => {
if (showMenu) {
const scrollY = window.scrollY;
document.body.style.position = "fixed";
document.body.style.top = `-${scrollY}px`;
} else {
const scrollY = parseInt(document.body.style.top || "0") * -1;
document.body.style.position = "";
document.body.style.top = "";
window.scrollTo(0, scrollY);
}
}, [showMenu]);
const pathname = usePathname();
// reset scroll position on path change
useEffect(() => {
if (!showMenu) {
window.scrollTo(0, 0);
}
}, [pathname, showMenu]);
// hide menu and reset active menu item on path change
// hide menu on path change
useEffect(() => {
setShowMenu(false);
setActiveMenuItem(undefined);
}, [pathname]);
const [activeMenuItem, setActiveMenuItem] = useState<string | undefined>(
undefined
);
const handleSearch = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key != "Enter") {
return;
@ -162,7 +145,9 @@ export const Header = () => {
</a>
</li>
<li className={styles.search}>
<label htmlFor="search" className="sr-only">Søk</label>
<label htmlFor="search" className="sr-only">
Søk
</label>
<input
name="search"
type="text"

View File

@ -14,6 +14,10 @@ body {
max-width: 100vw;
}
html:has(header[data-show=true]) {
overflow: hidden;
}
body {
height: 100%;
width: 100%;
@ -195,12 +199,12 @@ select {
.linkItem {
transition: opacity var(--transition-easing);
cursor: pointer;
&:hover {
opacity: .6;
transition: opacity var(--transition-easing);
}
&:has(:focus-visible) {
box-shadow: 0 0 0 1px var(--color-background), 0 0 0 3px var(--color-green-dark);
}