Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added active state for sub-pages #236

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/components/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ function useInitialValue<T>(value: T, condition = true) {
return condition ? initialValue : value
}

function matchPath(pathname: string, href: string) {
if (href === '/components') {
return pathname === '/components'
}
return pathname.startsWith(href)
}

function NavLink({
href,
children,
Expand Down Expand Up @@ -83,7 +90,8 @@ function VisibleSectionHighlight({
? Math.max(1, visibleSections.length) * itemHeight
: itemHeight
let top =
group.links.findIndex((link) => link.href === pathname) * itemHeight +
group.links.findIndex((link) => matchPath(pathname, link.href)) *
itemHeight +
firstVisibleSectionIndex * itemHeight

return (
Expand All @@ -108,7 +116,9 @@ function ActivePageMarker({
}) {
let itemHeight = remToPx(2)
let offset = remToPx(0.25)
let activePageIndex = group.links.findIndex((link) => link.href === pathname)
let activePageIndex = group.links.findIndex((link) =>
matchPath(pathname, link.href),
)
let top = offset + activePageIndex * itemHeight

return (
Expand Down Expand Up @@ -143,7 +153,7 @@ function NavigationGroup({
)

let isActiveGroup =
group.links.findIndex((link) => link.href === pathname) !== -1
group.links.findIndex((link) => matchPath(pathname, link.href)) !== -1

return (
<li className={clsx('relative mt-6', className)}>
Expand Down Expand Up @@ -173,13 +183,13 @@ function NavigationGroup({
<motion.li key={link.href} layout="position" className="relative">
<NavLink
href={link.href}
active={link.href === pathname}
active={matchPath(pathname, link.href)}
tag={link.tag}
>
{link.title}
</NavLink>
<AnimatePresence mode="popLayout" initial={false}>
{link.href === pathname && sections.length > 0 && (
{matchPath(pathname, link.href) && sections.length > 0 && (
<motion.ul
role="list"
initial={{ opacity: 0 }}
Expand Down
Loading