Skip to content

Commit f694d91

Browse files
authored
Less ideal but more stable active nav link highlight (#17)
2 parents f653996 + d2012eb commit f694d91

File tree

3 files changed

+35
-108
lines changed

3 files changed

+35
-108
lines changed

next/src/components/Navigation/NavGroup.tsx

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import Link from 'next/link'
88
import { useRouter } from 'next/router'
99
import { NavigationGroup } from './config'
1010
import NavLink from './NavLink'
11-
import { VisibleSectionHighlight } from './VisibleSectionHighlight'
12-
import { remToPx } from '@/lib/remToPx'
1311

1412
interface NavGroupProps {
1513
group: NavigationGroup
@@ -25,54 +23,64 @@ export function NavGroup({ group, className }: NavGroupProps) {
2523
[useRouter(), useSectionStore((s: any) => s.sections)],
2624
isInsideMobileNavigation,
2725
)
28-
2926
let isActiveGroup =
3027
group.group.href === router.pathname ||
3128
(group.links &&
3229
group.links.findIndex((link) => link.href === router.pathname) !== -1)
3330

34-
const groupTitleHeight = document.getElementById(
35-
`${group.group.href}-link`,
36-
)?.offsetHeight
37-
3831
return (
3932
<li className={clsx('relative mt-6', className)}>
4033
<motion.h2
4134
layout="position"
4235
className="text-xs font-semibold text-zinc-900 dark:text-white"
4336
>
4437
<Link
45-
id={`${group.group.href}-link`}
4638
href={group.group.href}
4739
aria-current={isActiveGroup ? 'page' : undefined}
48-
className={lora.className}
40+
className={`${lora.className} relative block`}
4941
>
50-
{group.group.title}
42+
{group.group.title}{' '}
43+
{group.group.href === router.pathname && (
44+
<div
45+
className={clsx(
46+
'absolute',
47+
'top-[-4px]',
48+
'bottom-[-4px]',
49+
'left-[-8px]',
50+
'right-0',
51+
'bg-indigo-600/30',
52+
'dark:bg-white/2.5',
53+
'rounded-[8px]',
54+
)}
55+
/>
56+
)}
5157
</Link>
5258
</motion.h2>
53-
<div className="mt-3 pl-2">
54-
<AnimatePresence initial={!isInsideMobileNavigation}>
55-
{isActiveGroup && (
56-
<VisibleSectionHighlight pathname={router.pathname} />
57-
)}
58-
</AnimatePresence>
59-
<div
60-
style={{ top: groupTitleHeight! + remToPx(1) }}
61-
className="absolute bottom-0 left-2 w-px bg-zinc-900/10 dark:bg-white/5"
62-
/>
59+
<div className="relative mt-3 pl-2">
60+
<div className="absolute inset-y-0 left-2 w-px bg-zinc-900/10 dark:bg-white/5" />
6361
<ul role="list" className="border-l border-transparent">
6462
{group.links?.map((link) => (
6563
<motion.li
6664
key={link.href}
6765
layout="position"
6866
className={`${spectral.className}`}
6967
>
70-
<NavLink
71-
id={`${link.href}-link`}
72-
href={link.href}
73-
active={link.href === router.pathname}
74-
>
68+
<NavLink href={link.href} active={link.href === router.pathname}>
7569
{link.title}
70+
{link.href === router.pathname && (
71+
<div
72+
className={clsx(
73+
'absolute',
74+
'top-[-4px]',
75+
'bottom-[-4px]',
76+
'left-[-16px]',
77+
'right-0',
78+
'bg-indigo-600/30',
79+
'dark:bg-white/2.5',
80+
'rounded-[8px]',
81+
)}
82+
/>
83+
)}
7684
</NavLink>
7785
<AnimatePresence mode="popLayout" initial={false}>
7886
{link.href === router.pathname && sections.length > 0 && (
@@ -89,7 +97,7 @@ export function NavGroup({ group, className }: NavGroupProps) {
8997
}}
9098
>
9199
{sections.map((section: any) => (
92-
<li id={`${section.id}-li`} key={section.id}>
100+
<li key={section.id}>
93101
<NavLink
94102
href={`${link.href}#${section.id}`}
95103
tag={section.tag}

next/src/components/Navigation/NavLink.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function NavLink(
2222
id={id}
2323
aria-current={active ? 'page' : undefined}
2424
className={clsx(
25-
'flex justify-between gap-2 py-1 pr-3 text-sm transition',
25+
'relative flex justify-between gap-2 py-1 pr-3 text-sm transition',
2626
isAnchorLink ? 'pl-7' : 'pl-4',
2727
active
2828
? 'text-zinc-900 dark:text-white'

next/src/components/Navigation/VisibleSectionHighlight.tsx

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)