Dev Helpers
← Back

Ancestor-aware section link

Place this file at components/AncestorAwareSectionLink.tsx in an App Router project.

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>
  );
}