Files
AndreaSlopFotografie/web/src/components/NavigationBar/NavigationBar.tsx
T
SluijsensandClaude Opus 5.5 783d47bac2 Adds mobile hamburger menu and sticky behavior to the navigation bar
Gives the bar a fixed height from the header size token so it lines up with
the hero, a paper background with a line border, and a shadow once scrolled.
On desktop it shrinks to 68px and the logo from 46px to 38px after scrolling;
on mobile it stays 66px and is always solid.

Below lg the links move into a full-screen MobileNav that unfolds from the
bar (clip-path), opened by the existing animated hamburger button. It closes
on link click and Escape, locks page scroll while open, and counts as closed
when the viewport grows to desktop. Fixes the button's misspelled aria-label
attribute. Menu items live in navItems.ts, shared by desktop and mobile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 23:12:50 +02:00

186 lines
8.0 KiB
TypeScript

import {
Flex,
HStack,
chakra,
Image,
Box,
Container,
useBreakpointValue,
} from '@chakra-ui/react';
import NavLink from './NavLink';
import MobileNav from './MobileNav';
import navItems from './navItems';
import useScrolledPast from '@/hooks/useScrolledPast';
import { useCallback, useState } from 'react';
import { useRouterState } from '@tanstack/react-router';
// Images
import logoLight from '../../assets/logo-light.png';
import logoDark from '../../assets/logo-dark.png';
export default function NavigationBar() {
let [open, setOpen] = useState(false);
const closeMenu = useCallback(() => setOpen(false), []);
// Het mobiele menu bestaat alleen onder lg; wordt het scherm breder, dan
// telt het als dicht.
const isDesktop = useBreakpointValue(
{ base: false, lg: true },
{ ssr: false },
);
const menuOpen = open && !isDesktop;
const pastHero = useScrolledPast(() => 150);
const isHome = useRouterState({
select: (state) => state.location.pathname === '/',
});
const isSolid = pastHero || !isHome;
// Doorzichtig over de hero, maar alleen op desktop. Op mobiel staat de
// balk altijd op papier, zoals in het design.
const overlay = <T,>(solid: T, transparent: T) =>
isSolid ? solid : { base: solid, lg: transparent };
return (
<>
<chakra.header
position="fixed"
top={0}
insetX={0}
zIndex="docked"
// Zelfde hoogte als waar de hero-content onder begint; kleiner na scrollen
h={pastHero ? 'header.sticky' : 'header'}
display="flex"
alignItems="center"
bg={overlay('bg', 'transparent')}
color={overlay('fg', 'white')}
borderBottomWidth="1px"
borderBottomColor={overlay('line', 'transparent')}
// Schaduw alleen na scrollen, zoals .header--sticky in het design
boxShadow={pastHero ? 'sm' : 'none'}
px={{ base: 4, md: 8 }}
transitionProperty="height, background-color, color, border-color, box-shadow"
transitionDuration="interaction"
transitionTimingFunction="smooth"
>
<Container>
<Flex
gap={6}
alignItems="center"
justifyContent="space-between"
>
<Box
flexBasis="auto"
flexGrow={1}
flexShrink={0}
aspectRatio="square"
// Kleiner na scrollen, maar alleen op desktop: op
// mobiel krimpt de balk ook niet
maxW={
pastHero ? { base: '46px', lg: '38px' } : '46px'
}
transitionProperty="max-width"
transitionDuration="interaction"
transitionTimingFunction="smooth"
>
{/* Twee logo's, zodat de kleur per schermbreedte kan wisselen */}
<Image
src={logoDark}
alt="Andrea Slop Fotografie"
display={overlay('block', 'none')}
/>
<Image
src={logoLight}
alt="Andrea Slop Fotografie"
display={overlay('none', 'block')}
/>
</Box>
<chakra.nav
aria-label="Hoofdmenu"
flexBasis="100%"
flexGrow={0}
flexShrink={1}
>
<HStack
as="ul"
gap={6}
listStyleType="none"
justifyContent="center"
// Op mobiel staan de links in het hamburgermenu
display={{ base: 'none', lg: 'flex' }}
>
{navItems.map((item) => (
<chakra.li key={item.to}>
<NavLink to={item.to}>
{item.label}
</NavLink>
</chakra.li>
))}
</HStack>
</chakra.nav>
<chakra.div
flexBasis="auto"
flexGrow={1}
flexShrink={0}
display="flex"
justifyContent="flex-end"
>
<chakra.button
onClick={() => setOpen(!menuOpen)}
aria-label={
menuOpen ? 'Menu sluiten' : 'Menu openen'
}
aria-expanded={menuOpen}
aria-controls="mobile-nav"
data-open={menuOpen || undefined}
// Op desktop opent deze knop straks het info-menu (side area)
display={{ base: 'block', lg: 'none' }}
position="relative"
w="32px"
h="32px"
cursor="pointer"
zIndex="modal"
css={{
'& > span': {
position: 'absolute',
left: '4px',
width: '24px',
height: '2px',
bg: 'currentColor',
borderRadius: '1px',
transition:
'transform 0.3s ease, opacity 0.2s ease, top 0.3s ease',
},
'& > span:nth-of-type(1)': { top: '9px' },
'& > span:nth-of-type(2)': { top: '15px' },
'& > span:nth-of-type(3)': { top: '21px' },
// open kruisje
'&[data-open] > span:nth-of-type(1)': {
top: '15px',
transform: 'rotate(45deg)',
},
'&[data-open] > span:nth-of-type(2)': {
opacity: 0,
top: '15px',
transform: 'rotate(45deg)',
},
'&[data-open] > span:nth-of-type(3)': {
top: '15px',
transform: 'rotate(-45deg)',
},
}}
>
<span />
<span />
<span />
</chakra.button>
</chakra.div>
</Flex>
</Container>
</chakra.header>
<MobileNav id="mobile-nav" open={menuOpen} onClose={closeMenu} />
</>
);
}