Files
AndreaSlopFotografie/web/src/components/NavigationBar/MobileNavGroup.tsx
T

118 lines
4.6 KiB
TypeScript

import { Box, chakra, Flex } from '@chakra-ui/react';
import { t } from '@/i18n';
import { isSectionCurrent } from '@/lib/navCurrent';
import type { NavMenuEvent } from '@/lib/navMenuState';
import type { NavItem } from '@/navigation';
import RouterLink from '../RouterLink';
import ChevronIcon from './ChevronIcon';
type MobileNavGroupProps = {
item: NavItem & { children: readonly NavItem[] };
// Id of the sub-list, for aria-controls
id: string;
open: boolean;
pathname: string;
dispatch: (event: NavMenuEvent) => void;
};
// Exact: only the page itself is current (BR-NAV-01), also with a query
const exactActive = { exact: true, includeSearch: false } as const;
const duration = '0.3s';
const easing = 'cubic-bezier(.22, .61, .36, 1)';
// The parent row of the mobile menu: the link to the overview, a chevron
// button and an expandable list of sub-pages (BR-NAV-30..36)
export default function MobileNavGroup({
item,
id,
open,
pathname,
dispatch,
}: MobileNavGroupProps) {
return (
<>
<Flex alignItems="center" gap={1}>
<RouterLink
to={item.to}
activeOptions={exactActive}
onClick={() => dispatch({ type: 'linkChosen' })}
display="block"
fontFamily="heading"
fontSizeAdjust="0.43"
fontSize="1.9rem"
color={isSectionCurrent(item, pathname) ? 'accent' : 'ink'}
_currentPage={{ color: 'accent' }}
>
{item.label}
</RouterLink>
<chakra.button
type="button"
data-requires-js
aria-expanded={open}
aria-controls={id}
aria-label={t('site.nav.submenu', { name: item.label })}
data-open={open || undefined}
display="flex"
alignItems="center"
justifyContent="center"
// 44px: minimaal klikdoel op touch (BR-NAV-40)
w="44px"
h="44px"
flexShrink={0}
cursor="pointer"
color="ink"
onClick={() => dispatch({ type: 'toggleGroup' })}
>
<ChevronIcon />
</chakra.button>
</Flex>
{/* Grid rows 0fr to 1fr animate the height without measuring. On
close the visibility switches only after the collapse, so the
collapse is seen; collapsed links are neither focusable nor
read (BR-NAV-36). */}
<Box
id={id}
display="grid"
gridTemplateRows={open ? '1fr' : '0fr'}
visibility={open ? 'visible' : 'hidden'}
transition={`grid-template-rows ${duration} ${easing}, visibility 0s linear ${open ? '0s' : duration}`}
_motionReduce={{ transition: 'none' }}
>
<Box overflow="hidden" minH={0}>
<chakra.ul
listStyleType="none"
pl="1.25rem"
pt={2}
display="flex"
flexDirection="column"
>
{item.children.map((child) => (
<li key={child.to}>
<RouterLink
to={child.to}
activeOptions={exactActive}
onClick={() =>
dispatch({ type: 'linkChosen' })
}
display="flex"
alignItems="center"
minH="44px"
fontFamily="heading"
fontSizeAdjust="0.43"
fontSize="1.4rem"
color="ink"
// accent.deep: 1.4rem is not large text
_currentPage={{ color: 'accent.deep' }}
>
{child.label}
</RouterLink>
</li>
))}
</chakra.ul>
</Box>
</Box>
</>
);
}