Skip to content
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

Modify nav link underline to match design #54

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Changes from 3 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
35 changes: 30 additions & 5 deletions src/app/components/NavigationLink/NavigationLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export function NavigationLink({
children,
onClick,
}: NavigationLinkProps) {
const pathname = usePathname();
const isActive = pathname.includes(href.toString());
const isActive = useIsActive(href);

return (
<Link
Expand All @@ -22,12 +21,38 @@ export function NavigationLink({
{...(isActive ? { 'aria-current': 'page' } : {})}
>
<span
className={classNames('lg:text-white', {
underline: isActive,
})}
className={classNames(
"lg:text-l font-['Public Sans'] font-bold leading-7 lg:text-white",
{
'underline underline-offset-8': isActive,
},
)}
>
{children}
</span>
</Link>
);
}

/**
* Given a nav Link URL, returns `true` if the current page in the application matches
* the navigation Link's URL. If the user is on a subroute of a matching page, that will also match.
* @param url URL of the navigation Link
* @returns `true` if the current route matches the nav Link's URL, otherwise `false`
*/
function useIsActive(url: NavigationLinkProps['href']) {
const pathname = usePathname();

// top-level route (/products, /case-studies) match
if (pathname === url.toString()) {
return true;
}

// subroutes (/products/ecr-viewer) match
const pathSegments = pathname.split('/').filter((segment) => segment !== '');
if (pathSegments.length > 0 && url.toString().includes(pathSegments[0])) {
return true;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely open to suggestions here if there is a better way to handle this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this

function useIsActive(url: NavigationLinkProps['href']) {
  const pathname = usePathname();
  const urlStr = url.toString();
  return (
    pathname === urlStr ||
    (pathname.startsWith(urlStr) && (urlStr !== '/' || pathname === '/'))
  );
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I like it.


return false;
}
Loading