Skip to content

Commit

Permalink
fix: active header navigation links should have the option to be matc…
Browse files Browse the repository at this point in the history
…hed by "exact" (#141)
  • Loading branch information
Fran McDade authored and Fran McDade committed Aug 7, 2024
1 parent 5b08062 commit 7993904
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/components/Layout/components/Header/common/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export type Navigation = [
NavLinkItem[] | undefined
]; // [LEFT, CENTER, RIGHT]

export enum SELECTED_MATCH {
EQUALS = "EQUALS",
STARTS_WITH = "STARTS_WITH", // Default value.
}

export interface SocialMedia {
label: ReactNode;
socials: Social[];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { SELECTED_MATCH } from "../../../../../common/entities";

/**
* Returns true if the navigation link is selected.
* @param url - The URL of the navigation link.
* @param pathname - The current pathname.
* @param selectedMatch - The selected match type.
* @returns true if the navigation link is selected.
*/
export function isNavigationLinkSelected(
url: string,
pathname?: string
pathname?: string,
selectedMatch: SELECTED_MATCH = SELECTED_MATCH.STARTS_WITH
): boolean {
if (!pathname) return false;
if (isSelectedMatchEqual(selectedMatch)) return url === pathname;
return pathname.startsWith(url);
}

/**
* Returns true if the selected match type is "EQUAL".
* @param selectedMatch - The selected match type.
* @returns True if the selected match type is "EQUAL".
*/
export function isSelectedMatchEqual(selectedMatch: SELECTED_MATCH): boolean {
return selectedMatch === SELECTED_MATCH.EQUALS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const NavigationMenuItems = ({
icon,
label,
menuItems: nestedMenuItems,
selectedMatch,
target = ANCHOR_TARGET.SELF,
url,
},
Expand Down Expand Up @@ -78,7 +79,11 @@ export const NavigationMenuItems = ({
REL_ATTRIBUTE.NO_OPENER_NO_REFERRER
);
}}
selected={isNavigationLinkSelected(url, pathname)}
selected={isNavigationLinkSelected(
url,
pathname,
selectedMatch
)}
>
{icon && <ListItemIcon>{icon}</ListItemIcon>}
<ListItemText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
REL_ATTRIBUTE,
} from "../../../../../../../Links/common/entities";
import { isClientSideNavigation } from "../../../../../../../Links/common/utils";
import { SELECTED_MATCH } from "../../../../common/entities";
import { HeaderProps } from "../../../../header";
import { isNavigationLinkSelected } from "./common/utils";
import { NavigationButtonLabel } from "./components/NavigationButtonLabel/navigationButtonLabel";
Expand All @@ -21,6 +22,7 @@ export interface NavLinkItem {
flatten?: Partial<Record<BreakpointKey, boolean>>;
label: ReactNode;
menuItems?: MenuItem[];
selectedMatch?: SELECTED_MATCH;
target?: ANCHOR_TARGET;
url: string;
visible?: Partial<Record<BreakpointKey, boolean>>;
Expand Down Expand Up @@ -53,7 +55,14 @@ export const Navigation = forwardRef<HTMLDivElement, NavigationProps>(
<Links ref={ref} className={className} style={style}>
{links.map(
(
{ divider, label, menuItems, target = ANCHOR_TARGET.SELF, url },
{
divider,
label,
menuItems,
selectedMatch,
target = ANCHOR_TARGET.SELF,
url,
},
i
) =>
menuItems ? (
Expand Down Expand Up @@ -91,7 +100,7 @@ export const Navigation = forwardRef<HTMLDivElement, NavigationProps>(
);
}}
variant={
isNavigationLinkSelected(url, pathname)
isNavigationLinkSelected(url, pathname, selectedMatch)
? "activeNav"
: "nav"
}
Expand Down

0 comments on commit 7993904

Please sign in to comment.