Open this example in the interactive library
Source code
"use client";
// components/AncestorAwareSectionLink.tsx
import Link from "next/link";
import { usePathname } from "next/navigation";
export default function AncestorAwareSectionLink({
href = "/studio",
label = "Studio",
}: { href?: string; label?: string } = {}) {
const path = usePathname();
const active = path === href || path.startsWith(href + "/");
return (
<Link href={href} aria-current={active ? "location" : undefined}>
{label}
</Link>
);
}
