Skip to content

Commit 0373077

Browse files
committed
Use conditionally visible absolutely positioned static background elements to highlight the currently active route.
1 parent 62d2396 commit 0373077

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

next/src/components/Navigation/NavGroup.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,23 @@ export function NavGroup({ group, className }: NavGroupProps) {
3838
<Link
3939
href={group.group.href}
4040
aria-current={isActiveGroup ? 'page' : undefined}
41-
className={lora.className}
41+
className={`${lora.className} relative block`}
4242
>
43-
{group.group.title}
43+
{group.group.title}{' '}
44+
{group.group.href === router.pathname && (
45+
<div
46+
className={clsx(
47+
'absolute',
48+
'top-[-4px]',
49+
'bottom-[-4px]',
50+
'left-[-8px]',
51+
'right-0',
52+
'bg-indigo-600/30',
53+
'dark:bg-white/2.5',
54+
'rounded-[8px]',
55+
)}
56+
/>
57+
)}
4458
</Link>
4559
</motion.h2>
4660
<div className="relative mt-3 pl-2">
@@ -54,6 +68,20 @@ export function NavGroup({ group, className }: NavGroupProps) {
5468
>
5569
<NavLink href={link.href} active={link.href === router.pathname}>
5670
{link.title}
71+
{link.href === router.pathname && (
72+
<div
73+
className={clsx(
74+
'absolute',
75+
'top-[-4px]',
76+
'bottom-[-4px]',
77+
'left-[-16px]',
78+
'right-0',
79+
'bg-indigo-600/30',
80+
'dark:bg-white/2.5',
81+
'rounded-[8px]',
82+
)}
83+
/>
84+
)}
5785
</NavLink>
5886
<AnimatePresence mode="popLayout" initial={false}>
5987
{link.href === router.pathname && sections.length > 0 && (

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'

0 commit comments

Comments
 (0)