Skip to content

Less ideal but more stable active nav link highlight #17

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

Merged
merged 5 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
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
60 changes: 34 additions & 26 deletions next/src/components/Navigation/NavGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import Link from 'next/link'
import { useRouter } from 'next/router'
import { NavigationGroup } from './config'
import NavLink from './NavLink'
import { VisibleSectionHighlight } from './VisibleSectionHighlight'
import { remToPx } from '@/lib/remToPx'

interface NavGroupProps {
group: NavigationGroup
Expand All @@ -25,54 +23,64 @@ export function NavGroup({ group, className }: NavGroupProps) {
[useRouter(), useSectionStore((s: any) => s.sections)],
isInsideMobileNavigation,
)

let isActiveGroup =
group.group.href === router.pathname ||
(group.links &&
group.links.findIndex((link) => link.href === router.pathname) !== -1)

const groupTitleHeight = document.getElementById(
`${group.group.href}-link`,
)?.offsetHeight

return (
<li className={clsx('relative mt-6', className)}>
<motion.h2
layout="position"
className="text-xs font-semibold text-zinc-900 dark:text-white"
>
<Link
id={`${group.group.href}-link`}
href={group.group.href}
aria-current={isActiveGroup ? 'page' : undefined}
className={lora.className}
className={`${lora.className} relative block`}
>
{group.group.title}
{group.group.title}{' '}
{group.group.href === router.pathname && (
<div
className={clsx(
'absolute',
'top-[-4px]',
'bottom-[-4px]',
'left-[-8px]',
'right-0',
'bg-indigo-600/30',
'dark:bg-white/2.5',
'rounded-[8px]',
)}
/>
)}
</Link>
</motion.h2>
<div className="mt-3 pl-2">
<AnimatePresence initial={!isInsideMobileNavigation}>
{isActiveGroup && (
<VisibleSectionHighlight pathname={router.pathname} />
)}
</AnimatePresence>
<div
style={{ top: groupTitleHeight! + remToPx(1) }}
className="absolute bottom-0 left-2 w-px bg-zinc-900/10 dark:bg-white/5"
/>
<div className="relative mt-3 pl-2">
<div className="absolute inset-y-0 left-2 w-px bg-zinc-900/10 dark:bg-white/5" />
<ul role="list" className="border-l border-transparent">
{group.links?.map((link) => (
<motion.li
key={link.href}
layout="position"
className={`${spectral.className}`}
>
<NavLink
id={`${link.href}-link`}
href={link.href}
active={link.href === router.pathname}
>
<NavLink href={link.href} active={link.href === router.pathname}>
{link.title}
{link.href === router.pathname && (
<div
className={clsx(
'absolute',
'top-[-4px]',
'bottom-[-4px]',
'left-[-16px]',
'right-0',
'bg-indigo-600/30',
'dark:bg-white/2.5',
'rounded-[8px]',
)}
/>
)}
</NavLink>
<AnimatePresence mode="popLayout" initial={false}>
{link.href === router.pathname && sections.length > 0 && (
Expand All @@ -89,7 +97,7 @@ export function NavGroup({ group, className }: NavGroupProps) {
}}
>
{sections.map((section: any) => (
<li id={`${section.id}-li`} key={section.id}>
<li key={section.id}>
<NavLink
href={`${link.href}#${section.id}`}
tag={section.tag}
Expand Down
2 changes: 1 addition & 1 deletion next/src/components/Navigation/NavLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function NavLink(
id={id}
aria-current={active ? 'page' : undefined}
className={clsx(
'flex justify-between gap-2 py-1 pr-3 text-sm transition',
'relative flex justify-between gap-2 py-1 pr-3 text-sm transition',
isAnchorLink ? 'pl-7' : 'pl-4',
active
? 'text-zinc-900 dark:text-white'
Expand Down
81 changes: 0 additions & 81 deletions next/src/components/Navigation/VisibleSectionHighlight.tsx

This file was deleted.